/**
 * @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,
								left: computeOffset($(this), "left") - 14,
								top: computeOffset($(this), "top") - zPoint.top
							}
							if($.browser.msie && jQuery.browser.version != "8.0")
								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);
								})
							var contentWidth = $($(this).attr("rel")+" .overlayContent .content").width();
							var overlayerHeight = $($(this).attr("rel")+" .overlayContent .content").height();
											
							if(firstTime){
								if($.browser.msie && (jQuery.browser.version == "7.0" || jQuery.browser.version == "6.0")){
									resizeOverlay(contentWidth + 48);/*48*/
								}else{
									resizeOverlay(contentWidth + 52);/*48*/
								}
							}
							
							/*alert($.browser.msie + " " + firstTime)*/
							if ($.browser.msie && jQuery.browser.version != "8.0" && firstTime) {
								zPoint = computeZeroPoint()
								overlayPos = {
									//left: computeOffset($(this), "left") + $(this).width() - zPoint.left,
									left: computeOffset($(this), "left") - 14,
									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;
								
							if (overlayerHeight > 310) {
								$(".bgCallBack select").hide(); 
							}
						})
						.mouseout(function(){
						
							overlayTarget = $(this).attr("rel");
							
							clearTimeout(overlayTimeOut);

							overlayTimeOut = setTimeout("hideOverlay()", 500);
							
							
							$(".bgCallBack select").show(); 
						});
					
					
				}
				
				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
		});
	}
}