window.addEvent('domready', function() {

	$$('.hide .personalisation').hide();

	$$('.group div.group').addEvent('click', function() {
		var id = jQuery('input[name=pid]', this).val();
		var name = jQuery('input[name=name]', this).val();
		var cutout = jQuery('input[name=cutout]', this).val();
		var title = jQuery('input[name=title]', this).val();
		var sku = jQuery('input[name=sku]', this).val();
		var desc = jQuery('input[name=description]', this).val();
		var price = jQuery('input[name=price]', this).val();
		var stock = jQuery('input[name=stock]', this).val();
		var in_stock = jQuery('input[name=in_stock]', this).val();
		var in_stock_date = jQuery('input[name=in_stock_date]', this).val();
		var url_identifier = jQuery('input[name=url_identifier]', this).val();
		var base_url = jQuery('input[name=base_url]').val();
		
		var personalise_item = jQuery('input[name=personalise_item]', this).val();
		var stock_alert = jQuery('input[name=stock_alert]', this).val();
		
		jQuery('.col3 .hide').hide();

		var holder = ($defined($('holder'))) ? $('holder') : new Element('div', {'id': 'holder'});
		holder.empty();
		
		var cutoutEl = new Element('img', {'id': 'cutout'}).inject(holder);
		var prodheadEl = new Element('img', {'id': 'prodhead'}).inject(holder);
		var skuEl = new Element('p', {'id': 'sku'}).inject(holder);
		var priceEl = new Element('p', {'id': 'price'}).inject(holder);
		var descEl = new Element('p', {'id': 'description'}).inject(holder);
		
		if (stock > 0 && in_stock != 0) {
			var addDiv = new Element('div', {'id': 'addDiv'}).inject(holder);
			var addP = new Element('p', {'id': 'addP'}).inject(addDiv);
			
			// Personalisation items need to first go through to be personalised before being added.
			if (personalise_item != 1) {
				var addSelect = new Element('select', {'id': 'addSelect'}).inject(addDiv);
				var addBtn = new Element('button', {'id': 'addBtn'}).inject(addDiv);
	            //var addWishlistLink = new Element('a', {'id': 'addWishlistLink'}).inject(addDiv);
				var addGiftlistLink = new Element('a', {'id': 'addGiftlistLink'}).inject(addDiv);
				
				addP.set('text', 'How many would you like? ');
				addSelect.empty();
			
				var max = (stock < 12) ? stock : 12;
			
				for (i=0; i < max; i++) {
					var opt = new Element('option', {'value': i+1}).inject(addSelect);
					opt.set('text', i+1);
				}
				addBtn.set('text', 'Buy');
				addBtn.addEvent('click', function() {
					var addUrl = base_url + "shop/checkout/cart/add?product="+id+"&qty="+$('addSelect').value;
					location.href = addUrl;
				});

	           	//addWishlistLink.set('href', 'shop/wishlist/index/add/product/'+id+'/store/2');
	            //addWishlistLink.set('text', 'Add to Wishlist');
				//addGiftlistLink.set('href', 'shop/adjgiftreg/event/addItem/product/'+id);
	            //addGiftlistLink.set('text', 'Add to Registry');
			} else {
				var addPersonaliseLink = new Element('button', {'id': 'addPersonaliseLink', 'onclick':'location.href="/shop/'+url_identifier+'"', 'text':'Buy now and personalise', 'style': 'width:200px;padding: 4px'}).inject(addP);
				// var addPersonaliseLink = new Element('a', {'id': 'addPersonaliseLink', 'href':'shop/'+url_identifier, 'text':'Personalise this item', 'class':'personalise_item'}).inject(addP);
				
				var addPXmas = new Element('p', {'id': 'addPXmas'}).inject(holder);
				addPXmas.set('text', 'For personalised orders please allow 14 days from order to dispatch.');
				
			}
		} else {
			var noStock = new Element('p', {'id': 'noStock'}).inject(holder);
			
			//check to see if red or blue cornish
			var cat = jQuery('#hot-links li a.active').attr('href');
			
			//check to see if next instock date is available
			var instockDateText = '.';
			if (in_stock_date.length > 0) {
				instockDateText = ' but we\'ll have some more in by <span class="stock-date">'+ in_stock_date + '</span>.';
			}
			
			if (cat.search("cornish-red") != -1) {
				noStock.set('html', 'Sorry, that one\'s out of stock right now' + instockDateText);

				var addOtherStockLink = new Element('a', {'id': 'addOtherStockLink'}).inject(holder);
				addOtherStockLink.set('text', 'Click here to see if we have it in blue.');
				addOtherStockLink.set('href', '');
				addOtherStockLink.addEvent('click', function() {
					jQuery('#hot-links .blue a').trigger('click');
					return false;
				});

			} else if (cat.search("cornish-blue") != -1) {			
				noStock.set('html', 'Sorry, that one\'s out of stock right now' + instockDateText);
				var addOtherStockLink = new Element('a', {'id': 'addOtherStockLink'}).inject(holder);
				addOtherStockLink.set('text', 'Click here to see if we have it in red.');
				addOtherStockLink.set('href', '');
				addOtherStockLink.addEvent('click', function() {
					jQuery('#hot-links .red a').trigger('click');
					return false;
				});
			} else {
				noStock.set('html', 'Sorry we are temporarily out of stock' + instockDateText);
			}
			
			
			var addDiv = new Element('div', {'id': 'addDiv'}).inject(holder);
			/*var addStockAlertLink = new Element('a', {'id': 'addStockAlertLink'}).inject(addDiv);
			addStockAlertLink.set('href', stock_alert);
        	addStockAlertLink.set('text', 'Get notified when this product is back in stock');
			addStockAlertLink.set('class', 'stock_alert');//*/
		}
		
		prodheadEl.set('src', title);
		prodheadEl.set('alt', name);
		skuEl.set('text', sku);
		priceEl.set('text', '£'+price);
		descEl.set('text', desc);
		
		holder.inject($$('.col3')[0]);
		
		cutoutEl.set('src', cutout);
		cutoutEl.addClass('js');
		
		holder.inject($$('.product')[0]);
		
		jQuery('#holder').show();
		
		// var fxscroll = new Fx.Scroll(window, { duration:500 });
		// fxscroll.start(0, $('buy_box_link').getCoordinates().top);
		
		var _pre_product_id = (location.href.indexOf('#cat=') != -1) ? location.href.substring(location.href.indexOf('#cat=')+1)+'&' : '';
		if (_pre_product_id.indexOf('prod=') != -1) {
			_pre_product_id = _pre_product_id.substring(0, _pre_product_id.indexOf('prod=')); // -1 to remove the leading hash or ampersand.
		} else if (_pre_product_id.indexOf('product=') != -1) {
			_pre_product_id = _pre_product_id.substring(0, _pre_product_id.indexOf('product=')); // -1 to remove the leading hash or ampersand.
		}
		
		location.href = '#' + _pre_product_id + 'prod=' + url_identifier;
	});
});

window.addEvent('load', function() {
	// If the page links to a category and/or product via a hashtag...
	var prod_hash = '#prod=';
	var prod_hash_2 = '#product=';
	var cat_hash = '#cat=';

	
	if (location.href.indexOf(prod_hash_2) != -1) {
		var prod = location.href.substring(location.href.indexOf(prod_hash_2)+prod_hash_2.length);
		var parent = $(document).getElement('input[value='+prod+']').parentNode;
		// Need to use the Mootools fireEvent function because the event is bound using Mootools.
		$(parent).fireEvent('click');
	} else if (location.href.indexOf(prod_hash) != -1) {
		var prod = location.href.substring(location.href.indexOf(prod_hash)+prod_hash.length);
		var parent = $(document).getElement('input[value='+prod+']').parentNode;
		// Need to use the Mootools fireEvent function because the event is bound using Mootools.
		$(parent).fireEvent('click');
	} else if (location.href.indexOf(cat_hash) != -1) { // If we're linking to a category tab.
		var cat = location.href.substring(location.href.indexOf(cat_hash)+cat_hash.length);
		var prod = false;
		if (cat.indexOf('&product=') != -1) {
			cat = cat.substring(0, cat.indexOf('&product='));
			prod = location.href.substring(location.href.indexOf('&product=')+9);
		} else if (cat.indexOf('&prod=') != -1) {
			cat = cat.substring(0, cat.indexOf('&prod='));
			prod = location.href.substring(location.href.indexOf('&prod=')+6);
		}
		jQuery('a[href=#'+cat+']').trigger('click');
		if (prod) { // If we're linking to a product as well.
			var parent = $(document).getElement('input[value='+prod+']').parentNode;
			// Need to use the Mootools fireEvent function because the event is bound using Mootools.
			$(parent).fireEvent('click');
		}
	}
});

jQuery(function() {
	jQuery('#hot-links a').click(function(e) {
		e.preventDefault();
		// ignore if this tab is already visible.
		if (jQuery('#col-container .section:visible').attr('id') != jQuery(this).attr('href').substring(1)) {
			var _href = jQuery(this).attr('href');
			var _colour = jQuery(this).parents('li').attr('class');
			
			if (_colour == "personalisation") {
				jQuery('#cornish-personalisation .group input[name=personalise_item][value=1]').each(function() {
					if (jQuery(this).siblings('input[name=name]').attr('value').indexOf('coloured') != -1) {
						jQuery(this).parent('.group').addClass('coloured');
					}
				});
			}
			
			jQuery('#hot-links a.active').removeClass('active');
			jQuery('img#category-image').attr('src', '/static/images/products/packs-'+_colour+'.jpg');
			jQuery(this).addClass('active');
			jQuery('#col-container .section:visible').slideUp('slow', function() {
				jQuery(_href).slideDown('slow');
			});
			jQuery('#holder').hide();
			jQuery('#product .hide div:visible').hide().siblings('.'+_colour).show();
			jQuery('#product .hide').show()
			
			location.href = '#cat='+_href.substring(1);
		}
		
		/*var _id = jQuery(this).attr('href').substring(1); // drop the hash
		var fxscroll = new Fx.Scroll(window, { duration:500 });
		fxscroll.start(0, $(_id).getCoordinates().top-20);*/
	});
	
	jQuery('#product-scroll').scrollFollow({
		container:'col-container',
		delay:50,
		speed:250
	});
});
