/*
	Main class to manage the size manager in product page/outfit page and shopping cart
*/
var SizeManager = new Class({
	initialize: function(object){
		this.row = object;
		this.rowId = this.row.get('id');
		var pos1 = this.rowId.indexOf("_");
		var pos2 = this.rowId.lastIndexOf("_");
		if(pos1 == pos2) {
			this.productId = this.rowId.substring(pos1+1);
			this.productSkuId = this.productId;
		} else {
			this.productId = this.rowId.substring(pos1+1, pos2);
			this.productSkuId = this.rowId.substring(pos2+1);
		}
		this.actionTarget = '';
		var jsonRequest = new Request.JSON({url: "/"+ locale +"/shop/services/GetProductDetails?productid=" + this.productId, onComplete: function(result){
			this.product = result.PRODUCT;
			this.skus = result.PRODUCT.SKUS;
			this.initSelectors();
			this.initActions();
			window.fireEvent('product_ready');
		}.bind(this)}).get();
	},
	setSizeButton: function(li, className) {
		if (className == 'out-of-sale') {
			li.getElement('img').set('title', getLocalizedText('unavailable'));
		} else {
			li.getElement('img').set('title', '');
		}
		if (className == 'available' && li.hasClass('selected')) {
			li.getElement('img').set('title', '');
			className = 'selected';
		}
		li.getElement('img').set('src', '/common/images/shop/sizes/size-buttons/'+ li.getElement('img').getProperty('alt') +'-'+ className.replace(/^\s+/g,'').replace(/\s+$/g,'') +'.gif');
	},
	initSelectors: function() {
		if (this.row.getElement('div.waist-block')) {
			this.row.getElements('div.waist-block li').each(function(waistLi,index) {
				var waist = waistLi.getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
				var unclickable = true;
				this.skus.each(function(sku,skuIndex) {
					if(sku.WAIST == waist && !sku.SOLDOUT){
						unclickable = false;
					}
				}.bind(this));
				if(unclickable){
					waistLi.addClass('unclickable');
					this.setSizeButton(waistLi, 'out-of-sale');
				}
				waistLi.addEvent('click', function(){
					if (waistLi.hasClass('selected')) {
						waistLi.removeClass('selected');
						this.setSizeButton(waistLi, 'available');
						this.setWaistAndLength({'waist':-1});
					} else if (waistLi.hasClass('available')) {
						if (this.row.getElement('div.waist-block li.selected')) { 
							var selectedLi = this.row.getElement('div.waist-block li.selected');
							selectedLi.removeClass('selected');
							this.setSizeButton(selectedLi, 'available'); 
						}
						waistLi.addClass('selected');
						this.setSizeButton(waistLi, 'selected');
						this.setWaistAndLength({'waist':waist});
					}
					if (this.row.getElement('a.apply-changes')) this.row.getElement('a.apply-changes').morph({opacity: 1});
				}.bind(this));
			}.bind(this));
		}
		if (this.row.getElement('div.length-block')) {
			this.row.getElements('div.length-block li').each(function(lengthLi,index) {
				var length = lengthLi.getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
				var unclickable = true;
				this.skus.each(function(sku,skuIndex) {
					if(sku.LENGTH == length && !sku.SOLDOUT){
						unclickable = false;
					}
				}.bind(this));
				if(unclickable){
					lengthLi.addClass('unclickable');
					this.setSizeButton(lengthLi, 'out-of-sale');
				}
				lengthLi.addEvent('click', function(){
					if (lengthLi.hasClass('selected')) {
						lengthLi.removeClass('selected');
						this.setSizeButton(lengthLi, 'available');
						this.setWaistAndLength({'length':-1});
					} else if (lengthLi.hasClass('available')) {
						if (this.row.getElement('div.length-block li.selected')) { 
							var selectedLi = this.row.getElement('div.length-block li.selected');
							selectedLi.removeClass('selected');
							this.setSizeButton(selectedLi, 'available'); 
						}
						lengthLi.addClass('selected');
						this.setSizeButton(lengthLi, 'selected');
						this.setWaistAndLength({'length':length});
					}
					if (this.row.getElement('a.apply-changes')) this.row.getElement('a.apply-changes').morph({opacity: 1});
				}.bind(this));
			}.bind(this));
		}
		if (this.row.getElement('div.size-block')) {
			this.row.getElements('div.size-block li').each(function(sizeLi,index) {
				var size = sizeLi.getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
				this.skus.each(function(sku,skuIndex) {
					if(sku.SIZE == size && sku.SOLDOUT){
						sizeLi.className = "out-of-sale";
						this.setSizeButton(sizeLi, 'out-of-sale');
					}
				}.bind(this));
				sizeLi.addEvent('click', function(){
					if (sizeLi.hasClass('selected')) {
						sizeLi.removeClass('selected');
						this.setSizeButton(sizeLi, 'available');
						this.setSize();
					} else if (sizeLi.hasClass('available')) {
						sizeLi.addClass('selected');
						this.setSizeButton(sizeLi, 'selected');
						this.setSize({'size':size});
					}
					if (this.row.getElement('a.apply-changes')) this.row.getElement('a.apply-changes').morph({opacity: 1});
				}.bind(this));
			}.bind(this));
		}
	},
	initActions: function() {
		if (this.row.getElement('.apply-changes')) {
			this.row.getElement('.apply-changes').addEvent('click', function(){
				if ((this.row.getElement('div.waist-block li.selected') && this.row.getElement('div.length-block li.selected') || this.row.getElement('div.size-block li.selected'))) {
					var stock = this.getStock();
					if (stock.toInt() == 0) {
						this.openPopup();
					} else {
						if(this.getTotalQuantity() > 15){
							this.openPopupLimitedQuantity();
							return false;
						}
						this.actionSubmit('replacesku');
					}
				}
				return false;
			}.bind(this));
		}
		if (this.row.getElement('.product-delete a')) {
			this.row.getElement('.product-delete a').addEvent('click', function(){
				liId = this.row.get('id');
				productId = liId.substring(10, liId.lastIndexOf('_'));
				this.actionSubmit('delete');
				return false;
			}.bind(this));
		}
		if (this.row.getElement('.dd-quantity-menu select')) {
			this.row.getElement('.dd-quantity-menu select').addEvent('change', function(){
				this.actionSubmit('changequantity');
				return false;
			}.bind(this));
		}
	},
	setWaistAndLength: function(option) {
		if (!option) {
			this.row.getElements('div.waist-block li').each(function(waistLi,index) {
				waistLi.removeClass('available').addClass('out-of-sale');
				this.setSizeButton(waistLi, 'out-of-sale');
			}.bind(this));
			this.row.getElements('div.length-block li').each(function(lengthLi,index) {
				lengthLi.removeClass('available').addClass('out-of-sale');
				this.setSizeButton(lengthLi, 'out-of-sale');
			}.bind(this));
		} else if (option.waist) {
			if (option.waist.toInt() == -1) {
				this.row.getElements('div.length-block li').each(function(lengthLi,index) {
					if(!lengthLi.hasClass('unclickable')){
						lengthLi.removeClass('out-of-sale').addClass('available');
						this.setSizeButton(lengthLi, 'available');
					}
				}.bind(this));
				return;
			}
			this.row.getElements('div.length-block li').each(function(lengthLi,index) {
				lengthLi.removeClass('available').addClass('out-of-sale');
				this.setSizeButton(lengthLi, 'out-of-sale');
			}.bind(this));
			this.skus.each(function(sku,skuIndex) {
				if (sku.WAIST == option.waist) {
					this.row.getElements('div.length-block li').each(function(lengthLi,index) {
						var length = lengthLi.getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
						if (sku.LENGTH == length) {
							if(!sku.SOLDOUT){
								lengthLi.removeClass('out-of-sale').addClass('available');
								this.setSizeButton(lengthLi, 'available');
							}
						}
					}.bind(this));
				}
			}.bind(this));
		} else if (option.length) {
			if (option.length.toInt() == -1) {
				this.row.getElements('div.waist-block li').each(function(waistLi,index) {
					if(!waistLi.hasClass('unclickable')){
						waistLi.removeClass('out-of-sale').addClass('available');
						this.setSizeButton(waistLi, 'available');
					}
				}.bind(this));
				return;
			}
			this.row.getElements('div.waist-block li').each(function(waistLi,index) {
				waistLi.removeClass('available').addClass('out-of-sale');
				this.setSizeButton(waistLi, 'out-of-sale');
			}.bind(this));
			this.skus.each(function(sku,skuIndex) {
				if (sku.LENGTH == option.length) {
					this.row.getElements('div.waist-block li').each(function(waistLi,index) {
						var waist = waistLi.getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
						if (sku.WAIST == waist) {
							if(!sku.SOLDOUT){
								waistLi.removeClass('out-of-sale').addClass('available');
								this.setSizeButton(waistLi, 'available');
							}
						}
					}.bind(this));
				}
			}.bind(this));
		}
		var stock = this.getStock();
		this.setDropdown(stock);
	},
	setSize: function(option) {
		if (!option) {
			this.row.getElements('div.size-block li').each(function(sizeLi,index) {
				sizeLi.removeClass('selected');
				this.setSizeButton(sizeLi, sizeLi.className);
			}.bind(this));
		} else if (option.size) {
			this.row.getElements('div.size-block li').each(function(sizeLi,index) {
				var size = sizeLi.getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
				if (option.size == size) {
					sizeLi.addClass('selected');
					this.setSizeButton(sizeLi, 'selected');
				} else {
					sizeLi.removeClass('selected');
					this.setSizeButton(sizeLi, sizeLi.className.replace(/^\s+/g,'').replace(/\s+$/g,''));
				}
			}.bind(this));
		}
		var stock = this.getStock();
		this.setDropdown(stock);
	},
	setDropdown: function(newStock) {
				var quantitySelected = $("quantity" + this.productSkuId).get('value');
				$("quantity" + this.productSkuId).set('html', '');
				$("dhtml_quantity" + this.productSkuId).set('html', '');
				if(newStock <= 1) {
					$("quantity" + this.productSkuId).options[0] = new Option(1,1);
				} else if(newStock == 2) {
					$("quantity" + this.productSkuId).options[0] = new Option(1,1);
					$("quantity" + this.productSkuId).options[1] = new Option(2,2);
				} else {
					$("quantity" + this.productSkuId).options[0] = new Option(1,1);
					$("quantity" + this.productSkuId).options[1] = new Option(2,2);
					$("quantity" + this.productSkuId).options[2] = new Option(3,3);
				}
				if (newStock == 0) {
					quantitySelected = 1;
				} else if (quantitySelected >= newStock) {
					quantitySelected = newStock;
				}
				$("quantity" + this.productSkuId).set('value', quantitySelected);
				dhtmlselect("quantity" + this.productSkuId, 'jsDropDownMenu', '30', quantitySelected);
				$("dhtml_quantity" + this.productSkuId).addEvent('click',function(e){
					e.stopPropagation();
				});
	},
	getStock: function() {
		var stock = 0;
		if(this.row.getElement('div.size-block li.selected')){
			var size = this.row.getElement('div.size-block li.selected').getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
			this.skus.each(function(sku,skuIndex) {
				if (sku.SIZE == size) {
					stock = sku.STOCK.toInt();
				}
			});
		}
		else if(this.row.getElement('div.waist-block li.selected') && this.row.getElement('div.length-block li.selected')){
			var waist = this.row.getElement('div.waist-block li.selected').getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
			var length = this.row.getElement('div.length-block li.selected').getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
			this.skus.each(function(sku,skuIndex) {
			if (sku.WAIST == waist && sku.LENGTH == length) {
				stock = sku.STOCK.toInt();
				}
			});
		}
		return stock;
	},
	getSkuId: function() {
		var skuId = null;
		if (this.row.getElement('div.size-block li.selected')) {
			var size = this.row.getElement('div.size-block li.selected').getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
			this.skus.each(function(sku,skuIndex) {
				if (sku.SIZE == size) {
					skuId = sku.ID;
				}
			});
		} else if (this.row.getElement('div.waist-block li.selected') && this.row.getElement('div.length-block li.selected')) {
			var waist = this.row.getElement('div.waist-block li.selected').getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
			var length = this.row.getElement('div.length-block li.selected').getElement('img').getProperty('alt').replace(/^\s+/g,'').replace(/\s+$/g,'');
			this.skus.each(function(sku,skuIndex) {
				if (sku.WAIST == waist && sku.LENGTH == length) {
					skuId = sku.ID;
				}
			});
		}
		return skuId;
	},
	getTotalQuantity: function() {
		var total_quantity = 0;
		$('shoppingcart-center').getElements('li.productIdBlock select').each(function(select_list,index){
			total_quantity += select_list.value.toInt();
		});
	},
	openPopup: function() {
		if (shopUserState == true) {
			var closeProductMsgPopup = new Fx.Morph(this.row.getElement('.product-msg-popup-availability-loggedin'), {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
			this.row.getElement('.product-msg-popup-availability-loggedin .product-msg-popup-titleerror').setStyle('display','block');
			closeProductMsgPopup.start({
				'opacity': 1
			});
			this.row.getElement('.product-msg-popup-availability-loggedin .product-msg-popup-close').addEvent('click',function(){
				this.closePopup();
			}.bind(this));
		} else {
			var closeProductMsgPopup = new Fx.Morph(this.row.getElement('.product-msg-popup-availability-notloggedin'), {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
			this.row.getElement('.product-msg-popup-availability-notloggedin .product-msg-popup-titleerror').setStyle('display','block');
			closeProductMsgPopup.start({
				'opacity': 1
			});
			this.row.getElement('.product-msg-popup-availability-notloggedin .product-msg-popup-close').addEvent('click',function(){
				this.closePopup();
			}.bind(this));
		}
	},
	openPopupLimitedQuantity: function() {
		var closeProductMsgPopup = new Fx.Morph(this.row.getElement('.product-msg-popup-limited-quantity'), {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
		closeProductMsgPopup.start({
			'opacity': 1
		});
		this.row.getElement('.product-msg-popup-limited-quantity .product-msg-popup-close').addEvent('click',function(){
			this.closePopup();
		}.bind(this));
	},
	openPopupLimitedSku: function() {
		if($chk(this.row.getElement('.product-msg-popup-limited-sku'))){
			var closeProductMsgPopup = new Fx.Morph(this.row.getElement('.product-msg-popup-limited-sku'), {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
			closeProductMsgPopup.start({
				'opacity': 1
			});
			this.row.getElement('.product-msg-popup-limited-sku .product-msg-popup-close').addEvent('click',function(){
				this.closePopup();
			}.bind(this));
		}
	},
	closePopup: function() {
		$$('.product-msg-popup-titleerror','.eshop-cart-popup-title').each(function(item){
			$(item).setStyle('display','none');
		});
		if (this.row.getElement('.product-msg-popup-availability-loggedin')) {
			var closeProductMsgPopup = new Fx.Morph(this.row.getElement('.product-msg-popup-availability-loggedin'), {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
			closeProductMsgPopup.start({
				'opacity': 0
			});
		}  if (this.row.getElement('.product-msg-popup-availability-notloggedin')) {
			var closeProductMsgPopup = new Fx.Morph(this.row.getElement('.product-msg-popup-availability-notloggedin'), {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
			closeProductMsgPopup.start({
				'opacity': 0
			});
		}  if (this.row.getElement('.product-msg-popup-limited-quantity')) {
			var closeProductMsgPopup = new Fx.Morph(this.row.getElement('.product-msg-popup-limited-quantity'), {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
			closeProductMsgPopup.start({
				'opacity': 0
			});
		}  if (this.row.getElement('.product-msg-popup-limited-sku')) {
			var closeProductMsgPopup = new Fx.Morph(this.row.getElement('.product-msg-popup-limited-sku'), {duration: 250, transition: Fx.Transitions.Sine.easeInOut});
			closeProductMsgPopup.start({
				'opacity': 0
			});
		}
	},
	actionSubmit: function(operation) {
		if (this.row.getElement('.apply-changes')) {
			if (operation == 'replacesku') {
				var formHtml = '<input type="hidden" name="action" value="replacesku" /><input type="hidden" name="oldsku" value="'+ this.productSkuId +'" /><input type="hidden" name="newsku" value="'+ this.getSkuId() +'" /><input  type="hidden"  name="quantity" value="'+ this.row.getElement('.dd-quantity-menu select').get('value') +'" />';
			} else if (operation == 'changequantity') {
				var formHtml = '<input type="hidden" name="action" value="changequantity" /><input type="hidden" name="sku" value="'+ this.productSkuId +'" /><input  type="hidden"  name="quantity" value="'+ this.row.getElement('.dd-quantity-menu select').get('value') +'" />';
			} else if (operation == 'delete') {
				var formHtml = '<input type="hidden" name="action" value="delete" /><input type="hidden" name="sku" value="'+ this.productSkuId +'" />';
			}
			var form = new Element('form', {action: this.actionTarget, method: 'post', html: formHtml});
			form.inject(this.row);
			form.submit();
		}
	}
});

