/**
 * @author Cedric Michaux (Lbi) :: cedric.michaux@lbigroup.be
 */

var overlayTimeOut, overlayTarget, firstTime = true;
jQuery.noConflict();
(function($){
		$(function(){
			$(".overlayTrigger").each(function(){
				if($(this).hasClass("over")){
					$(this)
						.mouseover(function(){
							clearTimeout(overlayTimeOut);
							zPoint = computeZeroPoint()
							overlayPos = {
								left: computeOffset($(this), "left") + $(this).width() - zPoint.left,
								top: computeOffset($(this), "top") - zPoint.top
							}
							if($.browser.msie)
								overlayPos.top -= $(window).scrollTop();
							$($(this).attr("rel"))
								.css({
									"left" : (overlayPos.left+1)+"px",
									"top" : overlayPos.top+"px"
								})
								.slideDown("fast")
								.mouseover(function(){
									clearTimeout(overlayTimeOut);
								})
								.mouseout(function(){
									overlayTarget = this;
									clearTimeout(overlayTimeOut);
									overlayTimeOut = setTimeout("hideOverlay()", 250);
								})
							resizeOverlay($($(this).attr("rel")+" .overlayContent .content").width()+48)
							//alert($.browser.msie + " " + firstTime)
							if ($.browser.msie && firstTime) {
								zPoint = computeZeroPoint()
								overlayPos = {
									left: computeOffset($(this), "left") + $(this).width() - zPoint.left,
									top: computeOffset($(this), "top") - zPoint.top
								}
								overlayPos.top -= $(window).scrollTop();
								$($(this).attr("rel"))
									.css({
										"left" : (overlayPos.left+1)+"px",
										"top" : overlayPos.top+"px"
									})
									.slideDown("fast")
									.mouseover(function(){
										clearTimeout(overlayTimeOut);
									})
									.mouseout(function(){
										overlayTarget = this;
										clearTimeout(overlayTimeOut);
										overlayTimeOut = setTimeout("hideOverlay()", 250);
									})
							}
							else
								firstTime = false;
							
							
						})
						.mouseout(function(){
							overlayTarget = $(this).attr("rel");
							clearTimeout(overlayTimeOut);
							overlayTimeOut = setTimeout("hideOverlay()", 500);
						});
					
					
				}
				
				if($(this).hasClass("click")){
					$(this).click(function(){
						console.log("clicked")
					})
				}
				
				if($(this).hasClass("out")){
					$(this).mouseout(function(){
						console.log("out")
					})
				}
			}
		)
		
		
		$("#volumeCapacity").change(function(){
			var volCap = $("#volumeCapacity").val();
			$("#mouseOverOverlay .countryStores a").each(function(){
				arguments = urlToArguments(this.href);
				arguments.volumeCapacity = volCap;
				this.href = this.href.substring(0, this.href.indexOf("?"))+"?"+argToString(arguments);	
			});
			
		})
		
	})
})(jQuery);





function computeZeroPoint(){
	return {
		left : (jQuery("#mouseOverOverlay").width()/100)*30,
		top : -20
	}
}

function hideOverlay(){
	jQuery(overlayTarget).slideUp("fast")
}

function resizeOverlay(width){
	jQuery("#mouseOverOverlay").width(width);
	jQuery("#mouseOverOverlay .overlayContent > table").width(width);
	if (jQuery.browser.msie && jQuery.browser.version == "6.0") {
		jQuery("#mouseOverOverlay .head .middle, #mouseOverOverlay .bottom .middle").width(width - 24 * 2).css({
			'position' : 'absolute',
			'left' : 24
		});
	}
}