
var rbow = {};

rbow.acInit = false;

rbow.URLs = {"Home":"./index.php", 
		"FindBook":"./searchResults.php?cat=hardcopy",
		"Search":"./searchResults.php",
		"iPhone":"./iphone.php",
		"Android":"./android.php",
		"detail":"./searchResults.php",
		"ViewBook":"http://content.yudu.com/Library/A1sige/RainbowPages20112012/resources/index.htm?referrerUrl=http%3A%2F%2Fwww.yudu.com%2Fitem%2Fdetails%2F344850%2FRainbow-Pages-2011-2012%3Frefid%3D84550",
		"Advertising":"./advertising.php",
		"About":"./about.php",
		"Facebook":"https://www.facebook.com/rainbowpages",
		"Twitter":"https://twitter.com/myrainbowpages" 		
}

var spinnerVisible = false;  


/* load secondary pages based upon menu clicks */
rbow.loadPg  = function (lbl) {

	// some links open in new tabs, most just replace this page.
	if (lbl=="Android" ) {
		rbow.showDownload(lbl);
		return;
	}
	
	if (lbl=="iPhone" ) {
		rbow.showDownload(lbl);
		return;
	}
	
	// some links open in new tabs, most just replace this page.
	if (lbl=="About" | lbl=="Advertising" | lbl=="Facebook" | lbl=="Twitter"  | lbl=="ViewBook") {
		window.open(rbow.URLs[ lbl ], '_blank');
		return;
	}

	window.location = rbow.URLs[ lbl ];

}


	rbow.showDownload  = function (lbl) {
	if (lbl=="iPhone" ) { 
	 $("#iphonebox" ).dialog('open');
	} else {
 	  $("#androidbox" ).dialog('open');
  }
}


 

/* show the categories for the selected letter */
rbow.showCats  = function (letter) {

	rbow.catLetter = letter.toUpperCase();
	var srcURL = "./getCatCounts.php?sessId=" + rbow.sessId + "&jsoncallback=?&query=" + letter + ""; 
	$.getJSON(srcURL, {	format: "json"},
			function(data) {
		rbow.handle_CategoryResponse(data); 
	}); 

}

/* hide the categories for the selected letter */
rbow.hideCats  = function () {
	$("#catlist").hide();
}

/* invoke the search page using the selected category */
rbow.catSearch  = function (cat) {	
	window.location = rbow.URLs['Search'] + "?cat="+ encodeURIComponent(cat) + "&loc="  + rbow.txtlocation;
}


/* invoke the search page and pre-load the selected listing */
rbow.showListing  = function ( listName,listId) {
//	alert (" stub for showListing  listId =" + listId);
//	window.location = rbow.URLs['detail'] + "?listId="+ listId;
	rbow.txtlocation = $('#txtlocation').val();
	var URL = rbow.URLs['Search'] + "?showListing=true&txt="+ encodeURIComponent(listName) + "&loc="  + rbow.txtlocation;
//	alert ("new url " + URL);
	window.location =URL;

}


/* invoke the search page using the selected category */
rbow.findIt  = function () {
	rbow.txtSearch = $('#txtSearch').val();
	rbow.txtlocation = $('#txtlocation').val();
 
//	alert (" stub for find it txtSearch ="  + rbow.txtSearch + "  txtlocation ="  + rbow.txtlocation );

	
	//urlencode  searchTerm = encodeURIComponent(searchTerm);
	window.location = rbow.URLs['Search'] + "?txt="+ encodeURIComponent(rbow.txtSearch) + "&loc="  + rbow.txtlocation;
}



/*
 * convert the JSON response into 4 columns for display
 */

rbow.handle_CategoryResponse  = function (jsonResponse) {

	if ( jsonResponse.return_code!=1) {
		alert ("unable to obtain categories");
	}
	var payload =  jsonResponse.payload ;

	/*
	 * The columns snake down, then across.  Calculate how many rows are needed.
	 * The first column always contains the full number of rows,  other columns may not.
	 */
	var rowCount =  getAssocArrayLength(payload);
	var numRows =  Math.ceil(rowCount / 4);
	var rowNum = 0;

	var cols =[];  // contains the column html as its being built.

	cols [0]=new StringBuffer();
	cols [1]=new StringBuffer();
	cols [2]=new StringBuffer();
	cols [3]=new StringBuffer();

	/*
	 * loop through the values and counts to build 4 columns of html.
	 */

	for ( var tempValue in payload ) {	
		var colNo =  Math.floor(rowNum /numRows);
		// display the value and the count
		cols [colNo].append ("<a href=\'javascript:rbow.catSearch(\"" + tempValue + "\")\'>" + tempValue+ "[" + payload[tempValue] + "] </a><br>" );	
		rowNum++;
	}
	$("#catcol_0").html(cols [0].toString());
	$("#catcol_1").html(cols [1].toString());
	$("#catcol_2").html(cols [2].toString());
	$("#catcol_3").html(cols [3].toString());

	// now show the results.
	$("#catCount").html(rbow.catLetter + " Categories [" + rowCount + "]");

	$("#catlist").show();

}
/*
 * basic functions for manipulating menu items.
 */

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_nbGroup(event, grpName, lbl) { //v6.0


	var i,img,nbArr,args=MM_nbGroup.arguments;
	if (event == "init" && args.length > 2) {
		if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
			img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
			if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
			nbArr[nbArr.length] = img;
			for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
				if (!img.MM_up) img.MM_up = img.src;
				img.src = img.MM_dn = args[i+1];
				nbArr[nbArr.length] = img;
			} }
	} else if (event == "over") {
		document.MM_nbOver = nbArr = new Array();
		for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
			if (!img.MM_up) img.MM_up = img.src;
			img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
			nbArr[nbArr.length] = img;
		}
	} else if (event == "out" ) {
		for (i=0; i < document.MM_nbOver.length; i++) {
			img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
	} else if (event == "down") {
		nbArr = document[grpName];
		if (nbArr)
			for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
		document[grpName] = nbArr = new Array();
		for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
			if (!img.MM_up) img.MM_up = img.src;
			img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
			nbArr[nbArr.length] = img;
		} 
		rbow.loadPg(lbl);
		// alert ("clicked on grpName =" + grpName + " " + lbl);

	}
}

//useful object for assembling html & other long strings

function StringBuffer() { this.buffer = []; }

StringBuffer.prototype.append = function(string)
{
	this.buffer.push(string);
	return this;
}

StringBuffer.prototype.toString = function()
{
	return this.buffer.join("");
}
//useful function for 
function showObjectValues (obj)  {
	for (var key in obj) {
		alert (" key " +  key +"  = " + obj[key] );
	}
}
//Javascript Associative Array objects do not have the same
//length method as standard Array objects.  This is
//a workaround.
function getAssocArrayLength(tempArray) {
	var result = 0;
	for ( tempValue in tempArray ) {
		result++;
	}

	return result;
}
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function showProgress() {  


	if (!spinnerVisible) { 
		$("div#spinner").fadeIn("fast");
		spinnerVisible = true;
	}
}  

function hideProgress() {


	if (spinnerVisible) {
		var spinner = $("div#spinner"); 
		spinner.stop(); 
		spinner.fadeOut("fast");
		spinnerVisible = false; 
	}  
}



