//
// Utility functions for ajax based tabs.  These function
// assist with page tracking by directly modifying the 
// omniture 's' object as appropriate.
//
// We register callbacks for each tab that gets called on the
// jQuery.UI.Tabs show event via doTrackingCallback.
//
// Anything prefixed with '__' is meant to be private.
//
var __tcbFuncA;
var __tcbAssoc = false;
var __tabsLoaded = false;

// reset all tracking variables that may be important in the
// context of ajax tabs
function __ajaxResetTrackingVariables() {
    if (s) {
        s.events = '';
        s.prop3 = '';
        s.prop10 = '';
        s.products = '';
        s.prop33 = '';
    }
}

// append events
function __ajaxAddEvents(events) {

    if (s.events.length == 0) {
        s.events = events;
    } else {
        s.events += "," + events;
    }

}


// set product views
function ajaxSetProductViews(products) {
    __ajaxAddEvents("prodView,scAdd");
    var pA = products.split(",");
    var first = (s.products.length == 0);
    var i;
    for (i=0; i < pA.length; i++) {
        if (pA[i] != '') {
            if (first) {
                s.prop10 = pA[i];
                first = false;
            } else {
                s.products += ",";
            }
            s.products += ";" + pA[i];
        }
    }
}

// add an ajax tracking callback
function ajaxAddTrackingCallback(tab, callback) {

    if (typeof(__tcbFuncA)=="undefined") {
        __tcbFuncA = new Array();
        __tcbAssoc = (typeof(tab) == "string");
    }
    __tcbFuncA[tab] = callback;

}


// Call a javascript function defined in the ajax tab that will
// set tracking data appropriately for the tab.
function ajaxTrackingCallback(event, ui) {

    if (__tabsLoaded) {
        if (s) {
            __ajaxResetTrackingVariables();
            if (__tcbFuncA) {
                var callback = (__tcbAssoc) ?
                               __tcbFuncA[ui.panel.id] :
                               __tcbFuncA[ui.index];
                if (callback) {
                    callback(event, ui);
                }
            }
            if (s.prop3.length == 0) {
                    s.prop3 = ui.panel.id;
            }
            void(s.t());   // generate page view
        }
    } else {
      __tabsLoaded = true;
    }

}

// AG: Add back button support to AJAX/JQuery tabs page and to 
// support the normal anchor tag at the same time.
function ajaxAddBackButtonSupport(tabIndex, refTabs)    {

	var tabs = $("#tabs");
	tab_a_selector = 'ul.ui-tabs-nav a';
	tabs.tabs({event: 'change'});
	tabs.find( tab_a_selector ).click(function()    {
		var state = {};
		var id = $(this).closest("#tabs").attr('id');
		var idx = $(this).parent().prevAll().length;	
		state[ id ] = idx;
		$.bbq.pushState(state);
	});

	$(window).bind( 'hashchange', function(e) {
		var normalAnchor = false;    
		tabs.each(function(){
			var params = $.param.fragment();
			if (params != undefined && params != null && params.length > 0) {
				if (params.indexOf("tabs=") == -1) {
					normalAnchor = true;
				}
			}
			if (!normalAnchor) {
				//var idx = $.bbq.getState(this.id, true) || tabIndex;
				var idx = $.bbq.getState(this.id, true);
				if (idx == undefined) {		
					idx = tabIndex;
				}
				$("#tabs").tabs('select', idx);
			}
			else {
				var selTab = $("#tabs").tabs('option', 'selected');
				var anchorLastChar = params.charAt(params.length-1);
				var iAnchor = parseInt(anchorLastChar);
				if (selTab != iAnchor ) {
					$("#tabs").tabs('select', iAnchor);
				}
				else {
					return true;
				}
			}
		});
	})

	var normalAnchor1 = false;
	var params1 = $.param.fragment();
	if (params1 != undefined && params1 != null && params1.length > 0) {
		var iAnchor1 = 0;
		if (params1.indexOf("tabs=") == -1) {
			var anchorLastChar1 = params1.charAt(params1.length-1);
			iAnchor1 = parseInt(anchorLastChar1);
		}
		var selTab = $("#tabs").tabs('option', 'selected');
		if (params1.indexOf("tabs=") == -1 && selTab == iAnchor1) {
			normalAnchor1 = true;
		}
	}
	if (!normalAnchor1) {
		$(window).trigger( 'hashchange' );
	}
}

// AG: Add back button support to AJAX/JQuery tabs navigation from the bottom of pages.
function ajaxAddBackButtonSupport2Buttons(targetTab)    {
	var state = {};
	var id = "tabs";
	state[ id ] = targetTab;
	$.bbq.pushState(state);					
}

// AG: Remove anchor tag from the page title
function ajaxRemoveAnchorFromTitle()    {

        var oldtitle = document.title || '';
        document.onpropertychange = function () {
                var newtitle, doctitle = document.title || '';
                if (window.event.propertyName != 'title' || doctitle == oldtitle) { return; }
                // clean the title if needed
                newtitle = doctitle.indexOf('#') != -1 ? doctitle.substring(0, doctitle.indexOf('#')) : doctitle;
                if (newtitle == '' && doctitle.indexOf('#') != -1) {
                        newtitle = oldtitle;
                }
                oldtitle = newtitle;
                document.title = newtitle;
        }
}

