function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

var gTopMenuCloseTimerId = null;

function startTopMenuCloseTimer()
{
    stopTopMenuCloseTimer();
    gTopMenuCloseTimerId = setTimeout(closeTopMenu, 800);
}

function stopTopMenuCloseTimer()
{
    if (gTopMenuCloseTimerId != null)
    {
        clearTimeout(gTopMenuCloseTimerId);
        gTopMenuCloseTimerId = null;
    }
}

function openTopMenu($menuElement)
{
	if ($menuElement.children().next().hasClass("Visible"))
	{
		// nothing..
	} 
	else 
	{
		$(".HeaderNav .Top .Visible").fadeOut().removeClass("Visible");
		$(".HeaderNav .Top").removeClass("Hover");
		if ($menuElement.children().length == 2)
		{
			$menuElement.addClass("Hover");
		} 
		else 
		{
			//
		}
		$menuElement.children().next().show().addClass("Visible");
		if (($menuElement.children("div.SubMenuWide").length > 0) &&
		    !($.browser.msie && ($.browser.version == '6.0')))
		{
		    // Det här gör menyerna i Våra produkter lika höga. I ie6 försvinner pilarna framför
		    // menyalternativen när man gör det. Därför gör vi inte det i ie6.
		    equalHeight($(".HeaderNav div div.SubMenuWide ul"));
		}
	}
}

function closeTopMenu()
{
    if ($(".HeaderNav .Top .SubMenu:visible")){
        $(".HeaderNav .Top").removeClass("Hover");
        $("div.SubMenu").fadeOut().removeClass("Visible");
    } else {
        // nothing..
    }
}

function AlternatePrimaryTable() {
    $("#Primary table.AlternateRows tr:odd").addClass('Alternate');
}

function WrapPrimaryTable() {
    $("#Primary table").wrap("<div class='PrimaryTable'></div>");
}


function RePreparePage() {

    // -------------------------
    // Equal height

    equalHeight($("ul.Flow li.first, .PictureAdArchive li.First"));
    equalHeight($("ul.Flow li.second, .PictureAdArchive li.Second"));
    equalHeight($("ul.Flow li.third, .PictureAdArchive li.Third"));

    equalHeight($(".PictureAdArchive li.Ligroup1"));
    equalHeight($(".PictureAdArchive li.Ligroup2"));
    equalHeight($(".PictureAdArchive li.Ligroup3"));
    equalHeight($(".PictureAdArchive li.Ligroup4"));
    equalHeight($(".PictureAdArchive li.Ligroup5"));
    equalHeight($(".PictureAdArchive li.Ligroup6"));
    equalHeight($(".PictureAdArchive li.Ligroup7"));
    equalHeight($(".PictureAdArchive li.Ligroup8"));
    equalHeight($(".PictureAdArchive li.Ligroup9"));
    equalHeight($(".PictureAdArchive li.Ligroup10"));
    equalHeight($(".PictureAdArchive li.Ligroup11"));
    equalHeight($(".PictureAdArchive li.Ligroup12"));
    equalHeight($(".PictureAdArchive li.Ligroup13"));
    equalHeight($(".PictureAdArchive li.Ligroup14"));
    equalHeight($(".PictureAdArchive li.Ligroup15"));
    equalHeight($(".PictureAdArchive li.Ligroup16"));
    equalHeight($(".PictureAdArchive li.Ligroup17"));
    equalHeight($(".PictureAdArchive li.Ligroup18"));
    equalHeight($(".PictureAdArchive li.Ligroup19"));

}

function PreparePage() {

    $("input[type='checkbox']").addClass('checkbox');
    FixCheckBoxesAndRadioButtons();

    // -------------------------
    // Equal height
    RePreparePage();
    
    equalHeight($(".StartPage .Teasers div.Item"));

    // -------------------------
    // CSS

    $("div.ProductInformation dl dd, div.ActiveLayers span, .Form li span.Information span.FormPopup").hide();

    $(".ItemLeafsBottomLeft, .QuickOrder").append("<span class='LeafsBottomLeft'></span>");
    $(".ShoppingList").append("<span class='LeafsTopRight'></span>");

    $(".CategoryPage, .ProductStartPage, .Searchresult, .SearchResultPage, .ErrorPage, .PictureAdArchive, .CatalogPage, .ProductPage .PrimaryColLarge, .OrderList, .CurrentItems, .OrderConfirmation, .ItemList").append("<span class='LeafsBottomLeft'></span>").prepend("<span class='LeafsTopRight'></span>");

    WrapPrimaryTable();

    //$(".CategoryPage").append("<span class='LeafsBottomLeft'>a</span>").prepend("<span class='LeafsTopRight'></span>");

    $(".CompanyStartpage").prepend("<span class='LeafsTopRight'></span>");

    $(".QuickOrderPage").prepend("<span class='LeafsBottomLeft'></span>");

    $("input[type='text']").addClass('Text');

    // Alternate rows 1
    $(".FilterForm li:odd").addClass('Alternate');
    AlternatePrimaryTable();

    // Alternate rows 2
    $(".ProductPageDetail tr").removeAttr('class');
    $(".ProductPageDetail tr:even").addClass('Alternate');

    $("ul#SecondaryNav ul").each(function() {
        $("ul#SecondaryNav ul li:first-child").addClass("First");
    });

    // -------------------------
    // Information Form Popup
    $(".FilterForm li span.Information span.Bubble").click(function() {
        if ($(this).hasClass('Visible')) {
            $(".Form li span.Information span.Bubble").removeClass('Visible');
            $(".Form li span.Information span.FormPopup").hide();
        } else if ($(".Form li span.Information span.Bubble").hasClass('Visible')) {
            $(".Form li span.Information span.Bubble").removeClass('Visible');
            $(".Form li span.Information span.FormPopup").hide();
            $(this).addClass('Visible');
            $(this).next().show();
        }

        else {
            $(this).addClass('Visible');
            $(this).next().show();
        }
    });

    $(".Form li span.Information span.Close").click(function() {
        $(".Form li span.Information span.FormPopup").hide();
        $(".Form li span.Information span.Bubble").removeClass('Visible');
    });

    // -------------------------
    // Reseller map
    ImagemapHover(".Layer1", "Active");
    ImagemapHover(".Layer2", "Active");
    ImagemapHover(".Layer3", "Active");
    ImagemapHover(".Layer4", "Active");
    ImagemapHover(".Layer5", "Active");
    ImagemapHover(".Layer6", "Active");
    ImagemapHover(".Layer7", "Active");
    ImagemapHover(".Layer8", "Active");
    ImagemapHover(".Layer9", "Active");
    ImagemapHover(".Layer10", "Active");
    ImagemapHover(".Layer11", "Active");
    ImagemapHover(".Layer12", "Active");
    ImagemapHover(".Layer13", "Active");
    ImagemapHover(".Layer14", "Active");
    ImagemapHover(".Layer15", "Active");
    ImagemapHover(".Layer16", "Active");
    ImagemapHover(".Layer17", "Active");
    ImagemapHover(".Layer18", "Active");
    ImagemapHover(".Layer19", "Active");
    ImagemapHover(".Layer20", "Active");
    ImagemapHover(".Layer21", "Active");
    ImagemapHover(".Layer22", "Active");
    ImagemapHover(".Layer23", "Active");
    ImagemapHover(".Layer24", "Active");
    ImagemapHover(".Layer25", "Active");


    function ImagemapHover(selector, classname) {
        $(selector).hover(
		function() {
		    $(selector).addClass(classname);
		    $(".ActiveLayers span" + selector).show();
		},
		function() {
		    $(selector).removeClass(classname);
		    $(".ActiveLayers span" + selector).hide();
		});
    }

    // -------------------------
    // Product Navigation

    ProductNavHover(".Presentation");
    ProductNavHover(".Information");
    ProductNavHover(".Matt");

    function ProductNavHover(selector) {
        $("area" + selector).hover(
		function() {
		    $(".ProductNav span" + selector).show();
		},
		function() {
		    $(".ProductNav span" + selector).hide();
		});
    }

    // -------------------------
    // Header Navigation

    $(".HeaderNav div div.SubMenu").hide();
    $(".HeaderNav div:has(div)").addClass("SubMenuHolder");
    $(".HeaderNav div:has(div) > *").removeClass("SubMenuHolder");

    $(".HeaderNav .Top").hover(
		function() {
		    stopTopMenuCloseTimer();
		    openTopMenu($(this));
		},
		function() {
		    startTopMenuCloseTimer();
		});

    $(".HeaderNav .Top .SubMenu").hover(
		function() {
		    stopTopMenuCloseTimer();
		    openTopMenu($(this).parent());
		},
		function() {
		    startTopMenuCloseTimer();
		});

    $("body").click(function() {
        closeTopMenu();
    });

}

$(document).ready(PreparePage);

function FixCheckBoxesAndRadioButtons() {
    $('input[type=checkbox],input[type=radio]').prettyCheckboxes({
        checkboxWidth: 14,
        checkboxHeight: 14,
        className: 'Checkbox',
        display: 'inline'
    });
}

function HandleEnterForAspNetButton(textbox, button) {
    $(textbox).keydown(function(e) {
        if (e.keyCode === 13) {
            e.preventDefault();
            eval($(button).attr("href"));
        }
    });
}
