;(function($){
	
	window.em = { fn: {} };
	
	em.fn.jsStyle = function() {
		document.body.className += " js";
	};
	
	em.fn.InfinitePatterns = function(){
		var sHeaderHeight = $("#header").outerHeight();
		var sHeaderWidth = $("#header").outerWidth();
		var sWindowWidth = $("#wrapper").outerWidth();
		var jPatternLeft = $("<div class=\"pattern\" id=\"pattern-left\"></div>");
		var jPatternRight = $("<div class=\"pattern\" id=\"pattern-right\"></div>");
		var sPatternWidth = sWindowWidth;
		jPatternLeft
			.css({
				top : 111 + "px",
				width : (sPatternWidth/2) - 470 + "px"
			})
			.appendTo("#wrapper");
		jPatternRight
			.css({
				top : 136 + "px",
				width : (sPatternWidth/2) - 470 + "px"
			})
			.appendTo("#wrapper");
		if ($("#content").hasClass('brand')) {
			$("#pattern-right").addClass('brand');
		}		
	};
	
	em.fn.initMiniHeader = function(){
		var jShopAccess = $('#header .box-shop-access');
		var jCtl = jShopAccess.find(".box-header");
		var jContent = jShopAccess.find(".box-content");
		var sContentHeight = jContent.outerHeight();
		var shopAccessTimer;
		
		jContent
			.attr("state","collapsed")
			.css({overflow:"hidden",height:0})
			.bind("collapse", function(){
				jContent.animate({height : 0}, 300, function(){
					jShopAccess.removeClass("expanded").attr("state","collapsed");
				});
			})
			.bind("expand", function(){
				jShopAccess.addClass("expanded");
				jContent.animate({height : sContentHeight + "px"}, 300, function(){
					jShopAccess.attr("state","expanded");
				});
			});
			
		jCtl.click(function(){
			if ( jContent.is(':hidden') ) {
				jContent.trigger("expand");
			} else {
				jContent.trigger("collapse");
			}
		});
		
		jShopAccess.hover(
			function(){
				window.clearTimeout(shopAccessTimer);
			},
			function(){
				shopAccessTimer = window.setTimeout(function(){
					jContent.not(":animated").trigger("collapse");
				}, 1600);
			}
		);
		
	};
	
	em.fn.initAddresses = function(loadTxt, addSubmit){
		var jDelivery = $('#change-delivery-address');
		var jBilling = $('#change-billing-address');
		
		jDelivery.add(jBilling).each(function(){
			var jLink = $(this);
			var ajaxUrl = jLink.attr('href');
			var jField = jLink.parents('fieldset');
			var jAddressField;
			var jFakeCheck = $('<span class="jquery-checkbox"></span>');
			var jRelInput = jLink.next("input[type=hidden]");
			var jLoader = $('<br /><span class="infos loading">'+ loadTxt +'</span>');
			jFakeCheck.prependTo(jLink);
			
			jLink.click(function(e){
				e.preventDefault();
				jFakeCheck.addClass('jquery-checkbox-checked');
				jRelInput.val('true');
				if ( !jLink.hasClass('loaded') ) {
					jLink.addClass('loaded');
					jLoader.insertAfter(jLink);
					$.get(ajaxUrl, function(data){
						jAddressField = $(data).find('#area-2 form fieldset:first').insertAfter(jField);
						if (!!addSubmit){
							var jSubmit = $('<p class="submit"><span class="button"><input type="submit" value="Valider" /></span></p>');
							jSubmit.appendTo(jAddressField);
						}
						jLoader.remove();
						jAddressField.find('input:first').focus();
						em.fn.initRegister();
					});
				} else {
					jAddressField.find('input:first').focus();
				}
				
				jField.find('input:radio').attr('checked','').click(function(){
					jLink.removeClass('loaded');
					jRelInput.val('false');
					jAddressField.remove();
					jFakeCheck.removeClass('jquery-checkbox-checked');
				});
			});
		});
		
	};
	
	em.fn.initRegisterForm = function(){
		var jBilling = $('#change-billing-address');
		var jField = jBilling.parents('fieldset');
		var jForm = jField.parents('form');
		var jRadios = jField.find('input[type=radio]').not(jBilling);
		var jHiddenField = jField.next('fieldset').hide().insertAfter(jForm);
		
		jHiddenField
			.bind("show", function(){
				jHiddenField.insertAfter(jField).end().show();
			})
			.bind("hide", function(){
				jHiddenField.hide().insertAfter(jForm);
			});
		
		if(jBilling.is(':checked')){
			jHiddenField.trigger("show");
		}
		
		jBilling.add(jRadios).bind("click", function(){
			if ( jBilling.is(':checked') ) {
				jHiddenField.trigger("show");
			} else {
				jHiddenField.trigger("hide");
			}
		});
		
	};
	
	em.fn.initBrowsersNotify = function(browsernotice){
		if ( $.browser.msie && $.browser.version < 7 && !document.cookie.match(/closebrowsersnotify=true/)) {
			var jNoticeCont = $('<div id="browsers-notice"><div class="box-in-1">' + browsernotice + '</div></div>').insertBefore("#wrapper").hide();
			var jCloseBtn = $('<a rel="close" class="close">X</a>').appendTo(jNoticeCont);
			var iWindowHeight = $(window).height();
			var jWrapper = $("#wrapper");
			jNoticeCont
				.slideDown(300, function(){
					$("html").css({overflowY:"hidden"});
					jWrapper.css({
						position: "relative",
						height: (iWindowHeight - (jNoticeCont.outerHeight())) + "px",
						overflowY : "auto"
					});
					$(window).resize(function(){
						iWindowHeight = $(window).height();
						jWrapper.css({
							height: (iWindowHeight - (jNoticeCont.outerHeight())) + "px"
						});
					});
				})
				.find("a").not(jCloseBtn).click(function(){
					jCloseBtn.click();
				});
			jCloseBtn
				.click(function(){
					jNoticeCont.slideUp(300, function(){
						$("html").css({overflowY:"scroll"});
						jWrapper.css({height: "100%"});
						document.cookie = "closebrowsersnotify=true;max-age=" + 604800; // 604800 = 60*60*24*7
					});
					$(window).resize(function(){
						jWrapper.css({height: "100%"});
					});
				})
				.hover(
					function(){ jNoticeCont.addClass("hover"); },
					function(){ jNoticeCont.removeClass("hover"); }
				);
		}
	};
	
	em.fn.initPrdModify = function(){
		var jRecap = $("#area-2 .box-cart-summary");
		var jPrdLines = jRecap.find("tbody .prd");
		var jPopIn;
		
		refreshLink = function(){
			var jLinks = jPopIn.find(".prd-options dl a");
			jLinks.click(function(e){
				e.preventDefault();
				var jLink = $(this);
				var sURI = jLink.attr("href");
				var jPopContent = jPopIn.find(".box-content");
				jPopContent.remove();
				jPopIn.parent().addClass("loader");
				$.get(sURI, function(data){
					jPopIn.parent().removeClass("loader");
					$(data).find("#change-prd-specs .box-content").hide().appendTo(jPopIn).fadeIn(300);
					refreshLink();
					$.unobtrusivelib(["popup", "external", "maxLength", "autoClearInput", "autoFocusInput"]);
				});
			});
		};
		
		jPrdLines.each(function(){
			var jPrd = $(this);
			var jModify = jPrd.find("a.modify");
			var sURI = jModify.attr("href");
			jModify.click(function(e){
				e.preventDefault();
				$.superbox.wait(function(){
					$.get(sURI, function(data){
						jPopIn = $(data).find("#change-prd-specs");
						$.superbox.open(jPopIn, {
							boxWidth : "520",
							boxHeight : "280"
						});
						refreshLink();
						$.unobtrusivelib(["popup", "external", "maxLength", "autoClearInput", "autoFocusInput"]);
					});
				});
			});
		});
		
	};
	
	em.fn.initSizesChart = function(iSelected){
		if (!iSelected) { var iSelected = 0; }
		var jBoxes = $("#area-1.sizes-chart .box"),
			jLink = $(".size-charts a"),
			jSizesChart;
			
		jBoxes.each(function(){
			var jBox = $(this),
				jBoxControler = jBox.find(".box-header").attr("tabindex","0"),
				jBoxContent = jBox.find(".box-content"),
				sBoxContentHeight = jBoxContent.outerHeight();
			
			jBox
				.bind("collapse", function(){
					jBox.removeClass("expanded").addClass("collapsed");
					jBoxControler.attr('title','Déplier la section');
					jBoxContent.hide();
				})
				.bind("expand", function(){
					jBox.removeClass("collapsed").addClass("expanded");
					jBoxControler.attr('title','Replier la section');
					jBoxContent.show();
				});
			jBoxControler.click(function(){
				if (jBoxContent.is(":visible")){
					jBox.trigger("collapse");
				} else {
					jBoxes.not(jBox).trigger("collapse");
					jBox.trigger("expand");
				}
			}).focus(function(){
				$(document).bind("keyup.toggle", function(e){
					e.preventDefault();
					if (e.keyCode == 13 || e.keyCode == 32){
						jBoxControler.click();
					}
				});
			}).blur(function(){
				$(document).unbind("keyup.toggle");
			});
		});
		jBoxes.trigger("collapse").filter(":eq("+iSelected+")").trigger("expand");
		
		jLink.one("click",function(e){
		e.preventDefault();
		var sUrl = $(this).attr("href");
			$.superbox.wait(function(){
				$.get(sUrl, function(data){
					var jSizeChart = $(data).find("#area-1");
					$.superbox.open(jSizeChart, { boxWidth: "800",boxHeight: "-1" });
					em.fn.initSizesChart(iSelected);
					$j("a.print").click(function(e){
						e.preventDefault();
						window.print(); 
					});
				});
			});
		});
	};
})(jQuery);

$j(function(){
	var jPrint = $j("a.print");
	jPrint.click(function(e){
		e.preventDefault();
		window.print(); 
	});
	$j(window).resize(function(){
		$j('#pattern-left').add('#pattern-right').remove();
		em.fn.InfinitePatterns();
	});
});