var initCloseMessages = function(){
	$(".msg .msg-close").unbind("hover");
	$(".msg .msg-close").unbind("click");
	$(".msg .msg-close").css({opacity:0.4});
	$(".msg .msg-close").hover(function(){
		$(this).css({opacity:1});
	},function(){
		$(this).css({opacity:0.4});
	});
	$(".msg .msg-close").click(function(){
		$(this).parent(0).fadeOut();
	});
};


$(document).ready(function(){
	/** HOVER & DOWN IMAGES **/
	$(".image-button").hover(function(){
		var src = $(this).attr("src");
		if($(this).attr("hover")){
			$(this).attr("src",$(this).attr("hover"));
			$(this).attr("hover",src);
		}	
	},function(){
		var src = $(this).attr("src");
		if($(this).attr("hover")){
			$(this).attr("src",$(this).attr("hover"));
			$(this).attr("hover",src);
		}	
	});
	$(".image-button").mousedown(function(){
		var src = $(this).attr("src");
		if($(this).attr("down")){
			$(this).attr("src",$(this).attr("down"));
			$(this).attr("down",src);
		}	
	});
	$(".image-button").mouseup(function(){
		var src = $(this).attr("src");
		if($(this).attr("down")){
			$(this).attr("src",$(this).attr("down"));
			$(this).attr("down",src);
		}	
	});
	
	/** SUBMIT FORM (IF POS) **/
	$(".image-button").click(function(){
		if($(this).parents("form:first").length == 1 && $(this).parents("form:first").attr("rel") != "ajax")
			$(this).parents("form:first").submit();
	});
	
	/** COUNTRY FLAG **/
	$("#country-choice, #online-chat").hover(function(){
		$(this).animate({backgroundPosition:'0px 2px'},"fast");
	},function(){
		$(this).animate({backgroundPosition:'0px 5px'},"fast");
	});
	$("body").append("<div class='tooltip'></div>");
	$("#country-choice[title]").tooltip({
   		tip:	'.tooltip',
   		position: "top center" 
	}); 

	
	/**
	 * COUNTRY SELECTOR
	 */
	$("#country-selector").css("display","none");
	var countrychoicestatus = "close";
	
	
	
	$("#country-choice").click(function(){
		
		$("#mainnav").data("edpmenu").hidebanners();
		$("#mainnav").data("edpmenu").closeAll();
		$("#mainnav").data("edpmenu").disable();
		$("#country-selector").slideDown(500,function(){
			$("#mainnav").data("edpmenu").showbanners();
			countrychoicestatus = "open";
		});
	});
	$("#country-selector-close,#country-choice").click(function(){
		if(countrychoicestatus == "open"){
			$("#mainnav").data("edpmenu").hidebanners();
			$("#country-selector").slideUp(500,function(){
				$("#mainnav").data("edpmenu").enable();
				$("#mainnav").data("edpmenu").reset();
				$("#mainnav").data("edpmenu").showbanners();
			});
			countrychoicestatus = "close"
		}
		return false;
	});
	$("#country-selector li").hover(function(){
		$(this).addClass("hover");
		$("#country-selector .country-view-holder img").addClass("hidden");
		$("#country-selector .country-view-holder img[rel='"+$(this).attr("rel")+"']").removeClass("hidden");
		
	},function(){
		$(this).removeClass("hover");
		$("#country-selector .country-view-holder img[rel='"+$(this).attr("rel")+"']").addClass("hidden");
		$("#country-selector .country-view-holder img[rel='int']").removeClass("hidden");
	});
	$("#country-selector li").click(function(){
		window.location = $("a",$(this)).attr("href");
	});
	
	
	$(".nav-items li").click(function(){
		var href = $(this).find("a").attr("href");
		window.location.href = href;
	});
	
	/** PRODUCT COMPARISONS **/
	if($(".product-comparison").length > 0){
		$(".product-comparison .comp-header li").click(function(){
			$(".product-comparison .active").removeClass("active");
			var rel = $(this).attr("rel");
			$(".product-comparison li[rel="+rel+"]").addClass("active");
		});
		
		var nrCols = $(".comp-body li").length;
		$(".comp-body li.def").css("width",((6-(nrCols*1))*20)+"%");
		
		/** LOOP THROUGH EVERY DIV OF EVERY LIST ITEM TO ADJUST LIST ITEM HEIGHTS **/
		var max = $(".comp-body li:first div").length;
		for(i=0;i<max;i++){
			var maxHeight = 0;
			$(".comp-body li").each(function(){
				var divs = $("div",$(this));
				if($(divs[i]).height() > maxHeight){
					maxHeight = $(divs[i]).height();
				}
			});
			$(".comp-body li").each(function(){
				var divs = $("div",$(this));
				$(divs[i]).css("height",maxHeight);
			});
		}
		/** ONLOAD HIGHLIGHT **/
		if($(".product-comparison .comp-header li.active").length > 0){
			$(".product-comparison .comp-header li.active").each(function(){
				var rel = $(this).attr("rel");
				$(".product-comparison li[rel="+rel+"]").addClass("active");
			});
		}
		
	}
	/** FILLING DIVS **/
	$(".filldiv").children().each(function(){
		$(this).css("height",$(this).parent(0).height());
	});
	
	
	$("#news-items li").click(function(){
		var href = $("a",$(this)).attr("href");
		window.location.href = href;
	});
	
	/** ALL BUTTONS WITH HREF ATTRIBUTE **/
	$("input[type='button']").click(function(){
		if($(this).attr("href")){
			window.location.href = $(this).attr("href");
			return false;
		}
		return true;
	});
	
	/** EDPNET MENU **/
	$("#mainnav").edpmenu({
		container	:	$("#sub-navigation"),
		panels 		: 	$("#navigation-panels"),
		crumbs		:	$("#crumbs")
	});	
	
	/** EMAIL SUBSCRIPTION **/
	$("#email-subscribe-button").click(function(){
		var email = $("#subscribe-email").attr("value");
		$("#subscribe-email").removeClass("error");
		$.ajax({
			   type		:	"POST",
			   url		:	baseHref+"/edp/subscribe/",
			   data		:	"email="+email,
			   dataType	:	"json",
			   success: function(ob){
			
					if(ob.response == false){
						$("#subscribe-email").addClass("error");
					}
					$(".subscribe-form").parent(0).find(".msg").fadeOut();
					$(".subscribe-form").parent(0).find("h2:first").after(ob.html);
					$(".subscribe-form").parent(0).find("p:first").hide();
					initCloseMessages();
					$(".subscribe-form").parent(0).find(".msg .msg-close").click(function(){
						$(".subscribe-form").parent(0).find("p:first").show();
					});
			   } 
		});
		return false;
	});
	
	
	
	/**
	 * BODY HEIGHT
	 */
	/*
	var eHeight = 0;
	if($("#sub-navigation").length > 0){
		eHeight = $("#sub-navigation").height();
	}
	var tHeight = $("#content").outerHeight() + $("#header").outerHeight() + $("#footer").outerHeight() + eHeight;
	
	if(tHeight < $(window).height()){
		var diff = $(window).height() - ($("#header").outerHeight() + $("#footer").outerHeight() + eHeight);
		$("#content").css("height",diff - 55);
	}
	*/
});

$(window).load(function(){
	$("#content").css("paddingBottom",$("#footer").height());
	var ntop = $("#footer").height() - ($("#footer").height()*2);
	$("#footer").css("marginTop",ntop);
});