// JavaScript Document
var txt;
var selectedCat;
txt="";
var refSet;
var CountryCode = "0";
var refSite = document.referrer;
if(refSite){
	if(refSite!="") {
		refSet = "&refSet=1";
	}
	else {
		refSet = "&refSet=0";
	}
}
else {
	refSet = "&refSet=0";
}
var searchHistory = new Array();

function setCountry(code) {
	CountryCode = code;	
}

function getCats(parent_cat) {
	var xmlHttp;
	txt="";
	var browser;
	selectedCat=parent_cat;
	document.getElementById("menu").innerHTML = "<div><img src='spinner.gif' alt='spinner'></img></div>";
	$(".menu").corner();
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
		browser=0;
	}
	catch (e) {
  		// Internet Explorer
  		try {
  		  	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			browser=1;
  		}
  		catch(e) {
    		try {
    			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				browser=2;
    		}
    		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
    		}
  		}
	}
	xmlHttp.onreadystatechange=function() 
	{
		if(xmlHttp.readyState==4) 
		{
    		x=xmlHttp.responseXML.documentElement.getElementsByTagName("category");
        	for (i=0;i<x.length;i++) {
			x1=x[i].getElementsByTagName("cat_id");
            	x2=x[i].getElementsByTagName("cat_name");
				x3=x[i].getElementsByTagName("cat_parent_id");
				txt = txt + "\n<div class='menuItem' onmouseover='this.style.backgroundColor=\"#FEFEFF\"' onmouseout='this.style.backgroundColor=\"#DEDEFF\"' onmousedown='this.style.backgroundColor=\"#FFFF00\"' onmouseup='this.style.backgroundColor=\"#DEDEFF\"' onclick='getParentCat(this.id); getCats(this.id);' id='" + x1[0].firstChild.nodeValue + "'>" + x2[0].firstChild.nodeValue + "</div>\n";
			}
	       	document.getElementById("menu").innerHTML = txt;
			$(".menu").corner();
     	}
	}	
	xmlHttp.open("GET","category.php?parent_cat="+parent_cat+"&site_id="+CountryCode,true);
	xmlHttp.send(null);
}

function getParentCat(cat) {
	var xmlHttp;
	var browser;
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
		browser=0;
	}
	catch (e) {
  		// Internet Explorer
  		try {
  		  	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			browser=1;
  		}
  		catch(e) {
    		try {
    			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				browser=2;
    		}
    		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
    		}
  		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			txt = "";
			y=xmlHttp.responseXML.documentElement.getElementsByTagName("category");
        	for (i=0;i<y.length;i++) {
        		y1=y[i].getElementsByTagName("cat_parent_id");
				y2=y[i].getElementsByTagName("cat_name_path");
				txt = txt + "\n<div><a onclick='getParentCat(this.id); getCats(this.id);' id='" + y1[0].firstChild.nodeValue + "'><img src='up-directory.gif' alt='up arrow' height='24px' width='24px' /></a></div>\n";
			}
			//document.getElementById("menu").innerHTML = txt;
			if(cat!="-1") {
					var temp = y2[0].firstChild.nodeValue;
					temp = temp.replace(/:/g, " --> ");
					document.getElementById("currentCat").innerHTML = "<span style='font-family: courier; font-size: .90em;'><b>Current Category:</b> " + temp + "&nbsp;<img class='searchButton' onclick='getItems()' src='search.gif' alt='search' height='24px' width='24px' /></span>";
			}
			
			else {
					document.getElementById("currentCat").innerHTML = "<span style='font-family: courier; font-size: .90em;'><b>Current Category:</b> No Category Selected</span>";
			}
			
			$(".menu").corner();
     	}
	}	
	xmlHttp.open("GET","category.php?cat_id="+cat+"&site_id="+CountryCode,true);
	xmlHttp.send(null);
}

	txt = "";
function getItems() {
	document.getElementById("contentBody").innerHTML = "<img src='spinner.gif' alt='spinner' />";
	var category = "";
	var searchWords = "";
	var itemResults;
	var country;
	country = "&siteid=" + CountryCode;
	if(selectedCat=='-1') {
		category = "";
	}
	else {
		category = "catID="+selectedCat;
	}
	if(document.getElementById("searchWords").value=="" || document.getElementById("searchWords").value=="Search terms...") {
		searchWords = "";
	}
	else {
		// Add words to search array for back button code.
		searchHistory.push(document.getElementById("searchWords").value);
		searchWords = "&keyword=" + escape(document.getElementById("searchWords").value);
	}
	if(searchHistory) {
		document.getElementById("backButton").innerHTML = "<button onclick='goBack(this);' class='goBack'>Back</button>";
	}
	else {
		document.getElementById("backButton").innerHTML = "";
	}
	var xmlHttp = null;
	var browser;
	var contentText="";
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
		browser=0;
	}
	catch (e) {
  		// Internet Explorer
  		try {
  		  	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			browser=1;
  		}
  		catch(e) {
    		try {
    			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				browser=2;
    		}
    		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
    		}
  		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			
    	  	itemResults = "<table class='itemTable'>\n";
			//itemResults += "<tr><td align='center' colspan='5'><a href='http://ebay.com'><img src='rightnow.gif' alt='Right now on eBay' /></a>\n";
			itemResults += "<thead><th><a href='http://ebay.com'><img src='rightnow.gif' alt='Right Now on eBay' /></a></th><th>Title</th><th>Bid Count</th><th>Watch Count</th><th>Current Price</th></thead>\n";
			z=xmlHttp.responseXML.documentElement.getElementsByTagName("item");
			
			var imgURL;
			var itemURL;
			var title;
			var bidCount;
			var watchCount;
			var price;
			for(count=0;count<z.length;count++) {
				imgURL		= z[count].getElementsByTagName("galleryURL");
				if(null===imgURL[0].firstChild) {
					imgURL		= "noImage.gif";
				}
				else {
					imgURL		= imgURL[0].firstChild.nodeValue;
				}
				itemURL		= z[count].getElementsByTagName("url");
				itemURL		= itemURL[0].firstChild.nodeValue;
				title		= z[count].getElementsByTagName("title");
				title		= title[0].firstChild.nodeValue;
				bidCount	= z[count].getElementsByTagName("bidCount");
				bidCount 	= bidCount[0].firstChild.nodeValue;
				watchCount	= z[count].getElementsByTagName("watchCount");
				watchCount 	= watchCount[0].firstChild.nodeValue;
				price		= z[count].getElementsByTagName("price");
				price		= price[0].firstChild.nodeValue;
				itemResults += "<tr class='itemRow'>";
				itemResults += "<td class='imgURL'><a target='_new' href='" + itemURL + "' rel='nofollow' ><img src='" + imgURL + "' alt='item image' /></a></td>\n";
				itemResults += "<td class='itemTitle'><a target='_new' href='" + itemURL + "' rel='nofollow' >" + title + "</a></td>\n";
				itemResults += "<td class='bidCount'>" + bidCount + "</td>\n";
				itemResults += "<td class='watchCount'>" + watchCount + "</td>\n";
				itemResults += "<td class='price'>" + price + "</td>\n";
				itemResults += "</tr>\n";
			}
			itemResults += "</table>\n";
			document.getElementById("contentBody").innerHTML = itemResults;
		  
		}
		
	}

	if(searchWords=="" && category=="") {
		document.getElementById("contentBody").innerHTML = "You must provide at least one of the following: Search String or Category.";
	}
	else {
		
		xmlHttp.open("GET","items.php?"+category+searchWords+country+refSet+"&refSite="+refSite , true);
		xmlHttp.send(null);	
	}
}

function goBack(a) {
	var oldSearchWords = searchHistory[searchHistory.length -2];
	searchHistory.pop();
	document.getElementById("searchWords").value = oldSearchWords;
	getItems(a);
}

$(function() {
	$(".menu").corner();
	$("div.corner").corner();
	$("#searchForm").submit(function(event){ event.preventDefault(); });
});

