// Turn off error display.

window.onerror = null;


// Cookie setter

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


// Set cookie to indicate that the site has been viewed. (This will
// prevent pop-up ad from appearing in certain contexts.)

setCookie('tm_site_viewed',1,null,'/',null,null);


// This is a cool cross-platform object reference getter.

function getObj(name) {

	if (document.getElementById) {
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	
	else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}

	else if (document.layers) {
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}

}


// This function runs the show, using helper functions. It is called
// at page load time. First it gets cross-browser references to all
// the blurbs (the number of which is constant set here); then it
// positions all of them; then it kicks off the display loop.

function startBlurbs() {


	// First get references to our blurbs divs, w/helper function.
	
	// Set number of blurbs as constant used throughout.

	num_blurbs = 6;
	
	
	// Now create array of max font sizes for all six blurbs
	
	max_fontsize = new Array(0, 40, 32, 28, 40, 32, 28);


	// Loop through all blurb divs getting references.

	for (i=1; i <= num_blurbs; i++) {
	
		eval("blurb_f"+i+" = new getObj('blurb_f"+i+"Div')");
		eval("blurb_b"+i+" = new getObj('blurb_b"+i+"Div')");
		eval("blurb_n"+i+" = new getObj('blurb_n"+i+"Div')");

	
	}

	
	// Now postion blurb divs, with another function.

	positionBlurbs();


	// Now set index to first blurb.
	
	blurb_index = 1;


	// Now kick off the expansion, disappearance, rotation cycle.
	
	runBlurbs();
	
	
	// Finally, set timer to show our little amazon seller icons.
	
	setTimeout ( 'showSeller()', 28000 )
	
	return true;

}


// This function, call at page load time, positions the blurb divs
// in the horizontal middle of the screen, in front of the palm.

function positionBlurbs() {


	// First get width of browser window.
	
	if (document.layers) {
		win_width = window.innerWidth;
	} 
	
	else if (document.all) {
		win_width = document.body.clientWidth+document.body.scrollLeft;
	} 
	
	else if (document.getElementById) {
		win_width = window.innerWidth;
	}

	
	// Now set postion vals (window width minus half width of div);
	
	var left_f_val = (win_width/2) - 400;
	var left_f = left_f_val + "px";
	
	var left_b_val = left_f_val + 1;
	var left_b = left_b_val + "px";

	var left_n_val = left_f_val + 2;
	var left_n = left_n_val + "px";


	// Loop through all blurb divs setting position.

	for (i=1; i <= num_blurbs; i++) {

		eval('blurb_f'+i+'.style.left = left_f');	
		eval('blurb_f'+i+'.style.top = "100px"');
	
		eval('blurb_b'+i+'.style.left = left_b');	
		eval('blurb_b'+i+'.style.top = "101px"');

		eval('blurb_n'+i+'.style.left = left_n');	
		eval('blurb_n'+i+'.style.top = "102px"');
	
	}
	
}



// This function cycles through the blurbs: first it shrinks the existing
// blurb to nothing; then it cycles the blurb; then in a few seconds it
// grows the blurb; then it calls itself again.

function runBlurbs() {

	
	// First grow the blurb in 1 second
	
	setTimeout('growBlurb(blurb_index, 1)', 1000);
	

	// Now shrink the existing blurb to invisible in 4 seconds
	// But leave last one longer. Increment index accordingly.
	
	if (blurb_index < num_blurbs) {

		setTimeout('blurb_index = blurb_index + 1', 4999);
	
		setTimeout('shrinkBlurb(blurb_index - 1)', 6400);

	}
	
	else {
		
		setTimeout('shrinkBlurb(blurb_index)', 8000);	
	
	}	

	
	// Now call us again, first four times.
	
	if (blurb_index < num_blurbs) {
	
		setTimeout('runBlurbs()', 5000);
	
	}

}



// This function grows the blurb text a fontsize at a time,
// using timeouts to call itself until size 32 (or whatever).
// * Now it also makes visible, to work on Safari*

function growBlurb(blurb_index, fontsize) {


	eval("blurb_f"+blurb_index+".style.visibility='visible'");	
	eval("blurb_b"+blurb_index+".style.visibility='visible'");
	eval("blurb_n"+blurb_index+".style.visibility='visible'");


	eval("blurb_f"+blurb_index+".style.fontSize='"+fontsize+"px'");	
	eval("blurb_b"+blurb_index+".style.fontSize='"+fontsize+"px'");
	eval("blurb_n"+blurb_index+".style.fontSize='"+fontsize+"px'");


	fontsize = fontsize + 1;

	
	if (fontsize <= max_fontsize[blurb_index]) {
	
		setTimeout('growBlurb('+blurb_index+', '+fontsize+')', 20);
	
	}

	return true;

}


// This function shrinks the blub back to invisibility.

function shrinkBlurb(blurb_index) {

	eval("blurb_f"+blurb_index+".style.fontSize='0px'");	
	eval("blurb_b"+blurb_index+".style.fontSize='0px'");
	eval("blurb_n"+blurb_index+".style.fontSize='0px'");
	
	eval("blurb_f"+blurb_index+".style.visibility='hidden'");	
	eval("blurb_b"+blurb_index+".style.visibility='hidden'");
	eval("blurb_n"+blurb_index+".style.visibility='hidden'");

	return true;

}


// This function drives Google Conversion tracking. It is called
// whenever a user on the site clicks on a link to buy a book (from Amazon).
// It is applied to all appropriate links at page load time by jquery.

function trackConv() {

	image = new Image(1,1);
	image.src = "http://www.googleadservices.com/pagead/conversion/1033517306/?value=0.6&amp;label=ImFRCOjLhwEQ-vHo7AM&amp;guid=ON&amp;script=0";
	
	return true;
	
}



// This function positions and reveals the little Amazon links on the side.

function showSeller() {


	// First get width of browser window.
	
	if (document.layers) {
		win_width = window.innerWidth;
	} 
	
	else if (document.all) {
		win_width = document.body.clientWidth+document.body.scrollLeft;
	} 
	
	else if (document.getElementById) {
		win_width = window.innerWidth;
	}
	


	// Now set postion val and show;

	var left_seller_val = (win_width/2) + 360;
	var left_seller = left_seller_val + "px";
	
	var seller = new getObj('sellerDiv');
	
	seller.style.left = left_seller;
	
	seller.style.visibility = "visible";
	
	return true;
	
}