/* <![CDATA[ */
window.addEvent('domready', function(){

	var currentDetailPane = 0;
	var currentProductPane = 0;
	
	/* Product Detail Logic */
	
	var detailScroll = new Fx.Scroll('detail_viewport', {
		wait: false,
		duration: 600,
		transition: Fx.Transitions.Quad.easeInOut
	});

	detailScroll.toElement('product' + currentDetailPane);

	var detailElements = $$('.product_detail_container');
	

	/* ******** Left and right arrows ********* */
	$('db_right').addEvent('click', function(event) {
		if (currentDetailPane < (detailElements.length-1)) {currentDetailPane++;} else {currentDetailPane = 0;}
		var pane = 'product' + currentDetailPane;
		
		event = new Event(event).stop();
		detailScroll.toElement(pane);
	});
	
	$('db_left').addEvent('click', function(event) {
		if (currentDetailPane > 0) {currentDetailPane--;} else {currentDetailPane = detailElements.length -1;}
		var pane = 'product' + currentDetailPane;

		event = new Event(event).stop();
		detailScroll.toElement(pane);
	});
	
	
	/* Specifications Logic */
	
	var specButtons = $$('.product_specs');
	
	$('specifications').setStyles({
		opacity:0
	});

	specButtons.each(function(element) {
		element.addEvent('click', function(event) {
			var btnIndex = element.getProperty('id').substring(10);
			var newSpecHtml = $('specs_' + btnIndex).innerHTML;
			
			event = new Event(event).stop();
			$('specs_content').innerHTML = newSpecHtml;
			$('specifications').setStyles({
				opacity:1
			});
		});
	});
	
	$('specs_close_button').addEvent('click', function(event) {
		event = new Event(event).stop();
		$('specifications').setStyles({
			opacity:0
		});
	});
	
	
	/* Product Browser Logic */

	var browserScroll = new Fx.Scroll('product_viewport', {
		wait: false,
		duration: 600,
		transition: Fx.Transitions.Quad.easeInOut
	});

	browserScroll.toElement('product_link' + currentProductPane);
	
	var productBrowseElements = $$('#pb_list li');
	
	/* ******** Left and right arrows ********* */
	$('pb_right').addEvent('click', function(event) {
		event = new Event(event).stop();
		if (currentProductPane < (productBrowseElements.length - 3)) {
			currentProductPane = currentProductPane + 3;
		} 
		else {
			currentProductPane = 0;
		}
		var pane = 'product_link' + currentProductPane;
		
		browserScroll.toElement(pane);
	});
	
	$('pb_left').addEvent('click', function(event) {
		event = new Event(event).stop();
		if (currentProductPane > 3) {
			currentProductPane = currentProductPane - 3;
		} 
		else if (currentProductPane > 0) {
			currentProductPane = 0;
		}
		else {
				currentProductPane = productBrowseElements.length -1;
		}
		var pane = 'product_link' + currentProductPane;

		browserScroll.toElement(pane);
	});
	
	
	productBrowseElements.each(function(element) {
		element.addEvent('click', function(event) {
			var liIndex = element.getProperty('id').substring(12);
			var pane = 'product' + liIndex;

			event = new Event(event).stop();
			detailScroll.toElement(pane);
			currentDetailPane = liIndex;
		});
	});

});
/* ]]> */