﻿$(document).ready(function () {

    $(".contentDisplayHeader").click(function () {
        hideAll();
        var container = $(this).parent();
        $(".contentItemContent", $(container)).slideToggle(500);
    });
    setHeight();



    $('.lightbox').lightBox();



    $("#contentRight li").click(function () {
        var pageId = $(this).attr("pageid");
        clearAllLi();
        $(this).addClass("current");
        setCurrent($(this).html(), false, pageId);
    });

    $("#contentLeft li").click(function () {
        var pageId = $(this).attr("pageid");
        clearAllLi();
        $(this).addClass("current");
        setCurrent($(this).html(), true, pageId);
    });

});

$(window).resize(function () {
    setHeight();
});



function setHeight() {

    var offset = 200;
    var hgt = $(window).height() - offset;
    $("#contentLeft").css('height', $(window).height() - offset);
    $("#contentMain").css('height', $(window).height() - offset);
    $("#contentRight").css('height', $(window).height() - offset);
}

function hideAll() {

    $(".contentItemContent").slideUp(300);
}

function clearAllLi() {

    $("#contentRight li").removeClass("current");
    $("#contentLeft li").removeClass("current");

}


function setCurrent(text, isLeft, pageId) {



    $("#contentMain").fadeOut(500);
    
    var divName = "#currentRight";
    var otherDivName = "#currentLeft";

    if (isLeft) {
        divName = "#currentLeft";
        otherDivName = "#currentRight";
    }

    $(otherDivName).animate({
        opacity: 0.0,
        top: '+=200'
    }, 500, function () {
        $(otherDivName).html();
        $(otherDivName).animate({
            opacity: 0.0,
            top: '-=200'
        }, 1);
        getPageContent(pageId);

    });

    $(divName).animate({
        opacity: 0.0,
        top: '+=200'
    }, 500, function () {

        $(divName).html(text);
        $(divName).animate({
            opacity: 1.0,
            top: '-=200'
        }, 500);

    });
}

function getPageContent(pageId) {
    $("#contentMain").fadeIn    (100);
    $("#contentMain").html("<p id='loader'><img src='/gfx/ajax-loader.gif' alt='Loading'><br />Laddar...</p>");

    $.ajax({
        type: "POST",
        url: "/Resources/services/pageservice.asmx/GetPage",
        data: "{'PageId': " + pageId + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $("#contentMain").fadeOut(100, "", function () {
                $("#contentMain").html("<h1>" + msg.d.ArticleHeader + "</h1>" + msg.d.ArticleText);
                $("#contentMain").fadeIn(200);
                $('.lightbox').lightBox();
            });
           
        },
        error: function (msg) { alert(msg.responseText); }
    });
    
}
