/*
*
* Copyright (c) 2007 Andrew Tetlaw
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* *
*
*
* FastInit
* http://tetlaw.id.au/view/javascript/fastinit
* Andrew Tetlaw
* Version 1.4.1 (2007-03-15)
* Based on:
* http://dean.edwards.name/weblog/2006/03/faster
* http://dean.edwards.name/weblog/2006/06/again/
* Help from:
* http://www.cherny.com/webdev/26/domloaded-object-literal-updated
*
*/
/*
var FastInit = {
	onload : function() {
		if (FastInit.done) { return; }
		FastInit.done = true;
		for(var x = 0, al = FastInit.f.length; x < al; x++) {
			FastInit.f[x]();
		}
	},
	addOnLoad : function() {
		var a = arguments;
		for(var x = 0, al = a.length; x < al; x++) {
			if(typeof a[x] === 'function') {
				if (FastInit.done ) {
					a[x]();
				} else {
					FastInit.f.push(a[x]);
				}
			}
		}
	},
	listen : function() {
		if (/WebKit|khtml/i.test(navigator.userAgent)) {
			FastInit.timer = setInterval(function() {
				if (/loaded|complete/.test(document.readyState)) {
					clearInterval(FastInit.timer);
					delete FastInit.timer;
					FastInit.onload();
				}}, 10);
		} else if (document.addEventListener) {
			document.addEventListener('DOMContentLoaded', FastInit.onload, false);
		} else if(!FastInit.iew32) {
			if(window.addEventListener) {
				window.addEventListener('load', FastInit.onload, false);
			} else if (window.attachEvent) {
				return window.attachEvent('onload', FastInit.onload);
			}
		}
	},
	f:[],done:false,timer:null,iew32:false
};
*/
/*@cc_on @*/
/*@if (@_win32)
FastInit.iew32 = true;
document.write('<script id="__ie_onload" defer src="' + ((location.protocol == 'https:') ? '//0' : 'javascript:void(0)') + '"><\/script>');
document.getElementById('__ie_onload').onreadystatechange = function(){if (this.readyState == 'complete') { FastInit.onload(); }};
/*@end @*/
//FastInit.listen();
/**
 * END OF FASTINIT
 */



function initAutocompleter() {
	if($('kante') && $('kante_autocomp')) {
		new Ajax.Autocompleter("kante", "kante_autocomp", "index.php?eID=tx_wmdbdoekantenfinder_eID", {
		  paramName: "value",
		  minChars: 2,
		  afterUpdateElement : setSelectionId,
		  parameters:'&cmd=autocomplete&lang=' + language + '&pid=' + pidForKante
		});
	}
}

function setSelectionId(input, li) {
	input.value = li.innerHTML;
	if(!$('id_kante')) {
		input.insert({'after' : '<input type="hidden" id="id_kante" value="' + li.id + '"/>'});
	} else {
		$('id_kante').value = li.id;
	}
	renderResults('kante');
}

function toggleStep(mode,step) {
	/**
	 * Hide all
	 */
	if( $('glue-row') ) {
		$('glue-row').hide();
	}

	$('step2').hide();
	$$('.toggleStep2').invoke('hide');
	$('step'+step).show();
	$$('.toggleStep'+step).invoke('show');
	if(mode == 'glue') {
		if( $('glue-row') ) {
			$('glue-row').show();
		}
		resMode = 'platten';
		getPlatten();
	}
	if(mode == 'hersteller') {
		//$('kante').options[0].selected = true;
		if( $('glue-row') ) {
			$('glue-row').show();
		}
		resMode = 'platten';
		getPlatten();
	}
	if(mode == 'dekore') {
		$('step2').show();
		$$('.toggleStep2').invoke('show');
	}
}

function getPlatten() {
	var glue = '';
	$('resultsset').innerHTML = '';
	if( $('glueFilter') && $('glueFilter').value != '' ) {
		glue = '&glue=' + $('glueFilter').value;
	}
	new Ajax.Request(
		'index.php?eID=tx_wmdbdoekantenfinder_eID',
		{
			method:'post',
			parameters:'&cmd=platten&params='+$F('hersteller')+'&mode='+resMode+'&store='+store+'&lang='+language+'&boardLang='+boardLang+'&displayColorFirst='+displayColorFirst+glue,
			onComplete:renderDropdown
		}
	);
}

function renderDropdown(retVal) {
	if( $('kante') ) {
		$('kante').value = '';
	}
	$('plattendropdown').innerHTML = retVal.responseText;
	
		// Change 'Download PDF'-Link
	var tempParam = $('pdf_download_link').href;
	var splitValues = tempParam.split(/man=/);
	$('pdf_download_link').href = splitValues[0] + "man=" + $F('hersteller');
}

function renderResults(mode) {
	var glue = '';
	if( $('kante').value == '' && $('platten').value == '' ) {
		return;
	}
	if($('hidden_detailpage')) {
		var detailpage = $('hidden_detailpage').value;
	}
	var params = '';
	$('resultsset').innerHTML = '<img src="/typo3conf/ext/wmdb_doe_kantenfinder/res/ajax-loader.gif" alt="Loading" />';
	if(mode == 'kante') {
		$('step2').hide();
		$$('.toggleStep2').invoke('hide');
		if( $('glue-row') ) {
			$('glue-row').hide();
		}
		resMode = 'kante'
	}

	if( $('glueFilter') && $('glueFilter').value != '' && resMode == 'platten' ) {
		glue = '&glue=' + $('glueFilter').value;
	}

	if(resMode == 'platten') {
		dataVal = $F('platten');
	} else {
		if( $('id_kante') ) {
			dataVal = $('id_kante').value;
			var idSplit = dataVal.split('_');
			dataVal = idSplit[1];
		} else {
			dataVal = $('kante').value;
		}
		$('hersteller').options[0].selected = true;
	}
	params = '&cmd=results&params='+dataVal+'&mode='+resMode+'&store='+store+'&lang='+language;
	params = params + '&displaymode='+displaymode+'&L='+L+'&hersteller='+$F('hersteller') + '&detailpage=' +detailpage;
	params = params + '&classification='+classification + glue;
	new Ajax.Request(
		'index.php?eID=tx_wmdbdoekantenfinder_eID',
		{
			method:'post',
			parameters:params,
			onComplete:processResults
		}
	);
}

function processResults(retVal) {
	$('resultsset').innerHTML = retVal.responseText;
}

Event.observe(window, 'load', function() {
	initAutocompleter();
});
