/*
	Manage the shopping cart page
	Check the form / display errors messages
*/

var product_array = new Array();
window.addEvent('loginready', function(){

	if($defined($('bg-layer'))) {
		var posEl = $('footer').getCoordinates();
		$('bg-layer').setStyles({
			'opacity':0,
			'height': posEl.bottom + "px"
		});
	}
	
	$$('div.lightbox-shadow').each(function(item){
		item.getParent().setStyles({
			'opacity': 0,
			'display': 'block'
		});
		item.setStyle('position','relative');
		item.addShadow();
	});
	
	$$('.lightbox-close').addEvent('click',function(){
		this.getParent('.small-lightbox').fade('out');
		return false;
	});
	
	$('shoppingcart-center').getElements('li.productIdBlock').each(function(object,index) {
		var product = new SizeManager(object);
		product.actionTarget = 'cart.html';
		product_array.push(product);
	});
	$$("#eshop-page-content .popup-cart").each(function(item,index) {
		item.setStyles({
			'opacity':0
		});
	});
	
	$$('.product-msg-popup-availability-notloggedin').setStyles({
		'display':'block',
		'opacity':0
	});
	$$('.product-msg-popup-availability-loggedin').setStyles({
		'display':'block',
		'opacity':0
	});
	$$('.product-msg-popup-limited-quantity').setStyles({
		'display':'block',
		'opacity':0
	});
	$$('.product-msg-popup-limited-sku').setStyles({
		'display':'block',
		'opacity':0
	});
	$$('.product-msg-popup-notification-ok').setStyles({
		'display':'block',
		'opacity':0
	});
	$$('.product-msg-popup-error').setStyles({
		'display':'block',
		'opacity':0
	});
	$$('.product-msg-popup-titleerror','.eshop-cart-popup-title').each(function(item){
		$(item).setStyle('display','none');
	});
	$$("#shoppingcart-top .link").each(function(item,index) {
		$$("#shoppingcart-top .link")[index].addEvent('click',function(){
				if($$("#eshop-page-content .popup-cart")[index].getStyle('opacity') == 1){
					var myEffect2 = new Fx.Morph($$("#eshop-page-content .popup-cart")[index], {duration: 250, transition: Fx.Transitions.linear});
					myEffect2.start({
						'opacity': 0
					});
					$$("#eshop-page-content .popup-cart")[index].getElement('.eshop-cart-popup-title').setStyle('visibility','hidden');
				} else {
					var myEffect1 = new Fx.Morph($$("#eshop-page-content .popup-cart")[index], {duration: 250, transition: Fx.Transitions.linear});
					$$("#eshop-page-content .popup-cart")[index].getElement('.eshop-cart-popup-title').setStyle('visibility','visible');
					myEffect1.start({
						'opacity': 1
					});				
					$$("#eshop-page-content .popup-cart").each(function(item,index) {
						if(item.getStyle('opacity') == 1){
							var myEffect2 = new Fx.Morph(item, {duration: 250, transition: Fx.Transitions.linear});
							myEffect2.start({
								'opacity': 0
							});	
							item.getElement('.eshop-cart-popup-title').setStyle('visibility','hidden');
						}
						item.addEvent('click',function(e){
							e.stopPropagation();
						});	
					});
					var myEffect1 = new Fx.Morph($$("#eshop-page-content .popup-cart")[index], {duration: 250, transition: Fx.Transitions.linear, onComplete: function () {
						$(document.body).addEvent('click',function(){
							$$("#eshop-page-content .popup-cart").each(function(item,index) {
								if(item.getStyle('opacity') == 1){
									var myEffect2 = new Fx.Morph(item, {duration: 250, transition: Fx.Transitions.linear});
									myEffect2.start({
										'opacity': 0
									});						
									item.getElement('.eshop-cart-popup-title').setStyle('visibility','hidden');
								}
								item.addEvent('click',function(e){
									e.stopPropagation();
								});
							});
							$(document.body).removeEvent('click');
						});
					}});
					myEffect1.start({
						'opacity': 1
					});
				}
		
				$$('.eshop-cart-popup-close').addEvent('click',function(){
					var myEffect1 = new Fx.Morph($$("#eshop-page-content .popup-cart")[index], {duration: 250, transition: Fx.Transitions.linear});
					myEffect1.start({
						'opacity': 0
					});						
					$$("#eshop-page-content .popup-cart")[index].getElement('.eshop-cart-popup-title').setStyle('visibility','hidden');
				})
			});
		});
	if($('giftwrapping')) $('giftwrapping').addEvent('click',function(){
		var formHtml = '<input type="hidden" name="action" value="addremovegiftwrap" /><input type="hidden" name="giftwrap" value="' + $('giftwrapping').checked +'" />';
		var form = new Element('form', {action: 'cart.html', method: 'post', html: formHtml});
		form.inject($('main'));
		form.submit();
	});
	if($('freeshipping')) $('freeshipping').addEvent('click',function(){
		var formHtml = '<input type="hidden" name="action" value="addremovefreeshipping" /><input type="hidden" name="freeshipping" value="' + $('freeshipping').checked +'" />';
		var form = new Element('form', {action: 'cart.html', method: 'post', html: formHtml});
		form.inject($('main'));
		form.submit();
	});
	if($('csstDiscountAdd')) $('csstDiscountAdd').addEvent('click',function(){
		var formHtml = '<input type="hidden" name="action" value="addremovecsstdiscount" /><input type="hidden" name="discount" value="' + $('csstDiscountValue').value +'" />';
		var form = new Element('form', {action: 'cart.html', method: 'post', html: formHtml});
		form.inject($('main'));
		form.submit();
	});
	$('promocode-link').addEvent('click',function(){
		if($('promocode').value){
			var formHtml = '<input type="hidden" name="action" value="addpromocode" /><input type="hidden" name="promocode" value="' + $('promocode').value +'" />';
			var form = new Element('form', {action: 'cart.html', method: 'get', html: formHtml});
			form.inject($('main'));
			form.submit();
		}
		else{
			return false;
		}
	});
	$('promocode').addEvent('keydown', function(event){
		if(event.key == "enter")
			$('promocode-link').fireEvent('click');
	});



	product_array.each(function(item,index){
		item.row.getElements('.product-msg-popup-notification-ok .product-msg-popup-close').addEvent('click',function(){ 
			$$('.product-msg-popup-availability-notloggedin .product-msg-popup-close','.product-msg-popup-availability-loggedin .product-msg-popup-close').fireEvent('click');
			var popupDisplay = new Fx.Morph(this.parentNode.parentNode, {
				duration: 500,
				transition: Fx.Transitions.Cubic.easeOut
			});
			popupDisplay.start({
				'opacity': 0
			});
			return false;
		});
		item.row.getElements('.product-msg-popup-error .product-msg-popup-close').addEvent('click',function(){ 
			var popupDisplay = new Fx.Morph(this.parentNode.parentNode, {
				duration: 500,
				transition: Fx.Transitions.Cubic.easeOut
			});
			popupDisplay.start({
				'opacity': 0
			});
			return false;
		});
	});
	
	$$(".product-msg-popup-availability-notloggedin .product-msg-popup-link").each(function(item,index){
		item.addEvent('click',function(){
			var jsonRequest = new Request.JSON({
				url: "/"+ locale +"/shop/services/SubscribeForNotification?" + $time(),
				onComplete: function(result){
					if(result.STATUS == "SUCCESS"){
						product_array[index].row.getElement('.product-msg-popup-notification-ok .product-msg-popup-titleerror').setStyle('display','block');
						var displayProductMsgPopup = new Fx.Morph(product_array[index].row.getElement('.product-msg-popup-notification-ok'), {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
						displayProductMsgPopup.start({
							'opacity': 1
						});
					}
					else{
						product_array[index].row.getElement('.product-msg-popup-error span.product-msg-popup-text').set('text',result.ERRORDESC)
						product_array[index].row.getElement('.product-msg-popup-error .product-msg-popup-titleerror').setStyle('display','block');
						var AnimationDisplay = new Fx.Morph(product_array[index].row.getElement('.product-msg-popup-error'), {duration: 250, transition: Fx.Transitions.Sine.easeOut});
						AnimationDisplay.start({
							'opacity': 1
						});
					}
				}
			}).post({"sku" : product_array[index].getSkuId(), "email" : product_array[index].row.getElement('.product-msg-popup-input').value});
		});
	});

	$$(".product-msg-popup-availability-loggedin .product-msg-popup-link").each(function(item,index){
		item.addEvent('click',function(){
			var jsonRequest = new Request.JSON({
				url: "/"+ locale +"/shop/services/SubscribeForNotification?" + $time(),
				onComplete: function(result){
					if(result.STATUS == "SUCCESS"){
						product_array[index].row.getElement('.product-msg-popup-notification-ok .product-msg-popup-titleerror').setStyle('display','block');
						var displayProductMsgPopup = new Fx.Morph(product_array[index].row.getElement('.product-msg-popup-notification-ok'), {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
						displayProductMsgPopup.start({
							'opacity': 1
						});
					}
					else{
						product_array[index].row.getElement('.product-msg-popup-error span.product-msg-popup-text').set('text',result.ERRORDESC)
						product_array[index].row.getElement('.product-msg-popup-error .product-msg-popup-titleerror').setStyle('display','block');
						var AnimationDisplay = new Fx.Morph(product_array[index].row.getElement('.product-msg-popup-error'), {duration: 250, transition: Fx.Transitions.Sine.easeOut});
						AnimationDisplay.start({
							'opacity': 1
						});
					}
				}
			}).post({"sku" : product_array[index].getSkuId()});
		});
	});
	
	if ($defined($('nolonger-stock-lightbox'))) {
		$('nolonger-stock-lightbox').setStyles({
			'opacity': 0,
			'display': 'block'
		});
		$('nolonger-stock-lightbox').fade('in');
	}
	
});

function isNumberKey(evt) {
   var charCode = (evt.which) ? evt.which : event.keyCode
   if (charCode > 31 && (charCode < 48 || charCode > 57)) {   
      return false;
   }

   return true;
}

