﻿var linkMap = new Array();
	
	var renderCheck = function (index, jsonEntry) {
		var checkDiv = document.createElement("div");
		checkDiv.className = "imcon";

		var affiliateLink = "http://clickserve.cc-dt.com/link/tplclick?lid=41000000027820726&pubid=21000000000211802&redirect=" + escape(jsonEntry.aff_link);
		
		/** The div for the large image **/
		var largeImageDiv = document.createElement("div");
		largeImageDiv.setAttribute("id", "uc" + (index+1));
		largeImageDiv.className = "big";
		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" + (index+1));
		largeImageHref.setAttribute("href", "javascript:void(0)");
		linkMap[largeImageHref.id] = affiliateLink;
		largeImageHref.onclick = function() { location.href = linkMap[this.id]; return false;}
		
		var images = jsonEntry.image_link;
		var smallImageUrl = "http://www.printe-z.com/itemimages/nothing.2.personal.jpg";
		if ( images ) {
			smallImageUrl = images;
		}
		var largeImageUrl = smallImageUrl.replace(/(.*\/itemimages\/)(.*)/, "$1large/personal/$2").replace(/(.*)\d\.(.*)/, "$1$2");
		
		var largeImageImg = document.createElement("img");
		largeImageImg.setAttribute("src",  largeImageUrl);
		largeImageImg.setAttribute("width", "400");
		largeImageImg.setAttribute("height", "183");
		
		largeImageHref.appendChild(largeImageImg);
		largeImageDiv.appendChild(largeImageHref);
		checkDiv.appendChild(largeImageDiv);
		
		/** The href for the small image **/
		
		var smallImageHref = document.createElement("a");
		smallImageHref.setAttribute("href", "javascript:void(0)");
		smallImageHref.setAttribute("id", "u" + (index+1));
		linkMap[smallImageHref.id] = affiliateLink;
		smallImageHref.onclick = function() { location.href=linkMap[this.id]; return false;}
		smallImageHref.onmouseover = function() { bigimg(this) };
		smallImageHref.onmouseout = function() { bigimg1(this) };
		
		var smallImageImg = document.createElement("img");
		smallImageImg.setAttribute("src", smallImageUrl);
		
		smallImageHref.appendChild(smallImageImg);
		checkDiv.appendChild(smallImageHref);
		
		/** The href for the Buy Now image **/
		var buyNowHref = document.createElement("a");
		buyNowHref.setAttribute("id", "bn" + (index+1));
		buyNowHref.setAttribute("href", "javascript:void(0)");
		linkMap[buyNowHref.id] = affiliateLink;
		buyNowHref.onclick = function() { location.href = linkMap[this.id]; return false; };
		
		var buyNowImg = document.createElement("img");
		buyNowImg.setAttribute("src", "http://www.printe-z.com/imagespersonal/bn2.gif");
		
		buyNowHref.appendChild(buyNowImg);
		checkDiv.appendChild(buyNowHref);
		
		/** The div for the title/subtitle **/
		var titleDiv = document.createElement("div");
		titleDiv.className = "txt2";
		
		var titleSpan = document.createElement("span");
		titleSpan.innerHTML = jsonEntry.title;
		
		var titleBr = document.createElement("br");
		
		var titleTextNode = document.createTextNode(jsonEntry.content);
		
		titleDiv.appendChild(titleSpan);
		titleDiv.appendChild(titleBr);
		titleDiv.appendChild(titleTextNode);
		
		checkDiv.appendChild(titleDiv);
		
		return checkDiv;
	}
	
	var renderChecks = function(json) {
		// Transform the JSON results into an unordered list of links.
		
		var data = document.getElementById("data");
		
		for (var i = 0; i < json.length; ++i) {
			var entry = json[i];
			//if ( entry.g$categories[0].$t.indexOf("~~" + c + "~" + p + "~~") != -1 ) {
				data.appendChild(renderCheck(i, entry));
			//}
		}
		
//		data.removeChild(data.firstChild)
		
		lookupMenu();
	}
		
	var p, c;
	
	var initPage = function(page, category) {
		if ( !category ) category = "checks";
		p = page;
		c = category;
		lookupChecks();
	}
	
	var lookupChecks = function() {
		lookup('http://www.cloudycommerce.com/frady/query.php?t=check&c=' + c + '&p=' + p + '&cb=renderChecks', 'data');
	}

	var lookupMenu = function() {
		lookup('http://www.cloudycommerce.com/frady/query.php?t=category&cb=renderMenus', 'sidemenu');
	}
	
	var lookup = function(url, divId) {
		var div = document.getElementById(divId);
		
		if ( div ) {
			//removeOldJSONScriptNodes();
			//removeOldResults(divId);
					
			//var p = document.createElement('p');
			//p.appendChild(document.createTextNode('Loading...'));
			//div.appendChild(p);

			// 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);
		}
	}
	
	var parsePosition = function(a) {
		return parseInt(a.charAt(0) == '0' ? a.substring(1) : a);
	}
	
	var renderMenus = function(json) {
		var div = document.getElementById('sidemenu');
		var ul = document.createElement("UL");
		
		div.appendChild(ul);
				
		var entries = json; //json.sort(compareMenus);

		for (var i = 0; i < entries.length; ++i) {
			var entry = entries[i];
			var parent = entry.parent;
			var position = entry.position;
			if ( position.length == 1 || position.length == 3 ) continue;
			
			if ( parent == "none" ) {
				var li = document.createElement("LI");
				var index = parsePosition(position) + 1;
				li.setAttribute("id", "c" + index);
				
				var a = document.createElement("A");
				a.setAttribute("href", "javascript:void(0)");
				a.setAttribute("id", "a" + index);
				a.title = index;
				a.innerHTML = entry.content;
				a.onclick = function() { eval("menu" + this.title + "(this);") };
				li.appendChild(a);
				var subUl = document.createElement("UL");
				subUl.style.display = "none";
				subUl.setAttribute("id", "b" + index);
				subUl.className = "sidesubmenu";
				
				li.appendChild(subUl);
				
				ul.appendChild(li);
			} else {
				var subUl = document.getElementById("b" + (parsePosition(parent) + 1));
				
				if ( subUl ) {					
					var subLi = document.createElement("LI");
					
					var a = document.createElement("A");
					a.id = "d" + index;
					if ( entry.category_link ) {
						var category_link = entry.category_link;
						linkMap[a.id] = category_link;
						if ( entry.internal_link == "TRUE" ) {
							a.setAttribute("href", category_link);
						} else {
							a.setAttribute("href", "javascript:void(0)");
							a.onclick = function() { location.href = linkMap[this.id]; return false; }
						}

						if ( position == c ) {
						    var parentLink = document.getElementById("a" + (parsePosition(parent) + 1));
						    if ( parentLink && parentLink.onclick ) parentLink.onclick();
						}
					}
					
					//category_link.replace(/http:\/\/www.printe-z.com\/(.*)\.([cfmhtl]{3,4})/, "http://www.cloudycommerce.com/frady/$1-test.$2"));
					a.innerHTML = entry.content;
					
					subLi.appendChild(a);
					subUl.appendChild(subLi);
					//alert(subUl.innerHTML);
				}
			}
		}
		
		//div.removeChild(div.firstChild);
		menuFix();
		doPageAnalytics();
	}
	
	function menubridge(whichmenu, el) {
		eval("menu" + whichmenu + "(" + el + ")");
	}
	
	var doPageAnalytics = function() {
            var divs = document.getElementsByTagName("DIV");
            var footer;
            for ( var i = 0 ; i < divs.length; i++ ) {
                if ( divs[i].className.indexOf("footer") != -1 ) {
                     footer = divs[i];
                }
            }
	    if ( footer ) {
 		footer.innerHTML = "<a href='http://www.printe-z.com/personal-checks.html'>Personal Checks</a> - <a href='http://www.printe-z.com/american-personal-checks.html'>American Checks</a> - <a href='http://www.printe-z.com/animal-personal-checks.html'>Animal Checks</a> - <a href='http://www.printe-z.com/causes-personal-checks.html'>Causes Checks</a> - <a href='http://www.printe-z.com/human-nature-checks.html'>Human Nature Checks</a> - <a href='http://www.printe-z.com/fun-checks.html'>Fun Checks</a> - <a href='http://www.printe-z.com/nature-checks.html'>Nature Checks</a> - <a href='http://www.printe-z.com/special-interest-personal-checks.html'>Special Interest Personal Checks</a> - <a href='http://www.printe-z.com/sports-checks.html'>Sports Checks</a> - <a href='http://www.printe-z.com/stylistic-personal-checks.html'>Stylistic Designs Personal Checks</a> - <a href='http://www.printe-z.com/vehicle-checks.html'>Vehicle Checks</a> - <a href='http://www.printe-z.com/computer-checks.html'>Computer Checks</a> - <a href='http://www.printe-z.com/business-checks.html'>Business Checks</a> - <a href='http://www.printe-z.com/business-forms.html'>Business Forms</a> - Check Categories - Most Popular - Special Offers - <a href='http://www.printe-z.com/about-us.html'>About Us</a> - <a href='http://www.printe-z.com/contact-us.html'>Contact Us</a><br/><span>Copyright �2009</span> www.printe-z.com <br/><font>All rights reserved, including the right of reproduction in whole or in part in any form.</font>";
	    }


		var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

		includeJavascript(gaJsHost + "google-analytics.com/ga.js", "googleAnalytics");
		includeJavascript("http://count1.123count.com/wtslog.js", "counter");
//		
<!-- BEGIN LivePerson Monitor. --><script language='javascript'> var lpMTagConfig = {'lpServer' : "server.iad.liveperson.net",'lpNumber' : "6664135",'lpProtocol' : "https"}; function lpAddMonitorTag(src){if(typeof(src)=='undefined'||typeof(src)=='object'){src=lpMTagConfig.lpMTagSrc?lpMTagConfig.lpMTagSrc:'/hcp/html/mTag.js';}if(src.indexOf('http')!=0){src=lpMTagConfig.lpProtocol+"://"+lpMTagConfig.lpServer+src+'?site='+lpMTagConfig.lpNumber;}else{if(src.indexOf('site=')<0){if(src.indexOf('?')<0)src=src+'?';else src=src+'&';src=src+'site='+lpMTagConfig.lpNumber;}};var s=document.createElement('script');s.setAttribute('type','text/javascript');s.setAttribute('charset','iso-8859-1');s.setAttribute('src',src);document.getElementsByTagName('head').item(0).appendChild(s);} if (window.attachEvent) window.attachEvent('onload',lpAddMonitorTag); else window.addEventListener("load",lpAddMonitorTag,false);</script><!-- END LivePerson Monitor. -->

		try {
			var pageTracker = _gat._getTracker("UA-5437831-1");
			pageTracker._trackPageview();
		} catch(err) {}
		try {
			wtslog('al4219','1','http');
		} catch(err) {}
	}

	var includeJavascript = function(src, id) {
		var scriptElement = document.createElement("script");
		scriptElement.setAttribute("src", src);
		scriptElement.setAttribute("type", "text/javascript");
		if ( id ) {
			scriptElement.setAttribute("id", id);
		}
		document.documentElement.firstChild.appendChild(scriptElement);
		return scriptElement;
	}

	/* helper methods */
	
	function removeOldJSONScriptNodes() {
		var jsonScript = document.getElementById("jsonScript");	
		if (jsonScript) {
			jsonScript.parentNode.removeChild(jsonScript);
		}
	}

	/**
	* Deletes pre-existing children of the data div from the page. The data div
	* may contain a "Loading..." message, or the results of a previous query.
	* This old data should be removed before displaying new data.
	*/

	function removeOldResults(divId) {
		var div = document.getElementById(divId);
		if ( div.hasChildNodes() ) {
			while ( div.childNodes.length > 0 ) {
				div.removeChild( div.firstChild );
			}
		}
	}
	
	function query(parameter) {
		var query = window.location.search.substring(1);
		var parms = query.split('&');
		for (var i=0; i<parms.length; i++) {
			var pos = parms[i].indexOf('=');
			if (pos > 0) {
				var key = parms[i].substring(0,pos);
				var val = parms[i].substring(pos+1);
				if ( key == parameter ) {
					return val;
				}
			}
		}
		return null;
	}
	
	function pageNumber() {
		var page = query('page');
		if ( page == null ) {
			page = 1;
		}
		return page;
	}
