var images = new Array();
var imageIndex = 0;

Event.observe(window, 'load', function(e){
	
	loaded = true;
	
	$$('form.cart_form').each(function(element){
		Event.observe(element, 'submit', function(e){
			Event.stop(e);
			var id = element.id.split('_');
			
			//Prevent sale of sold out items
			if(
				$(id[0] + '_' + id[1] + '_os0') 
				&& $F(id[0] + '_' + id[1] + '_os0') == 'Ice Blue' 
				&& $F(id[0] + '_' + id[1] + '_os1') == '2T'
			) {
				alert('We apologize but this combination is currently sold out.');
				return;
			}
			
			//Apply promo code
			var promo = $F(id[0] + '_' + id[1] + '_promo');
			
			if(promo != '') {
				//Check validity of promo code
				new Ajax.Request('get_promo_discount.php', {
					method: 'get',
					parameters: 'promo=' + promo + '&product=' + id[0],
					onSuccess: function(transport) {
						var amount = $F(id[0] + '_' + id[1] + '_orig-amount');
						var discountedAmount = amount * (1 - transport.responseText);
						discountedAmount = discountedAmount.toFixed(2);
						$(id[0] + '_' + id[1] + '_amount').value = discountedAmount;
						
						element.submit();
					}
				});	
			}
			else {
				element.submit();
			}
		});
	});
	
	//Patch IE's lack of support for the :hover pseudo class
	$$('#nav li').each(function(element){
		Event.observe(element, 'mouseover', function(e){
			element.addClassName('over');
		});
		Event.observe(element, 'mouseout', function(e){
			element.removeClassName('over')
		});
	});
	
	//Open popup window
	$$('a.popup').each(function(element){
		Event.observe(element, 'click', function(e){
			window.open(element.href, 'popup', 'resizable=1,width=500,height=500');
			Event.stop(e);
		});
	});
	
	//Preload product images
	$$('img.color').each(function(element){
		var id = element.id.split('_');
		images[imageIndex] = new Image();
		images[imageIndex].src = 'img/products/' + id[0] + '/' + id[1] + '_' + id[2] +'.jpg';
		imageIndex++;
	});
	
	//Change product image when color block is rolled over
	$$('img.color').each(function(element){
		Event.observe(element, 'mouseover', function(e){
			var id = element.id.split('_');
			$(id[1]).src = 'img/products/' + id[0] + '/' + id[1] + '_' + id[2] +'.jpg';
			$(id[1]+'_color').innerHTML = element.alt;
		});
	});
	
	//Change product image when new color is selected
	$$('select.color').each(function(element){
		Event.observe(element, 'change', function(e){
			var id = element[element.selectedIndex].id.split('_');
			$(id[0] + '_' + id[1]).src = 'img/products/' + id[0] + '/' + id[1] + '_' + id[2] +'.jpg';
		});
	});
	
	$$('form.cart_form select').each(function(element){
		Event.observe(element, 'change', function(e) {
			var id = element.id.split('_');
			
			//Alert users of sold out combinations
			var soldOut = false;
			var name = $(element.id).name;
			if(name == 'os0') {
				if($F(element.id) == 'Ice Blue') {
					if($F(id[0] + '_' + id[1] + '_os1') == '2T') {
						soldOut = true;
					}
				}
			}
			else if(name == 'os1') {
				if($F(element.id) == '2T') {
					if($F(id[0] + '_' + id[1] + '_os0') == 'Ice Blue') {
						soldOut = true;
					}
				}
			}
			if(soldOut) {
				$(id[0] + '_' + id[1] + '_message').innerHTML = 'Sold Out';
			}
			else {
				$(id[0] + '_' + id[1] + '_message').innerHTML = '';
			}
		});
	});
	
});