var page, category, categoryname;
var linkMap = new Array();
var cardsperpage = 25;

var displaycards = function(json) {
		// Transform the JSON results into an unordered list of links.
		
		var data = document.getElementById("data");
		
		var startingrecord = (page-1)*cardsperpage;
		var endingrecord = Math.min(json.length, startingrecord + cardsperpage);
		var numpages = Math.ceil(json.length / cardsperpage);
		for (var i = startingrecord; i < endingrecord; ++i) {
			var entry = json[i];
			data.appendChild(displaycard(i, entry));
		}
		
		var li = document.createElement("LI");
		li.className = "pageLinks tc";
/*		for ( var i = 1; i <= numpages; i++ ) {
			if ( page == i ) {
			  var b = document.createElement("B");
			  b.innerHTML = "&nbsp;" + i + "&nbsp;";
			  li.appendChild(b);
			} else {
			  var a = document.createElement("A");
			  linkMap["a-page-" + i] = "lookupcards(" + i + ",'" + category + "','" + escape(categoryname) + "')";
			  a.setAttribute("id", "a-page-" + i);
			  a.setAttribute("href", "javascript:void(0)");
			  a.onclick = function() { eval(linkMap[this.id]); return false; };
			  a.innerHTML=i;
			  li.appendChild(a);
			}
		}*/
		
		if ( page > 1 ) {
			var firstPage = document.createElement("A");
			linkMap["a-first"] = _getPageLinkAsString(1);
			firstPage.setAttribute("id", "a-first");
			firstPage.setAttribute("href", "javascript:void(0)");
			firstPage.onclick = function() { eval(linkMap[this.id]); return false; };
			firstPage.innerHTML="First Page";
			li.appendChild(firstPage);
			li.appendChild(document.createTextNode("|"));
			data.appendChild(li);

			var a = document.createElement("A");
			linkMap["a-prev"] = _getPageLinkAsString(page-1);
			a.setAttribute("id", "a-prev");
			a.setAttribute("href", "javascript:void(0)");
			a.onclick = function() { eval(linkMap[this.id]); return false; };
			a.innerHTML="Prev Page";
			li.appendChild(a);
			
			var text = document.createTextNode("|");
			li.appendChild(text);
		}
		
		if ( page < numpages ) {
			var a = document.createElement("A");
			linkMap["a-next"] = _getPageLinkAsString(page+1);
			a.setAttribute("id", "a-next");
			a.setAttribute("href", "javascript:void(0)");
			a.onclick = function() { eval(linkMap[this.id]); return false; };
			a.innerHTML="Next Page";
			li.appendChild(a);
			
			var text = document.createTextNode("|");
			li.appendChild(text);

			var lastPage = document.createElement("A");
			linkMap["a-last"] = _getPageLinkAsString(numpages);
			lastPage.setAttribute("id", "a-last");
			lastPage.setAttribute("href", "javascript:void(0)");
			lastPage.onclick = function() { eval(linkMap[this.id]); return false; };
			lastPage.innerHTML="Last Page";
			li.appendChild(lastPage);		
			li.appendChild(document.createTextNode("|"));
			data.appendChild(li);
		}
		
		var categoryheader = document.getElementById("categoryheader");
		categoryheader.innerHTML = "";
		var categoryheadertext = document.createTextNode(categoryname + " ~ Page " + page + " of " + numpages + " ");
		if ( page > 1 ) {
			var prevPageTop = document.createElement("A");
			linkMap["a-prev-top"] = _getPageLinkAsString(page-1);
			prevPageTop.setAttribute("id", "a-prev-top");
			prevPageTop.setAttribute("href", "javascript:void(0)");
			prevPageTop.onclick = function() { eval(linkMap[this.id]); return false; };
			var img = document.createElement("IMG");
			img.setAttribute("src", "images/pub/arr_right.gif");
			prevPageTop.appendChild(img);
			categoryheader.appendChild(prevPageTop);			
		}
		categoryheader.appendChild(categoryheadertext);
		if ( page < numpages ) {
			var nextPageTop = document.createElement("A");
			linkMap["a-next-top"] = _getPageLinkAsString(page+1);
			nextPageTop.setAttribute("id", "a-next-top");
			nextPageTop.setAttribute("href", "javascript:void(0)");
			nextPageTop.onclick = function() { eval(linkMap[this.id]); return false; };
			var img = document.createElement("IMG");
			img.setAttribute("src", "images/pub/arr_left.gif");
			nextPageTop.appendChild(img);
			categoryheader.appendChild(nextPageTop);
		}


		
		lookupcategories();
}

var displaycard = function(num, json) {
	var li = document.createElement("LI");
	
	var link = 'http://www.print.cceasy.com/order/index.cfm?sjumptoproduct=' + json.aff_link;
		
	var largeImageDiv = document.createElement("DIV");
	largeImageDiv.setAttribute("id", "uc" + (num+1));
	largeImageDiv.style.zIndex = 100;
	largeImageDiv.style.marginLeft = "50px";
	largeImageDiv.style.marginTop = "-100px";
	largeImageDiv.style.cssFloat = "right";
	largeImageDiv.style.position = "absolute";
	largeImageDiv.style.display = "none";
	largeImageDiv.onmouseover = function() { this.style.display='block' }
	largeImageDiv.onmouseout = function() { this.style.display='none' }
	
	var largeImageHref = document.createElement("A");
	largeImageHref.setAttribute("id", "li" + (num+1));
	largeImageHref.setAttribute("href", "javascript:void(0)");
	linkMap[largeImageHref.id] = link;
	largeImageHref.onclick = function() { location.href = linkMap[this.id]; return false;}
		
	var largeImageImg = document.createElement("IMG");
	largeImageImg.setAttribute("src",  "itemimages/holiday/large/" + json.image_link);
	largeImageImg.style.borderStyle = "none";
	largeImageHref.appendChild(largeImageImg);
	largeImageDiv.appendChild(largeImageHref);
	li.appendChild(largeImageDiv);
	
	var smallImageDiv = document.createElement("DIV");
	smallImageDiv.setAttribute("id", "c" + (num+1));
	smallImageDiv.style.position = "relative";
	smallImageDiv.onmouseover = function() { document.getElementById("u" + this.id).style.display = "block"; }
	smallImageDiv.onmouseout = function() { document.getElementById("u" + this.id).style.display = "none"; }
	
	var a = document.createElement("A");
	linkMap["a" + (num+1)] = link;
	a.setAttribute("id", "a" + (num+1));
	a.setAttribute("href", "javascript:void(0)");
	a.onclick = function() { window.location=linkMap[this.id]; return false; };
	
	var img = document.createElement("IMG");
	img.setAttribute("src", "itemimages/holiday/small/" + json.image_link);
	img.style.borderStyle = "none";
	a.appendChild(img);
	smallImageDiv.appendChild(a);
	li.appendChild(smallImageDiv);
	
	var p = document.createElement("P");
	p.className = "tc";
	var span = document.createElement("SPAN");
	span.className = "fc_grey";	
	var b = document.createElement("B");
	b.innerHTML = json.title;
	var hr = document.createElement("HR");
	var b2 = document.createElement("B");
	b2.innerHTML = json.content;
	span.appendChild(b);
	span.appendChild(hr);
	span.appendChild(b2);
	var br = document.createElement("BR");
	var span2 = document.createElement("SPAN");
	span2.className="fc_red";
	span2.innerHTML="";
	p.appendChild(span);
	p.appendChild(br);
	p.appendChild(span2);
	li.appendChild(p);
	return li;
}

var displaycategories = function(json) {
		
		var data = document.getElementById("categories");
		
		for (var i = 0; i < json.length; ++i) {
			var entry = json[i];

			var rightNav = document.createElement("LI");
			var img = document.createElement("IMG");
			img.setAttribute("src", "images/pub/rightMenu_arr.gif");
			var lc = "lookupcards(1," + entry.position + ",'" + entry.content + "')";
			var href = entry.category_link;// + "?page=1&category=" + entry.position + "&categoryname=" + escape(entry.content);
			var a = _createJavascriptLink("c" + i, document.createTextNode(entry.content), href, lc, false);
			rightNav.appendChild(img);
			rightNav.appendChild(a);
			data.appendChild(rightNav);
			var topNavCategory = document.getElementById("top" + entry.parent);
			if ( topNavCategory != null ) {
				var topNav = document.createElement("LI");
				var topNavLink = _createJavascriptLink("d" + i, document.createTextNode(entry.content), href, lc, false);
				topNav.appendChild(topNavLink);
				topNavCategory.appendChild(topNav);
			}
		}	
}

var _createJavascriptLink = function(id, inner, href, fn, needsEval) {
	var a = document.createElement("A");
	a.setAttribute("id", id);
	a.setAttribute("href", href == null ? "javascript:void(0)" : href);
	linkMap[id] = href == null ? fn : href;
	if ( needsEval ) {
		a.onclick = function() { eval(linkMap[this.id]); return false; };
	} else {
		a.onclick = function() { window.location=linkMap[this.id]; return false; };
	}
	a.appendChild(inner);
	return a;
}

var _getPageLinkAsString = function(p) {
	var link = "window.location=location.pathname";
	if ( p != 1 ) {
		link += " + '?page=" + p + "&category=" + category + "&categoryname=" + escape(categoryname) + "'"
	}
	return link;
}

var lookupcards = function (p, c, cname) {
	page = p;
	category = c;
	categoryname = cname;
	document.getElementById("banner").style.display = ( p == 1 && c == "%25" ) ? "block" : "none";
	document.getElementById("paragraph").style.display = ( p == 1 ) ? "block" : "none";
	lookup("http://www.mosesrosenthal.com/query.php?t=check&s=2&c=" + c + "&p=1&cb=displaycards", "data");
}

var lookupcategories = function () {
	lookup("http://www.mosesrosenthal.com/query.php?t=category&s=2&cb=displaycategories", "categories");
}

var lookup = function(url, divId) {
		var div = document.getElementById(divId);
		
		if ( div ) {
			//removeOldJSONScriptNodes();
			removeOldResults(divId);
					

			// Add a script element with the src as the user's Google Base query.
			// JSON output is specified by including the alt=json-in-script argument
			// and the callback funtion is also specified as a URI argument.
			var scriptElement = document.createElement("script");
			//scriptElement.setAttribute("id", "jsonScript");
			scriptElement.setAttribute("src", url);
			scriptElement.setAttribute("type", "text/javascript");
			document.documentElement.firstChild.appendChild(scriptElement);
		}
}

function removeOldResults(divId) {
		var div = document.getElementById(divId);
		div.innerHTML = "";
}

function getUrlParameter( name ) {
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		return results == null ? null : unescape(results[1]);
}