﻿if("\v" != "v")
{
    //loadFirebugConsole();
}

$(document).ready(function()
{
    //Set beta image(for ie)
    $(".topBetaLogo").css("border", "0px solid red");
    //Set first tab(for ie6)
    if($.browser.msie && $.browser.version == "6.0")
    {
        $(".boxStandardLargeTabs > .inner > .head li.first.selected").css("margin", "0 2px");
        // $(".boxStandardLargeTabs > .inner > .head li.first.selected a").css("padding-right", "10px");
    }
    
    //RUN MENU
    $("#menuSub").hide();
    
    var isHoverMenu;
    //Add event to menu li's
    $("#menuParent > ul > li").bind("mouseenter",function()
    {
        $("#menuSub").html($(this).html());
        if(isHoverMenu)
        {
			if($("#menuParent > ul > .selected")[0])
				$("#menuParent > ul > .selected")[0].className = "";
			
			this.className = "selected";
        }
    });
    
    //Add open/close event to topMenu
    $("#topMenu").bind("mouseenter",function()
    {
        isHoverMenu = true;
        
        setTimeout("OpenMenu()", 300);
        
    }).bind("mouseleave",function()
    {
        isHoverMenu = false;
    });
    
    OpenMenu = function()
    {
        if(isHoverMenu)
        {
            if ($("#menuSub").is(":hidden"))
            {
                $("#menuSub").toggle("blind", { direction: "vertical" }, 300);

                // Showing the submenu causes the pricegraph image to be moved down
                // on the page, so until the user hovers the images, hide the price box.
                if (window.priceGraph !== undefined)
                    window.priceGraph.hide();
                }
        }
    };
    
    
    
    //Global Expand
    GlobalExpand = function(elm)
    {
        $("." + elm + "> .expand").toggle("blind", { easing: "easeOutQuad", times: 1 }, 300);
    };
    
    
    
    //Filter Expand
    var filterOldHeight = 0;
    FilterExpand = function(btn, elm)
    {
        var height = (jQuery.browser.msie) ? $("#" + elm).height() + 80 : $("#" + elm).height() + 15;

        filterOldHeight = (filterOldHeight == 0) ? $("." + elm + " fieldset").height() : filterOldHeight;
        
        if(filterOldHeight >= $("." + elm + " fieldset").height())
        {
            $("." + elm + " fieldset").animate({height: height, easing: "easeOutQuad"}, 300);
            btn.className = "btnLessfilters";
        }
        else
        {
            $("." + elm + " fieldset").animate({height: filterOldHeight, easing: "easeOutQuad"}, 300);
            btn.className = "btnMorefilters";
        }
    };
    
    //Filter price
    FilterSlider = function(id, e, ui, minPrice, maxPrice)
    {
        var maxSliderWidth = $('#slidePrice').width() - 8;
        var leftOffset = $(".ui-slider-handle")[0].offsetLeft;
        var leftOffsetPercentage = (leftOffset / maxSliderWidth);
        var currentMinPrice = leftOffsetPercentage * (maxPrice - minPrice) + minPrice;
        var currentMaxPrice = (leftOffsetPercentage + (ui.range / 100)) * (maxPrice - minPrice) + minPrice;
        
        $("#priceMin")[0].value = Math.round(currentMinPrice);
        $("#priceMax")[0].value = Math.round(currentMaxPrice);
    };
    
    //Init Filter tabs
    FilterTabs = function(id, hiddenId)
    {
        if($(hiddenId)[0].value != "")
        {
            CheckFilterTabs(id, hiddenId);
        }
        
        $(id).bind("mouseover",function()
        {
            $(id + " .tabContent .select")[0].style.maxHeight = "300px";
        });
        
        $(id).bind("mouseout",function()
        {
            $(id + " .tabContent .select")[0].style.maxHeight = "45px";
        });
        
        $(id + " > .tabs > ul > li").bind("click",function()
        {
            $(id + " .tabContent .image").attr("class", "image " + $("#select_" + this.id + " ul")[0].className);
                        
			$(id + " > .tabs > ul > .selected").removeClass("selected");
			this.className = "selected";
			
            $(id + " .tabContent .select div").css("display", "none");
    		$("#select_" + this.id).css("display", "block");
        });
        
		$(id + " .tabContent .select ul > li").bind("click",function()
        {
		    this.className = (this.className == "selected") ? "" : "selected";
		    
		    $(hiddenId)[0].value = "";
		    
		    for(var i = 0; i < $(id + " .tabContent .select ul > .selected").length; i++)
            {
                $(hiddenId)[0].value += $(id + " .tabContent .select ul > .selected")[i].id + ",";
            }
            
            //Smid Ajax kald her... $(hiddenId)[0].value indeholder id'er på de valgte items.
        });
        
        //Run
        $(id + " > .tabs > ul > li")[0].className = "selected";
        $(id + " .tabContent .image").attr("class", "image " + $(id + " .tabContent .select ul")[0].className);
        $(id + " .tabContent .select div")[0].style.display = "block";
    };
    
    //Check if filter is selected
    CheckFilterTabs = function(id, hiddenId)
    {
        var hidden = $(hiddenId)[0].value;
        for(var i = 0; i < hidden.split(",").length-1; i++)
        {
            for(var j = 0; j < $(id + " .tabContent .select ul > li").length; j++)
            {
                if(hidden.split(",")[i] == $(id + " .tabContent .select ul > li")[j].id)
                {
                    $(id + " .tabContent .select ul > li")[j].className = "selected";
                }
            }
        }
    }
    
    
    
    //Image hover
    ToggleImageHover = function(elm, close, text)
    {
        if(close)
        {
            $(".productListImageHover").remove();
        }
        else
        {
            var offset  = $(elm).offset();
            var style = "left: " + (offset.left + 82) + "px; top: " + (offset.top - 90) +"px";
            var img = $(elm).html();
            $("<div class=\"productListImageHover\" style=\"" + style + "\">" + img + "<p>" + text + "</p></div>").appendTo("body");
        }
    };
    
    //Image hover 2
    ToggleImageHover2 = function(elm)
    {
        if($(".productListImageHover2").is(":visible"))
        {
            $(".productListImageHover2").remove();
        }
        else
        {
            var offset  = $(elm).offset();
            var hoverDiv = document.createElement("div");
            hoverDiv.innerHTML = $(elm).parent().get(0).innerHTML;
            hoverDiv.className = "productListImageHover2";
            
            $(hoverDiv).appendTo("body");
            hoverDiv.style.left = (offset.left - 70) + "px";
            hoverDiv.style.top = (offset.top - 95) + "px";
        }
        if($(".productListImageHover2 > .image").get(0))
        {
            $(".productListImageHover2 > .image").get(0).onmouseover = function (){}
            $(".productListImageHover2").get(0).onmouseout = function (){$(".productListImageHover2").remove();}
        }
    };
    
    
    
    //Rating hover
    ToggleRating = function(elm, text, type)
    {
        if(text == "")
        {
            $(".ratingHover").remove();
        }
        else
        {
            var offset  = $(elm).offset();
            var style = "left: " + (offset.left + 10) + "px; top: " + (offset.top + 15) +"px";
            $("<div class=\"ratingHover\" style=\"" + style + "\"><p class=\"" + type + " \">" + text + "</p></div>").appendTo("body")
        }
    };
    
    
    var isTypingActive = true;
    //Check type ahead typing
    Checkypeahead = function(elm, isActive)
    {
		OpenTypeahead(elm);
        //isTypingActive = isActive;
        
        /*$(elm).keydown(function(e)
        {
            if(e.which != 27)
            {
                if(!isActive)
                {
                    setTimeout(function() {isTypingActive = false; }, 300);
                    OpenTypeahead(elm);
                }
            }
        });*/
    };
    
	var TypeAheadQueryValue = '';
	var Timer;
	var NavigationMode = false;
	
    //Open type ahead
    Opentypeahead = function(elm)
    {
		if(NavigationMode == false)
		{
			if(Timer)
			{
				clearTimeout(Timer);
				//console.log('clearing timer');
			}
		
			//console.log('starting new timer');
			Timer = setTimeout(function(){RunTypeaheadSearch(elm);},300);
		}
		else if(TypeAheadQueryValue != $(elm)[0].value)
		{
			//$('.typeahead ul')[0].innerHTML = ''; //clear med jquery
			//TypeAheadQueryValue = $(elm)[0].value;
			//suggestionAjax(elm);
			//console.log('exiting navigation mode');
			NavigationMode = false;
			Opentypeahead(elm);
		}
		else
		{
			//console.log('in navigation mode');
			RunTypeahead(elm);
		}
    };
    
	RunTypeaheadSearch = function(elm)
	{		
		if($(elm)[0].value == "")
        {
            CloseTypeahead();
			TypeAheadQueryValue = '';
            $('.typeahead ul')[0].innerHTML = ''; //clear med jquery			
        }
        else
        {

			if(TypeAheadQueryValue != $(elm)[0].value)
			{
				$('.typeahead ul')[0].innerHTML = ''; //clear med jquery
				TypeAheadQueryValue = $(elm)[0].value;
				suggestionAjax(elm);
			}
			else
			{
				//console.log("typeaheadvalue " + TypeAheadQueryValue);
				//console.log("elm value " + $(elm)[0].value);
				RunTypeahead(elm);
			}
        }
	};
	var child = 0;
    //Run type-ahead
    RunTypeahead = function(elm)
    {
		NavigationMode = true;
		//console.log(elm.value);
        
        var totalChilds = $(".typeahead ul > li").length;
        if(totalChilds > 0)
        {
			
            $(elm).keydown(function(e)
            {
                switch(e.which)
                {
                    case 40:
                        //console.log(child);
                        ////console.log($(".typeahead > ul > li:first"));
                        if(child >= 0 && child < totalChilds)
                        {
                            ////console.log($(".typeahead li:nth-child(" + child + ")")[0].className);
                            extraChild = ($(".typeahead li:nth-child(" + (child + 1) + ")")[0].className == "headline first" || $(".typeahead li:nth-child(" + (child + 1) + ")")[0].className == "headline") ? 2 : 1;
                            child += extraChild;
                            $(".typeahead li:nth-child(" + (child - extraChild) + ")").removeClass("selected");
							//$("#typeahead ul > li").removeClass("selected");
                            $(".typeahead li:nth-child(" + child + ")").addClass("selected");
                            ////console.log($(".typeahead li:nth-child(" + (child) + ")"));
                            ////console.log(child);
                        }
                    break;
                    
                    case 38:
                        ////console.log($(".typeahead > ul > li:first"));
                        if(child > 1 && child < (totalChilds + 1))
                        {
                            extraChild = ($(".typeahead li:nth-child(" + (child - 1) + ")")[0].className == "headline first" || $(".typeahead li:nth-child(" + (child - 1) + ")")[0].className == "headline") ? 2 : 1;
                            child = (child - extraChild);
                            $(".typeahead li:nth-child(" + (child + extraChild) + ")").removeClass("selected");
							//$("#typeahead ul > li").removeClass("selected");
                            $(".typeahead li:nth-child(" + child + ")").addClass("selected");
                        }
                        ////console.log(child);
                    break;
                    
                    case 13:
                        if($(".typeahead li:nth-child(" + child + ")")[0])
                            window.location = $(".typeahead li:nth-child(" + child + ")")[0].id;
					CloseTypeahead();
					stopSuggestionAjax();
                    break;
                    
                    case 27:
                        CloseTypeahead();
                    break;
                }
                
                ////console.log(child);
            });
        }
        else
        {
            CloseTypeahead();
        }
    };
    
    //Close type ahead
    CloseTypeahead = function()
    {
        if ($("#typeahead").is(":visible"))
        {
            $("#typeahead").slideUp(300);
            $("#typeahead ul > li").removeClass("selected");
            $("#colRightTopAd .inner").css("display", "block");
        }
    };
    
    FocusTypeahead = function()
	{
		var totalChilds = $(".typeahead ul > li").length;
        if(totalChilds > 0)
		{
			if ($("#typeahead").is(":hidden"))
	        {
	            $("#typeahead").slideDown(300);
				$("#colRightTopAd .inner").css("display", "none");
	        }
		}
	};
	
    
    //Scroll box
    ScrollBox = function(elm, direction)
    {
        var list = $("#"+ elm);
        
        if(direction == 'left')
        {
            $(list).prepend($("#"+ elm + " li:last"));
        }
        
        if(direction == 'right')
        {
            //add elm
            //$("#"+ elm + " li:first").appendTo($("#" + elm + " > li"));
            $(list).append($("#"+ elm + " li:first"));
            //$("#"+ elm + " li:first").animate({width: 0, easing: "easeOutQuad"}, 300);
            //$("#"+ elm + " li:last").css("width", "143px");
            //del elm
            //$("#"+ elm + " li:first").remove();
            //$(list).append($("#"+ elm + " li:first"));
        }
    };
    
    
    
    //Compare functions
    CompareClose = function(elm)
    {
        var image = $("#" + elm + " > .top")[0];
        image.className = (image.className == "top") ? "top closed" : "top";
        
        $("#" + elm + " > table").toggle("blind", { direction: "vertical" }, 300);
    }
    
    CompareDelete = function(row)
    {   
        var count = $(".boxStandardCompare .delete").length;
        if(count > 2)
        { 
            for(var i = 0; i < $(".boxStandardCompare table tr").length; i++)
            {
                for(var j = 0; j < $(".boxStandardCompare table tr:eq(" + i + ") td").length; j++)
                {
                    if(j == row)
                    {
                        $(".boxStandardCompare table tr:eq(" + i + ") td:eq(" + row + ")").remove();
                    }
                }
            }
        }
        
        for(var r = 0; r < $(".delete").length; r++)
        {
            $(".delete")[r].id = r + 1;
        }
    };
    
    
    
    //Product info toggle
    ProductInfo = function(elm, info)
    {
        var height = $("#" + info + " h3").height();
        if($("#" + info).height() == 56)
        {
            //$(elm).fadeOut(200);
            $(elm).html("Skjul tekst");
            $("#" + info).animate({height: height, easing: "easeOutQuad"}, 300);
        }
        else
        {
            $(elm).html("Læs mere");
            $("#" + info).animate({height: 56, easing: "easeOutQuad"}, 300);
        }
    };
    
    
    
    //DealerInfo toggle
    ToggleDealerInfo = function(elm)
    {
        if($("#" + elm).is(":hidden"))
        {
            //$("#" + elm).css("display", "block");
            $("#" + elm).fadeIn(300);
        }
        else
        {
            $("#" + elm).css("display", "none");
            //$("#" + elm).fadeOut(300);
        }
    };
    
    
    //Full list expand
    FulllistExpand = function(elm, btn)
    {
        var minHeight = 0;
        var maxHeight = $("#" + elm + " ul").height() + 15;
        
        for(var i = 0; i < $("#" + elm + " ul li").length; i++)
        {
            if($("#" + elm + " ul li")[i] && i < 5)
            {
                minHeight += $("#" + elm + " ul li")[i].offsetHeight;
            }
        }
        
        if($("#" + elm)[0].offsetHeight == minHeight)
        {
            $("#" + elm).animate({height: maxHeight, easing: "easeOutQuad"}, 300);
            btn.className = "btnClose";
        }
        else
        {
            $("#" + elm).animate({height: minHeight, easing: "easeOutQuad"}, 300);
            btn.className = "btnFulllist";
        }
    };
    
    //Full list set height
    SetListHeight = function(elm)
    {
        var height = 0;
        
        for(var i = 0; i < 5; i++)
        {
            if($("#" + elm + " ul li")[i])
            {
                height += $("#" + elm + " ul li")[i].offsetHeight;
            }
        }
        
        $("#" + elm)[0].style.height = height + "px";
    };
    
    //Toggle product spec sub level
    ProductSpecToggle = function(elm)
    {
        for(var i = 0; i <$(elm).nextAll().length; i++)
        {
            if($(elm).nextAll()[i].nodeName == "UL")
            {
                $(elm).parent().get(0).className = ($(elm).parent().get(0).className == "sub") ? "sub open" : "sub";
                $(elm).nextAll()[i].style.display = ($(elm).nextAll()[i].style.display == "" || $(elm).nextAll()[i].style.display == "none") ? "block" : "none";
            }
        }
    };
    
    //Toggle all product spec sub levels
    ProductSpecToggleAll = function(elm)
    {
        for(var i = 0; i <$(".boxStandardProductSpecList h2.sub").length; i++)
        {
            ProductSpecToggle($(".boxStandardProductSpecList h2.sub")[i]);
        }
    };
});