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();
	});
};
var printPage = false;

var positionBanners;
$(document).ready(function(){
	/** CENTER TOPBANNERS **/
	positionBanners = function(){
		var pageWidth = $("#banners").width();
		$("#banners img").each(function(){
			if($(this).width() > 0){
				var posLeft = (pageWidth - $(this).width())/2;
				$(this).css({left:posLeft});
			}
		});
		$("#banners .banner-wrapper").each(function(){
			if($(this).width() > 0){
				var posLeft = (pageWidth - $(this).width())/2;
				$(this).css({left:posLeft});
			}
		});
	};
	$("#banners img").css("visibility","hidden");
	$("#banners img:first").css("visibility","visible");
	positionBanners();
	$(window).bind("resize", function(){positionBanners();});
	
	/**CENTER NAVIGATION **/
	var positionNav = function(){
		var containerWidth = $("#navigation .inner-container").width();
		var count = $("#navigation a").length;
		var width = Math.floor((920 - (count*1) - 1) / count);
		$("#navigation a span").css("width",width);
		$("#sub-navigation ul").css("width",width-10);
		$("#sub-navigation ul li").hover(function(){
			$(this).css("cursor","pointer");
			$("a",$(this)).addClass("hover");
			$("a",$(this)).animate({
				paddingLeft				:	"20",
				backgroundPosition		:	"5px center"	
			},250);
		},function(){
			$("a",$(this)).stop();
			$("a",$(this)).removeClass("hover");
			$("a",$(this)).css({
				paddingLeft 		: 	5,
				backgroundPosition	:	"-10px center"		
			});
		});
		$("#sub-navigation ul li").click(function(){
			if($("a",$(this)).length > 0 && $("a:first",$(this)).attr("href")){
				window.location.href = $("a:first",$(this)).attr("href");
			}
		});
		var maxulheight = 0;
		$("#sub-navigation ul").each(function(){
			if($(this).height() > maxulheight){
				maxulheight = $(this).height();
			}
		});
		$("#sub-navigation ul").css("height",maxulheight);
	};
	$(window).bind("resize",function(){positionNav();});
	positionNav();
	
	//if(curlang != "ru-RU"){
		Cufon.replace('h1,h2,h3,h5,h6,#content h4');
		if($.browser.msie && $.browser.version > 6){
		}else{
			Cufon.replace('#footer h4');
		}
		Cufon.replace('.customfont');
	//}
	
	if($.browser.msie && $.browser.version < 7){
		$("#content,#footer .footer-content,body.splash").pngFix();
		$("#header #logo").pngFix();
		$("#header #myedpnet").pngFix();
	}
	if($.browser.msie && $.browser.version < 9){
		$(".btn-black").each(function(){
			if(!$(this).hasClass("red-gradient") && !$(this).hasClass("purple-gradient")){
				$(this).corner("5px");
			}
		});
		$(".btn-order").corner("20px");
	}
	
	if($(".product-block-inline .product-info").length > 0){
		var maxh = 0;
		$(".product-block-inline .product-info").each(function(){
			if($(this).height()>maxh)
				maxh = $(this).height();
		});
		$(".product-block-inline .product-info").css("height",maxh);
	}
	// PUSH DOWN FOOTER 
	$("#content").css("paddingBottom",$("#footer").height()+50);
	
	// ADD SCROLLTOTOP 
	if($(window).height() < $("#wrapper").height()){
		var ignoreScrollTop = false;
		$("body").append("<div id='scrolltop'></div>");
		if($.browser.msie && $.browser.version < 9){
			var scrlimg = $("#scrolltop").css("backgroundImage").replace('url("','').replace('")','');
			$("#scrolltop").css({
				background	:	"none",
				filter		:	'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+scrlimg+'", sizingMethod="scale")'
			});
		}
		$("#scrolltop").click(function(){
			ignoreScrollTop = true;
			$("#scrolltop").fadeOut(750);
				
			var scrollelement = 'html';
			if($.browser.webkit)
				scrollelement = 'body';
			$(scrollelement).animate({scrollTop:0},1000,"easeOutCubic",function(){ignoreScrollTop = false;});
		});
		if($.browser.msie && $.browser.version < 7)
			$("#scrolltop").pngFix();
		
		/**
		 * SHOW ON SCROLL
		 */
		$(window).scroll(function (event) {
			var y = $(this).scrollTop();
			var pos = Math.floor(($(window).height()+y) * 0.90);
			if(!ignoreScrollTop){
				if(y > 0){
					$("#scrolltop").fadeIn(750);
				}else{
					$("#scrolltop").fadeOut(750);
				}
			}
		});
	}
		
	// HOWTO SHOW SUBNAVIGATION
	$("#navigation a:first").addClass("first");
	$("#sub-navigation ul:first").addClass("first");
	var subnavheight = $("#sub-navigation").height() + 8;
	var shown = false;
	var timeout = false;
	var showtimeout = false;
	$("#sub-navigation").css({
				marginTop	: 	-(subnavheight+10),
				visibility	:	"visible"
			});
	$("#navigation a").css("cursor","pointer");
	
	if($("#sub-navigation ul li").length > 0){
		$("#navigation a").hover(function(){
			$(this).addClass("hover");
			if(!shown){
				showtimeout = setTimeout(function(){_showsubmenu();},250);
			}else{
				_clearHideTimer();
			}
		},function(){
			$(this).removeClass("hover");
			_setHideTimer();
			clearTimeout(showtimeout);
		});
	}else{
		$("#sub-navigation ul").remove();
	}
	
	$("#sub-navigation").hover(function(){
		_clearHideTimer();
	},function(){
		_setHideTimer();
	});
	var _showsubmenu = function(){
		if(shown == false){
			$("#sub-navigation").animate({marginTop: -8},250,"swing");
			shown=true;
			_clearHideTimer();
		}
	};
	var _hidesubmenu = function(){
		$("#sub-navigation").animate({marginTop: -(subnavheight+10)},250,"swing");
		shown = false;
	}
	var _clearHideTimer = function(){
		if(timeout){
			clearTimeout(timeout);
			timeout = false;
		}
	};
	var _setHideTimer = function(){
		if(!timeout){
			timeout = setTimeout(function(){_hidesubmenu();},20);
		}
	};
	
	// LANG MENU
	$(".input-field").each(function(){
		var menuObjectData = [];
		var inputfield = $(this);
		
		if($(".dropdown",$(this)).length > 0){
			$(".dropdown",$(this)).children().each(function(){
				var el = $(this);
				 var listItem = {
			         label		: 	$(this).html(),
			         id			: 	$(this).html(),
			         onclick	:	function(item){
					 				TRTracker._link(el.attr("href"));
					 				return false;
					 				//window.location = el.attr("href");
				 				}	
			         };
			         menuObjectData.push(listItem);
			         if(el.hasClass("active")){
			        	 $("input",inputfield).attr("value",$(this).html());
			         }
			});
			$(".dropdown",inputfield).remove();
			$("input",inputfield).css("cursor","pointer");
			$("input",inputfield).click(function(){
				$(this).blur();
			});
			var conf = {
				data			:	menuObjectData,
				preload			:	true
			};
			var menu = new MiniMenu(conf);
			inputfield.append(menu);
			if($.browser.version < 7 && $.browser.msie){
				$("ul",menu).css("width",inputfield.width()-5);
			}
			$("img,input",inputfield).click(function(){
				menu.show();
				return false;
			});
		}
	});
	
	
	/** GET ALL COL .last for IE6 */
	if($.browser.msie && $.browser.version < 6){
		var numbers = array("one","two","three","four","five","six","seven","eight","nine");
		$(".col").each(function(){
			if($(this).hasClass("last")){
				var classname = $(this).attr("class");
				var classes = classname.split(" ");
				var replaced = false;
				for(a=0;a<classes.length;a++){
					for(i=0;i<numbers.length;i++){
						if(classes[a].indexOf(numbers[i]) !== -1){
							classes[a] = classes[a]+"-last";
							replaced = true;
						}
					}
				}
				if(replaced == true){
					$(this).attr("class",classes.join(" "));
				}
			}
		});
	}
	
	/** ONLINE SUPPORT **/
	if(department){
		var url = escape( location.toString() ) ;
		//department = 8;
		
		var lng = $('meta[http-equiv=Content-language]').attr("content");
		switch(lng){
			case "dut-NL":	lng = "nl";break;
			case "dut-BE":	lng = "nl";break;
			case "fr-BE":	lng = "fr";break;
			case "en-GB":	lng = "en";break;
		}
		var request_url = "/phplive/request.php?lng="+lng+"&l=edpnet&x=1&deptid="+department+"&pagex="+url ;
		
		$(".open-chat-button").attr("href",request_url);
		$(".open-chat-button").colorbox({width:500, height:560, iframe:true});
	}
	$(".colorbox-link").colorbox();
	
	/** SEARCH **/
	var searchSite = function(){
		var value = $("#search").attr("value");
		if(value){
			window.location.href = baseHref+"/spider/search/?query="+value+"&search=1";
		}
	};
	$("#search").keyup(function(e){
		if(e.which == 13){
			searchSite();
		}
	});
	$("#search-button").click(function(){
		searchSite();
	});
	
	
	printPage = function(el){
		var url 	= $(el).attr("href");
		var iframe	= false;
		if($("iframe.printpage").length == 0){
			iframe = $("<iframe src='' width='50' height='50' frameborder='0' scrolling='no'></iframe>");
			iframe.css({
				visibility	:	"visible",
				opacity		:	1,
				position	:	"absolute",
				right		:	0,
				bottom		:	0
			});
			iframe.addClass("printpage");
			$("body").append(iframe);
		}else{
			iframe = $("iframe.printpage");
		}
		iframe.attr("src",url);
		iframe.load(function(){
			
		});

		return false;
	}
	
	if($(".rotating-image").length > 1){
		
		var rotateBanner = function(){
			setTimeout(function(){
				$(".rotating-image:last").fadeOut(500,function(){
					$(this).parent(0).prepend($(this));
					$(this).css("display","block");
				});
				rotateBanner();
			},5000);
		}
		
		$(".rotating-image").css({
			position	:	"absolute",
			top			:	0
		});
		var count = 1;
		$(".rotating-image img").each(function(){
			if($(this).attr("height") > $(this).parent(0).parent(0).height()){
				 $(this).parent(0).parent(0).css("height",$(this).attr("height"));
			}
			//$(this).parent(0).css("zIndex",100-count);
			count++;
		});
		rotateBanner();
	}
	
	
	
});
$(window).load(function(){
	if($.browser.version < 9 && $.browser.msie){
		$(".minitabs .mt-tab-holder li").corner("top 10px");
	}
	if($.browser.msie && $.browser.version < 7){
		// BUG IN IE6 for footer
		$("#footer .boxed").css("height",$("#footer .boxed").height());
	}
	positionBanners();
	//WRAP DIV AROUND BANNERS, FOR IE TRANSPARENCY

	$("#banners img").each(function(){
		var imgReplace = $("<div class='banner-wrapper'></div>");
		imgReplace.css({
			width		:	$(this).width(),
			height		:	$(this).height(),
			left		:	$(this).css("left"),
			visibility	:	"hidden"
		});
		if($(this).parent(0).is("a")){
			$(this).attr("href",$(this).parent(0).attr("href"));
			$(this).unwrap();
		}
		if($(this).attr("href")){
			var href = $(this).attr("href");
			imgReplace.css("cursor","pointer");
			imgReplace.attr("href",href);
			imgReplace.attr("title",$(this).attr("title"));
			imgReplace.click(function(){
				if (typeof(TRTracker) == "object") {
					TRTracker._trackEvent('TopBanner', 'Click', $(this).attr("title"));
				}
				window.location.href = href;
			});
		}
		if(($.browser.msie && $.browser.version < 9)){
			imgReplace.append($("<div class='banner-image'></div>"));
			$(".banner-image",imgReplace).css({
				width				:	"100%",
				height				:	"100%",
				background			:	"transparent url('"+$(this).attr("src")+"') no-repeat bottom center",
				backgroundSize		:	"100%",
				background			:	"none",
				filter				:	'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+$(this).attr("src")+'", sizingMethod="scale")'
			});
			$(this).after(imgReplace);
			$(this).remove();
		}else{
			$(this).after(imgReplace);
			imgReplace.append($(this));
			$(this).css({
				left 		: 0,
				width		: "100%",
				height		: "100%",
				visibility	: "inherit",
				position	: "static"
			});
		}
	});
	$("#banners .banner-wrapper:first").css({visibility:"visible"});
	$("#banners a").click(function(){return false});
	
	if($("#banners .banner-wrapper").length > 1){
		var currentBanner = 1;
		var maxBanner = $("#banners .banner-wrapper").length;
		var bannerInterval = false;
		var initBannerScroll = function(){
			$("#banners").append('<div class="scrollNext"></div>');
			$("#banners").append('<div class="scrollPrev"></div>');
			$(".scrollNext",$("#banners")).click(function(){showNextBanner();});
			$(".scrollPrev",$("#banners")).click(function(){showPrevBanner();});
			
			if($.browser.msie && $.browser.version < 9){
				var bgimg = $(".scrollNext",$("#banners")).css("backgroundImage").replace('url("','').replace('")','');
				$(".scrollNext",$("#banners")).css({
					background			:	"none",
					filter				:	'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+bgimg+'", sizingMethod="crop")'
				});
				bgimg = $(".scrollPrev",$("#banners")).css("backgroundImage").replace('url("','').replace('")','');
				$(".scrollPrev",$("#banners")).css({
					background			:	"none",
					filter				:	'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+bgimg+'", sizingMethod="crop")'
				});
			}
			
			$(".scrollNext,.scrollPrev",$("#banners")).css({opacity:0});
			$("#banners").hover(function(){
				$(".scrollNext").css({right:"50px"});
				$(".scrollNext").animate({right:"25px",opacity:1},250);
				
				$(".scrollPrev").css({left:"50px"});
				$(".scrollPrev").animate({left:"25px",opacity:1},250);
				clearBannerInterval();
			},function(){
				$(".scrollNext,.scrollPrev",$("#banners")).css({opacity:0});
				setBannerInterval();
			});
			setBannerInterval();
		};
		var setBannerInterval = function(){
			if(bannerInterval)
				clearInterval(bannerInterval);
			bannerInterval = setInterval(function(){showNextBanner();},5000);
		};
		var clearBannerInterval = function(){
			clearInterval(bannerInterval);
		};
		var showNextBanner = function(){
			var newBanner = currentBanner + 1;
			if(newBanner > maxBanner){
				newBanner = 1;
			}
			bannerAction(newBanner,currentBanner);
			currentBanner = newBanner;
		};
		var showPrevBanner = function(){
			var newBanner = currentBanner - 1;
			if(newBanner == 0){
				newBanner = maxBanner;
			}
			bannerAction(newBanner,currentBanner);
			currentBanner = newBanner;
		};
		var bannerAction = function(showIndex,hideIndex){
			var counter = 1;
			var show = hide = null;
			$("#banners .banner-wrapper").each(function(){
				if(counter == showIndex)
					show = $(this);
				if(counter == hideIndex)
					hide = $(this);
				counter++;
			});
			
			if(hide.height()){
				var oldWidth = hide.width();
				var oldLeft = hide.css("left");
				var oldHeight = hide.height();
				var newWidth = Math.floor(oldWidth*1.25);
				var newLeft = Math.floor(($("#banners").width() - newWidth)/2);
				var newHeight =Math.floor(oldHeight*1.25);
				hide.animate({opacity:0,width:newWidth,left:newLeft,height:newHeight},500,"swing",function(){$(this).css({width:oldWidth,left:oldLeft,height:oldHeight});});
				setTimeout(function(){showBanner(show);},300);
			}
		};
		var showBanner = function(banner){
			var oldWidth = banner.width();
			var oldLeft = banner.css("left");
			var oldHeight = banner.height();
			var newWidth = Math.floor(oldWidth*0.75);
			var newHeight = Math.floor(oldHeight*0.75);
			var newLeft = Math.floor(($("#banners").width() - newWidth)/2);
			banner.css({opacity:0,visibility:"visible",width:newWidth,left:newLeft,height:newHeight});
			banner.animate({opacity:1,width:oldWidth,left:oldLeft,height:oldHeight},500,"swing",function(){});
			
		};
		initBannerScroll();
	}
	
	/**
	 * FADING URGENT MESSAGES
	 */
	var msie6 = $.browser.msie && $.browser.version < 7;
	var msie7 = $.browser.msie && $.browser.version > 6 && $.browser.version < 8;
	var msie9 = $.browser.msie && $.browser.version > 8;
	if($("#nav-message a").length > 0){
		var currentMsgIndex = 0;
		var maxMsgIndex = $("#nav-message a").length;
		var msgInterval = false;
		var setMsgInterval = function(){
			if(msgInterval)
				clearMsgInterval();
			msgInterval = setInterval(function(){nextMessage();},5000);
		};
		var clearMsgInterval = function(){
			if(msgInterval)
				clearInterval(msgInterval);
		};
		
		var nextMessage = function(){
			var nextMsgIndex = currentMsgIndex+1;
			if(nextMsgIndex > maxMsgIndex){
				nextMsgIndex = 1;
			}
			var current = next = null;
			var count = 1;
			$("#nav-message a").each(function(){
				if(count == currentMsgIndex){
					current = $(this);
				}
				if(count == nextMsgIndex){
					next = $(this);
				}
				count++;
			});
			if(msie6){
				if(current)
					current.css("display","none");
				setTimeout(function(){next.css("display","block");},450);
			}else{
				if(current)
					current.fadeOut(500);
				setTimeout(function(){next.fadeIn(500);},450);
			}
			currentMsgIndex = nextMsgIndex;
		}
		$("#nav-message").hover(function(){
			clearMsgInterval();
		},function(){
			setMsgInterval();
		});
		nextMessage();
		//$("#nav-message a:first").fadeIn();
		setMsgInterval();
	}
		
	var initInfoBoxShades = function(){	
		var colors = new Array("green","red","yellow","pink","white","purple","dpurple");
		if(!msie6){
			$(".column-right .infobox").each(function(){
				if($(this).hasClass("noshade"))
					return;
				/** get all classes **/
				var extraclass = "";
				var extramin = 0;
				var container = $(this);
				var bwidth = 190;
				if($(this).css("paddingLeft").replace("px","") == "0"){
					bwidth = 210;
				}
				for(i=0;i<colors.length;i++){
					if($(this).attr("class").indexOf(colors[i]) !== -1){
						extraclass = colors[i]+"-shade";
					}
				}
				$("img",$(this)).each(function(){
					if(($(this).css("float") == "left" || $(this).css("float") == "right") && $("p",container).length > 0){
						if(msie6 || msie7 || msie9){
							extramin = 5 ;
						}
					}
				});
				if((msie6 || msie7 || msie9) && $("h3 .cufon,h4 .cufon",container).length > 0){
					extramin = extramin + 10;
				}
				$(this).wrap("<div class='infobox-wrapper "+extraclass+"'></div>");
				$(this).parent(0).css({
					overflow		:	"visible",
					float			:	"left",
					width			:	210,
					height			:	$(this).outerHeight()
				});
				$(this).css({
					marginLeft	:	1,
					marginRight	:	-1,
					marginTop	:	1,
					overflow	:	"visible"
				});
				$(this).resize(function(){
					$(this).parent(0).css({
						height	:	$(this).outerHeight()
					});
				});
			});
			$(".column-right .infobox-footer").each(function(){
				if($(this).hasClass("noshade"))
					return;
				var extraclass = "";
				var bwidth = 190;
				
				if($(this).css("paddingLeft").replace("px","") == "0"){
					bwidth = 210;
				}
				if($(this).attr("class").indexOf(colors[i]) !== -1){
					extraclass = colors[i]+"-shade";
				}
				$(this).wrap("<div class='infobox-footer-wrapper "+extraclass+"'></div>");
				
				$(this).parent(0).css({
					overflow		:	"visible",
					clear			:	"left",
					float			:	"left",
					width			:	210,
					backgroundColor	:	"#839c78"
				});
				$(this).parent(0).css({height:$(this).outerHeight()});
				$(this).css({
					marginLeft	:	1,
					marginRight	:	-1,
					marginTop	:	1,
					overflow	:	"visible"
				});
			});
			var maxHF = 0;
			$(".footer-nodes .infobox").each(function(){
				if($(this).height() > maxHF)
					maxHF = $(this).height();
			});
			$(".footer-nodes .infobox").css("height",maxHF);
			if($.browser.msie){
				$(".banner-block img.mtop25").each(function(){
					//$(this).parent(0).addClass("noshade");
				});
			}
			$(".column-left .infobox,.footer-nodes .infobox").each(function(){
				if($(this).hasClass("noshade"))
					return;
				/** get all classes **/
				var extraclass = "";
				var extramin = 0;
				var container = $(this);
				for(i=0;i<colors.length;i++){
					if($(this).attr("class").indexOf(colors[i]) !== -1){
						extraclass = colors[i]+"-shade";
					}
				}
				$(this).wrap("<div class='infobox-wrapper "+extraclass+"'></div>");
				$(this).parent(0).css({
					height			:	$(this).outerHeight(),
					overflow		:	"visible",
					float			:	"left"
				});
				$(this).css({
					marginLeft	:	1,
					marginRight	:	-1,
					marginTop	:	1,
					overflow	:	"visible"
				});
			});
		}
		$(".column-right .infobox .middle").each(function(){
			var height = $(this).parent(0).height();
			var mgleft = 10;
			if($.browser.msie && $.browser.version < 8){
				height = height-20;
				mgleft = 10;
			}
			var sheight = $(this).height();
			var newtop = Math.floor((height-sheight)/2);
			
			var div = $("<div></div>").css({
				height		:	height,
				position	:	"relative"
			});
			$(this).wrap(div);
			if(newtop < 0)
				newtop = 10;
			$(this).css({
				top			:	newtop,
				position	:	"absolute",
				margin 		:	"0",
				left		:	mgleft
			});
		});
	};//END INFOBOX SHADE
	$("a.infobox").click(function(){
		if($(this).parent(0).parent(0).attr("rel")){
			if (typeof(TRTracker) == "object") {
				TRTracker._trackEvent('Banner', 'Click', $(this).attr("title"));
			}
		}
		
	});
	/**
	 * LOAD BANNERS + INFOBOX (COLUMN LEFT)
	 */
	Cufon.DOM.ready(function() {
		initInfoBoxShades();
		if($.browser.msie && $.browser.version == "8.0"){
			$(".product-full-head span.sign cufon").css({top:"-7",position:"relative"});
			$(".product-block-inline span.sign cufon").css({top:"-10",position:"relative"});
			$(".product-bubble span.sign cufon").css({top:"-4",position:"relative"});
		}
		if($.browser.msie && $.browser.version < 8){
			
			
		}
	});
	
});


