var product_array = new Array();


////////////////////////////////////////////////////////////////
// Interaction JS/Flash

function initializeFlashContentMain(productId) {
	var params = {
		quality: "high", 
		wmode: "transparent",
		allowscriptaccess: "always"
	};
	
	var flashvars = {
		fv_standalone:"true",
		fv_locale:locale,
		fv_configuration_file:"/microsites/corners/engine/xml/configuration/washviewer-conf.xml",
		fv_productid:productId
	};	
    swfobject.embedSWF("/common/swf/washviewer.swf", "eshop-productpage-flashContent-otherwashes", "727", "362", "9.0.45.0","/common/swf/expressinstall.swf", flashvars, params);

}

function initializeFlashContentZoomImage(divId,fv_image,fv_initialZoomed,fv_canZoom) {
	if($('eshop-productpage-flashContent-zoomimage').tagName == "EMBED"){
		$$('.flashContent-zoomimages')[0].setStyles({
			'display':'block'
		});
		(function (){$('eshop-productpage-flashContent-zoomimage').loadImage(fv_image);}).delay(100);
	}
	else{
		$$('.flashContent-zoomimages')[0].setStyles({
			'display':'block'
		});
		var params = {
			quality: "high", 
			wmode: "transparent"
		};
		
		var flashvars = {
			fv_image : fv_image,
			fv_initialZoomed : fv_initialZoomed,
			fv_canZoom: fv_canZoom
		};
		swfobject.embedSWF("/microsites/shop/product/product-2modes-template.swf", divId, "727", "362", "9.0.45.0","/common/swf/expressinstall.swf", flashvars, params);
	}
}

function cleanFlashContent () {
 $$('.flashContent-otherwashes')[0].removeProperty('style');
	$$("#eshop-page-content .previous-next-product").each(function(item,index) {
		item.setStyles({
			'display':'block'
		});
	});
	
	if ($('previousproduct')) $('previousproduct').fade('in');
	if ($('nextproduct')) $('nextproduct').fade('in');
	if ($('views_buttons').getStyle('display') != 'none') {
		$('views_buttons').setStyle('display','inline');
		$('views_buttons').fade('in');
	}
	$$('li.display-only-when-zoom').each(function(item,index) {
		item.setStyles({
			'display':'none'
		});
	});	
	$$('.outfit')[0].fade('in');
	$$('.flashContent-zoomimages').each(function(item,index) {
		item.setStyles({
			'display':'none'
		});
	});	
}

function getOtherColorWashData() {
	return window.otherWashes;
}

/////////////////////////////////////////////////////////////////

window.addEvent('domready', function(){

	var viewMode = (Cookie.read('PRODUCT_VISUALIZATION')) ? Cookie.read('PRODUCT_VISUALIZATION') : 'onmodel';
	// Try to get the product image according to the cookie if possible
	var productPicture = $('product-picture').getElement('img.'+ viewMode);
	if (!$defined(productPicture)) {
	    var inverseViewMode = (viewMode == "onmodel" ? "productonly" : "onmodel");
	    productPicture = $('product-picture').getElement('img.'+ inverseViewMode);
	}

	if ($defined(productPicture))
	    productPicture.setStyle('visibility', 'visible');

	if (viewMode == 'productonly') {
		$('views_buttons').getElement('a.productonly img').set('src', '/common/images/shop/utils/picto-product-on.gif');
		$('views_buttons').getElement('a.onmodel img').set('src', '/common/images/shop/utils/picto-model.gif');
	} else if (viewMode == 'onmodel') {
		$('views_buttons').getElement('a.onmodel img').set('src', '/common/images/shop/utils/picto-model-on.gif');
		$('views_buttons').getElement('a.productonly img').set('src', '/common/images/shop/utils/picto-product.gif');
	}

	$('productpage-center').addShadow();
	$$('.productPreviewBox').addShadow();

	var msgPopupHiddenTop = 142;
	var msgPopupDisplayTop = 0;
	var addToCartButtonEnabled = true;
// Closing Dropdown when click outside
	$(document.body).addEvent('click',function(){
		$$('.dropdown').setStyle('display','none');
	});
	
	$$('.jsDropDownMenu').addEvent('click',function(e){
		e.stopPropagation();
	});
//Animation Onmodel/Productonly view
	$$('#productpage-top .productonly').addEvent('click',function(){
		if($$('#productpage-center #product-picture .productonly').getStyle('visibility')=='hidden'){
			$$('#productpage-center #product-picture .productonly').setStyle('opacity',0);
			$$('#productpage-center #product-picture .productonly').fade('in');
			$$('#productpage-center #product-picture .onmodel').fade('out');
			$$('#productpage-top .productonly img').setProperty('src','/common/images/shop/utils/picto-product-on.gif');
			$$('#productpage-top .onmodel img').setProperty('src','/common/images/shop/utils/picto-model.gif');
			var viewCookie = Cookie.write('PRODUCT_VISUALIZATION', 'productonly',{path:'/'});
			viewMode = 'productonly';
		}
		return false;
	});
	$$('#productpage-top .onmodel').addEvent('click',function(){
		if($$('#productpage-center #product-picture .onmodel').getStyle('visibility')=='hidden'){
			$$('#productpage-center #product-picture .onmodel').setStyle('opacity',0);
			$$('#productpage-center #product-picture .onmodel').fade('in');
			$$('#productpage-center #product-picture .productonly').fade('out');
			$$('#productpage-top .productonly img').setProperty('src','/common/images/shop/utils/picto-product.gif');
			$$('#productpage-top .onmodel img').setProperty('src','/common/images/shop/utils/picto-model-on.gif');
			var viewCookie = Cookie.write('PRODUCT_VISUALIZATION', 'onmodel',{path:'/'});
			viewMode = 'onmodel';
		}
		return false;
	});

	$('eshop-page-content').getElements('div.productIdBlock').each(function(object,index) {
		//Size Management Class
		var product = new SizeManager(object);
		product.actionTarget = 'product-page.html';
		product_array.push(product);
		window.product = product;

		// add to cart actions - start
		// checks if the size mode is based on length/waist (double) or size (single)
		var singleSizeMode = false;
		var doubleSizeMode = false;
		if(object.getElement('div.size-block') != null){
			var singleSizeMode = true;
		} else if(object.getElement('div.waist-block') != null && object.getElement('div.length-block') != null){
			var doubleSizeMode = true;
		}


		var itemSizeSelected = false;
		
		$$('.cart-link').addEvent('click',function(){
			$$('.cart-link img').fade('hide');
			$$(".product-msg-popup-availability-loggedin .product-msg-popup-link")[0].removeEvents('click');
			$$(".product-msg-popup-availability-notloggedin .product-msg-popup-link")[0].removeEvents('click');
			if(addToCartButtonEnabled == true) {
				// checks if the size item is selected according to the mode (double / single)
				if(singleSizeMode == true) {
					if(object.getElement('div.size-block li.selected') != null) {
						itemSizeSelected = true;
					} else {
						itemSizeSelected = false;
					}
				}
				if(doubleSizeMode == true) {
					if(object.getElement('div.waist-block li.selected') != null && object.getElement('div.length-block li.selected') != null) {
						itemSizeSelected = true;
					} else {
						itemSizeSelected = false;
					}
				}
				if(itemSizeSelected == true) {
					if(product.getStock() > 0) {
						// there is enough stock so let's check the service
						var jsonRequest = new Request.JSON({url: "/"+ locale +"/shop/services/AddToCart?" + $time(), onComplete: function(result){
							if(result.RESULT.STATUS == 'SUCCESS'){
								$$('#product-sheet .product-msg-popup-additem')[0].getElement('span.product-msg-popup-title-quantity').set('text',$('quantity'+product.productSkuId).value)
								$$('.product-msg-popup-container').setStyle('visibility','visible');
								var AnimationDisplay = new Fx.Morph($$('#product-sheet .product-msg-popup-additem')[0], {duration: 250, transition: Fx.Transitions.Sine.easeOut});
								AnimationDisplay.start({
									'top': msgPopupDisplayTop
								});
								updateCartPreview(result.CARTPREVIEW.NUMITEMS, result.CARTPREVIEW.NUMITEMS_TEXT, result.CARTPREVIEW.PRICE);
								var tag = new Tag();
								if(!Cookie.read('FIRST_PRODUCT_ADDED')){
									tag.set({'events': 'scOpen', 'products': tag.get('products'),'name':'open cart','linkTrackVars':'events'});
									tag.sendClick();
									Cookie.write('FIRST_PRODUCT_ADDED', '1',{path:'/'});
								}
								tag.set({'events': 'scAdd', 'products': tag.get('products'),'name':'add to cart','linkTrackVars':'events'});
								tag.sendClick();
							} else {
								$$('#product-sheet .product-msg-popup-error')[0].getElement('span.product-msg-popup-content-text').set('text',result.RESULT.ERRORDESC)
								$$('.product-msg-popup-container').setStyle('visibility','visible');
								var AnimationDisplay = new Fx.Morph($$('#product-sheet .product-msg-popup-error')[0], {duration: 250, transition: Fx.Transitions.Sine.easeOut});
								AnimationDisplay.start({
									'top': msgPopupDisplayTop
								});
								var tag = new Tag();
								tag.set({'events': 'event17', 'pageName': 'shop error', 'channel': 'error', 'eVar7': escape(result.RESULT.ERRORDESC)});
								tag.send();
							}
						}}).post({"json" : JSON.encode([{"sku" : product.getSkuId(), "quantity" : $('quantity'+product.productSkuId).value}])});
					} else { 
					// stock at zero so let's show the alert message logged in / not logged in
							if (shopUserState == true) {
								$$('.product-msg-popup-container').setStyle('visibility','visible');
								var displayProductMsgPopup = new Fx.Morph($$('.product-msg-popup-availability-loggedin')[0], {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
								displayProductMsgPopup.start({
									'top': msgPopupDisplayTop
								});
								var tag = new Tag();
								tag.set({'events': 'event22', 'products': tag.get('products')});
								tag.sendClick();

								$$(".product-msg-popup-availability-loggedin .product-msg-popup-link")[0].addEvent('click',function(){
									var jsonRequest = new Request.JSON({url: "/"+ locale +"/shop/services/SubscribeForNotification?" + $time(), onComplete: function(result){
										$$('.product-msg-popup-close').fireEvent('click');
										if(result.STATUS == "SUCCESS"){
											var displayProductMsgPopup = new Fx.Morph($$('.product-msg-popup-notification-ok')[0], {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
											displayProductMsgPopup.start({
												'top': msgPopupDisplayTop
											});										
										}
										else{
											$$('#product-sheet .product-msg-popup-error')[0].getElement('span.product-msg-popup-content-text').set('text',result.ERRORDESC)
											var AnimationDisplay = new Fx.Morph($$('#product-sheet .product-msg-popup-error')[0], {duration: 250, transition: Fx.Transitions.Sine.easeOut});
											AnimationDisplay.start({
												'top': msgPopupDisplayTop
											});
										}
									}}).post({"sku" : product.getSkuId()});
									var tag = new Tag();
									tag.set({'events': 'event23', 'products': tag.get('products')});
									tag.sendClick();
									return false;
								});
							} else {
								$$('.product-msg-popup-container').setStyle('visibility','visible');
								var displayProductMsgPopup = new Fx.Morph($$('.product-msg-popup-availability-notloggedin')[0], {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
								displayProductMsgPopup.start({
									'top': msgPopupDisplayTop
								});

								var tag = new Tag();
								tag.set({'events': 'event22', 'products': tag.get('products')});
								tag.sendClick();
								
								$$(".product-msg-popup-availability-notloggedin .product-msg-popup-link")[0].addEvent('click',function(){
									var emailNotification = $$(".product-msg-popup-availability-notloggedin .product-msg-popup-content")[0].getElement("input.product-msg-popup-input").get("value");
									var jsonRequest = new Request.JSON({url: "/"+ locale +"/shop/services/SubscribeForNotification?" + $time(), onComplete: function(result){
										if(result.STATUS == "SUCCESS"){
											var displayProductMsgPopup = new Fx.Morph($$('.product-msg-popup-notification-ok')[0], {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
											displayProductMsgPopup.start({
												'top': msgPopupDisplayTop
											});										
										}
										else{
											$$('#product-sheet .product-msg-popup-error')[0].getElement('span.product-msg-popup-content-text').set('text',result.ERRORDESC)
											var AnimationDisplay = new Fx.Morph($$('#product-sheet .product-msg-popup-error')[0], {duration: 250, transition: Fx.Transitions.Sine.easeOut});
											AnimationDisplay.start({
												'top': msgPopupDisplayTop
											});
										}	
									}}).post({"sku" : product.getSkuId(), "email" : emailNotification});
									var tag = new Tag();
									tag.set({'events': 'event23', 'products': tag.get('products')});
									tag.sendClick();
									return false;
								});
							}
					}
					addToCartButtonEnabled = false;
				}
				else{
					$$('.product-msg-popup-container').setStyle('visibility','visible');
					var AnimationDisplay = new Fx.Morph($$('#product-sheet .product-msg-popup-size-not-selected')[0], {duration: 250, transition: Fx.Transitions.Sine.easeOut});
					AnimationDisplay.start({
						'top': msgPopupDisplayTop
					});
				}
			}
		});
		// add to cart actions - end

		// Init flash divs
		initializeFlashContentMain(product.productId); //only once on click
	});

	// Animation -  "in motion" slide
	var productInmotionObj = $$('#product-picture .product-info-slide')[0];
	
	if(productInmotionObj) {
	
		// set to 0 ( visible) via JS
		productInmotionObj.setStyles({
			'right': 222
		});
		
		// First slide onload
		var productFeaturesSlideOnLoad = new Fx.Morph(productInmotionObj, {duration: 500, transition: Fx.Transitions.Sine.easeInOut});
		(function(){
			productFeaturesSlideOnLoad.start({
			'right': 175
			});
		}).delay(2000);						
	
		var productFeaturesSlide = new Fx.Morph(productInmotionObj, {duration: 500, transition: Fx.Transitions.Sine.easeInOut});
		productInmotionObj.addEvent('mouseover',function(){ 
			productFeaturesSlideOnLoad.cancel(); // stops initial slide on rollover
			productFeaturesSlide.cancel(); // stops previous slide on rollout
			productFeaturesSlide.start({
				'right': 222
			});
		});
		productInmotionObj.addEvent('mouseout',function(){ 
			productFeaturesSlideOnLoad.cancel(); // stops initial slide on rollout
			productFeaturesSlide.cancel(); // stops previous slide on rollout
			productFeaturesSlide.start({
				'right': 175
			});
		});
	}



// Animation -  Wear and Care
	var coordpopup = $$('#product-sheet .product-wearcare-popup')[0].getCoordinates($('product-sheet'));
	var wearscarPopupDisplayLeft = 0;
	var wearscarPopupHiddenLeft = -221;

	var button_wearcare = $$('#product-sheet .product-wearcare')[0];

	if ($defined(button_wearcare)) {
		button_wearcare.addEvent('click',function(){ 
			if($$('#product-sheet .product-wearcare-popup')[0].getStyle('left') == wearscarPopupHiddenLeft + "px") {
				var productWearCareDisplayIn = new Fx.Morph($$('#product-sheet .product-wearcare-popup')[0], {duration: 500, transition: Fx.Transitions.Sine.easeIn});
				productWearCareDisplayIn.start({
					'left': wearscarPopupDisplayLeft
				});
			}
		});
	}
	$$('#product-sheet .product-wearcare-popup-close')[0].addEvent('click',function(){ 
			var productWearCareDisplayOut = new Fx.Morph($$('#product-sheet .product-wearcare-popup')[0], {duration: 500, transition: Fx.Transitions.Cubic.easeOut});
			productWearCareDisplayOut.start({
				'left': wearscarPopupHiddenLeft + "px"
			});
	});



// Animation -  add to cart
	$$('.product-msg-popup-close').addEvent('click',function(){ 
		var popupDisplay = new Fx.Morph(this.parentNode.parentNode, {
			duration: 500,
			transition: Fx.Transitions.Cubic.easeOut,
			onComplete: function(){
				switch(this.element.className){
				case 'product-msg-popup-notification-ok':
					$$('.product-msg-popup-availability-notloggedin .product-msg-popup-close','.product-msg-popup-availability-loggedin .product-msg-popup-close').fireEvent('click');
				/*case 'product-msg-popup-error':
				break;*/
				default:
					$$('.product-msg-popup-container').setStyle('visibility','hidden');
					$$('.cart-link img').fade('in');
					addToCartButtonEnabled = true;
				break;
				}
			}
		});
		popupDisplay.start({
			'top': msgPopupHiddenTop
		});
		return false;
	});

		$$('.product-msg-popup-additem .product-msg-popup-link')[0].addEvent('click',function(){
			$$('.product-msg-popup-close').fireEvent('click');
			return false;
		});
		
		$$('.product-msg-popup-additem .product-msg-popup-link')[1].addEvent('click',function(){
			document.location = $$('.product-msg-popup-additem .product-msg-popup-link')[1].get('href');
			return false;
		});
		
		$$('.product-msg-popup-error .product-msg-popup-link')[0].addEvent('click',function(){
			$$('.product-msg-popup-error .product-msg-popup-close').fireEvent('click');
			return false;
		});

// Animation -  flash center
	if($$('.other-washes-link')[0]) $$('.other-washes-link')[0].addEvent('click',function(){											   
		cleanFlashContent();															   
		$$('.flashContent-otherwashes')[0].setStyles({
			'display':'block'
		});
		
		(function(){
			//$('eshop-productpage-flashContent-otherwashes').showFlashContentMain();
			document.getElementById('eshop-productpage-flashContent-otherwashes').showFlashContentMain();
		}).delay(500);						
		if ($('previousproduct')) $('previousproduct').fade('out');
		if ($('nextproduct')) $('nextproduct').fade('out');
		$('views_buttons').fade('out');
	});	
	

// Animation -  flash zoom popup
	$$('#product-picture .picture').addEvent('click',function(){
		(function(){
			var indexViewMode = (viewMode == 'productonly')?0:1;		
			var imageSrc = $$("#product-picture .extra-images-src")[indexViewMode].get("text");
			initializeFlashContentZoomImage("eshop-productpage-flashContent-zoomimage", imageSrc,"true","true");		

			$$('li.display-only-when-zoom').each(function(item,index) {
				item.setStyles({
					'display':'block'
				});
				item.fade('in');
			});						

		}).delay(400);
		if ($('previousproduct')) $('previousproduct').fade('out');
		if ($('nextproduct')) $('nextproduct').fade('out');
		$('views_buttons').fade('out');
		$$('.outfit')[0].fade('out');
	});
 

	$$('li.extra-images').each(function(item,index) {
		$$('li.extra-images')[index].addEvent('click',function(){
			(function(){
				var imageSrc = $$("#product-picture .extra-images-src")[index].get("text");
				initializeFlashContentZoomImage("eshop-productpage-flashContent-zoomimage", imageSrc,"true","true");

				$$('li.display-only-when-zoom').each(function(item,index) {
					item.setStyles({
						'display':'block'
					});
				});						

			}).delay(400);
			
			if ($('previousproduct')) $('previousproduct').fade('out');
			if ($('nextproduct')) $('nextproduct').fade('out');
			$('views_buttons').fade('out');
			$$('.outfit')[0].fade('out');
		});	
	});	
});	

window.addEvent('product_ready',function(){
		if (window.product) {
			window.otherWashes = window.product.product.OTHER_COLOR_WASHES;
		}
})					

