Event.observe(window, 'load', function() {
	if ($('intro-slideshow')) {
		var productSlides = new protoShow('intro-slideshow', {		
			buildNavigation		: false,
			buildControls		: true,
			interval			: 5000,
			captions			: false
		});	
	}
});









/**
 *	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').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').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;
			}
		}
	});
});



/**
 *	Protip GoodBetterBest Tooltips
 */

Event.observe(document, 'dom:loaded', function() {
	$('buyers-guide') &&
	new Tip( $('buyers-guide'), gbbTooltip, {
	  title: $('gbb-title'),
	  style: 'default',
	  stem: 'topRight',
	  hook: { tip: 'topRight', mouse: true }
	});
	$$('.prod-rating').each(function(element){
		new Tip( $(element), gbbTooltip, {
		  title: element.title,
		  style: 'default',
		  stem: 'topRight',
		  hook: { tip: 'topRight', mouse: true }
		});
	});
	
});

/* */

/**
 *	Little Image Fold/Unfold
 */
var cookie    = new HTTP.Cookies;

function __dbsFold(e){
	if ( $('bigHeaderImage').hasClassName('folded') ) {
		e.innerHTML = 'Shrink Image';
		$('bigHeaderImage').removeClassName('folded');
		cookie.write('folded','','+1y','/');
	} else {
		e.innerHTML = 'Show Image';
		$('bigHeaderImage').morph('folded');	// automatically adds className to the div (clever!)
		cookie.write('folded','true','+1y','/');
	}
}




/*	LANGUAGE SWITCHER JS 
	
Event.observe(document, 'dom:loaded', function() {
	$('switch-language').hide();
	var langExpanded = 0;
	$('current-language').observe('mouseover',function(){
		if(langExpanded != 1) {
			var theElement = $('switch-language');
			theElement.setStyle({left: 0});
			theElement.appear({queue: 'end', limit: 2, delay:0.2});
			langExpanded++
		}
	});
	
	$('current-language').observe('mouseout',function(){
		if(langExpanded == 1) {
			var theElement = $('switch-language');
			theElement.fade({queue: 'front', limit: 2, delay:0.2});
			langExpanded = 0;
		}
	});

	
		
});*/



function embroideryMultiCheck() {

	// 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();
	
	
	
	function checkboxMessage() {
		if (itemsChecked > 1) {
			
			warnMessage.show();
		} else {
			
			warnMessage.hide();
		}
	}
	
	
	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();
			}
		});
		
	});
}






