﻿/// <reference path="../library/jquery-1.3.1.js" />

var _compareDropdownTop;

$(document).ready(function() {
    _compareDropdownTop = $(".CompareDropdown").offset({ scroll: false }).top;
    $(window).scroll(MoveCompare);
    MoveCompare();
    InitiateFilterForm();
    BindECommerceButtons();
    FixProductGroups();
});

$(window).load(function() {
    FixProductPlaceholderHeights();
    FixLoggedInMargins();
});

function FixLoggedInMargins() {
    $("div.Searchresult ul li.LoggedIn span.CheckboxTable").not(":has(div)").addClass("LoggedInMargin");
}

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndAsyncRequest);
function EndAsyncRequest(sender, args) {
    FixProductGroups();
    ResetProductPlaceholderHeights();
    FixProductPlaceholderHeights();
    FixLoggedInMargins();
    BindECommerceButtons();
    if (sender._postBackSettings.sourceElement.id == "ctl00_ContentPlaceHolderMain_ContentPlaceHolderMiddleAndRight_ProduktFilter_AvMarkeraAllaVal3")
        FixCheckBoxesAndRadioButtons();
    InitiateFilterForm();
}

function InitiateFilterForm() {
    $(".FilterForm").show('slow');
}

function FixProductGroups() {

    // Check if page has productgroups
    var $lis = $("div.Searchresult ul.Flow li");
    var headerDiv = "div.header";

    if ($lis.children(headerDiv).length === 0)
        return;

    $lis.removeClass(function(index, css) {
        return (css.match(/\bligroup\S+/g) || []).join(' ');
    }).removeClass("Last");

    var groupIndex = 0;
    var productInSameGroupIndex = 1;
    $lis.each(function() {
        var $this = $(this);
        var hasHeadline = $this.children(headerDiv).children("h2").length > 0;

        if (hasHeadline || productInSameGroupIndex % 3 === 0) {
            groupIndex++;
        }

        if (hasHeadline)
            productInSameGroupIndex = 1
        else
            productInSameGroupIndex++


        if (productInSameGroupIndex > 3) {
            $this.children(headerDiv).remove();
        }

        $this.addClass("ligroup" + groupIndex);

        if (productInSameGroupIndex % 3 === 0)
            $this.addClass("Last");

        if ($this.next().children(headerDiv).children("h2").length > 0) {
            $("<div></div>")
                    .css("clear", "left")
                    .insertAfter($this);
        }

    });
}

function BindECommerceButtons() {
    // Informationsbubblor
    $("div.ProduktlistaPage p.h4x strong").click(function(e) {

        e.preventDefault();

        var DlTitle = $(this).text() + ' ' + $(this).attr("produktnamn");
        var DlDesc = $(this).parent().next().html();

        if ($(this).parent().hasClass('h4xwiz')) {
            //
        } else if ($("div.ProduktlistaPage p.h4x #DlPopup:visible")) {
            $("div.ProduktlistaPage p.h4x #DlPopup:visible").hide();
            $("div.ProduktlistaPage p.h4x").removeClass("h4xwiz");
            $(this).parent().append("<div id='DlPopup'><div class='Content'><strong>" + DlTitle + "</strong>" + DlDesc + "<a class='CloseLink' onclick='return closeProduktlistaPageECommercePanel();'>test</a></div><div class='Shadow'>&#160;</div></div>").addClass('h4xwiz');
        } else {
            $("div.ProduktlistaPage p.h4x #DlPopup:visible").hide();
            $("div.ProduktlistaPage p.h4x").removeClass("h4xwiz");
            $(this).parent().append("<div id='DlPopup'><div class='Content'><strong>" + DlTitle + "</strong>" + DlDesc + "<a class='CloseLink' onclick='return closeProduktlistaPageECommercePanel();'>test</a></div><div class='Shadow'>&#160;</div></div>").addClass('h4xwiz');
        }

        RegisterChooseArticleChangeEvent("div.ProduktlistaPage p.h4x #DlPopup div.ChooseArticelCont select");

    });
}

function ResetProductPlaceholderHeights() {
    $("div.Searchresult ul.Flow li img").css("position", "static");
    $("div.Searchresult ul li span.CheckboxTable").css("position", "static");
    $("ul.Flow li").css("height", "auto");
}

function FixProductPlaceholderHeights() {

    equalHeight($("ul.Flow li.ligroup1 > a"));
    equalHeight($("ul.Flow li.ligroup2 > a"));
    equalHeight($("ul.Flow li.ligroup3 > a"));
    equalHeight($("ul.Flow li.ligroup4 > a"));
    equalHeight($("ul.Flow li.ligroup5 > a"));
    equalHeight($("ul.Flow li.ligroup6 > a"));
    equalHeight($("ul.Flow li.ligroup7 > a"));
    equalHeight($("ul.Flow li.ligroup8 > a"));
    equalHeight($("ul.Flow li.ligroup9 > a"));
    equalHeight($("ul.Flow li.ligroup10 > a"));
    equalHeight($("ul.Flow li.ligroup11 > a"));
    equalHeight($("ul.Flow li.ligroup12 > a"));
    equalHeight($("ul.Flow li.ligroup13 > a"));
    equalHeight($("ul.Flow li.ligroup14 > a"));
    equalHeight($("ul.Flow li.ligroup15 > a"));
    equalHeight($("ul.Flow li.ligroup16 > a"));
    equalHeight($("ul.Flow li.ligroup17 > a"));
    equalHeight($("ul.Flow li.ligroup18 > a"));
    equalHeight($("ul.Flow li.ligroup19 > a"));

    equalHeight($("ul.Flow li.ligroup1"));
    equalHeight($("ul.Flow li.ligroup2"));
    equalHeight($("ul.Flow li.ligroup3"));
    equalHeight($("ul.Flow li.ligroup4"));
    equalHeight($("ul.Flow li.ligroup5"));
    equalHeight($("ul.Flow li.ligroup6"));
    equalHeight($("ul.Flow li.ligroup7"));
    equalHeight($("ul.Flow li.ligroup8"));
    equalHeight($("ul.Flow li.ligroup9"));
    equalHeight($("ul.Flow li.ligroup10"));
    equalHeight($("ul.Flow li.ligroup11"));
    equalHeight($("ul.Flow li.ligroup12"));
    equalHeight($("ul.Flow li.ligroup13"));
    equalHeight($("ul.Flow li.ligroup14"));
    equalHeight($("ul.Flow li.ligroup15"));
    equalHeight($("ul.Flow li.ligroup16"));
    equalHeight($("ul.Flow li.ligroup17"));
    equalHeight($("ul.Flow li.ligroup18"));
    equalHeight($("ul.Flow li.ligroup19"));

    $("div.Searchresult ul.Flow li img").css("position", "absolute");
    $("div.Searchresult ul li span.CheckboxTable").css("position", "absolute");

}

function MoveCompare() {

    if (_compareDropdownTop < 1) {
        _compareDropdownTop = $(".CompareDropdown").offset({ scroll: false }).top;
    }

    var winscrolltop = ($(window).scrollTop() + 10);

    if (winscrolltop > _compareDropdownTop) {
        $(".CompareDropdown").animate({ top: (winscrolltop - _compareDropdownTop + 10) + "px" }, { queue: false, duration: 350 });
    } else {
        $(".CompareDropdown").css("top", "0");
    }
}

function closeProduktlistaPageECommercePanel() {
    $("div.ProduktlistaPage #DlPopup").remove();
    $("div.ProduktlistaPage p.h4xwiz").removeClass("h4xwiz");
    return false;
}
