//-------------------------------------------------------------------------------------------------
//History Manager
//
//start with an href on each element, this object will remove them and work them into push state.
//set all internal content links to "a.navigation-main" or "a.navigation-sub" to integrate them
//into the manager.
//
//MAIN_NAV_NODES is lookup for main nav
//"#prev" and "#next" are lookups for pagination buttons
//SUB_NAV_NODES is lookup for any sub nav
//-------------------------------------------------------------------------------------------------
var HistoryUI = {
    
    navStates:new Array(),
    subNavStates:new Array(),
    slideCount:0,
    
    TITLE_BASE:"MMGY Global | Integrated Travel Marketing",
    MAIN_NAV_NODES:".pagination a",
    //turned on sub nav temporarily
    // SUB_NAV_NODES:"",
    SUB_NAV_NODES:".subNav-list a",
    
    
    init:function(){
        if(window.history && window.history.pushState){
            HistoryUI.setSlideCount();
            HistoryUI.setMainNavInformation();
            HistoryUI.setSubNavInformation();
            HistoryUI.addListeners();
            HistoryUI.addSubNavListeners();
            HistoryUI.setInnerContentButtons();
            HistoryUI.checkForHash();
        }
    },
    setSlideCount:function(){
      HistoryUI.slideCount = Number($("#content .slide").attr("data-state"));
    },
    updateSlideCount:function(newCount){
        if(newCount >= HistoryUI.navStates.length){
            HistoryUI.slideCount = 0;
        }else if(newCount < 0){
            HistoryUI.slideCount = HistoryUI.navStates.length-1;
        }else{
            HistoryUI.slideCount = newCount;
        }
    },
    setMainNavInformation:function(){      
        //removes all basic html functionality and adds in javascript push state functionality
        $(".pagination a").each(function(i, ele){
            var hMS = new HistoryUIState($(ele).attr("href"), $(ele).attr("data-title"));
            HistoryUI.navStates.push(hMS);
            
            $(ele).removeAttr("href");
            $(ele).attr("id", i);
        });
        $("#prev").removeAttr("href");
        $("#next").removeAttr("href");
    },
    setSubNavInformation:function(){
        //removes all basic html functionality and adds in javascript push state functionality
        HistoryUI.subNavStates = new Array();
        if($(HistoryUI.SUB_NAV_NODES).length > 0){
            $(HistoryUI.SUB_NAV_NODES).each(function(i, ele){
                var hMS = new HistoryUIState($(ele).attr("href"), $(ele).attr("data-title"));
                HistoryUI.subNavStates.push(hMS);
                
                $(ele).removeAttr("href");
                $(ele).attr("id", i);
            });
        }
    },
    setInnerContentButtons:function(){
        $("a.navigation-main").each(function(i, ele){
            $(ele).attr("id", HistoryUI.findMainNavIndex($(this).attr("href")));
            $(ele).removeAttr("href");
            $(ele).click(HistoryUI.pageClickHand);
        });
        $("a.navigation-sub").each(function(i, ele){
            $(ele).attr("id", HistoryUI.findSubNavIndex($(this).attr("href")));
            $(ele).removeAttr("href");
            $(ele).click(HistoryUI.subPageClickHand)
        });
    },
    navigateTo:function(id){
        HistoryUI.updateSlideCount(id);
        var hMS = HistoryUI.navStates[HistoryUI.slideCount];
        //Math.random() fixes IE stagechange bug
        
        //track to Google analytics
        _gaq.push(['_trackPageview', hMS.href]);
        
        History.pushState({state:id}, HistoryUI.getTitle(hMS.title), hMS.href);
    },
    subNavigateTo:function(id){
        var hMS = HistoryUI.subNavStates[id];
        var title = HistoryUI.navStates[HistoryUI.slideCount].title+": "+hMS.title;
        
        //track to Google analytics
        _gaq.push(['_trackPageview', hMS.href]);
        
        History.pushState({state:"sub"+id}, HistoryUI.getTitle(title), hMS.href);
    },
    getTitle:function(title){
        if(title == undefined || title.length <= 0){
            return HistoryUI.TITLE_BASE;
        }else{
            return title+" | "+HistoryUI.TITLE_BASE
        }
    },
    //-------------------------------------------
    //LOOK UPS - used for special case buttons
    //-------------------------------------------
    findMainNavIndex:function(url){
        //gets an index from a main nav url
        for(var i = 0; i<HistoryUI.navStates.length; i++){
            var nS = HistoryUI.navStates[i];
            if(nS.href == url){
                return i;
            }
        }
        return 0;
    },
    findSubNavIndex:function(url){
        //gets an index from a main nav url
        for(var i = 0; i<HistoryUI.subNavStates.length; i++){
            var nS = HistoryUI.subNavStates[i];
            if(nS.href == url){
                return i;
            }
        }
        return 0;
    },
    //-------------------------------------------
    //HASH FUNC
    //-------------------------------------------
    checkForHash:function(){
        var href = $(window)[0].location.href;
        var location = "";
        if(href.indexOf("#") > 0){
            location = href.split("#")[1];
            location = "/"+location.slice(0, location.lastIndexOf("/")+1);
            if(location.split(/\//g).length-1 <= 2){
                HistoryUI.navigateTo(HistoryUI.findMainNavIndex(location));
            }else{
                HistoryUI.subNavigateTo(HistoryUI.findSubNavIndex(location));
            }
        }
    },
    //-------------------------------------------
    //UI
    //-------------------------------------------
    addListeners:function(){
        //main nav
        $(HistoryUI.MAIN_NAV_NODES).click(HistoryUI.pageClickHand);
        $("#next").click(HistoryUI.advance);
        $("#prev").click(HistoryUI.retreat);
        
        $(window).bind('statechange', HistoryUI.stageChangeHand);
    },
    addSubNavListeners:function(){
        $(HistoryUI.SUB_NAV_NODES).click(HistoryUI.subPageClickHand);
    },
    removeSubNavListeners:function(){
        $(HistoryUI.SUB_NAV_NODES).unbind();
    },
    loadNewElement:function(url){
        $("#content").load(url+" #content .slide", HistoryUI.loadedHand);
        HistoryUI.updateUI();
    },
    updateUI:function(){
        $(".pagination li").removeClass("active");
        $($(".pagination li")[HistoryUI.slideCount]).addClass("active");
    },
    //-------------------------------------------
    //HANDLERS
    //-------------------------------------------
    //main nav
    pageClickHand:function(){
       HistoryUI.navigateTo(Number($(this).attr("id")));
    },
    stageChangeHand:function(){
        HistoryUI.removeSubNavListeners();
        HistoryUI.loadNewElement(History.getState().url);
    },
    advance:function(){
        HistoryUI.navigateTo((HistoryUI.slideCount+1));
    },
    retreat:function(){
        HistoryUI.navigateTo((HistoryUI.slideCount-1));
    },
    //sub nav
    subPageClickHand:function(){
        HistoryUI.subNavigateTo(Number($(this).attr("id")));
    },
    //loading
    loadedHand:function(){
        HistoryUI.setInnerContentButtons();
        HistoryUI.setSubNavInformation();
        HistoryUI.addSubNavListeners();
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//History Manager State
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function HistoryUIState(href, title){
    this.href = href;
    this.title = title;
}

if (screen.width >= 600) {
	$(document).ready(HistoryUI.init);
}
