/* stainsolver 2.0 core javascipt - Feb. 2009 */ 


var DropdownNav = 
{
	init: function() 
	{
		if ($$('li.button'))
		{
			var mainListItemsArray = $$('li.button'); //get li that hold hidden lists
		
			for (var i = 0; i < mainListItemsArray.length; i++) //assign listeners to each li
			{
				Event.observe(mainListItemsArray[i], "mouseover", DropdownNav.showBox);
				Event.observe(mainListItemsArray[i], "mouseout", DropdownNav.hideBox);
			}
		}
	},
	showBox: function()
	{
		if (this.select('div')[0]) //if div exists inside li, show dropdown box
		{
			clearTimeout(this._timer);
			this.addClassName('hover');
			var hiddenDiv = this.select('div')[0];
			hiddenDiv.removeClassName('hiddenList');
		}
	},
	hideBox: function()
	{
		if (this.select('div')[0]) //if div exists inside li, hide dropdown box
		{
			var theItem = this;
			theItem._timer = setTimeout(function()
			{
				theItem.removeClassName('hover');
				var hiddenDiv = theItem.select('div')[0];
				hiddenDiv.addClassName('hiddenList');
			}, 10);
		}
	}
};

Event.observe(window, 'load', DropdownNav.init);



//START search button hove script
// copyright 1999-2001 Idocs, Inc. http://www.idocs.com/tags/
// Distribute this script freely, but keep this 
// notice with the code.
var submitRolls = new Object();

function submitroll(src, oversrc, name)
{
this.src=src;
this.oversrc=oversrc;
this.name=name;
this.alt="Search";
this.write=submitroll_write;
}

function submitroll_write()
{
var thisform = 'document.forms[' + (document.forms.length - 1) + ']';
submitRolls[this.name] = new Object();
submitRolls[this.name].over = new Image();
submitRolls[this.name].over.src = this.oversrc;
submitRolls[this.name].out = new Image();
submitRolls[this.name].out.src = this.src;

document.write
	(
	'<A onMouseOver="if (document.images)document.images[\'' + this.name + "'].src=submitRolls['" + this.name + '\'].over.src"' + 
	' onMouseOut="if (document.images)document.images[\'' + this.name + "'].src=submitRolls['" + this.name + '\'].out.src"' + 
	' HREF="javascript:'
	);

if (this.sendfield)
	{
	if (! this.sendvalue)
		this.sendvalue = 1;
	document.write(thisform, ".elements['", this.sendfield, "'].value='", this.sendvalue, "';");
	}

document.write(thisform + '.submit();void(0);"');
if (this.msg)document.write(' onClick="return confirm(\'' , this.msg, '\')"');
document.write('>');

document.write('<IMG SRC="' + this.src + '" ALT="' + this.alt + '" BORDER=0 NAME="' + this.name + '"');
if (this.height)document.write(' HEIGHT=' + this.height);
if (this.width)document.write(' WIDTH='  + this.width);
if (this.otheratts)document.write(' ' + this.otheratts);
document.write('></A>');
if (this.sendfield)
	{
	document.write('<INPUT TYPE=HIDDEN NAME="' + this.sendfield + '">');
	document.forms[document.forms.length - 1].elements[this.sendfield].value='';
	}
}
//End search button hover script




/*SS DISCOUNT LIGHT BOX
** ORIGINALLY CREATED JUNE 28, 2009*/

//captures 'seconds' state when leaving page
Event.observe(window, 'beforeunload', function(e){
	if (Cookies.readCookie('seconds'))
	{
		Cookies.createCookie('seconds', DiscountLightBox.seconds, 365);
	}
	else
	{
		Cookies.createCookie('seconds', 0, 365);	
	}
});


var DiscountLightBox =
{
	seconds: '',
	timer: '',
	
	go: function(delaySeconds)
	{
		var cookieEnabled=(navigator.cookieEnabled)? true : false //test cookies on/off for modern browsers

		//if not IE4+ nor NS6+
		if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled) //text cookies for older browsers
		{ 
			document.cookie="testcookie"
			cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
		}
		if (cookieEnabled) //if cookies are enabled on client's browser
		{	
			DiscountLightBox.seconds = (Cookies.readCookie('seconds'))? Cookies.readCookie('seconds') : 0 //set current seconds visitor has been on the site
			DiscountLightBox.runCheckTimer(delaySeconds); //run the timer until the ad can be displayed
		}
	},
	
	runCheckTimer: function(delaySeconds) 
	{	
		if (Cookies.readCookie('alreadySawAd') != 1) //if they haven't seen the ad yet
		{
			if (DiscountLightBox.seconds < delaySeconds) //if it's a visitor who is waiting for the ad
			{
				DiscountLightBox.seconds++;
				DiscountLightBox.timer = setTimeout(function()
				{
					DiscountLightBox.runCheckTimer(delaySeconds);
				}, 1000);
			}
			if (DiscountLightBox.seconds >= delaySeconds) //if it's a visitor who is going to see the ad
			{
				clearTimeout(DiscountLightBox.timer); //clear timer to prevent double display
				Cookies.createCookie('alreadySawAd', 1, 365); //set the cookie so they don't get multiple discounts
				DiscountLightBox.showAd(); //display the ad
			}
		}
	},
	
	showAd: function()
	{
		//set screen width and height to page size
		$('lightBoxScreen').setStyle({height: document.documentElement.scrollHeight + 'px'}); 
		$('lightBoxScreen').setStyle({width: document.documentElement.scrollWidth + 'px'});
		
		//set location of light box to curren position of browser window using multi-browser workaround
		var windowTop = document.body.scrollTop;
		if (windowTop == 0)
		{
		    if (window.pageYOffset)
		        windowTop = window.pageYOffset;
		    else
		        windowTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
		windowTop += 50;
		$('discountLightBox').setStyle({top:windowTop + "px"});
		
		//create the content inside light Box
		var adImageLink = document.createElement('a'); //create link
		adImageLink.setAttribute('href', "http://askthebuilder.pinnaclecart.com/catalog/Stain_Solver-1-1.html?utm_source=stainsolver.com&utm_medium=ad&utm_content=Adv1_361x458_15seconds&utm_campaign=20OffFirstTimeVisitor");
		
		var adImage = document.createElement('img'); //create image ad
		adImage.setAttribute('src', 'http://www.stainsolver.com/2.0/images/Adv1_361x458.jpg');
		adImage.setAttribute('height', '458');
		adImage.setAttribute('width', '361');
		adImage.setAttribute('border', '0');
		adImage.setAttribute('alt', "the 20 percent off discount code is firstbuy20");
		
		adImageLink.appendChild(adImage);
		$('discountLightBox').appendChild(adImageLink);
		
		//var boxTitle = document.createTextNode("The Box Title");
		//var boxTitleElement = document.createElement('h3');
		//boxTitleElement.appendChild(boxTitle);
		//$('discountLightBox').appendChild(boxTitleElement);
		
		//var boxText = document.createTextNode(
		//	"Lorem ipsum dolor sit amet, consectetur elit Donald Duck Lives Here!"
		//);
		//var boxTextElement = document.createElement('p');
		//boxTextElement.appendChild(boxText);
		//$('discountLightBox').appendChild(boxTextElement);
		
		//display screen and box
		Effect.Appear('lightBoxScreen', {from: 0, to: 0.75, duration: 1.0 }); //make screen appear
		$('discountLightBox').appear(); //make light box appear
		
		//set close button action to hide screen and light box
		Event.observe($$('#lightBoxScreenClose a')[0], 'click', function(e){
			$('lightBoxScreen').fade();
			$('discountLightBox').fade();
			Event.stop(e);
		}, false);
	}
};





/*object creates, reades and deletes cookies*/
var Cookies = 
{
	createCookie: function(name,value,days) 
	{
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},

	readCookie: function(name) 
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},

	eraseCookie: function(name) 
	{
		Cookies.createCookie(name,"",-1);
	}

};
