﻿var addhistory = true;
var showCLTimer;
var showMLTimer;

$(document).ready(function() {

    //Highlight Parent or first child depending on starting key
    var startKey = $("li.TreeMenu_Active a").attr('key');
    var parentkey = $(".TreeMenu a[key='" + startKey + "']").attr('parentkey');
    if (parentkey.length > 0) $(".TreeMenu a[key='" + parentkey + "']").closest("li").addClass("TreeMenu_Active");
    $(".TreeMenu a[parentkey='" + startKey + "']:first").closest("li").addClass("TreeMenu_Active");


    $(".TreeMenu a").unbind("click");
    $(".TreeMenu a").click(function() {
        clearTimeout(showCLTimer);
        clearTimeout(showMLTimer);
        var key = $(this).attr('key');
        if ($(".TreeMenu a[parentkey='" + key + "']:first").attr('key'))
            key = $(".TreeMenu a[parentkey='" + key + "']:first").attr('key');
        showCLTimer = setTimeout(function() {
            $(".BodyText").html('<div style=\'text-align: center;\'><br /><img src=\'' + loadingImage + '\' /></div>');
            $(".BodyMedia").html('<div style=\'text-align: center;\'><br /><img src=\'' + loadingImage + '\' /></div>');
        }, 100);
        PageMethodWithPath(service, "GetSelectionContent", ["key", key, "suffix", suffix], switchContent, AjaxError);
        return false;
    });

    if ($.history.getCurrent().length > 0) {
        addhistory = false;
        PageMethodWithPath(service, "GetSelectionContent", ["key", $.history.getCurrent(), "suffix", suffix], switchContent, AjaxError);
    }

    $(".ActiveMedia").media({ width: 360 });

    $(".MediaLink img").css({ opacity: .6 });
    $(".MediaLink img").hover(function() { $(this).stop().animate({ opacity: 1 }) }, function() { $(this).stop().animate({ opacity: .6 }) });

});
$(window).history(function(e, hash) {
    var key = $(".TreeMenu li[default='true']").children('a').attr('key');
    if (hash.length > 0)
        key = hash;

    addhistory = false;
    PageMethodWithPath(service, "GetSelectionContent", ["key", key, "suffix", suffix], switchContent, AjaxError);
});
function switchContent(result) {
    clearTimeout(showCLTimer);

    var split = result.d.indexOf('|');
    var key = result.d.substring(0, split);
    var txt = result.d.substring(split + 1);
    $(".TreeMenu li").removeClass("TreeMenu_Active");
    $(".TreeMenu a[key='" + key + "']").closest("li").addClass("TreeMenu_Active");

    var parentkey = $(".TreeMenu a[key='" + key + "']").attr('parentkey');
    if (parentkey.length > 0) $(".TreeMenu a[key='" + parentkey + "']").closest("li").addClass("TreeMenu_Active");
    $(".TreeMenu a[parentkey='" + key + "']:first").closest("li").addClass("TreeMenu_Active");


    $(".BodyText").html(txt);
    applysIFR();
    $(".BodyMedia").html('');
    if (addhistory) $.history.add(key);
    addhistory = true;

    var pagesuffix = $(".TreeMenu a[key='" + key + "']").attr('href');
    var pagedirectory = $.url.attr('directory');
    if (pagesuffix.substr(0, 3) == ("../")) {
        pagesuffix = pagesuffix.substr(3);
        pagedirectory = pagedirectory.substr(0, pagedirectory.substr(0, pagedirectory.length - 1).lastIndexOf("/")) + "/";
    }
    else if (pagesuffix.substr(0, 2) == ("./")) {
        pagesuffix = pagesuffix.substr(2);
    }

    try {
        pageTracker._trackPageview(pagedirectory + pagesuffix);
    }
    catch (err) {}

    
    showMLTimer = setTimeout(function() {
        $(".WorkMedia").html('<div style=\'text-align: center;\'><br /><img src=\'' + loadingImage + '\' /></div>');
    }, 100);
    PageMethodWithPath(service, "GetSelectionMedia", ["key", $.history.getCurrent(), "suffix", suffix], switchMedia, AjaxError);

}

function switchMedia(result) {
    clearTimeout(showMLTimer);
    var txt = result.d;
    $(".BodyMedia").html(txt);
    $(".ActiveMedia").media();
    $("a[rel^='lightbox']").slimbox({/* Put custom options here */
}, null, function(el) {
    return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
});
$(".MediaLink img").css({ opacity: .6 });
$(".MediaLink img").hover(function() { $(this).stop().animate({ opacity: 1 }) }, function() { $(this).stop().animate({ opacity: .6 }) });

}
function AjaxError(result) {
    clearTimeout(showCLTimer);
    clearTimeout(showMLTimer);
    $(".BodyText").html('');
    $(".BodyMedia").html('');
    addhistory = true;
}