/* 
 * This javascript allows ajax pager to have history (only for main central pager)
 * using hash to store current page
 */

$(function() {
    bsNavigation_init();
});

// master pager id - this id is using to check if calling pager is historyable or not
var bsNavigation_master_pager_id = '';
// if hash is changed from inside of script do not call change event
var bsNavigation_hash_changed = false;
// hash storage
var bsNavigation_hash = '';

/**
 * This function is initializing pager history and is searching for master pager
 */
function bsNavigation_init() {
    // add ids to all ajax_boxes
    $('div.pager').each(function() {
        var id =  $(this).find('span.number a:first').attr('rel');        
        $(this).closest('div.ajax_box').attr('id', id);
    });

    // find master pager
    var master_pager = $('div.outer_action_content div.ajax_box div.pager, div.module_bsPage_component_pageSection div.ajax_box div.pager'); // master pager is first inside content pager
    if (master_pager.length > 0) { // master pager inside of content found
        master_pager.each(function(i) {
            bsNavigation_master_pager_id = $(this).find('span.number a:first').attr('rel');
        });
    }

    // start hash checking
    setTimeout(bsNavigation_onhashchange, 42); // dont know why 42ms is good time but some russian guy said so - and you know Russian guyes are awesome !!
}

/**
 * This function is always called by pagers on page change and if pager is master
 * pager it saves it value to history
 */
function bsNavigation_call_page_change(e, page_number) {
    if (bsNavigation_master_pager_id != '') { // master pager is presented
        var ajax_box = $(e).closest('div.ajax_box');
        if (ajax_box.length > 0) { // invokerer is enveloper with pager (mandatory)
            var id = ajax_box.attr('id');
            if (id == bsNavigation_master_pager_id) { // and pager is master pager
                bsNavigation_hash_changed = true; // do not invoke change event on next hash change
                document.location.hash = 'p' + page_number; // add page to hash to simulate new history item
            }
        }
    }
    return false; // use ajax to change page
}

/**
 * This function is called repeately to check if hash is same. If hash has changed
 * funtion will call master pager page change call.
 */
function bsNavigation_onhashchange() {
    if (bsNavigation_hash != (bsNavigation_hash = document.location.hash)) {
        if (bsNavigation_hash_changed == false && bsNavigation_master_pager_id != '') { // hash changed action is enabled and master pager is set
            if (bsNavigation_hash.substring(1,2) == 'p') { // hash contains page identifier
                var page_number = bsNavigation_hash.substring(2) * 1; // get page number from hash (trim hashmark)
                if (page_number > 0) { // page number is given                    
                    var e = $('#' + bsNavigation_master_pager_id).find('div.pager span.number a:first');
                    var fname = 'bsNavigation_call_page_change_' + bsNavigation_master_pager_id;
                    eval('var fexists = (typeof ' + fname + ' == "function")');
                    if (fexists) {
                        eval(fname + '(' + page_number + ', e)'); // change page
                    }
                }
            }
        }
        bsNavigation_hash_changed = false;
    }
    setTimeout(bsNavigation_onhashchange, 42);
}
