// JavaScript Document
// JPreusche - WB NewMedia - 2008-12-18 ~ 10:50
// Komprimiert mit
// http://fmarcia.info/jsmin/test.html

String.prototype.get = function(p){
	return (match = this.match(new RegExp("[?|&]?" + p + "=([^&]*)"))) ? match[1] : false;
}

function idInPathname(s){
/*
	Input:
		/1/18-36_marken_bis20/18-36-monate-starke-marken-bis-20-eur.html ||
		1/18-36_marken_bis20/18-36-monate-starke-marken-bis-20-eur.html ||
		1/18-36_marken_bis20/
	Output:
		18-36_marken_bis20
*/
	return (m = s.match(/^\/?[^\/]+\/([^\/]+)\//)) ? m[1] : false;
}




///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
window.addEvent('domready', function()
{
	// mittels Mootools wird dasScript ausgefuÌˆhrt, wenn DOM fertig (ist schneller,
	// weil sonst muÌˆssen erst alle styles und bilder etc geladen werden)
	
	// checke ob die page ID einer derjenigen entspricht, die in urls.js definiert sind
	specialId = idInPathname(window.location.pathname);	

	var tempNavTree = new Array();
	document.navigationTree = new Array();
	if (specialId!=document.startSeitenID) // nur bei nicht-Startseite 
	{
		$each(document.dropDownFinderJSON, function(value, key){
			var subObj_1 = value;
			if(document.treffer!=true) { tempNavTree[0]=key; }
			$each(subObj_1[0], function(value, key){
				var subObj_2 = value;
				if(document.treffer!=true) { tempNavTree[1]=key; }
				$each(subObj_2[0], function(value, key){
					if(idInPathname(value)==specialId){
						// Treffer -- die Struktur werden wir nachher wieder abbilden
						tempNavTree[2]=key;
						
						
						/* */					// Fuegt an die Headline der Liste den Text an -- nur bei Geschenkefinder Spielwaren
												if(document.do_letzteHierachieEbeneAnHeadline)
												{
													letzteHierachieEbeneAnHeadline(tempNavTree[2]);
												}
						/* */					// ENDE
						
						
						document.navigationTree = tempNavTree;
						document.treffer = true;
					}
				});
			});
		});	
	}

	// erstes Level aufbauen
	// durchlaufe nur die erste Ebene und hole die Keys heraus
	$each(document.dropDownFinderJSON, function(value, key){
		// key entspricht der Notation "_0_bis_18_Monate" , "_18_bis_36_Monate"etc
			if(document.navigationTree[0]==key){
				var myOption = new Element('option', {
					'value' : key,
					'html'  : key,
					'selected' : 'selected'
				});
			} else 
			{
				var myOption = new Element('option', {
					'value' : key,
					'html'  : key
				});
			}
		myOption.inject('jsonDD_level1', 'bottom'); // fuÌˆgt die Option  bei 'jsonDD_level1' ein (am ende : 'bottom' )
	});
	
	// sub-DropDowns ausklappen
	if (document.treffer)
	{
		init2ndLevel($('jsonDD_level1'));
		var opts = $$('#jsonDD_level2 option');
		for(i=0; i<opts.length; i++)
		{
			if (document.navigationTree[1]==opts[i].value)
			{	
				
				$(opts[i]).addClass('selectedOption');
				i=opts.length;

			}
		}
		
		init3rdLevel($('jsonDD_level2'));
		
		var opts = $$('#jsonDD_level3 option');
		for(i=0; i<opts.length; i++)
		{
			if (document.navigationTree[2]==opts[i].value)
			{	

				i=opts.length;
			}
		}
		$('goButton').addClass('enabledBtn');
		
	}
	levelIndicatorInit();
	document.navigationTree = new Array(null,null, null);
});

function init2ndLevel(obj)
{
	myOption = new Element('option', {
		'value' : 0,
		'html'  : document.standardHtmlersteOption,
		'selected' : 'selected',
		'class' : 'themawaehler'
	});
	if (obj.value!=0)
	{
		$('jsonDD_level2').removeProperty('disabled');
		$('jsonDD_level2').empty();
		myOption.inject('jsonDD_level2', 'bottom');
		
		$each(document.dropDownFinderJSON[obj.value][0], function(value, key){
			if(document.navigationTree[1]==key){
				var myOption = new Element('option', {
					'value' : key,
					'html'  : key,
					'selected' : 'selected'
				});
			} else 
			{
				var myOption = new Element('option', {
					'value' : key,
					'html'  : key
				});
			}
			myOption.inject('jsonDD_level2', 'bottom'); // fuÌˆgt die Option  bei 'jsonDD_level2' ein (am ende : 'bottom' )
		});
	} else
	{
		$('jsonDD_level2').setProperty('disabled', 'disabled');
		$('jsonDD_level2').empty();
		myOption.inject('jsonDD_level2', 'bottom');
	}
	init3rdLevel($('jsonDD_level2'));
}

function init3rdLevel(obj)
{
	myOption = new Element('option', {
		'value' : 0,
		'html'  : document.standardHtmlersteOption,
		'selected' : 'selected',
		'class' : 'themawaehler'
	});
	
	if (obj.value!=0)
	{
		$('jsonDD_level3').removeProperty('disabled');
		$('jsonDD_level3').empty();
		myOption.inject('jsonDD_level3', 'bottom');
		$each(document.dropDownFinderJSON[$('jsonDD_level1').value][0][obj.value][0], function(value, key){
			var myOption = new Element('option', {
				'value' : key,
				'html'  : key
			});
			if(document.navigationTree[2]==key){
				myOption.set('selected', 'selected');
				myOption.addClass('selectedOption');
			}
			myOption.inject('jsonDD_level3', 'bottom'); // fuÌˆgt die Option  bei 'jsonDD_level3' ein (am ende : 'bottom' )
		});
	
	} else
	{
		$('jsonDD_level3').setProperty('disabled', 'disabled');
		$('jsonDD_level3').empty();
		myOption.inject('jsonDD_level3', 'bottom');
	}
	evalButtonAktiv();
}

function evalButtonAktiv()
{

	if ($('jsonDD_level3').value!=0)
	{
		
		var relHref = ""+document.dropDownFinderJSON[$('jsonDD_level1').value][0][$('jsonDD_level2').value][0][$('jsonDD_level3').value];
		if(window.location.search.get('s'))
		{
			var stageParam = "?s="+window.location.search.get('s');
		} else
		{
			var stageParam = "";
		}
		
		// abschicken Button einblenden
		$('goButton').setProperty('src',document.goBtnEnabledSrc);
		$('goButton').addClass('enabledBtn');
		$('goButton').set({
				'events': {
					'mouseover': function(){ window.status = window.location.protocol+"//"+window.location.hostname+relHref+stageParam ;},
					'mouseout': function(){ window.status = ''; }
				}
			});
					

	} else
	{	
		// abschicken Button ausblenden
		$('goButton').setProperty('src', document.goBtnDisabledSrc);
		$('goButton').removeClass('enabledBtn');
		$('goButton').set({
				'events': {
					'mouseover': function(){ window.status = '' ;}
				}
			});
	
	}
	levelIndicatorInit();
	
	
}


function levelIndicatorInit()
{
	var bgflag = true;
	
	$('Schritt1').removeClass('diasabledStep');
	$('Schritt2').removeClass('diasabledStep');
	$('Schritt3').removeClass('diasabledStep');
	$('Schritt1').addClass('enabledStep');
	$('Schritt2').addClass('enabledStep');
	$('Schritt3').addClass('enabledStep');

	// falls alle werte befuÌˆllt sind, ist es die letzte Hierachieebene:
	if ($("jsonDD_level"+(document.auswahlEbenenBGs.length-1)).value != 0)
	{
		$('levelIndicator').set('style', "background:url("+ document.auswahlEbenenBGs[document.auswahlEbenenBGs.length-1]+") no-repeat;");
	}
	else
	{
		// ermittle die Tiefe und setze das entsprechende HintergrundBild
		for (i=1; i<document.auswahlEbenenBGs.length; i++)
		{
			var ii = i+1;
			if (ii>document.auswahlEbenenBGs.length){ii=document.auswahlEbenenBGs.length;}
			if ($("jsonDD_level"+i).value == 0)
			{
				if (bgflag)
				{
					 $('levelIndicator').set('style', "background:url("+ document.auswahlEbenenBGs[i-1]+") no-repeat;");
					 bgflag = false;
				}
				if($('Schritt'+ii)!=null)
				{
					$('Schritt'+ii).removeClass('enabledStep');
					$('Schritt'+ii).addClass('disabledStep');
				}
			}
		}
	}
	// check: ist die unten angezeigte Liste entsprechend der ID die im Dropdown angefordert wird?
	// indiCurrentSelection

	try
	{
		var p = ""+document.dropDownFinderJSON[$('jsonDD_level1').value][0][$('jsonDD_level2').value][0][$('jsonDD_level3').value];
		if(p == window.location.search.get('p'))
		{
			// zeige Text / Nachricht an
			$('indiCurrentSelection').set('html', document.suchergebnisBereitsDaIndicatorHTML);
		} else 
		{
			$('indiCurrentSelection').set('html',document.suchergebnisNochNichtDaIndicatorHTML);

		}
	} catch (e)
	{ // nothing here ...
	}

}

function geheZurURLCheck() {
	if( $('goButton').hasClass('enabledBtn') ) // wenn der Button aktiv ist
	{
		var relHref = ""+document.dropDownFinderJSON[$('jsonDD_level1').value][0][$('jsonDD_level2').value][0][$('jsonDD_level3').value];
		if(window.location.search.get('s'))
		{
			var stageParam = "?s="+window.location.search.get('s');
		} else
		{
			var stageParam = "";
		}
		window.location.href = window.location.protocol+"//"+window.location.hostname+relHref+stageParam;
	}

}
