var StateIgnModels = true;
var StateVisibleSettingsNormal = false;
var stateGetLivesearchText = true;

var strTitle = "";
var currentPage = 1;

var strLivesearchText = "";
var strArticlenumber = "";
var strArticlenumberSearchmode ="";
var strArticlename = "";
var strArticlenameSearchmode ="";
var strPricefrom = "";
var strPriceto = "";
var strManufacturers = "";
var strTradegroups = "";
var strModels = "";
var strOrder = "";
var strEquipment = "N";
var strColor = "";
var strMaterial = "";
var strDesignCode = "";

var StateGetManufacturerParameter = true;
var StateGetTradegroupParameter = true;
var StateGetModelParameter = true;
$(document).ready(function() 
{
	initForm();
	
	var click_dropdown;
	var click_textfield;
	//EVENTS
	//$("#searchValue").focus()
	
	//ARTIKELSUCHE AUSFÜHREN
	$("#showSearchResults").click
	(
		function()
		{
			showSearchResults(false);
		}
	);
	$("#searchButton").click
	(
		function()
		{
			showSearchResults(false);
		}
	);
	
	
	//BEIM KLICK IRGENDWOHIN LIVESEARCH-LISTE VERSTECKEN
	$("#body").click
	(
		function()
		{
			if(!click_dropdown) click_dropdown = false;
			if(!click_textfield) click_textfield = false;
			if(click_textfield == false && click_dropdown == false)
			{
				$("#searchDropdown").hide();
			}
			click_dropdown = false;
			click_textfield =  false;
		}
	);
	
	//KLICK AUF WARENGRUPPEN-COMBOBOX
	$("#searchCombobox").click
	(
		function()
		{	
			click_dropdown = true;
			showLivesearchList();
		}
	)
	
	//KLICK AUF TEXTFIELD-COMBOBOX
	$("#searchDropdown").click
	(
		function()
		{	
			click_textfield = true;
		}
	)
	
	//KLICK AUF TEXTFIELD
	$("#searchValue").click
	(
		function()
		{	
			click_textfield = true;
			var value =  $(this).val();
			//WENN WERT IN TEXTBOX STEHT, DROPDOWN AUFKLAPPEN
			if(value !="")
			{
				$("#searchDropdown").show();
			}
		}
	)
	
	$("#searchValue").keyup
	(
		function(event)
		{
			showLivesearchList();
		}
	);
	//EVENTS ENDE
});

//FUNCTIONS
function IgnoreModels()
{
	$("#articleSearchModels").hide();
	$("#articleSearchModelsExpand").hide();
	$("#articleSearchModelsIgnoreCheckbox").attr("checked","true")
	StateIgnModels = true;
}

function NotIgnoreModels()
{
	$("#articleSearchModels").show();
	$("#articleSearchModelsExpand").show();
	$("#articleSearchModelsIgnoreCheckbox").attr("checked","")
	StateIgnModels = false;
}
function ToggleIgnoreModels()
{
	//HERSTELLER BEI SUCHE IGNORIEREN
	if(StateIgnModels == false)
	{
		IgnoreModels();
	}else
	{
		NotIgnoreModels();
	}
}

function initForm()
{	
	if(StateIgnModels == true) IgnoreModels();
	else NotIgnoreModels();
}

function renewSearchSettings(manufacturers,tradegroups,models,pricefrom,priceto,lowestprice,highestprice,articlename,articlenumber,equipment,materialgroups)
{		
	manufacturers = JSON.stringify(manufacturers);
	tradegroups = JSON.stringify(tradegroups);
	models = JSON.stringify(models);
	materialgroups = JSON.stringify(materialgroups);
	
	$.post("admin/frontend/handleSearch.php",{
		type:'setSettings2',
		manufacturers: manufacturers,
		materialgroups: materialgroups,
		tradegroups: tradegroups,
		models: models,
		pricefrom: pricefrom,
		priceto: priceto,
		equipment: equipment,
		lowestprice: lowestprice,
		highestprice: highestprice,
		color: strColor,
		material: strMaterial
	}, 
	function(data)
	{
		$('#showSettings').show();
		$('#settings').html(data);
	});
}

function showSearchResults(stateGetParameters)
{	
	if(stateGetLivesearchText == true) strLivesearchText = $("#searchValue").val();
	$('#ContentBlock').show();
	$('#ContentBlock').html('Suchvorgang...<img width="540" height="15" src="_img/loadingAnimation.gif">');
	$.post("admin/frontend/handleSearch.php",{
		renew:true, 
		livesearchText: strLivesearchText, 
		pricefrom: strPricefrom,
		priceto: strPriceto,
		articlenumber: strArticlenumber,
		articlenumberSearchmode: strArticlenumberSearchmode,
		articlename: strArticlename,
		articlenameSearchmode: strArticlenameSearchmode,
		manufacturers: strManufacturers,
		tradegroups: strTradegroups,
		material: strMaterial,
		color: strColor,
		models: strModels,
		ordervalue: strOrder,
		page: currentPage, 
		equipment: strEquipment,
		equipment: strEquipment,
		designCode : strDesignCode,
		type: "searchResults"
		}, 
		function(data)
		{			
			$("#ContentBlock").html(data);
			$("#searchDropdown").hide();
			initForm();
			$("#searchOrder").val(strOrder);
			if(document.getElementsByName('contents')[0])window.scrollTo(0,document.getElementsByName('contents')[0].offsetTop); 
		});
}

function showResultPage(page)
{
	currentPage = page;
	showSearchResults();
}

function setPriceFrom(price)
{
	strPricefrom = price;
}

function setPriceTo(price)
{
	strPriceto = price;
}

function setOrder(order)
{
	strOrder = order;
}

function setDesignCode(designCode){
	strDesignCode = designCode;
}

function setPage(page)
{
	currentPage = page;
}

function setManufacturer(manufacturer)
{
	currentPage = 1;
	strManufacturers = manufacturer;
	StateGetManufacturerParameter = false;
}

function setTradegroup(tradegroup)
{
	currentPage = 1;
	strTradegroups = tradegroup;
	StateGetTradegroupParameter = false;
}

function setMaterial(material)
{
	currentPage = 1;
	strMaterial = material;
}

function setColor(color)
{
	currentPage = 1;
	strColor = color;
}

function setModel(model)
{
	currentPage = 1;
	strModels = model;
	StateGetModelParameter = false;
}

function setEquipment(equipment)
{
	currentPage = 1;
	strEquipment = equipment;
}
function setLivesearchtext(String)
{
	currentPage = 1;
	stateGetLivesearchText = false;
	strLivesearchText = String;
	StateGetModelParameter = false;
}

function showLivesearchList()
{
	$.get("admin/frontend/handleSearch.php",{
	livesearchText: $("#searchValue").val(), 
	type: "smallSearch"}, 
	function(data)
	{
		$("#searchDropdown").show();
		$("#searchDropdown").html(data);
	});
}


//FUNCTIONS ENDE
