﻿/// <reference path="jquery-1.3.2.min.js" />
/// <reference path="jquery.hoverIntent.minified.js" />

$(document).ready(function() {

    SearchLoad();

    var branchDropDownVisible = false;
    var menuSlideUpCheck = false;

    $('.sub').mouseover(function() {
        menuSlideUpCheck = true;
    });

    $('.sub').mouseout(function() {
        menuSlideUpCheck = false;
    });

    $('.showMegaMenu').mouseover(function() {
        menuSlideUpCheck = true;
    });

    $('.showMegaMenu').mouseout(function() {
        menuSlideUpCheck = false;
    });

    if (branchDropDownVisible == true) {
        fixMenuTopImage();
    }

    $(".sub ul").children(":last-child").addClass("last");

    // Load services in drop-down menu if it exists
   // if ($("#ddBranchSelectorSpan select").length != 0) LoadServiceList();

    //SubMenu Hover-effect in MainMenu
    function megaHoverOver() {
        $("#branchMegaDropDown").find(".sub").stop().slideDown();

    }

    function megaHoverOut() {
        $("#branchMegaDropDown").find(".sub").stop().slideUp();
    }

    function showMegaDropDown(visible) {
        if (visible == false) {
            megaHoverOut();
            branchDropDownVisible = false;
        }
        else {
            megaHoverOver();
            branchDropDownVisible = true;

        }
    }

    //Set custom configurations
    var config = {
        sensitivity: 2,
        interval: 100,
        over: megaHoverOver,
        timeout: 500,
        out: megaHoverOut
    };

    // Dölj menyn vid click
    /**/$('#closeMegaDropDownBtn').click(function() {
        if (branchDropDownVisible == true) {
            showMegaDropDown(false);
        }

    }
    );

    // Visa/Dölj menyn vid click
    /**/$('.showMegaMenu a').click(function() {
        if (branchDropDownVisible == true) {
            showMegaDropDown(false);
        }
        else {
            showMegaDropDown(true);
        }
    });

    $('body').click(function() {
        if (branchDropDownVisible == true && menuSlideUpCheck == false) {

            showMegaDropDown(false);
        }
    }
    );

    // Visa menyn vid click på breadcrumbs
    /**/$('.breadCrumbs .showMegaMenu').click(function() {
        if (branchDropDownVisible == false) {
            showMegaDropDown(true);
        }
    });

    function btnMouseOver() {
    }

    function btnMouseOut() {
    }

    //toggle jobitems
    $(".jobItemHeader").click(function(event) {
        var currentJobItem = $(this).next(".jobItem");
        var currentReadMoreItem = currentJobItem.find(".readMoreItem");

        if (currentJobItem.is(":hidden")) {
            currentJobItem.slideDown();
            $(this).removeClass("jobItemHeader");
            $(this).addClass("jobItemHeaderSelected");
        }
        else {
            if (!currentReadMoreItem.is(":hidden")) {
                currentReadMoreItem.hide();
            }
            currentJobItem.slideUp();
            $(this).removeClass("jobItemHeaderSelected");
            $(this).addClass("jobItemHeader");
        }
    });

    //toggle jobitems
    $(".jobItemReadMore").click(function(event) {
        var currentReadMoreItem = $(this).next(".readMoreItem");
        currentReadMoreItem.slideToggle();
    });

    $(".navigationSphere").hover(function(event) {
        $(this).find(".sphere").stop().animate({ marginTop: "-10px" }, 200);
        $(this).find(".shadow").stop().animate({ marginTop: "10px", width: "65px" }, 200);
        $(this).find(".navigationText").stop().animate({ marginTop: "0px" }, 200);
    }, function() {
        $(this).find(".sphere").stop().animate({ marginTop: "0px" }, 300);
        $(this).find(".shadow").stop().animate({ marginTop: "0px", width: "59px" }, 300);
        $(this).find(".navigationText").stop().animate({ marginTop: "0px" }, 300);
    });


    $(function() {
        $(".scrollable").scrollable({ size: 2, clickable: false }).circular(); //.mousewheel();
    });


    // Adds attributes to elements that used to make the page invalid (xhtml transitional)
    $('#searchBox .searchBoxInput').attr('autocomplete', 'off');


    $("#ddBranchSelectorSpan").find("select").change(function() {
        LoadServiceList();
    });
});

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}

function LoadServiceList() {

    var currentCultureDiv = $("#CurrentCulture");
    var currentCulture = "sv";
    if (currentCultureDiv != null) {
        currentCulture = $(currentCultureDiv).text().trim();
    }

    var branchSelector = document.getElementById('ddBranchSelectorSpan').getElementsByTagName("select")[0];
    var selectedPageId = document.getElementById('ddBranchSelectorSpan').getElementsByTagName("select")[0].options[branchSelector.selectedIndex].value.split(",")[0];

    $.ajax({
        type: "POST",
        url: "/generichandlers/LoadCompanyServices.ashx?pageId=" + selectedPageId + "&CurrentCulture=" + currentCulture,
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json/text",

        success: function(msg) {
            var jsonValue = (new Function("return(" + msg + ");"))();
            var serviceList = document.getElementById('ddServiceSelectorSpan').getElementsByTagName("select")[0];

            // Delete all the options previously loaded.
            $(serviceList).find('option').remove();

            // Add all page options.
            for (var i = 0; i < jsonValue.length; i++) {
                $("<option>").attr("value", jsonValue[i].Key).text(jsonValue[i].Value).appendTo(serviceList);
            }

            if ($("#ddBranchSelectorSpan").find("select").attr("selectedIndex") != 0) $("#ddServiceSelectorSpan").find("select").find("option").eq(1).attr("selected", "selected");
        }
    });
}

function changeMargin() {

    var contentAreaHeight = document.getElementById('topContentWithLeftMenuContent').offsetHeight;
    var mediumBoxContentArea = document.getElementById('MediumBoxContentArea').offsetHeight;
    var rightContent = document.getElementById('rightContent').offsetHeight;
    var leftMenu = document.getElementById('leftMenu').offsetHeight;

    if (leftMenu > rightContent) {
        var diff = leftMenu - rightContent;

        $("#MediumBoxContentArea").css("height", diff + mediumBoxContentArea);
    }
}

function fixMenuTopImage() {
    var topImage = $(".sub .topImage").css("background-image");
    topImage = topImage.substr(5, topImage.length - 7);
    var img = new Image();
    img.src = topImage;
    if (img.width < 398) {
        var newPos = 542 + (398 - img.width);
        $(".sub .topImage").css("background-position", newPos);
        $(".sub .topImageFilter").css("background-position", newPos);
    }
}
