var UnicCatalog= Class.create();

UnicCatalog.prototype = {
		
	currentMain: null,
	currentPosition: null,
	
	curColor: null,
	curColorId: null,
	
	allowedColorsCollection: null,
	availableSimpleProductCollection: null,
	
	currentTableSizeOpen: null,
	productSku: null,
	productIdPrefix: null,
	defaultPosition: '1',
	
	spConfig: null,
	
	initialize: function(currentMain, currentPosition){
		
		this.currentMain = currentMain;
		if(currentPosition != null){
			
			this.currentPosition = currentPosition;
		}
		// set the product sku
		// cat_01102001009-1 => 01102001
		var tmp = currentMain.split('_');
		this.productIdPrefix = tmp[0];
		tmp = tmp[1].split('-');
		this.productSku = tmp[0].substr(0, (tmp[0].length - 3));
	},
	
	
	change: function(id/*, productId, color*/){
		if(this.currentMain != null && $(this.currentMain) != null){
			
			$(this.currentMain).hide();
		}
		this.currentMain = id;
		
		if(this.currentMain != null && $(this.currentMain) != null){
			$(this.currentMain).show();
			
			// get image path from this.currentMain div > img
			
//			this.currentMain == main_01102006351-2;
			var tmp = this.currentMain.substring(5);

			var tmp= tmp.split('-');
			
			var skuColor = tmp[0];
			var currentMainPos = tmp[1];
			var currentMainSku = skuColor.substring(0,skuColor.length-3);
			var currentMainColor = skuColor.substring(skuColor.length-3);
			if($('product-img-box-zoom')) {
				$('product-img-box-zoom').stopObserving('click');
				$('product-img-box-zoom').observe('click',function(event)
						{
							uLightBox.display('/modifications/index/productzoom','sku='+currentMainSku+'&color=' + currentMainColor + '&pos=' + currentMainPos);
						});
				this.setWishlistLink(currentMainColor);
			}
		}

	},
		
	setWishlistLink: function(color){
		var href = $('wishlist_link').href;
		var tmpHref = href.substr(0, href.length-3);
		tmpHref = tmpHref + color;
		$('wishlist_link').href = tmpHref;
	},
	
	showPosition: function(id){
		
		if(this.currentPosition != null && $(this.currentPosition) != null){
			$(this.currentPosition).hide();
		}
		if(id != null && $(id) != null){
			$(id).show();
			this.currentPosition = id;
		}
	},
	
	setSPConfig: function(spConfig){
		this.spConfig = spConfig;
	},
	
	
	setSelectColor: function(attribute, color, colorId){
		var colorName = color;
		if(this.curColorId != null && $('color-image-' + this.curColorId) != null){
			$('color-image-' + this.curColorId).removeClassName('active');
		}
		
		this.curColorId = colorId;
		if(this.curColorId != null && $('color-image-' + this.curColorId) != null){
			$('color-image-' + this.curColorId).addClassName('active');
			colorName = $('color-image-' + this.curColorId).getAttribute('title');
		}
		
		this.curColor = colorName;
		
		//$('current-color-name').update(this.curColor);
		
		
		if(attribute != null)
		{
			var attribute = 'attribute' + attribute;
			// check if value exists
			var options = $(attribute).options;
			for(var i=0; i<options.length; i++){
				if(options[i].value == this.curColorId){
					$(attribute).value = this.curColorId;

	
					// enable the Size dropdown select
					var selectList = document.getElementsByClassName('super-attribute-select');
					if(!Object.isUndefined(selectList[1])){
						spConfig.fillSelect(selectList[1]);
						selectList[1].disabled = false;
					}
					break;
				}
				else{
					$(attribute).value = '';
				}
			}
			
			if(this.spConfig != null){
				this.spConfig.configureElement($(attribute));
			}
			
		}
		
		
	},

	setImageColor: function(selectId){
		if(selectId!= null && $(selectId) != null){
			var value = $(selectId).value;
			
			//this.setSelectColor(null, null, value);
			if($('color-image-' + value) != null)
			{
				$('color-image-' + value).onclick();
			}
		}

		
	},
	
	initTableSize: function(imagePath){

		this.imagePath = imagePath;
		
	},
	
	initShippingCosts: function(imagePath){
		this.shippingCostsImagePath = imagePath;
	},
	
	showTableSize: function(){

		var table_size = $('product_table_size');

		if (table_size)
		{
			table_size.show();

		} else {

			var table = '<div id="product_table_size"><div id="product_table_container"><img src="'+this.imagePath+'"><a href="javascript:void(0);" id="product_table_size_close">&nbsp;&nbsp;&nbsp;&nbsp;</a></div></div>';
			$('page').insert( {top:table} );

		}

		$('product_table_size_close').observe('click', (function(){
			this.closeTableSize();}).bind(this));
			var id = "product_table_size";
			if($(id) != null){
				this.currentTableSizeOpen = id;
				$(this.currentTableSizeOpen).show();
			}


	},
	
	showShippingCosts: function(){

		var shipping_costs = $('product_shipping_costs');

		if (shipping_costs)
		{
			shipping_costs.show();

		} else {
			var table = '<div id="product_shipping_costs"><div id="product_shipping_costs_container"><img src="'+this.shippingCostsImagePath+'"><a href="javascript:void(0);" id="product_shipping_costs_close">&nbsp;&nbsp;&nbsp;&nbsp;</a></div></div>';
			$('page').insert( {top:table} );

		}

		$('product_shipping_costs_close').observe('click', (function(){
			this.closeShippingCosts();}).bind(this));
			var id = "product_shipping_costs";
			if($(id) != null){
				this.currentShippingCostsOpen = id;
				$(this.currentShippingCostsOpen).show();
			}


	},
	
	closeTableSize: function(){
		if(this.currentTableSizeOpen != null && $(this.currentTableSizeOpen) != null){
			$(this.currentTableSizeOpen).hide();
		}
	},
	
	closeShippingCosts: function(){
		if(this.currentShippingCostsOpen != null && $(this.currentShippingCostsOpen) != null){
			$(this.currentShippingCostsOpen).hide();
		}
	},
	
	// set the allowed consolidated Colors
	setAllowedColors: function(allowedColorsCollection, availableSimpleProductCollection){
		this.allowedColorsCollection = allowedColorsCollection;
		this.availableSimpleProductCollection = availableSimpleProductCollection;
	},
	// set by the filter
	changeToColor: function(colorList, available, size){
		var colorCode = null;
		var id = this.currentMain;
		for(var i=0; i<colorList.length; i++){
			var possibleColors = this.allowedColorsCollection['_'+colorList[i]];
			
			if(possibleColors != null && possibleColors[0] != null){
				colorCode = possibleColors[0];
				if(/*size != false || */available == true){
					var availableKeys = Object.keys(this.availableSimpleProductCollection);
					var foundFlag = false;
					for(var i=0; i < availableKeys.length; i++){
						var _sku = availableKeys[i];
						if(_sku.substr(_sku.length-5,3) == colorCode  
								&& this.availableSimpleProductCollection[availableKeys[i]]['avail'] == 1
								&& (this.availableSimpleProductCollection[availableKeys[i]]['size'] == size
										|| this.availableSimpleProductCollection[availableKeys[i]]['size'] == undefined 
										|| size == false)){
							foundFlag = true;
							break;
						}
					}
					if(!foundFlag){
						colorCode = null;
					}
				}
				
				break;
			}

		}
//		if(colorCode == null && (size != false || available)){
//			// find a available product with such a size
//			var availableKeys = Object.keys(this.availableSimpleProductCollection);
//			for(var i=0; i < availableKeys.length; i++){
//				
//				if(this.availableSimpleProductCollection[availableKeys[i]]['avail'] == 1
//						&& (this.availableSimpleProductCollection[availableKeys[i]]['size'] == size 
//								|| this.availableSimpleProductCollection[availableKeys[i]]['size'] == undefined)){
//					
//					var _sku = availableKeys[i];
//					colorCode = _sku.substr(_sku.length-5,3); 
//				}
//			}
//		}
		
		if(colorCode != null){
			var id = this.productIdPrefix + '_' + this.productSku + colorCode + '-' + this.defaultPosition;
			this.change(id);
		}
//		else if(colorCode == null && available != true){
//			var _possibleColors = this.allowedColorsCollection['_'+colorList[0]];
//			var _colorCode = _possibleColors[0];
//			var id = this.productIdPrefix + '_' + this.productSku + _colorCode + '-' + this.defaultPosition;
//			this.change(id);
//		}
		else{
			this.removeConfigurableProduct(id);
		}
	},
	
	removeConfigurableProduct: function(id){
		var parentLi = $(id).up();
		var parentUl = $(parentLi).up(); // products-grid
		var parentDiv = $(parentUl).up(); // category-products
		parentLi.remove();
		

		
	},
	
	updateLink: function(id, color){
		
		if($(id) != null){
			var href = $(id).href;
			var hrefItems = href.split('?');
			var path = hrefItems[0];
			var newQuery = '';
			var newHref = '';
			
			if(hrefItems.length > 1){
				
				var queryList = hrefItems[1].split('&');
				
				for(var i=0; i<queryList.length; i++){
					if(newQuery != ''){
						newQuery += '&';
					}
					if(queryList[i].substr(0,2) == 'c='){
						newQuery += 'c=' + color;
					}
					else{
						newQuery += queryList[i];
					}
				}
			}
			else{
				newQuery = 'c=' + color;
			}
			
			newHref = path;
			if(newQuery != ''){
				newHref += '?' + newQuery;
			}
			$(id).href = newHref;
			
		}
	}
	
};


var CatalogUtility = {
		// used in combination with UnicCatalog.changeToColor
		// needed in order to remove empty ul elements in the product list
		// after a system color is choosen in the filter for available products
		cleanProductGrid: function(id){
			// redistribute the li elements into the ul element
			var parentDiv = $(id);
			var ulList = parentDiv.childElements();
			var liList = [];
			for(var i=0; i<ulList.length; i++){
				var el = ulList[i];
				liList = liList.concat(el.childElements());
			}
			//console.log(liList);
			if(liList.length > 0){
				for(var i=0; i<liList.length; i=i+4){
					
					ulElement = Element.extend(document.createElement('ul'));
					ulElement.addClassName('products-grid');
					
					for(var k=0; k<4; k++){
						if(i+k >= liList.length){
							break;
						}
						var liElement = liList[i+k];
						liElement.removeClassName('last');
						liElement.removeClassName('first');
						if(k == 0){
							liElement.addClassName('first');
						}
						else if(k == 3){
							liElement.addClassName('last');
						}
						ulElement.appendChild(liList[i+k]);
					}
					parentDiv.appendChild(ulElement);
				}
				// remove empty ul
				var ulList = parentDiv.childElements();
				for(var i=0; i<ulList.length; i++){
					if(ulList[i].childElements().length == 0){
						ulList[i].remove();
					}
				}
			}
			else{
				var url = document.domain;
				//console.log(url);
				var options = {
						method: 'get',
						evalJS: 'false',
						onSuccess: function(transport){
							$('category-products').innerHTML = '<div class="alert-text">'+transport.responseText + '</div>';
							
						}
				};
					var url = 'http://'+ document.domain + "/modifications/index/getNoProductsAfterFilterRequest";

					new Ajax.Request(url, options);
				
			}
		}
};



