if (typeof smartandsexy == "undefined" || !smartandsexy) {
    var smartandsexy = {};
}

smartandsexy.product = Class.create({	
	initialize: function(config, searchUrl) {
		this.config = config;
		this.searchUrl = searchUrl;
		this.attributeIds = [];

		$$('.super-attribute-select').each(function(element) {
			this.attributeIds[this.attributeIds.length] = element.id.replace(/[^0-9]/g, '');
		}, this);
		
		$$('ol.size li').each(function(element) {
			element.observe('click', this.clickController.bindAsEventListener(this, element));
		}, this);

		$$('.show-size-help').each(function(li) {
			li.observe('click', function(event) {
				$('size-help-content').show();
				$('dont-see-size-left').hide();
				$$('#size-help-content .size').first().update('Select a size...');
				$('size-help-button').href = 'javascript:;';
			});
		});

		$$('.show-dont-see-size-left').each(function(li) {
			li.observe('click', function(event) {
				$('dont-see-size-left').show();
				$('size-help-content').hide();
			});
		});

		var getVars = this.getGetVariables();
		this.attributeIds.each(function(id) {
			var code = this.config.attributes[id].code;
			if(getVars[code] && $('color-option-' + getVars[code])) {
				this.clickController(null, $('color-option-' + getVars[code]));
			}
		}, this);
		
		
		$$('.color-options li a').each(function(a) {
			a.observe('click', this.prepColorSwitchUrl.bindAsEventListener(this));
		}, this);
		
	},
	
	prepColorSwitchUrl: function(event) {
		var gets = new Array();
		
		this.attributeIds.each(function(id) {
			var id = this.config.attributes[id].id;
			var code = this.config.attributes[id].code;
			if($('attribute' + id).value) {
				gets[gets.length] = code + '=' + $('attribute' + id).value;
			}
		}, this, gets);
		
		if(gets.length) {
			a = event.findElement('a');
			event.stop();
			window.location = a.href + (a.href.indexOf('?')>0 ? '&' : '?') + gets.join('&');
		}
	},
	
	clickController: function (event, element) {
		if(element.hasClassName('selected')) {
			this.clickDeselect(element);
		} else if(element.hasClassName('is-in-stock')) {
			this.clickedInStock(element);
		} else if(element.hasClassName('out-of-stock')) {
			this.clickedOutOfStock(element);
		}
	},
	
	clickDeselect: function (element) {
		element.removeClassName('selected');		
		var id = element.up('ol').id.replace(/[^0-9]/g, '');
		$('attribute' + id).value = '';
		
		this.resetAll();
		if($$('ol.size li.selected').length) {
			this.clickedInStock($$('ol.size li.selected').first());
		}
	},
	
	clickedInStock: function (element) {
		element.siblings().invoke('removeClassName', 'selected');
		element.addClassName('selected');
		$('size-help-content').hide();
		
		var id = element.up('ol').id.replace(/[^0-9]/g, '');
		var option = this.config.attributes[id].options.find(function(s) {
			if(s.label == element.title) return true;
		}, element);

		$('attribute' + id).value = option.id;
		this.updateAvailability(id, option.id);
	},
	
	clickedOutOfStock: function (element) {
		$('dont-see-size-left').hide();
		$('size-help-content').show();
		$$('#size-help-content .size').first().update(element.title);
		
		var optionId = element.id.replace(/[^0-9]/g, '');
		var attributeId = element.up('ol').id.replace(/[^0-9]/g, '');
		var attributeCode = this.config.attributes[attributeId].code
		
		if(optionId && attributeCode) {
			$('size-help-button').href = this.searchUrl + '?' + attributeCode + '=' + optionId;
		}
	},
	
	getGetVariables: function () {
		var map = {};
		var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
			map[key] = value;
		});
		return map;
	}, 
	
	updateAvailability: function(attributeId, optionId) {
		var option = this.config.attributes[attributeId].options.find(function(s) {
			if(s.id == optionId) return true;
		}, optionId);
		var allowedProducts = option.products;
		
		this.attributeIds.each(function(id) {
			if(id != attributeId) {
				this.config.attributes[id].options.each(function(option) {
					var possibleOptions = option.products;
					var li = $('color-option-' + option.id);
					
					var found = false;
					for(x=0; x<allowedProducts.length; x++) {
						for(y=0; y<possibleOptions.length; y++) {
							if(allowedProducts[x] == possibleOptions[y]) {
								found = true;
							}
						}
					}

					if(found) {
						li.removeClassName('out-of-stock');
						li.addClassName('is-in-stock');
					} else {
						li.removeClassName('selected');
						li.removeClassName('is-in-stock');
						li.addClassName('out-of-stock');
					}				
				}, this, allowedProducts);
			}
		}, this, allowedProducts, attributeId);
	}, 
	
	resetAll: function() {
		this.attributeIds.each(function(id) {
			this.config.attributes[id].options.each(function(option) {
				var possibleOptions = option.products;
				var li = $('color-option-' + option.id);
				
				li.removeClassName('out-of-stock');
				li.addClassName('is-in-stock');
			}, this);
		}, this);
	}

});