//IE Browser detection fix
jQuery.browser.version = jQuery.browser.msie &&
    parseInt(jQuery.browser.version) == 6 &&
    window["XMLHttpRequest"] ?
        "7.0" :
        jQuery.browser.version;
        
var pageObj = {
    activeMap: [],
    active: 'home',
    loading: false,
    ieFix: true,

    init: function(scope) {
        /* IE History fix, create history frame */
        if (jQuery.browser.msie) {
            $("body").append('<div class="hide"><iframe id="historyFrame" src="blank.html"></iframe></div>');
            if (location.hash.length > 1) {
                this.loadPage(location.hash.substr(1));
            }
        }    
        this.loadPage('home');
        this.setNavigation();
        //this.bindBehaviors(scope);
        //this.monitorActivePage();
    },    
    setActive: function(page) {
        if (pageObj.loading) location.hash = page;
        this.activeMap = page.split("/");
        this.active = page;
        
        /* IE history fix */
        if(jQuery.browser.msie) {
            var doc = document.getElementById("historyFrame").contentWindow.document;
            doc.open();
            doc.write(page);
            doc.close();
        }
        $('a.active').removeClass('active'); 
        
        /* set loaded page active */
        if (this.activeMap[1] == undefined) this.activeMap[1] = "";
        $('[title="'+this.activeMap[0]+'/'+this.activeMap[1]+'"]').addClass('active');
        if (this.activeMap[2] != undefined)
            $('[title="'+this.activeMap[0]+'/'+this.activeMap[1]+'/'+this.activeMap[2]+'"]').addClass('active');
        document.title = "Taekwondo Tiger - "+page;
    },
    
    checkActivePage: function() {
        if (!this.loading) {
            var hash = location.hash.substr(1);
            hash = (hash.length <= 0) ? undefined : hash;
            if (hash != undefined) { 
                hash.replace(/\?/, '');
                hash.replace(/#/, '');
                hashMap = hash.split("/");
                /* IE Browser history fix */
                if (jQuery.browser.msie && pageObj.ieFix) {
                    try {
                        var ieHistory = document.getElementById("historyFrame").contentWindow.document.body.innerHTML;
                    } catch (err) {
                        var ieHistory = undefined;
                        this.loadPage(hash);  
                    }
                          
                    if (hash != ieHistory && hash != undefined && ieHistory != undefined && ieHistory != 'game' && ieHistory != "") {                
                        location.hash = ieHistory;
                        hash = ieHistory; 
                        this.loadPage(hash);  
                    }
                } else {
                    if (this.active != hash && hashMap[0] != 'game') {
                        this.loadPage(hash);
                    } else return true;
                }                  
            } else return true;
        } else return true;
    },
    monitorActivePage: function() {
        setTimeout("pageObj.checkActivePage();pageObj.monitorActivePage();", 100);
    },
    loadPage: function(page, fade) {
        document.title = "Taekwondo Tiger - "+page;
        if (typeof(pageTracker) == "object") pageTracker._trackPageview("/"+page);
        pageObj.loading = true;
        var fade = fade || true;
        if (fade) $("#siteContent").fadeOut();
        pageObj.setActive(page);
        $.ajax({
            method: "get",
            url: "/page/"+page,
            data: "",
            success: function(html){
                pageObj.clearEvents();
                $("#siteTitle").html(page.toUpperCase());
                $("#siteContent").html(html);
                pageObj.bindBehaviors($("#siteContent"));
                pageObj.setActive(page);
                if (fade) $("#siteContent").fadeIn();
                else $("#siteContent").show();
                pageObj.loading = false;
            },
            error: function(html) {
                //alert(html);
                pageObj.loading = false;
            }
        });
    },
    clearEvents: function(){
        $("#siteContent").empty(); //clear all behaviors
        subPageObj = null;
    },
    bindBehaviors: function(scope) {
        //pageObj.setNavigation(scope);
    },
    setNavigation: function() {
        //IE navigation fix & fast response mozilla ao
        $('a[title]:not([rel=lightbox])').live('click', function(event) { 
            var page = $(this).attr("title");
            if (page != pageObj.active) {
                pageObj.loadPage(page.replace(/#/, ''));
            }
            return false; //stop event propagation and prevent default
        });
    }
}

