$(document).ready(function(){
    
//make sure the CSS menu works in IE

    $("ul.mainmenu > li").hover(
        function(){
            $(this).children("ul").show();
        },
        function(){
            $(this).children("ul").hide();
        }
    );
   
//add arrows to leftmenu

    $("ul.submenu > li a").not(".selected").prepend('<img src="/images/arrow_normal.gif" border="0" />&nbsp;');
    $("ul.submenu > li a:first-child.selected").prepend('<img src="/images/arrow_selected.gif" border="0" />&nbsp;');
    
    
//handle hover for article images

    //put a href into my own attribute, and remove the a
    $(".shoppingcartitemimagecontainer .thumbnail").each(function(){
        $.preloadImages.add([$(this).children("a").attr("href")]);
        $(this).children("a").children("img").attr("hoverimage", $(this).children("a").attr("href")).css("cursor","pointer").appendTo($(this));
    });

    //do something on hover
    $(".shoppingcartitemimagecontainer .thumbnail img").hover(
        function(){
            if($(".shoppingcartitemimagecontainer .mainimage img").attr("src") != $(this).attr("hoverimage")) {
                var hoverimage = $(this).attr("hoverimage");
                $(".shoppingcartitemimagecontainer .mainimage").fadeOut(500, function() {
                    $(".shoppingcartitemimagecontainer .mainimage img").attr("src",hoverimage);
                    $(".shoppingcartitemimagecontainer .mainimage").fadeIn(500);
                });
            }
        }
    );
    
    
//make sure options are selected for ordering

    $(".shoppingcartform").submit(function() {
        var missingStuff = '';
         $(".shoppingcartitemproperty").each(function(){
            if($(this).find("select").val() == "") {
                missingStuff = missingStuff + ", " + $(this).find(".shoppingcartitempropertylabel").html().replace(": ","");
            }
         });
         if(missingStuff == "") {
            return true;
         } else {
            alert("You did not choose any options: " + missingStuff.substring(1));
            return false;
         }
    });
    
//add events on selects in the variatywizzardform

	$("#variatywizzardform select").change(function(){
		$("#variatywizzardform").submit();
	});
	
//missing fields handeling
	$(".missingfield").parents("tr").children("td").css("color","red");
	


//handle promo code
    $("#btnEnterpromocode").click(function(){
        openPromoCodePanel();
    });

    $("#btnEnterpromocodeExit").click(function(){
        closePromoCodePanel();
    });

    $("#btnEnterpromocodeApply").click(function(){
        $("#inputPromoCode").val($("#inputEnterPromoCode").val());
        $("#shoppingCartForm").submit();
    });

    $("#enterPromoCodeHelp").bt({
        fill: 'red',
        cssStyles: {color: 'white', fontWeight: 'bold'},
        shrinkToFit: true,
        padding: 10,
        cornerRadius: 10,
        spikeLength: 15,
        spikeGirth: 5,
        positions: ['top','left'],
        showTip: function(box){
            $(box).fadeIn();
        },
        hoverIntentOpts: {
            interval: 300,
            timeout: 100
        }
    });

    
});

$(window).load(function(){
    $.preloadImages.start();
    
//make sure middle content container has same hight as right or left container, after complete load

    var rightHight = $("#rightcontainer").height();
    var leftHight = $("#leftcontainer").height();
    var middleHight = $("#middlecontainer").height();
    
    if(rightHight > middleHight || leftHight > middleHight) {
        $("#middlecontainer").css("height", leftHight > rightHight ? leftHight : rightHight);
    }
    
});

function checkConditions() {
    if($("#conditionscheckbox:checked").length == 1) {
        return true;
    }
    alert("Please agree with our terms and conditions.");
    return false;
    
}


function openPromoCodePanel() {

    btnEnterpromocodeActive = true;

    $("#btnEnterpromocode").animate(
        {height: "79px"},
        200,
        "swing",
        function(){
            $("#enterPromoCodeContainer").fadeIn(300,function(){
				$("#inputEnterPromoCode").focus();
                $("#btnEnterpromocode").unbind("click");
                $("#enterPromoCodeButtonName").click(function(){
                    closePromoCodePanel();
                });
            });
        }
    );

    return true;
}
function closePromoCodePanel() {
    $("#enterPromoCodeContainer").fadeOut(300,function(){
	    $("#btnEnterpromocode").animate({height: "18px"},200,"swing",function(){
	        $("#inputEnterPromoCode").val("");
	        $("#btnEnterpromocode").click(function(){
	            openPromoCodePanel();
	        });
	    });
	});
}
