var valueSort = "";
var gSub_cat_url = "";
var gSorterby = "";
var gSortermethod = "";
var gPage = "";
var auxPage = "";
var gCat_url = "";
var prevTinyURLFilter = "tinyURLFilter";

function expandContent( show)
{
	var content = document.getElementById("contentHide");
	if (!show)
	{
		content.style.display = "none";
		var inputHigher = document.getElementById("price_higher");
		inputHigher.value = "";
	}
	else
	{
		content.style.display = "inline";
		/*content.innerHTML += "<br />";*/
	}
}



function expandBudget (optionSelected)
{
	var content = document.getElementById("contentBudget");
	
	if (optionSelected=="between")
	{
		content.style.display = "block";
	}
	else
	{
		content.style.display = "none";	
	}
}

function applyFilter(sub_cat_url, sorterby, sortermethod, page, ordering, cat_url, anchor )
{
	var url = "item-list.php?sid=" + Math.random() + "&compact=true";
	var pVars = "";
	
	url +=  "&sub_cat_url="+sub_cat_url;
	url +=  "&sort="+sorterby;
	url +=  "&type="+sortermethod;
	url +=  "&offset="+page;
	
	gCat_url = cat_url;
	gSub_cat_url = sub_cat_url;
	gSorterby = sorterby; 
	gSortermethod = sortermethod;
	auxPage = gPage;
	gPage = page;
	
	if (anchor == undefined)
	{
		anchor = "#divOrderList";
	}
	
	if (cat_url != undefined)
	{
		if (navigator.userAgent.indexOf("MSIE")>=0)
		{
			window.location.href = gSub_cat_url + anchor;
		}
		else
		{
			window.location.href = gCat_url + "/"+gSub_cat_url + anchor;
		}
	}
	
	
	filter_price = document.getElementById("filter_price");
	valueFilterPrice = filter_price.value;
	price_lowest = document.getElementById("price_lowest").value;
	price_higher = document.getElementById("price_higher").value;
	
	valueRating_lowest = document.getElementById("rating_lowest").value;
	valueRating_higher = document.getElementById("rating_higher").value;
	manufacturers = document.getElementById("manufacturers");
	valueManuf = manufacturers.options[manufacturers.selectedIndex].value;
	valueRlsdate_lowest = document.getElementById("rlsdate_lowest").value;
	valueRlsdate_higher = document.getElementById("rlsdate_higher").value;
	
	//valueRlsdate_lowest = Math.round(valueRlsdate_lowest/30.4);
	//valueRlsdate_higher = Math.round(valueRlsdate_higher/30.4);
	/*filter_date = document.getElementById("filter_date");
	valueFilterDate = filter_date.options[filter_date.selectedIndex].value;*/
	sort = document.getElementById("sort");
	valueSort = sort.options[sort.selectedIndex].value;
	/*titleSubCategory = document.getElementById("titleSubCategory");
	titleText = document.getElementById("titleSubCategory").getAttribute("text");
	titleSubCategory.innerHTML = sort.options[sort.selectedIndex].text + " " + titleText;*/
	pVars = "filter_price="+ valueFilterPrice 
	pVars += "&price_lowest="+price_lowest;
	pVars += "&price_higher="+price_higher;
	pVars += "&rating_lowest="+valueRating_lowest;
	pVars += "&rating_higher="+valueRating_higher;
	pVars += "&manufacturers="+valueManuf;
	pVars += "&rlsdate_lowest="+valueRlsdate_lowest;
	pVars += "&rlsdate_higher="+valueRlsdate_higher;
	pVars += "&sorter="+valueSort;
	pVars += "&include=" + document.getElementById("include").value;
	xmlHttp=GetXmlHttpObject(resultFilter);
	xmlHttp.open("POST", url , true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(pVars);
	
	//ajax.send("t1="+t1+"&t2="+t2)
	
	//document.getElementById('divItems').innerHTML = "<div class=\"FilterAdvice\">Filtering...</div>";
	divItemsObj =  document.getElementById("divItems");

	if ( ordering == null)
	{
		filteringDiv = document.getElementById("divFiltering");
		filteringDiv.style.display = 'block';
	}
	else
	{
		filteringDiv = document.getElementById("divOrdering");
		filteringDiv.style.display = 'block';
	}
	/*filtering.offsetLeft = 20+divItems.offsetLeft;
	filtering.offsetTop = 20+divItems.offsetTop;*/
	return true;
}

function setOrder(critery, method)
{
	cbSort =   document.getElementById('sort');
	var total = cbSort.length;
	//method = method.toLowerCase();
	//critery = critery.toLowerCase();
	
	value = critery + "-" + method;
	
	for (i=0; i< total ; i++)
	{
		if (cbSort.options[i].value == value)
		{
			cbSort.selectedIndex = i;
			break;
		}
	}
}

function resultFilter()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var datos;
		
		datos = (xmlHttp.responseText);
		filteringDiv = document.getElementById("divFiltering");
		filteringDiv.style.display = 'none';
		filteringDiv = document.getElementById("divOrdering");
		filteringDiv.style.display = 'none';
		AllResponse = datos.split("-&&&-"); 
		document.getElementById("divItems").innerHTML = AllResponse[0];
		document.title= AllResponse[1];
		document.getElementById("titleSubCategory").innerHTML = formatTitleSubcategory(AllResponse[1]);
		document.getElementById("titleSubCategory").text = AllResponse[1];
		tinyURLID =  AllResponse[3];
		document.getElementById(prevTinyURLFilter).id = "divtinyURL"  + tinyURLID;
		prevTinyURLFilter ="divtinyURL"  + tinyURLID;
		index = window.location.href.indexOf("#");
		window.location.href = document.getElementById("Base").href + AllResponse[2];
		updateOrderList( );
	}
}


function proccessURLFilter ()
{
	var val1=document.getElementById("critery").value;
	var val2=document.getElementById("search_type").value;
	var val3=document.getElementById("submit").value;
	redirect("search/"+val1+"/"+val2+"/"+ val3);
}
function updateOrderList( )
{
	var valueSort = document.getElementById("sort").value;
	var datos = valueSort.split("-");
	
	var orderPrice = document.getElementById("orderPrice");
	var orderDate = document.getElementById("orderDate");
	var orderProduct = document.getElementById("orderProduct");
	var orderRating = document.getElementById("orderRating");
	orderDate.className = "";
	orderProduct.className = "";
	orderRating.className = "";
	orderPrice.className = "";
	
	switch (datos[0])
	{
		case "price":
			if (datos[1] == "desc")
			{
				orderPrice.className = "up on";
				orderPrice.onclick = function ()
				{
					setOrder("price","asc");
					//applyFilter(gCat_url, gSub_cat_url, gSorterby, gSortermethod, gPage, true);
					applyFilter(gSub_cat_url, gSorterby, gSortermethod, gPage, true);
				}
			}
			else
			{
				orderPrice.className = "on";
				orderPrice.onclick = function ()
				{
					setOrder("price","desc");
					//applyFilter(gCat_url, gSub_cat_url, gSorterby, gSortermethod, gPage, true);
					applyFilter(gSub_cat_url, gSorterby, gSortermethod, gPage, true);
				}
			}
			
		break;
		case 'date':
			if (datos[1] == "desc")
			{ 
				orderDate.className ="up on";
				orderDate.onclick = function ()
				{
					setOrder("date","asc");
					applyFilter(gSub_cat_url, gSorterby, gSortermethod, gPage, true);
				}
			}
			else
			{
				orderDate.className = "on"
				orderDate.onclick = function ()
				{
					setOrder("date","desc");
					applyFilter(gSub_cat_url, gSorterby, gSortermethod, gPage, true);
				}
			};
		break;
		case 'rating':
			if (datos[1] == "worst")
			{
				orderRating.className = "up on";
				orderRating.onclick = function()
				{
					setOrder("rating","best");
					applyFilter(gSub_cat_url, gSorterby, gSortermethod, gPage, true);
				}
			}
			else
			{
				orderRating.className = "on";
				orderRating.onclick = function()
				{
					setOrder("rating","worst");
					applyFilter(gSub_cat_url, gSorterby, gSortermethod, gPage, true);
				}
			}
		break;
		case 'product':
			if	(datos[1] == "desc")
			{
				orderProduct.className = "up on";
				orderProduct.onclick = function()
				{
					setOrder("product","asc");
					applyFilter(gSub_cat_url, gSorterby, gSortermethod, gPage, true);
				}
			} 
			else 
			{
				orderProduct.className ="on"
				orderProduct.onclick = function()
				{
					setOrder("product","desc");
					applyFilter(gSub_cat_url, gSorterby, gSortermethod, gPage, true);	
				}
			};
		break;
	}
	
}

function tinyURLExecute ( tinyURLID )
{
	var url = "item-list.php?sid=" + Math.random() + "&compact=true";
	var pVars= "";
	pVars = "tinyURLID="+ tinyURLID + "&";
	document.getElementById("divItems").innerHTML = "Loading";
	
	
	index = window.location.href.indexOf("#");
	if (index > -1)
	{
		window.location.href = window.location.href.substr(0,index) + "#"+ prevTinyURLFilter;
	}
	else
	{
		window.location.href = window.location.href+ "#"+ prevTinyURLFilter;
	}
	
	xmlHttp=GetXmlHttpObject(tinyURLResponse);
	xmlHttp.open("POST", url , true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(pVars);
}



function formatTitleSubcategory( text)
{
	
	var textLength = text.length;
	var formatted = '';
	if (textLength <= 70)
	{
		formatted = text;
	}
	else if ( textLength <=85)
	{
		
		formatted = '<span id="point1">'+text+'</span>';
	}
	else if ( textLength <=95)
	{
		formatted = '<span id="point2">'+text+'</span>';
	}
	else if ( textLength <=116)
	{

		formatted = '<span id="point3">'+text+'</span>';
	}
	else 
	{
		formatted = '<span id="point4">'+text+'</span>';
	}
	return formatted;
}


function tinyURLResponse()
{
	var datos;
	var tinyURLID;
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
	
		datos = (xmlHttp.responseText);
		AllResponse = datos.split("-&&&-"); 
		
		document.getElementById("divItems").innerHTML = AllResponse[0];
		

		document.getElementById("titleSubCategory").innerHTML = formatTitleSubcategory(AllResponse[1]);
		document.getElementById("titleSubCategory").text = AllResponse[1];
		document.getElementById("manufacturers").value = AllResponse[2];
		document.getElementById("sort").value = AllResponse[9];
		$("price_lowest").value =  (AllResponse[3] == "default")? ctrSlider.minValue : AllResponse[3];
		$("price_higher").value =  (AllResponse[4] == "default")? ctrSlider.maxValue : AllResponse[4];
		
		
		ctrSlider.setData($("price_lowest").value, $("price_higher").value);
		
		$("rating_lowest").value = (AllResponse[5] == "default") ? ctrRating.minValue : AllResponse[5];
		$("rating_higher").value = (AllResponse[6] == "default") ? ctrRating.maxValue : AllResponse[6];
			
		ctrRating.setData( $("rating_lowest").value, $("rating_higher").value);
		
		
		$("rlsdate_lowest").value = (AllResponse[7] == "default") ? ctrDate.minValue : AllResponse[7];
		$("rlsdate_higher").value = (AllResponse[8] == "default") ? ctrDate.maxValue/2 : AllResponse[8];
			
		ctrDate.setData( $("rlsdate_lowest").value, $("rlsdate_higher").value);
		
		
		if (AllResponse[9]=="rating-best")
		{
			document.getElementById("orderPrice").className = "";
			document.getElementById("orderDate").className= "";
			document.getElementById("orderRating").className= "on";
			document.getElementById("orderProduct").className= "";
		}
		else if (AllResponse[9]=="rating-worst")
		{
			document.getElementById("orderPrice").className = "";
			document.getElementById("orderDate").className= "";
			document.getElementById("orderRating").className= "up on";
			document.getElementById("orderProduct").className= "";
		}
		else if (AllResponse[9]=="price-desc")
		{
			document.getElementById("orderPrice").className= "up on";
			document.getElementById("orderDate").className= "";
			document.getElementById("orderRating").className= "";
			document.getElementById("orderProduct").className= "";
		}
		else if (AllResponse[9]=="price-asc")
		{
				document.getElementById("orderPrice").className= "on";
				document.getElementById("orderDate").className= "";
				document.getElementById("orderRating").className= "";
				document.getElementById("orderProduct").className= "";
		}
		else if (AllResponse[9]=="date-asc")
		{
			document.getElementById("orderPrice").className= "";
			document.getElementById("orderDate").className= "on";
			document.getElementById("orderRating").className= "";
			document.getElementById("orderProduct").className= "";
		}
		else if (AllResponse[9]=="date-desc")
		{
			document.getElementById("orderPrice").className = "";
			document.getElementById("orderDate").className= "up on";
			document.getElementById("orderRating").className= "";
			document.getElementById("orderProduct").className= "";
		}
		else if (AllResponse[9]=="product-desc")
		{
			document.getElementById("orderPrice").className= "";
			document.getElementById("orderDate").className= "";
			document.getElementById("orderRating").className= "";
			document.getElementById("orderProduct").className= "up on";
		}
		else if (AllResponse[9]=="product-asc")
		{
			document.getElementById("orderPrice").className = "";
			document.getElementById("orderDate").className= "on";
			document.getElementById("orderRating").className= "";
			document.getElementById("orderProduct").className= "on";
		}
		

		document.title= AllResponse[1];
		tinyURLID = AllResponse[11].slice(AllResponse[11].indexOf("#")+1); 
		document.getElementById(prevTinyURLFilter).id = "divtinyURL"  + tinyURLID;
		prevTinyURLFilter = "divtinyURL"  + tinyURLID;
		//window.location.href =   document.getElementById("Base").href + AllResponse[10];
		
		index = window.location.href.indexOf("#");
		if (index > -1)
		{
			window.location.href = window.location.href.substr(0,index) + "#"+ prevTinyURLFilter;
		}
		else
		{
			window.location.href = window.location.href+ "#"+ prevTinyURLFilter;
		}
		
		updateOrderList( );

/*
		id="orderPrice" 
		id="orderRating"
		id="orderProduct"
		id="orderDate"
*/
		
	}
	
}

function tryTinyURLID ( subject)
{
	var indexAnchor;
	var strAnchor;
	var elements;

	indexAnchor  = subject.indexOf("#");
	if (indexAnchor> -1)
	{
		strAnchor = subject.slice(indexAnchor+1);
		index = strAnchor.indexOf("divtinyURL");
		if (index>-1)
		{
			numberAnchor = strAnchor.slice(index+10);
			
		}
		else
		{
			numberAnchor = strAnchor;
		}
	}
	numberAnchor = parseInt(numberAnchor);
	if (numberAnchor>0)
	{
		return numberAnchor;
	}
	return false;
	
}

function tinyURLLoad()
{
	var indexAnchor = 0;
	var indexTiny = 0;
	var numTinyURL = 0;
	var strAnchor = "";
	var strTinyURL = "";

	numTinyURL = tryTinyURLID(window.location.href);
	if (numTinyURL>0)
	{
		tinyURLExecute(numTinyURL);
	}
	
}

if (window.addEventListener) 
{
	window.addEventListener("load",tinyURLLoad,false);
} 
else if (window.attachEvent) 
{
	window.attachEvent("onload",tinyURLLoad);
} 
else 
{
	window.onload = function() {tinyURLLoad();}
}



