/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function() {
    // Set up the tab links
    $("#MainNavigation a").click(function(event) {
        event.preventDefault();

        if(this.getAttribute('href')) {
            // Get the path details for the current url
            var paths = parsePaths(this.href);
            var rThis = this;

            // Create AJAX url or fall back on the original url
            var ajaxUrl = "";
            if(paths!=null) {
                if(typeof paths.original.page == "undefined") {
                    ajaxUrl = paths.original.root + paths.original.language +"/";
                }
                else {
                    ajaxUrl = paths.original.root + paths.original.language +"/"+ paths.original.page +"/";
                }
            }
            else {
                ajaxUrl = this.href;
            }

            // Load page
            var contentLoadingTimeout = window.setTimeout(function() {
                showLoading($("#Content"), '-5', '5');
            }, 250);
            
            $("#InnerContent").fadeTo(50, 0.25).load(ajaxUrl + "?ajax=true&rand=" + Math.random(), "", function() {
                 // Set the location hash
                if(paths!=null && paths.original.page) {
                    window.location.hash = paths.original.page + "/";
                }
                else {
                     window.location.hash = "/";
                }

                switch(paths.original.page) {
                    case "gallery":
                        document.title = lang['GalleryTitle'];
                        Gallery.initialize(paths.original.extra); 
                        break;
                    case "contact":
                        document.title = lang['ContactTitle'];
                        ContactForm.initialize(paths.original.extra);
                        break;
                    case "map":
                        document.title = lang['MapTitle'];
                        break;
                    default:
                        document.title = lang['IndexTitle'];
                        break;
                }

                $("#InnerContent").fadeTo(250, 1);
                window.clearTimeout(contentLoadingTimeout);
                hideLoading();

                // Re-arrange the tab links
                $("#MainNavigation a[href!=jPath]").each(function() {
                    this.href = this.getAttribute('jPath');
                    $(this).removeClass('ActiveLink');
                })
                $(rThis).removeAttr('href').addClass("ActiveLink").blur();
            });
        }
    });

    // Load the appropriate page if a page location was sent was sent
    var currentPath = parsePaths();
    if(currentPath.client.page) {
        $("#MainNavigation a[href]").each(function() {
            var linkPaths = parsePaths(this.href);
            
            if(linkPaths && currentPath &&
               linkPaths.original.page == currentPath.client.page)
            {
                var nUrl = currentPath.original.root + "/" + currentPath.original.language + "/";
                    nUrl += currentPath.client.page + "/";
                    for(var i = 0; i < currentPath.client.extra.length; i++) {
                        nUrl += currentPath.client.extra[i] + "/";
                    }
                $(this).attr('href', nUrl);
                $(this).click();
            }
        })
    }
});