
var productFinderSliders = [];


function initProductFinderSlider(slider_id, input_id, start, end, step, init) {
	
	var newSlider = new Slider(slider_id, input_id);
	
	// cap init
	init = Math.max(Math.min(end, init), start);
	
	newSlider.setBlockIncrement(step);
	newSlider.setUnitIncrement(step);
	newSlider.setMaximum(end);
	newSlider.setValue(init);
	newSlider.setMinimum(start);
	
	productFinderSliders.push(newSlider);
	
}




// rotating content container...requires prototype & scriptaculous
var protoaculous_included = (typeof Prototype != 'undefined' && typeof Scriptaculous != 'undefined');
if (protoaculous_included) {

	function productFinderInit() {
		var contentDiv = $('contentCentre');
		if (!contentDiv) contentDiv = $('contentFull');
		if (!contentDiv) return;
		
		var productFinderElement = contentDiv.select('div.productFinder');
		if (productFinderElement) productFinderElement.each(function(pf) {
			new productFinder(pf);
		});
	}

	productFinder = Class.create({
		initialize: function(ele) {
			this.ele = ele;
			this.activeItem = 1;
			
			// register click event on handles
			var t = this;
			if (this.ele.down('.finderTabHandles')) {
				var tabs = ele.select('.finderTabHandle div');
				if (tabs) tabs.each(function(tab) {
					tab.setStyle({cursor: 'pointer'});
					Event.observe(tab, 'click', t.tabClick.bindAsEventListener(t));
				});
			}
			
			this.items = ele.select('.finderContentItem');
			this.itemCount = this.items.length;
			for (var i = 0; i < this.items.length; ++i) {
				// register send event on button
				Event.observe(this.items[i].down('button.finderItemSubmit'), 'click', this.sendAjaxRequest.bindAsEventListener(this));
				// hide all tabs after the first one
				if (i > 0) this.items[i].setStyle({display: 'none', position: 'static', top: '0px'});
			}
		},
		
		tabClick: function(evt) {
			if (!evt) return;
			
			// which number has been clicked?
			var clickedHandle = Event.element(evt).up('.finderTabHandle');
			var handleNum;
			if (clickedHandle.className) {
				clickedHandle.className.split(" ").each(function(v){
					if (v.match(/^tabHandleNum_\d+$/))
						handleNum = parseInt(v.gsub(/^tabHandleNum_(\d+)/, '#{1}'));
				})
			}
			if (handleNum == this.activeItem) return;	// same item clicked
			
			this.changeItems(handleNum);
		},
		
		changeItems: function(newItem) {
			if (!newItem) return;
		
			if (this.ele.down('.tabHandleNum_' + this.activeItem)) {
				this.ele.down('.tabHandleNum_' + this.activeItem).removeClassName('handleActive');
				this.ele.down('.tabHandleNum_' + this.activeItem).down('div').removeClassName('handleContentActive');
			}
			
        	var activeItem = this.activeItem;
        	if (this.items) this.items.each(function(item) {
        		var classNames = item.className.split(' ');
        		for (var i = 0; i < classNames.length; ++i) {
        			if (classNames[i].match(new RegExp('contentItemNum_' + newItem + '$')))
        				item.show();
	        			//new Effect.Appear(item, {duration: 0.7, queue: { position: 'end', scope: 'item' + newItem}});
	        		if (classNames[i].match(new RegExp('contentItemNum_' + activeItem + '$')))
	        			item.hide();
	        			//new Effect.Fade(item, {duration: 0.7, queue: { position: 'end', scope: 'item' + activeItem}});
        		}
        	});
	        this.activeItem = newItem;
	        
	        if (this.ele.down('.tabHandleNum_' + this.activeItem))
	        	this.ele.down('.tabHandleNum_' + this.activeItem).addClassName('handleActive');
	        	this.ele.down('.tabHandleNum_' + this.activeItem).down('div').addClassName('handleContentActive');
		},
		
		sendAjaxRequest: function(evt) {
			// get parameters from tab
			var activeTab = Event.element(evt).up('.finderContentItem');
			
			var tabParam = activeTab.down('input.finderItemTabParam').readAttribute('value');
			var fields = activeTab.select('div.finderItem');
			var fieldParams = '';
			for (var i = 0; i < fields.length; ++i) {
				var fieldParamName = fields[i].down('input.finderItemParamName').readAttribute('value');
				var fieldParamValue = fields[i].down('div.finderItemSlider input').readAttribute('value');
				fieldParams += '&' + fieldParamName + '=' + fieldParamValue;
			}
			
			var ajaxUrl = window.location.href + '&ajaxReq&' + tabParam + fieldParams;
			
			//alert(ajaxUrl);
			$('productFinderResultWrapper').show();
			$('productFinderResult').update('');
			$('productFinderAjaxWaiting').show();
			
			new Ajax.Request(ajaxUrl, { 
				method: 'get',
				requestHeaders:  ['Pragma', 'no-cache', 'Cache-Control', 'must-revalidate','If-Modified-Since', document.lastModified],
				onSuccess: function(transport) {
					var resultHTML = transport.responseText;
					$('productFinderResult').update(resultHTML);
					$('productFinderAjaxWaiting').hide();
				}
			});
		}
	});
	

	Event.observe(window, 'load', productFinderInit);
}


	
