$(document).ready(function(){
    // Accordion
    $("#accordion").accordion({
        header: ".heading",
        autoHeight: false
    });

    //Bind address handlers for Ajax url
    $.address.init(jsRoute.urlPathContext.addressHandlers.afterInit);
    $.address.change(jsRoute.urlPathContext.addressHandlers.change);
    $.address.internalChange(jsRoute.urlPathContext.addressHandlers.internalChange);
    $.address.bind('externalChange', {}, jsRoute.urlPathContext.addressHandlers.externalChange);

});
jsRoute = {};

jsRoute.urlPathContext =  {
    pathParams : [],
    route : '',
    changeType: '',
    initRequest : false,

    addressHandlers : {
        // Handler for first call of page
        afterInit : function(event) {
            //Redirect google url to our
            if(!location.href.match(/#!/)) {
                var path = location.href.replace(/(http:\/\/www\.|http:\/\/)([^\.]+\.[a-z]{2})(\/[^$]*$)/, "$1$2#!$3");
                location.href = path;
            }

            jsRoute.urlPathContext.initRequest =  true;

            $('a.ajax-control').address(function() {
                return $(this).attr('href').replace(location.pathname, '');
            });
        },

        change : function(event){
        // track all url changes
        },

        internalChange : function(event) {
            jsRoute.urlPathContext.changeType = 'internal';
            jsRoute.urlPathContext.navigate(event);
        },

        externalChange : function(event) {
            jsRoute.urlPathContext.changeType = 'external';
            jsRoute.urlPathContext.navigate(event);
        }
    },

    navigate    :   function(event) {
        jsRoute.urlPathContext.initPath(event);

        //Load of content
        $("#"+jsRoute.urlPathContext.route).load("/ajax-loading", {
            route: jsRoute.urlPathContext.route,
            accId: $("#"+jsRoute.urlPathContext.route).attr('data-acc-id')
        }, function(){
            $('#accordion').accordion('resize');
        });


        jsRoute.urlPathContext.initRequest =  false;
    },

    initPath    : function(event) {
        var path = event.value.split("/");
        var i =1;
        var route = [];

        jsRoute.urlPathContext.pathParams = [];
        jsRoute.urlPathContext.route = '';

        for (i = 1; i < path.length; i++) {
            var pp = i+1;
            jsRoute.urlPathContext.pathParams.push({
                name:path[i],
                value : path[pp]
            });

            if(path[i] != 'page' && path[i] != 'results') {
                route.push(path[i]);
            }

            i++;
        }

        jsRoute.urlPathContext.route = route.join('/');

        if(jsRoute.urlPathContext.route == '') {
            jsRoute.urlPathContext.route = 'home';
        }
    },

    getRequestParameter : function(name,defaultValue) {
        var i=0;

        for (i = 0; i < jsRoute.urlPathContext.pathParams.length; i++) {
            if(name == jsRoute.urlPathContext.pathParams[i].name) {
                return jsRoute.urlPathContext.pathParams[i].value;
            }
        }

        if(typeof defaultValue == 'undefined') {
            defaultValue = null;
        }

        return defaultValue;
    }
};
