// custom.js
$(document).ready( function() {
	new HeaderSearch();
	oAHoverSwapImage.init();
	// Slideshow:
	new SlideShow( {
		 container: '#divHomeSlideItems'
		,button_items: '#divHomeSlideButtonsItem'
		,button_prev: '#aHomeSlidePrev'
		,button_next: '#aHomeSlideNext'
		,timeout: 5000 // miliseconds between transitions
		,speed: 1000 // transition duration in miliseconds
		,fx: 'scrollHorz'
		,sync: true
	} );
	// Project list paging:
	new SlideShow( {
		 container: '#divProductList div.Pages'
		,button_items: '#divProductList .PageNumbers'
		,button_prev: '#divProductList .PagePrev'
		,button_next: '#divProductList .PageNext'
		,timeout: 0 // miliseconds between transitions
		,speed: 200 // transition duration in miliseconds
		,fx: 'fade'
		,sync: false
	} );
	// Product Sheet Tabs
	new Tabs('div.ProductSheetData ul.Tabs li a', 'div.ProductSheetData div.Page', 'div.ProductSheetData div.Page h2')
	// Home Family Search
	homeSearchFamily();
} );


function HeaderSearch() {
	this.jqText = $('#inputHeaderSearchText');
	this.jqReset = $('#inputHeaderSearchReset');
	this.jqText.data('this', this);
	this.jqReset.click( function() {
		$(this).blur();
	} );
	this.jqText.keyup( function() {
		var jqReset = $(this).data('this').jqReset;
		if($(this).val() == '') {
			jqReset.hide();
		} else { 
			jqReset.show();
		}
	} );	
	this.jqReset.click( function() {
		$(this).hide();
	} );
	this.jqText.keyup();
	this.jqReset.attr('value', '');
}

// HoverImageSource
oHoverImage = {
	 sHoverSuffix: '-hvr'
	,getSrc: function(sSrc, bHvr) {
		var sHvrSuf = oHoverImage.sHoverSuffix;
		var re = new RegExp('('+sHvrSuf+')?\.(gif|jpg|png)$');
		sSrc = sSrc.replace(re, bHvr ? sHvrSuf+'.$2' : '.$2');
		return sSrc;
	}
	,preload: function(sSrc) {
		jQuery('<img>').attr('src', oHoverImage.getSrc(sSrc, true));
	}
}
// HoverSwapImage class
oAHoverSwapImage = {
	 init: function() {
		$('.HoverSwapImage').each(
			function() {
				$(this).hover(
					function(eventObj) {
						$(this).find('img').attr('src', oHoverImage.getSrc($(this).find('img').attr('src'), eventObj.type == 'mouseenter'));
					}
				);
				oHoverImage.preload($(this).find('img').attr('src'));
				jQuery('<img>').attr('src', oHoverImage.getSrc($(this).find('img').attr('src'), true));
			}
		);
	}
}

// Slide show
function SlideShow(param) {
	this.container = param.container;
	this.button_items = param.button_items;
	this.button_next = param.button_next;
	this.button_prev = param.button_prev;
	this.timeout = param.timeout;
	this.speed = param.speed;
	this.fx = param.fx;
	this.sync = param.sync;
	this.onPrevNextEvent = param.onPrevNextEvent;
	this.init = function() {
		var jqSlider = $(this.container);
		var jqButtons = $(this.button_items);
		var jqPrev = $(this.button_prev);
		var jqNext = $(this.button_next);
		jqSlider.find('> div').show();
		jqSlider.find('> div').data('objSlideShow', this);
		jqSlider.find('> div').data('jqSlider', jqSlider);
		jqSlider.cycle( {
			 timeout: this.timeout
			,speed: this.speed
			,pause: 1
			,random: 0
			,fx: this.fx
			,next: this.button_next
			,prev: this.button_prev
			,pager: this.button_items
			,sync: this.sync
			,onPrevNextEvent: this.onPrevNextEvent
		} );
		jqButtons.find('a').add(jqPrev).add(jqNext).click( function() {
			$(this).blur();
		} );
	}
	this.init();
}


// Product sheet tab
function Tabs(tabs_sel, pages_sel, to_strip_sel) {
	this.init = function() {
		this.tabs.data('this', this);
		this.to_strip.hide();
		this.tabs.each( function(index) {
			$(this).attr('href', 'javascript:void(0)');
			$(this).click( function() {
				$(this).data('this').setPage(index);
			} );
		} );
		if(this.tabs.size() > 0) this.setPage(0);
	}
	this.setPage = function(selected_index) {
		this.pages.each( function(index) {
			if(index == selected_index) {
				$(this).show();
			} else {
				$(this).hide();
			}
		} );
		this.tabs.each( function(index) {
			if(index == selected_index) {
				$(this).addClass('Selected');
			} else {
				$(this).removeClass('Selected');
			}
		} );
	}
	this.tabs = $(tabs_sel);
	if(this.tabs.length > 0) {
		this.pages = $(pages_sel);
		this.to_strip = $(to_strip_sel);
		this.init();
	}
}
	 	

function homeSearchFamily() {
	this.select = $('#selectHomeSearchFamily'); 
	this.slide = $('#divHomeSearchFamilyImage div.Items');
	this.select.data('slide', this.slide);
	this.onPrevNextEvent = function(index, element) {
		$('#selectHomeSearchFamily option').eq(element).attr('selected', 'selected');
	}
	// Home Search Family:
	new SlideShow( {
		 container: '#divHomeSearchFamilyImage div.Items'
		,button_prev: '#divHomeSearchFamilyImage a.SlideBt35Prev'
		,button_next: '#divHomeSearchFamilyImage a.SlideBt35Next'
		,timeout: 0 // miliseconds between transitions
		,speed: 100 // transition duration in miliseconds
		,fx: 'scrollHorz'
		,sync: false
		,onPrevNextEvent: this.onPrevNextEvent
	} );
	this.select.change( function() {
		var value = $(this).val();
		var index = $(this).find('option').index($(this).find('option[value="'+value+'"]'));
		$(this).data('slide').cycle(index);
	} );
	this.select.change();
}


