﻿window.dhtmlHistory.create({
    toJSON: function(o) {
        return Object.toJSON(o);
    }
    , fromJSON: function(s) {
         return s.evalJSON();
    }
});

var yourListener = function(newLocation, historyData) {
    if (newLocation && newLocation != "") {
        replaceMainSection(newLocation);
    }
}

window.onload = function() {
    
    if (window.dhtmlHistory.isSupported) {    
    
        dhtmlHistory.initialize();
        dhtmlHistory.addListener(yourListener);
    
        // hack for going from a normal-url page to a hash-value
        
        if (window.location.hash != "") {
            //alert(window.location.hash);
            var hash = window.location.hash;
            hash = hash.substr(1, hash.length-1);                
            openPage(hash);
        } else if (window.location.pathname == RootFolder + "page.asp") {
            openPage("home", "index.asp", "");
        }
    } else {
        openPage("home", "index.asp", "");
    }
};


function openPage(newLocation, newLocationNonAjax, title) {
    
    if (window.dhtmlHistory.isSupported) {
        window.dhtmlHistory.add(newLocation);       
        replaceMainSection(newLocation, title);
    } else {
        document.location.href = newLocationNonAjax;
    }
}
  
function replaceMainSection(newLocation, title) {
    newLocation = decodeURIComponent(newLocation);
        
    if (location.pathname != RootFolder + "page.asp") {
        //alert("hello:" + "<%= rootfolder %>#" + newLocation);
        window.location.href = RootFolder + "page.asp#" + newLocation;
    } else {
            
        var loc = newLocation.split("/");
        var params = { 'headless': 1 };
        var url = loc[0];
        if (url == RootFolder || url == "") {         
            url = RootFolder;            
        } else {
            url = url + ".asp"
        }
                
        if (loc.length >= 1) {
            if (loc[0] == "category") {
                url = "products.asp";
                if (loc.length > 1) {
                    params["category"] = "/" + loc[1];
                    
                    if (loc.length > 2) {
                        params["category"] = params["category"] + "/" + loc[2];
                    }
                }
            } else if (loc[0] == "product") {
                url = "products.asp";           
                if (loc.length > 1) {
                    params["category"] = "/" + loc[1];
                                            
                    if (loc.length > 2) {
                        params["product"] = loc[loc.length - 1];
                        
                        if (loc.length > 3) {
                            params["category"] = params["category"] + "/" + loc[2];
                        }
                    }
                }
                
            }
            
            params["cachebuster"] = Math.floor(Math.random()*1000000);
                
            updatePage(url, params, title);
            updateTab(loc[0]);

        }
    }
}

function updatePage(url, params, title) {
    //$('main').update("Loading...");
    
    var mains = $$("#torso .main");
    if (mains.length > 0) {
    
        for (var i = 0; i < mains.length; i++) {
            mains[i].fade({ 
                duration: 0.4,
                afterFinish: function(theEffect) {
                    var el = theEffect.element;                        
                    // remove me, unless another page transition has already
                    if (el.parentNode != null) {
                        el.remove();
                    }
                }
            });
        }
    }

    new Ajax.Request(url, {
        parameters: params,
        method: 'get',
        evalJS: false,
        evalJSON: false,
        onSuccess: function(response) {
            var newPage = new Element('div', { 'class' : 'main' }).update(response.responseText).setOpacity(0.001);
            $('torso').insert(newPage);
            newPage.appear({ 
                duration : 0.4, 
                afterFinish : function(theEffect) {
                    if (title !== undefined) {
                        var newTitle = "Swann Agencies";
                        if (title != "") {
                            newTitle = newTitle + " - " + title;
                        }
                        document.title = newTitle;
                    }
                }
            });
            
            if (response.request.url == 'map.asp') {
                loadGoogleMap();    
            }
            
        },
        onFailure: function() { 
            $('torso').update("Error loading page:" + url);
        }
    }); 
    
}

function updateTab(page) {
    new Ajax.Request('uicomponents/tabs.asp', {
        parameters: { "SCRIPT_NAME" : page },
        method: 'get',
        evalJS: true,
        evalJSON: false,
        onSuccess: function(response) {
            $('tabs2').update(response.responseText);
        },
        onFailure: function() { 
            $('tabs2').update("Error loading tabs.");
        }
    }); 
}