liltype.endoil = new liltype();

liltype.endoil.initPrimaryNav = function(id)
{
	var sf = new liltype.SFMenu(id);
	sf.init(id);
	sf = null;
}

liltype.endoil.initFooterNavXV = function(node)
{
	if ((node.nodeType == 1) &&
	    (node.nodeName == "LI"))
	{
		if (!this.foundFirstFooterLI)
		{
			this.foundFirstFooterLI = true;
			node.className = liltype.CNU.add(node.className, "first_item");
		}		
		return false;
	}
	return true;
}

liltype.endoil.initFooterNav = function(id)
{
	this.foundFirstFooterLI = false;
	liltype.xVerseNode(liltype.ds(id), this, "initFooterNavXV");
}

liltype.endoil.initDidYouKnowXV = function(node)
{
	var copys = new Array();
	var links = new Array();

	if ((node.nodeType == 1) &&
	    (node.nodeName == "DIV") &&
	    (liltype.CNU.match(node.className, "did_you_know_container")))
	{
		node.style.display = "none";
		this.dyns.push(node);
	}
	return true;
}

liltype.endoil.initDidYouKnow = function(id)
{
	var n;
	this.dyns = new Array();
	liltype.xVerseNode(liltype.ds(id), this, "initDidYouKnowXV");
	n = Math.round(Math.random() * (this.dyns.length - 1));
	this.dyns[n].style.display = "block";
	this.dyns = null;
}

liltype.endoil.initBreadCrumb = function(id, sectionCode)
{
	var rgs, regexp, understandHTML, node = liltype.ds(id);
	var urlC = liltype.endoil.getURLC();
	var urlB = liltype.endoil.getURLB();

	if (node && node.innerHTML)
	{
		node.innerHTML = node.innerHTML.replace(/<a\s[^>]+>\s*<\/a>\s*\&gt;/i, "");
		understandHTML = 
			'<a class="Path" href="http://www.kintera.org/site/c.'+urlC+'/b.4093913/">Understand</a> &gt;';
		regexp = new RegExp("(<a\\s[^>]+>Home</a>\\s+&gt;)", "i");

		if ((urlB == "4090879"))
		{
			// Nothing
		}
		else if (sectionCode)
		{
			switch (sectionCode)
			{
				case "fbg":
					node.innerHTML =
						node.innerHTML.replace(regexp, "$1 "+understandHTML+' <a class="Path" href="http://www.kintera.org/site/c.'+urlC+'/b.4090047/">Fool\'s Black Gold</a> &gt;');
					break;
				case "coc":
					node.innerHTML =
						node.innerHTML.replace(regexp, "$1 "+understandHTML+' <a class="Path" href="http://www.kintera.org/site/c.'+urlC+'/b.4090073/">Costs of Consumption</a> &gt;');
					break;
				case "sar":
					node.innerHTML =
						node.innerHTML.replace(regexp, "$1 "+understandHTML+' <a class="Path" href="http://www.kintera.org/site/c.'+urlC+'/b.4104349/">Science &amp; Research</a> &gt;');
					break;
				case "kth":
					node.innerHTML =
						node.innerHTML.replace(regexp, "$1 "+' <a class="Path" href="http://www.kintera.org/site/c.'+urlC+'/b.4126613/">Kick The Habit</a> &gt;');
					break;
				case "about":
					if (!document.title.match(/^\s*about\s*\|/i))
					{
						node.innerHTML =
							node.innerHTML.replace(regexp, "$1 "+' <a class="Path" href="http://www.kintera.org/site/lookup.asp?c='+urlC+'&b=4097567">About</a> &gt;');
					}
					break;
			}
		}
		else
		{
			switch (urlB)
			{
				case "4090047":
				case "4090073":
				case "4104349":
					node.innerHTML =
						node.innerHTML.replace(regexp, "$1 "+understandHTML);
					break;
			}
		}
		
		node.innerHTML = node.innerHTML.replace(/\&gt;\s+<font[^>]+>.+<\/font>/i, "");
	}
}

liltype.endoil.getURLB = function()
{
	var r;
	if (r = window.location.href.match(/\/b\.([0-9]+)\//))
	{
		return r[1];
	}
	else if (r = window.location.href.match(/(\?|\&)b=([0-9]+)(\&|$)/))
	{
		return r[2];
	}
}

liltype.endoil.getURLC = function()
{
	var r;
	if (r = window.location.href.match(/\/c\.([0-9a-z]+)\//i))
	{
		return r[1];
	}
	else if (r = window.location.href.match(/(\?|\&)c=([0-9a-z]+)(\&|$)/i))
	{
		return r[2];
	}
}

liltype.endoil.onClickWTH = function(a, code)
{
	switch (code)
	{
		case "email":
			popUpDialogResizeableVarSize
			(
				"F1EE9E21EEF24BE888C44660079AF138",
				"email2friend",
				"/site/email2friend.asp?c="+liltype.endoil.getURLC()+"&b="+liltype.endoil.getURLB()+"&email_url="+escape("/site/pp.aspx?c="+liltype.endoil.getURLC()+"&b="+liltype.endoil.getURLB()),
				700,
				525
			);
			return false;
			break;
		case "print":
			a.href = 
				'http://www.kintera.org/site/pp.aspx?c='+liltype.endoil.getURLC()+'&b='+liltype.endoil.getURLB()+'&printmode=1';
			return true;
			break;
		case 'digg':
			a.href =
				"http://digg.com/submit?phase=2&url="+encodeURIComponent(window.location.href)+"&title="+encodeURIComponent(document.title)+"bodytext=&topic=environment";
			return true;
			break;
		case 'del.icio.us':
			a.href="http://del.icio.us/post";
			window.open("http://del.icio.us/post?v=4&noui&jump=close&url="+encodeURIComponent(location.href)+"&title="+encodeURIComponent(document.title), "delicious","toolbar=no,width=700,height=400");
			return false;
			break;
		case 'furl':
			a.href =
				"http://www.furl.net/storeIt.jsp?u="+encodeURIComponent(window.location.href)+"&t="+encodeURIComponent(document.title);
			return true;
			break;
		case 'hugg':
			liltype.endoil.formHugg.submit();
			return false;
			break;
	}
}

liltype.endoil.realPricePos = new Array
(
	new Array(98, 25),
	new Array(96, 66),
	new Array(94, 107),
	new Array(92, 148),
	new Array(98, 232),
	new Array(96, 273),
	new Array(94, 314),
	new Array(92, 355),
	new Array(98, 436),
	new Array(96, 477),
	new Array(94, 518),
	new Array(92, 559)
);

liltype.endoil.realPriceImgPre = "/atf/cf/%7BF1EE9E21-EEF2-4BE8-88C4-4660079AF138%7D/";
liltype.endoil.realPriceImgPost = ".JPG";

liltype.endoil.getPumpPrice = function()
{
	var el = liltype.ds("spPrice");
	if (el && el.innerHTML.length)
	{
		return el.innerHTML.replace(/[^0-9\.]/g, "");
	}
	return "3.95";
}

liltype.endoil.pumpPriceToSubsidy = function(price)
{
	return price + 4.60;
}

liltype.endoil.pumpPriceToDef = function(price)
{
	return price + 14.14;
}

liltype.endoil.printRealPrice = function(id)
{
	var i, srcStub, priceStrInd;
	var atPumpPriceStr = this.getPumpPrice();
	var atPumpPrice = parseFloat(atPumpPriceStr);
	var priceStr = atPumpPriceStr;
	var price = atPumpPrice;
	var html =
		'<div id="'+id+'" style="position: relative; z-index: 1; width: 634px; overflow: hidden;">'+
			'<img style="position: relative; top: 0px; left: 0px; right: 0px;" src="'+this.realPriceImgPre+'REAL_PRICE_BG'+this.realPriceImgPost+'" width="638" height="228">';
	for (i = 0; i < this.realPricePos.length; i++)
	{
		srcStub = "";
		switch (i)
		{			
			// Avg price
			case 0:
				if (price > 9.99)
				{
					priceStrInd = 1;
					srcStub = "NUMBER"+priceStr.substr(0, 1);
				}
				else
				{
					priceStrInd = 0;
					srcStub = "";
				}
				break;
			case 1:
			case 2:
			case 3:
				srcStub = "NUMBER"+((i == 2) ? "_DOT" : "")+priceStr.substr(priceStrInd++, 1);
				if (i == 1)
				{
					priceStrInd++;
				}
				break;

			// w/subsidies
			case 4:
				price = this.pumpPriceToSubsidy(atPumpPrice);
				priceStr = ""+price;
				if (price > 9.99)
				{
					priceStrInd = 1;
					srcStub = "NUMBER"+priceStr.substr(0, 1);
				}
				else
				{
					priceStrInd = 0;
					srcStub = "";
				}
				break;
			case 5:
			case 6:
			case 7:
				srcStub = "NUMBER"+((i == 6) ? "_DOT" : "")+priceStr.substr(priceStrInd++, 1);
				if (i == 5)
				{
					priceStrInd++;
				}
				break;

			// w/defense
			case 8:
				price = this.pumpPriceToDef(atPumpPrice);
				priceStr = ""+price;
				if (price > 9.99)
				{
					priceStrInd = 1;
					srcStub = "NUMBER"+priceStr.substr(0, 1);
				}
				else
				{
					priceStrInd = 0;
					srcStub = "";
				}
				break;
			case 9:
			case 10:
			case 11:
				srcStub = "NUMBER"+((i == 10) ? "_DOT" : "")+priceStr.substr(priceStrInd++, 1);
				if (i == 9)
				{
					priceStrInd++;
				}
				break;
		}

		if (srcStub)
		{
			html +=
				'<img style="position: absolute; z-index: 2; top: '+this.realPricePos[i][0]+'px; left: '+this.realPricePos[i][1]+'px;" src="'+this.realPriceImgPre+srcStub+this.realPriceImgPost+'">';
		}
	}
	
	html += '</div>';

	document.writeln(html);
}

liltype.endoil.printRealPrice2XV = function(node)
{
	if ((node.nodeType == 1) && (node.nodeName == "A"))
	{
		if (node.href.match(/#average/))
		{
			node.innerHTML = this.atPumpPrice;
		}
		else if (node.href.match(/#subsidies/))
		{
			node.innerHTML = this.subsidyPrice;
		}
		else if (node.href.match(/#defense/))
		{
			node.innerHTML = this.defensePrice;
		}
	}
	return true;
}

liltype.endoil.printRealPrice2 = function(id)
{
	this.atPumpPrice = Math.round(parseFloat(this.getPumpPrice()) * 1000) / 1000;
	this.subsidyPrice = Math.round(this.pumpPriceToSubsidy(this.atPumpPrice) * 1000) / 1000;
	this.defensePrice = Math.round(this.pumpPriceToDef(this.atPumpPrice) * 1000) / 1000;
	liltype.xVerseNode(liltype.ds(id), this, "printRealPrice2XV");
}

liltype.endoil.kthCWLXV = function(node)
{
	if (node.nodeType == 1)
	{
		if (node.nodeName == "UL")
		{
			node.style.display = "none";
			this.uls.push(node);
		}
		else if ((node.nodeName == "LI") &&
		         liltype.CNU.match(node.className, "icl_title"))
		{
			node.style.display = "none";
			this.titles.push(node.innerHTML);
		}
	}
	return true;
}

liltype.endoil.kthCWL = function(id, idTitle)
// Kick the habit: change your world bottom left
{
	var selectedInd;

	this.uls = new Array();
	this.titles = new Array();

	liltype.xVerseNode(liltype.ds(id), this, "kthCWLXV");

	selectedId = Math.round(Math.random() * (this.uls.length - 1));

	this.uls[selectedId].style.display = "block";
	this.uls = null;

	liltype.ds(idTitle).innerHTML = this.titles[selectedId].toUpperCase();
	this.titles = null;
}


liltype.endoil.contactFormHackXV = function(node)
{
	if (node.nodeType == 1)
	{
		if ((node.nodeName == "B") && node.innerHTML.match(/general\s+subscription/i))
		{
			node.innerHTML = "Subscribe to our eNewsletter!";
			return false;
		}
	}
	return true;
}

liltype.endoil.contactFormHack = function(id)
// Perform Various hacks to the Contact Form
{
	liltype.xVerseNode(liltype.ds(id), this, "contactFormHackXV");
}



liltype.endoil.donateFormHackXV = function(node)
{
	if (node.nodeType == 1)
	{
		if ((node.nodeName == "B") && node.innerHTML.match(/general\s+subscription/i))
		{
			node.innerHTML = "I&rsquo;d Like to Get EndOil eNews Updates";
			return false;
		}
		else if ((node.nodeName == "TR") && 
						 liltype.CNU.match(node.className, "CTsubtitle") &&
						 node.innerHTML.match(/subscribe\s+to\s+our\s+enewsletter/i) && 
						 node.innerHTML.match(/<font[^>]*>/i) && 
						 node.innerHTML.match(/<b[^>]*>/i))
		{
			node.style.display = "none";
		}
		else if (node.id == "donation_type_copy")
		{
			node.style.display = "none";
		}
	}
	return true;
}

liltype.endoil.donateFormHack = function(id)
// Perform Various hacks to the Join Us! Form
{
	liltype.xVerseNode(liltype.ds(id), this, "donateFormHackXV");
}

liltype.endoil.onJoinSubmit = function(form)
{
	var expry, rgs;

	expry = new Date();
	expry.setTime(expry.getTime() + 60000);

	document.cookie =
		"eojufe="+encodeURIComponent(form.email_address.value)+
		"; expires="+(expry.toUTCString ? expry.toUTCString() : expry.toGMTString())+
		"; path=/";

	document.cookie =
		"eojufz="+encodeURIComponent(form.zip.value)+
		"; expires="+(expry.toUTCString ? expry.toUTCString() : expry.toGMTString())+
		"; path=/";

	return true;
}

liltype.endoil.onJoinLoad = function(formId)
{
	var rgs, form;
	form = liltype.ds(formId);

	rgs = document.cookie.match(/eojufe=([^;]+)(;|$)/);
	if (rgs && rgs[1].length)
	{
		form.email_address.value = decodeURIComponent(rgs[1]);
	}

	rgs = document.cookie.match(/eojufz=([^;]+)(;|$)/);
	if (rgs && rgs[1].length)
	{
		form.zip.value = decodeURIComponent(rgs[1]);
	}
}

