Event.observe(window, 'load', function() {



	/* HOME SLIDES
	------------------------------------------------*/
	if ($('welcome')) {
		var productSlides = new protoShow('welcome', {		
			buildNavigation		: true,
			buildControls		: false,
			interval			: 25000,
			captions			: false
		});	
	}

	/* PRODUCT DETAIL IMAGE SLIDES
	------------------------------------------------*/

	if ($('product-detail-images')) {
		var productSlides = new protoShow('product-detail-images', {		
			buildNavigation		: false,
			buildControls		: false,
			captions			: false,
			autoplay			: true,
			autoRestart			: false,
			interval			: 10000
		});	
	}


	/* CATALOGUE VIEWER
	------------------------------------------------*/

	if ($('catalogue-viewer')) {
		var productSlides = new protoShow('catalogue-viewer', {		
			buildNavigation		: true,
			captions			: false,
			autoplay			: false,
			autoRestart			: false
			/*initialSlide 		: 2, */
		});	
	}
	

	
});









/**
 *	Search Box
 */
Event.observe(document, 'dom:loaded', function() {


	embroideryMultiCheck();



	$('1').onfocus = function() {
	  // if already cleared, do nothing
	  if (this._cleared) return
	
	  // when this code is executed, "this" keyword will in fact be the field itself
	  this.clear()
	  this._cleared = true
	}
	
	if ( $('bodycontent') && $('bodycontent').select('.part-embroidery-10-logo').length){
		
		//////////////* PART ONE */////////////////
		///////////////////////////////////////////
		
		//hide all on page load
		hideLogoImage();
		(!$('logo_colours_1').checked) && $('logo_colours_1').up('div.f-block').next('div.f-block').hide();
		(!$('logo_colours_1').checked) && $('logo_colours_1').up('div.f-block').next('div.f-block').next('div.f-block').hide();
		
		///////// FIELDSET: LOGO IMAGE
			
		//show upload box under 'Yes'
		$('logo_image_1').observe('click',function(e) {
			e.element().up('div.f-block').next('div.f-block').show();
			$('logo_image_3').up('div.f-block').next('div.f-block').hide();
		});
		
		//hide both logo-image fields
		$('logo_image_2').observe('click',function(e) {
			hideLogoImage();
		});	
		
		//show text field under 'No'
		$('logo_image_3').observe('click',function(e) {
			e.element().up('div.f-block').next('div.f-block').show();
			$('logo_image_1').up('div.f-block').next('div.f-block').hide();
		});
		
		
			///////// FIELDSET: LOGO COLOURS
		
		$('logo_colours_1').observe('click',function(e) {
			e.element().up('div.f-block').next('div.f-block').show();
			e.element().up('div.f-block').next('div.f-block').next('div.f-block').show();
		});
		
		$('logo_colours_2').observe('click',function(e) {
			$('logo_colours_1').up('div.f-block').next('div.f-block').hide();
			$('logo_colours_1').up('div.f-block').next('div.f-block').next('div.f-block').hide();
		});
		
		
	} else if ( $('bodycontent') && $('bodycontent').select('.part-embroidery-20-garment').length ){
		
		//////////////* PART TWO */////////////////
		///////////////////////////////////////////		
		
		( !$('logo_position_7').checked ) && hideOther();
		( !$('logo_dimensions_1').checked ) && hideWidthHeight();	
		
		// Garment OTHER
		(!$('garment_type_6').checked) && $('garment_type_other').up('div.f-block').hide();
		$('garment_type_6').observe('click',function(e) {
			if ( e.element().checked ) {
				e.element().up('div.f-block').next('div.f-block').show();
			} else {
				e.element().up('div.f-block').next('div.f-block').hide();
			}
		});
		
		$('logo_dimensions_1').observe('click',function(e) {
			e.element().up('div.f-block').next('div.f-block').show();
			e.element().up('div.f-block').next('div.f-block').next('div.f-block').show();
		});
		
		$('logo_dimensions_2').observe('click', hideWidthHeight);
		
		
		$('logo_position_7').observe('click',function(e) {
			e.element().up('div.f-block').next('div.f-block').show();	
		});
		
		$('logo_position_1').observe('click', hideOther);
		$('logo_position_2').observe('click', hideOther);
		$('logo_position_3').observe('click', hideOther);
		$('logo_position_4').observe('click', hideOther);
		$('logo_position_5').observe('click', hideOther);
		$('logo_position_6').observe('click', hideOther);
		
		
	}
});

function hideLogoImage() {
		(!$('logo_image_1').checked) && $('logo_image_1').up('div.f-block').next('div.f-block').hide();
		(!$('logo_image_3').checked) && $('logo_image_3').up('div.f-block').next('div.f-block').hide();
}

function hideWidthHeight() {
		$('logo_dimensions_1').up('div.f-block').next('div.f-block').hide();
		$('logo_dimensions_1').up('div.f-block').next('div.f-block').next('div.f-block').hide();
}

function hideOther() {
	$('logo_position_7').up('div.f-block').next('div.f-block').hide();
};


/**
 *	Product ImageSwap.. 
 */
Event.observe(document, 'dom:loaded',function(){
	$('productlist') && 
	$('productlist').observe('mouseover',function(e){
		var element = $(Event.element(e));
		if ( element.hasClassName('img-token') ) {
			var src = element.style.backgroundImage.substring(4,element.style.backgroundImage.length-1);
			// Fix FF3.6 url("") behaviour!?
			    src = src.replace(/^\"(.*)\"/, "$1");
			if ( i = element.up('li').down('.prod-img') ) {
				i.src = src;
			}
		}
	});
});










function embroideryMultiCheck() {
	
	if ( $('garment_type') || $('logo_type') ) {
		// Checks how many options are select on checkboxes in the Embroidery section and reacts accordingly
		// If you need to extend this function then it needs to be made into an object
	
		// Cache checkboxes
		
		if ($('garment_type') ) {
			var checkBoxes 	= $('garment_type').select('.f-bln input');
		} else if ($('logo_type')){	
			var checkBoxes 	= $('logo_type').select('.f-bln input');
		}
		
		var warnMessage = $('logo-checkbox-warn');
		var itemsChecked = 0;
		
		
		// Check which boxes are initially checked
		checkBoxes.each(function(input) {
			if (input.getValue() == 1) {
				itemsChecked = itemsChecked + 1;
			} 
		});
		
		
		// Check whether to display warning message
		
		checkboxMessage();
		
		
		checkBoxes.each(function(e) {		
			new Form.Element.Observer(e, 0.2, function(ele) {
				if (ele.getValue() == 1) {
					itemsChecked = itemsChecked + 1;
					
					// Check whether to display warning message
					checkboxMessage();
				} else {
					itemsChecked = itemsChecked - 1;
					
					// Check whether to display warning message
					checkboxMessage();
				}
			});
			
		});
	}


	function checkboxMessage() {
		if (itemsChecked > 1) {
			warnMessage.show();
		} else {
			warnMessage.hide();
		}
	}
}



		




