﻿var ie6 = navigator.userAgent.indexOf("MSIE 6") > 0;

document.createElement("header");
document.createElement("nav");
document.createElement("article");
document.createElement("section");
document.createElement("footer");
document.createElement("figure");
document.createElement("figcaption");

function showHideFloorelementsCenter() {
//    $(".leftcontentmeasurement").height() >= ($(".leftcontent").height() - 65) ?
//            $(".floorelementscenter").fadeOut("fast") : $(".floorelementscenter").fadeIn("fast");
}

var sliderLeftTimeout;
var sliderTimeout;

function showHideScrollbar() {
    clearTimeout(sliderTimeout);
    var needLeft = $(".leftcontentmeasurement").height() >= $(".leftcontent").height();
    var need = $(".contentmeasurement").height() >= $(".content").height();

    if (needLeft) sliderLeftTimeout = setTimeout(function() { $(".leftslider").css({ "opacity": 1 }); }, 100);
    if (!needLeft) sliderLeftTimeout = setTimeout(function() { $(".leftslider").css({ "opacity": 0 }); }, 100);
    

    if (need) sliderTimeout = setTimeout(function() { $(".contentslider").css({ "opacity": 1 }); }, 100);
    if (!need) sliderTimeout = setTimeout(function() { $(".contentslider").css({ "opacity": 0 }); }, 100);
}

function relocateElements() {

    //var fallposition = 0;

    if (!simplified && !ie6) {
        // Uten gulv
        if ($(window).height() < 900) {
            $("#floor").height(50);
            $(".floorelements").css({ bottom: '-150px' });
            //$(".trolleycontainer").css({ bottom: '-150px' });
            $(".sheet").height($(window).height() - 60);
            $(".setmyheight").height($(window).height() - 150); // content, leftcontent og contentslider
            //fallposition = $(window).height() - 100;

            // Med gulv
        } else if ($(window).height() > 1050) {
            $("#floor").height(200);
            $(".floorelements").css({ bottom: '0px' });
            //$(".trolleycontainer").css({ bottom: '0px' });
            $(".sheet").height($(window).height() - 210);
            $(".setmyheight").height($(window).height() - 300); // content, leftcontent og contentslider
            //fallposition = $(window).height() - 250;

            // Overgangsfasen
        } else {
            var floorheight = 50 + ($(window).height() - 900);

            $("#floor").height(floorheight);
            $(".floorelements").css({ bottom: -($(window).height() - ($(window).height() - 200 + $("#floor").height())) + 'px' });
            //$(".trolleycontainer").css({ bottom: -($(window).height() - ($(window).height() - 200 + $("#floor").height())) + 'px' });
            $(".sheet").height($(window).height() - (floorheight) - 10);
            $(".setmyheight").height($(window).height() - floorheight - 100); // content, leftcontent og contentslider
            //fallposition = $(window).height() - floorheight - 50;
        }
    }

    var lefty = (($(window).width() - 1000) / 2) + 20;
    var righty = (($(window).width() - 1000) / 2) - 0;

    if (lefty > 0) {
        if (lefty > 470) $(".floorelementsleft").width(470).css({ "margin-left": lefty - 470 + 'px' });
        else $(".floorelementsleft").width(lefty).css({ "margin-left": "0px" });
        $(".floorelementsleft").show();
    } else $(".floorelementsleft").hide();

    if (righty > 0) {
        if (righty > 684) $(".floorelementsright").width(684).css({ "margin-right": righty - 684 + 'px' });
        else $(".floorelementsright").width(righty).css({ "margin-right": "0px" });
        $(".floorelementsright").show();
    } else $(".floorelementsright").hide();

    var outsideConstruct = parseInt((($(window).width() - 1000) / 2));
    if (outsideConstruct <= 0) outsideConstruct = 0;

    if (!simplified) {
        $("#construct").css({ "position": "fixed", "top": "0", "left": outsideConstruct + 'px' });
    }

    //$(".floorelementscenter").css({ left: outsideConstruct - 90 + 'px' });
    //$(".trolleycontainer").css({ left: outsideConstruct + 850 + 'px' });
    //$(".trolleycontainer").css({ left: outsideConstruct + 200 + 'px' });
    $(".floorelementsdias").css({ left: outsideConstruct + 440 + 'px' });

    $("#tag").css({ bottom: 0 + 'px', left: outsideConstruct + 10 + 'px' });

    $(".sheet").css({ left: outsideConstruct + 200 + 'px' });

    if ($("#construct").height() < $(window).height() - 120) $("#construct").css({ height: $(window).height() - 150 + 'px' });

    showHideFloorelementsCenter();

    setTimeout(function() { showHideScrollbar(); }, 500);

    /*
    if (!$.browser.msie && $("#floor").is(":visible")) {
        $(".fallable").mouseover(function() {
            var toppy = $(this).children(":first-child").offset().top;
            var lefty = $(this).children(":first-child").offset().left;
            $(this).after("<a href=\"#\"></a>");
            $("body").append($(this));
            $(this).css({ "position": "fixed", "top": toppy + 'px', "left": lefty + 'px' });
            $(this).animate({ "top": fallposition - 10 + 'px' }, { duration: 600, easing: "easeOutBounce" });
            $(this).css({ "z-index": 4 });
            $(this).unbind("mouseover");
        });
    }
    */

    setTimeout(function() {
        $(".images a").each(function(i, val) {
            var pos = $(this).position().top + 130 + 50;
            if (pos > $(".images").parent().height()) $(this).css({ "visibility": "hidden" });
            else $(this).css({ "visibility": "visible" });
        });
    }, 300);

    /*
    $(".leftinnercontent article").each(function(i, val) {
        var pos = $(this).position().top + $(this).height();
        if (pos > $(".leftcontent").height()) $(this).css({ "visibility": "hidden" });
        else $(this).css({ "visibility": "visible" });
    });
    */
}

function setupLabel() {
    if ($('.collections input').length) {
        $('.collections label').each(function () {
            $(this).removeClass('fancycheck_on');
        });
        $('.collections input:checked').each(function () {
            $(this).parent('label').addClass('fancycheck_on');
        });
    };
};

$(document).ready(function () {
    //<![CDATA[

    $('.collections label').click(function () { setupLabel(); });
    setupLabel();

    if (simplified || ie6) {
        $("#tag").css({ "position": "static", "width": "1000px", "color": "#FFFFFF", "opacity": "1" }).unbind("mouseenter mouseleave");

        $("#floor").remove();
        $(".sheet").remove();

        if (ie6) {
            $("#menu a, #construct").css({ "behavior": "url(/js/pngfix/iepngfix.htc)" });
            $("head").append("<script type=\"text/javascript\" src=\"/js/pngfix/iepngfix_tilebg.js\" />");
        }

        $(".content").css({ "background": "none", "background-color": "#FFFFFF", "margin-left": "20px", "padding-left": "20px", "padding-right": "20px" }).removeClass("mousescroll");
        $(".contentmeasurement").css({ "margin": "20px 0 20px 0" });
        $(".leftslider").remove();
        $(".contentslider").remove();
        $(".flags a").css({ "background": "none", "line-height": "65px" });
        $("#construct").css({ "position": "static" });
        $(".menuitem1").unbind("mouseenter mouseleave");
    } else {

        // Add floor with floorelements for 'normal' browsers
        if (!business) {
            $("#construct").parent().append("<div id=\"floor\"></div>");
            $("#floor").prepend("<div class=\"floorelements floorelementsleft\"></div>");
            $("#floor").prepend("<div class=\"floorelements floorelementsright\"></div>");
            //$("#floor").prepend("<div class=\"floorelements floorelementscenter\"></div>");
            //$("#floor").append($(".trolleycontainer"));
            //$(".trolleycontainer").show();
            $("#floor").append($("#tag"));
            $("#floor").append("<div id=\"dias\" class=\"" + diasClass + "\"></div>");
        }

        if (showSheet) $("#construct").append("<img src=\"/App_Themes/Standard/images/sheet3.png\" alt=\"sheet\" class=\"sheet\" />");
    }

    /*
    var fadeOutLoginScreen = function () {
    $("#construct").show();
    $("#floor").show();
    $("#loadingDialog").dialog('destroy').remove();
    }
    */

    $("a[rel='colorbox']").colorbox();
    $("a[data-id='colorbox']").colorbox();
    $("a[rel='colorbox-iframe']").colorbox({ width: "850px", height: "80%", iframe: true, scrolling: true });
    $("a[data-id='colorbox-iframe']").colorbox({ width: "850px", height: "80%", iframe: true, scrolling: true });

    $(".rtLI").click(function () {
        setTimeout(function () {
            showHideFloorelementsCenter();
        }, 500)
    });

    // Google Plus button
    if (window.gapi) {
        gapi.plusone.render("gplusone_tall", { "size": "tall", "count": "true", "lang": "no" });
        gapi.plusone.render("gplusone_medium", { "size": "medium", "count": "true", "lang": "no" });
    }

    //if (!$.browser.msie && !simplified) {
    if (!simplified) {

        //$(".content").css({ "overflow": "hidden", "width": "660px", "margin-left": "30px" });
        $(".content").css({ "overflow": "hidden", "width": "732px", "margin-left": "0px" });

        $(".leftcontent").css({ "overflow": "hidden", "width": "230px" });
        $(".leftslider").show().css({ "opacity": 0 });
        $(".contentslider").show().css({ "opacity": 0 });

        // Left content scroll

        $(".leftscroll").mousewheel(function (event, delta) {
            var up = delta > 0;
            var velocity = Math.abs(delta);
            var val = $(".leftslider").slider('option', 'value') + (up ? velocity : -velocity);

            if (val > 0) val = 0;
            if (val < -100) val = -100;

            $(".leftslider").slider('option', 'value', val);
            handleSlideLeft(null, null);
        });

        function handleChangeLeft(e, ui) {
            //var maxScroll = $(".leftcontent").attr("scrollHeight") - $(".leftcontent").height();
            //$(".leftcontent").animate({ scrollTop: -ui.value * (maxScroll / 100) }, 1000);
        }

        function handleSlideLeft(e, ui) {
            var maxScroll = $(".leftcontent").attr("scrollHeight") - $(".leftcontent").height();
            $(".leftcontent").attr({ scrollTop: -$(".leftslider").slider('option', 'value') * (maxScroll / 100) });
        }

        $(".leftslider").slider({
            change: handleChangeLeft,
            slide: handleSlideLeft,
            min: -100,
            max: 0,
            orientation: 'vertical',
            value: -15
        });

        // Content scroll

        $(".mousescroll").mousewheel(function (event, delta) {
            var up = delta > 0;
            var velocity = Math.abs(delta);
            var val = $(".contentslider").slider('option', 'value') + (up ? velocity : -velocity);

            if (val > 0) val = 0;
            if (val < -100) val = -100;

            $(".contentslider").slider('option', 'value', val);
            handleSlide(null, null);
        });

        function handleChange(e, ui) {
            //var maxScroll = $(".content").attr("scrollHeight") - $(".content").height();
            //$(".content").animate({ scrollTop: -ui.value * (maxScroll / 100) }, 1000);
        }

        function handleSlide(e, ui) {
            var maxScroll = $(".content").attr("scrollHeight") - $(".content").height();
            $(".content").attr({ scrollTop: -$(".contentslider").slider('option', 'value') * (maxScroll / 100) });
        }

        $(".contentslider").slider({
            change: handleChange,
            slide: handleSlide,
            min: -100,
            max: 0,
            orientation: 'vertical',
            value: -15
        });
    }

    $(".contentfrontpage").css({ "width": "710px", "margin-left": "0", "padding-left": "20px" });

    $(window).resize(function () { relocateElements(); });
    relocateElements();

    $(window).load(function () { relocateElements(); });

    //]]>
});
