current_hash = "";
no_refresh = false;

deserializeFormElement = function(text) {
    if (encodeURIComponent) {
        return decodeURIComponent(text);
    } else {
        return unescape(text);
    }
}

function onpopuphide()
{
    no_refresh = true;
    window.history.back();
}

function setcurrenthash( hash_ )
{
    var decoded = deserializeFormElement( hash_ );
    if( decoded != null && decoded.length > 0 )
    {
        current_hash = '#' + decoded;
    }
    else
    {
//        window.location.hash = hash;
        current_hash = "";
    }

//    if(  window.location.hash != hash
//        && !( window.location.hash != "#" && hash == ""  ) )
    if(  window.location.hash != decoded )
    {
//        if( hash != "" || ( hash == "" && window.location.hash != "#"))
        if( decoded != "" )
        {
            window.location.href = window.location.href.replace( /#.*/, '' ) + '#' +  decoded;
        }
        else
        {
            window.location.href = window.location.href.replace( /#.*/, '' );
        }
//        window.location.hash = hash;
    }
}

function  hashfromurl( url )
{
    var retVal = notWicketParamsFromUrl( url );
/*
    var idx = url.indexOf('?');
    if( idx != -1 )
    {
        var qp = url.substring( idx +1 );
        var splited = qp.split( '&' );

        for( str in splited )
        {
            var s = splited[str];
            if( s.match(/^wicket:\w*=/) != null )
            {
                continue;
            }

            if( retVal.length != 0 )
            {
                retVal += "&";
            }
            retVal += s;
        }
    }
*/
    if( retVal == "sw=0")
    {
        return "";
    }
    return retVal;
}

function bbtnSuccessHandler()
{
    var url = new String( this.request.url );

    if( url.indexOf('&sw=') != -1 ||
            url.indexOf('?sw=') != -1
        )
    {
//        console.log( 'intercept switch call: ' + url );
        var hash = hashfromurl( this.request.url );
        setcurrenthash( hash );
    }

    trkpg( url );
    return this.oldSuccessHandler();
}

function bbtnCall()
{
    if( this.oldSuccessHandler == undefined )
    {
        this.oldSuccessHandler = this.successHandler;
        this.successHandler = bbtnSuccessHandler;
    }    
    return this.originalCall();
}

$(function()
{
    // ----------  Initialize
    Wicket.Ajax.Call.prototype.originalCall = Wicket.Ajax.Call.prototype.call;
    Wicket.Ajax.Call.prototype.call = bbtnCall;

  // Bind an event to window.onhashchange that, when the hash changes, gets the
  // hash and adds the class "selected" to any matching nav link.
  $(window).bind( 'hashchange', function()
  {
    var hash = window.location.hash;
    console.log( 'hash changed: ' + hash );

    if( current_hash != hash  )
    {
        // ----------  bbtn was pressed reloading needed
        // 
        // hide popup
        $("#smspopupwindow").hide();

        console.log( 'refresh needed' + hash );
        
        if( no_refresh )
        {
            console.log( 'skipping refresh because no_refresh flag is set.' );
            no_refresh = false;
            current_hash = hash;
        }
        else
        {
            if( hash.length > 0 && hash != "#" )
            {
                window.location.assign( window.location.protocol + window.location.host + '?' + hash.replace('#','&') );
                //ajxrfr( hash.replace('#','&') );
            }
            else
            {
                // Main page
                //ajxrfr( "&sw=0" );
                window.location.hash='';
                window.location.url=null;
                window.location.search = '';
            }
        }
    }
  })
  
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).trigger( 'hashchange' );

});
