var lastParam1=0;
var lastParam2=0;

$(document).ready(function(){
	getCatFinder($('#actCategory').val());
});

function getCatFinder(catAlias){
	if(document.getElementById('FinderFrame')){
		$('#FinderFrame').hide();
		$('#finderResults').hide();
		buildCatFinder(catAlias);
	}
}

function buildCatFinder(catAlias){
	$.post('/modules/mod_products/lib/finder/finder-response.php',{type:'getSettings',catAlias:catAlias},function(data){buildFinder_partTwo(data);},'json');
}

function getFinder(typesAlias){
	if(document.getElementById('FinderFrame')){
		$('#FinderFrame').hide();
		$('#finderResults').hide();
		buildFinder(typesAlias);
	}
}

function buildFinder(typesAlias){
	$.post('/modules/mod_products/lib/finder/finder-response.php',{type:'getSettings',typesAlias:typesAlias},function(data){buildFinder_partTwo(data);},'json');
}

function removeFinder(){
	$('#FinderFrame').hide();
	$('#finderResults').hide();
	document.getElementById('FinderFrame').innerHTML='';
	document.getElementById('finderResults').innerHTML='';
}

function buildFinder_partTwo(data){
	if(data.error==''){
		var finderHtml='' +
					'<h4>'+data.finder_name+'<p style="float: right; margin-right:10px; font-size: 12px;" ><a href="javascript: removeFinder()">schließen</a></p></h4>' +
					'<div id="finderControls">' +
					'	<div id="param1Select">' +
					'		<span class="paramName">'+data.param1_text+'</span>' +
					'		<select id="parameter1select" onclick="if(this.selectedIndex!=0 && this.selectedIndex!=lastParam1){getParams2_init(this.options[this.selectedIndex].value); lastParam1=this.selectedIndex;}">'+data.param1_html+'</select>' +
					'	</div>' +
					'	<div id="param2Select">' +
					'		<span class="paramName">'+data.param2_text+'</span>' +
					'   <div id="parameter2select_wrapper" style="display: inline;">' +
							
					'		</div>' +
					'	</div>' +
					'</div>' +
		'';
		document.getElementById('FinderFrame').innerHTML=finderHtml;
		$('#FinderFrame').show('fast');
	}
}

function getParams2_init(data){
	if(data.substring(0,1)=='c'){
		getParams2ByCat(data.substring(2));
	}else{
		getParams2(data);
	}
}

function getParams2(typesKey){
	$('#param2Select').hide();
	$('#finderResults').hide();
	var param1=document.getElementById('parameter1select').options[document.getElementById('parameter1select').selectedIndex].text;
	$.post('/modules/mod_products/lib/finder/finder-response.php',{type:'getParams2',param1:param1,typesKey:typesKey},function(data){getParams2_callback(data);},'json');
}

function getParams2ByCat(catKey){
	$('#param2Select').hide();
	$('#finderResults').hide();
	var param1=document.getElementById('parameter1select').options[document.getElementById('parameter1select').selectedIndex].text;
	$.post('/modules/mod_products/lib/finder/finder-response.php',{type:'getParams2',param1:param1,catKey:catKey},function(data){getParams2_callback(data);},'json');
}

function getParams2_callback(data){
	if(data.error==""){
		document.getElementById('parameter2select_wrapper').innerHTML=data.param2_html;
		$('#param2Select').show('fast');
	}
	if(data.msg){
		getResults_callback(data);
	}
}


function getResults_init(data){
	if(data.substring(0,1)=='c'){
		getResultsByCat(data.substring(2));
	}else{
		getResults(data);
	}
}

function getResults(typesKey){
	if(document.getElementById('parameter1select').selectedIndex>0 && document.getElementById('parameter2select').selectedIndex>0){
		var param1=document.getElementById('parameter1select').options[document.getElementById('parameter1select').selectedIndex].text;
		var param2=document.getElementById('parameter2select').options[document.getElementById('parameter2select').selectedIndex].text;
		$.post('/modules/mod_products/lib/finder/finder-response.php',{type:'getArticles',param1:param1,param2:param2,typesKey:typesKey},function(data){getResults_callback(data);},'json');
	}
}

function getResultsByCat(catKey){
	if(document.getElementById('parameter1select').selectedIndex>0 && document.getElementById('parameter2select').selectedIndex>0){
		var param1=document.getElementById('parameter1select').options[document.getElementById('parameter1select').selectedIndex].text;
		var param2=document.getElementById('parameter2select').options[document.getElementById('parameter2select').selectedIndex].text;
		$.post('/modules/mod_products/lib/finder/finder-response.php',{type:'getArticles',param1:param1,param2:param2,catKey:catKey},function(data){getResults_callback(data);},'json');
	}
}

function getResults_callback(data){
	if(data.error==""){
		
		var html="";
		var cat=document.getElementById('param1').value;
		var mod=document.getElementById('module').value;
		var typ=document.getElementById('selectedType').value;
		
		for(var i=0;i<data.msg.length;i++)
		{
			typ=data.msg[i].typeAlias;
			var art=data.msg[i].alias;
			html+='<div class=\"box product_main_box\">';
			html+='<div class=\"product_main_head\">';
			html+='<h4><a href=\"/'+art+'/'+cat+'/'+typ+'/'+mod+'\">'+data.msg[i].name+'</a></h4><p class=\"articlenumber\">';
			html+='</div>';
	
			html+='<div class=\"product_main_content\">';
			html+='<div class=\"product_main_thn\"><a href=\"/'+art+'/'+cat+'/'+typ+'/'+mod+'\"><img src="/'+data.msg[i].pic.path+'thn_'+data.msg[i].pic.filename+'" alt=""/></a></div>';
			html+='<div class="\product_main_details\">';
			html+= data.msg[i].sub_headline;
			html+='<ul class=\"types\">';
			
			for(var x=0;x<data.msg[i].attributes.length;x++){
			
				html+='<li><strong>'+data.msg[i].attributes[x].name+':</strong> '+data.msg[i].attributes[x].attributes_value+'</li>';		
			}
	
			html+='</ul>';
			
			html+='</div>';
			html+='</div>';
			/*
	    var absTax=Math.floor(data.msg[i].tax);
	   	var restTax=(data.msg[i].tax-absTax)*100;
	    if(restTax<100)
	    {
	     		var tax=String((Math.round(restTax)/100)+absTax).replace(".",",");
			}else{
			 		var tax=String(absTax+1)+",00";
	    }
	    */
		  html+='<div class=\"product_main_price\"><h5>'+data.msg[i].price.replace(".",",")+' EUR</h5><p class=\"tax\">inkl. gesetzlicher USt.</p>';
		  html+='<form action=\"/products/'+cat+'/\" method=\"post\">';
		  html+='<input type=\"hidden\" id=\"product_id\" name=\"articles_key\" value=\"'+data.msg[i].price+'\" />';
			html+='<input type=\"hidden\" name=\"article_type\" value=\"'+data.msg[i].price+'\" />';
			html+='<input type=\"hidden\" id=\"myOriPrice\" value=\"'+data.msg[i].price+'\" />';
			html+='<input type=\"hidden\" id=\"myOriUnitText\" value=\"Preis pro {$value.reference_unit}\" />';
		  html+='<input type=\"text\" class=\"amount\" name=\"amount\" value=\"1\" />&nbsp;&nbsp;<input type=\"submit\" id=\"cart\" name=\"cart\" value=\"In den Warenkorb\" /></form>';
		  html+='</div>';
			html+='<p class=\"clear\"></p>';
			html+='</div>';
		}
		document.getElementById('finderResults').innerHTML=html;
		$('#finderResults').show('fast');
		if(document.getElementById('articles')){
			document.getElementById('articles').innerHTML='';
		}
	}
}

