//First and Last LI Selector
//Note: Prototype Driven
function liFirstLast() {
	var firstLIs =	$$('ul > li:first-child');
	var lastLIs = $$('ul > li:last-child');
	
	firstLIs.each(function(liFirst) {
		liFirst.addClassName('first');
		});
		
	lastLIs.each(function(liLast) {
		liLast.addClassName('last');
	});
}

//Input Clear
//Clears text inputs on a page on focus
//Note: Prototype driven
function inputClear() {
	var textInputs = $$('input[type="text"]');
	
	textInputs.each(function(textInput){
		textInput.initialValue = textInput.value;
		textInput.observe('focus', function(event) {
			if(textInput.value == textInput.initialValue){
				textInput.clear();
			}
		});
		textInput.observe('blur', function(event){
			if(textInput.value.blank() == true) {
				textInput.value = textInput.initialValue;
			}
		});
	});
}

//Header Text Adjustment
function replaceHeaderText() {
	var moduleHeaders = $$('#xg_body .xg_module_head h2, ul.navigation li a, #xg_body .xg_headline h1, .xg_module_foot .xg_sprite-add');
	moduleHeaders.each(function(moduleHeader){
		moduleHeader.innerHTML = moduleHeader.innerHTML.sub('Photos', "Gathering Support to Build a Future, World-Class Aquarium on Cleveland's Waterfront");
		moduleHeader.innerHTML = moduleHeader.innerHTML.sub('Notes', 'News');
		moduleHeader.innerHTML = moduleHeader.innerHTML.sub('Note', 'News Article');
	});
}

//Donation Text Adjustment
function donationModule() {
	var donationModules = $$('#xg_body > .xg_colgroup > .xg_1col .module_text');
	var accountModule = $('xg_module_account');
	
	donationModules.each(function(donationModule){
		donationModule.remove();
		
		accountModule.insert({ before: donationModule });
	});
}

//News Text Adjustment
function replaceNewsText() {
	var moduleContinues = $$('#xg_body #xg_layout_column_2 .module_notes p.last-child a');
	var moduleContents = $$('#xg_body #xg_layout_column_2 .module_notes p.last-child');
	
	moduleContinues.each(function(moduleContinue){
		moduleContinue.remove();
	});
	
	moduleContents.each(function(moduleContent){
		moduleContent.innerHTML = moduleContent.innerHTML.truncate(120, '[...]');
	});
}

//Cufon Font Replacement
function cufonReplacement() {
	Cufon.replace('#xg_body #xg_layout_column_1 .xg_module_head h2');
	Cufon.replace('#xg_body #xg_layout_column_2 .xg_module_head h2');
	Cufon.replace('#xg_body .xg_1col .xg_module_head h2');
	Cufon.replace('#xg_body .xg_span-4 .xg_module_head h2');
	Cufon.replace('#xg_body .xg_1col .module_text ul li h3');
	Cufon.replace('#xg_body .xg_span-4 .module_text ul li h3');
	Cufon.replace('.xg_headline h1');
}

// Cookie Functions
// Set the cookie 
function setCookie(name,value,days) { 
	if (days) { 
		var date = new Date(); 
		date.setTime(date.getTime()+(days*24*60*60*1000)); 
		var expires = ";expires="+date.toGMTString(); 
	} else { 
		expires = ""; 
	} 
	document.cookie = name+"="+value+expires+";"; 
}

// Read the cookie 
function readCookie(name) { 
	var needle = name + "="; 
	var cookieArray = document.cookie.split(';'); 
	for(var i=0;i < cookieArray.length;i++) { 
		var pair = cookieArray[i]; 
		while (pair.charAt(0)==' ') { 
			pair = pair.substring(1, pair.length); 
		} 
		if (pair.indexOf(needle) == 0) { 
			return pair.substring(needle.length, pair.length); 
		} 
	} 
	return null; 
}

//Replacement for Window Onload - Loads before images, cross-browser
document.observe("dom:loaded", function() {
	//dynamicShadow('/images/global/shadow.png', 'page-container', 16, 0);
	liFirstLast(); // Adds classes 'first' and 'last' to respective LIs
	replaceHeaderText();
	replaceNewsText();
	cufonReplacement();
	donationModule();
});