var cicshop_addToBasket = {
	cnf: $H({errors: 	$H({
							req 		: 'Please select a color, size and quantity',
							notinstock	: 'Sorry, this product is not available in that color/size combination'
						}),
	}),
	data: $H(),
	
	start:function() {
		this.formListener();
		//TODO: this should be abstracted as well
		var selWidth = $('addToBasketSize').getWidth();
		$('addToBasketSize').setStyle({width: selWidth + 'px'});
		
		$('addToBasket').observe('submit',function(event) {
			if(cicshop_addToBasket.validateForm() == false) {
				cicshop_addToBasket.showError();
				event.stop();
			}
		});
	},
	
	showError:function(type) {
		if(cicshop_addToBasket.currentError != '') {
			$('addToBasketErrorMsgs').update('<p class="error">' + cicshop_addToBasket.currentError + '</p>');
		}
	},
	
	hideError:function() {
		$('addToBasketErrorMsgs').update('');		
	},

	validateForm:function() {
		var isValid = true;
		var requiredFields = $$('#addToBasket .addToBasketRequired');
		requiredFields.each(function(el,index) {
			if(el.value == '') isValid = false;
		});
		if(isValid != true) {
			cicshop_addToBasket.currentError = cicshop_addToBasket.cnf.get('errors').get('req');
			return false;
		}		
	},
	
	formListener:function() {
		
		new Form.Observer($('addToBasket'),0.3,function() {
			var selectedVal = $F('addToBasketImageSwitch');			
			
			// update size based on color
			// TODO: color and size relation to availability needs to be abstracted somehow
			var color = selectedVal;
			if(color != '') {
				$$('#addToBasketSize option').each(function(el,index) {
					if(cicshop_addToBasket.data.get(color).indexOf(el.value) != -1) {
						if(el.value != '') {
							el.disabled = false;							
						}
					} else {
						el.disabled = true;
						if($('addToBasketSize').selectedIndex == index) {
							$('addToBasketSize').selectedIndex = 0;
						}
					}
				});
			}			
			
			// switch images
			if($('addToBasketImageSwitch_' + selectedVal)) {
				$('addToBasket').isValid = true;
				swapImg = $F('addToBasketImageSwitch_' + selectedVal);
				if(swapImg != 'undefined') {
					cicshop_miniGallery.updateFull(swapImg);
				}
			}
		});
	},
	
	checkRequired: function() {
		var pass = true;
		if($('addToBasketImageSwitch') == null) pass = false;
		if($('miniGalleryThumbs') == null) pass = false; 
		if($('miniGalleryFull') == null) pass = false; 
		return pass;		
	}
};

var cicshop_miniGallery = {
	cnf: $H({
		disableThumbLinks: false,
		hasClose: false
	}),
	data: $H(),

	start: function() {
		$$('#miniGalleryThumbs .miniGalleryThumb').each(function(el,index) {
			var fullImg = el.down('input').readAttribute('value');
			cicshop_miniGallery.preloadImg(fullImg);
			var link = el.down('a');
			if(cicshop_miniGallery.cnf.get('disableThumbLinks') == true) {
				link.writeAttribute('href','javascript:void(0)');
				link.writeAttribute('style','cursor: default;');
			} else {
				link.writeAttribute('target','');
				link.writeAttribute('href','javascript:window.cicshop_miniGallery.popUp(\'' + link.readAttribute('href') + '\')');
			}
			link.observe('mouseover',function() {
				cicshop_miniGallery.updateFull(fullImg,link.readAttribute('href'));
			})
		});

		var fullLink = $('miniGalleryFull').up('a');
		if(fullLink) {
			fullLink.writeAttribute('target','');
			fullLink.writeAttribute('href','javascript:window.cicshop_miniGallery.popUp(\'' + fullLink.readAttribute('href') + '\')');			
		}
		
		// close button
		if($('closeButton')) {
			$('closeButton').up('a').setAttribute('href','javascript:window.close()');
		}
		
	},
	
	popUp: function(url) {
		popup = window.open('/'+ url,'productGallery','height=900,width=900,left=100,top=0,resizable=yes,scrollbars=yes,toolbar=no,status=no');
	},
	
	preloadImg: function(imgPath) {
		var image = new Image;
		image.src = imgPath;
	},
	
	updateFull: function(fullImg,newHref) {
		$('miniGalleryFull').writeAttribute('src',fullImg);			
		if($('miniGalleryFull').up('a')) {
			$('miniGalleryFull').up('a').writeAttribute('href',newHref);			
		}
	},
	
	
	checkRequired: function() {
		var pass = true;
		if($('miniGalleryThumbs') == null) pass = false; 
		if($('miniGalleryFull') == null) pass = false; 
		return pass;
	}
};

var cicshop_initialize = {
	views: new Hash(),
	initViews:function() {
		this.views.set('showProductModelDetail',new Array(cicshop_miniGallery,cicshop_addToBasket));
		this.views.set('showProductModelGallery',new Array(cicshop_miniGallery));
	},

	start:function() {
		this.initViews();		
		var view = cicshop_cnf.currentView;		
		var cnf = cicshop_cnf.getCurrentCnf();
		var data = cicshop_cnf.getCurrentData();
		var viewObjects = this.views.get(view);		
		if(viewObjects) {
			viewObjects.each(function(obj,index) {
				if(obj.checkRequired() == true) {
					// merge set conf with default conf for the object
					cnf.each(function(pair) {
						obj.cnf.set(pair.key,pair.value)
					})
					// set incoming data to object data
					obj.data = data;
					obj.start();
				}
			});			
		}
	}
};

var cicshop_cnf = {
	currentView: false,
	cuurrentData: false,
	viewCnf: $H({
		showProductModelDetail: $H({
		}),
		showProductModelGallery: $H({
			disableThumbLinks: true			
		})
	}),
	init: function() {
		this.setCurrentCnf();
	},
	getCurrentData: function() {
		return this.currentData;
	},
	setCurrentCnf: function() {
		this.currentCnf = this.viewCnf.get(this.currentView); 
	},
	getCurrentCnf: function() {
		return this.currentCnf;
	}
	
}

Element.observe(document, 'dom:loaded', function(){
	cicshop_cnf.init();
	cicshop_initialize.start();
});