//Home Main Expander...
function homeHelper(){
	var moreLinks = $$('em.more');
	var hiddenDIV = $('expandedHome');

	if(!moreLinks){
		return false;
	} 

	moreLinks.each(function(moreLink){
		moreLink.observe('click', function(event){
			hiddenDIV.toggleClassName('closed');
			hiddenDIV.previous('p').toggleClassName('active');
		});
	});
	
}

//Resource List Expansion
function resourceListHelper(){
	var resourceListTopics = $$('ul.resource li h3');
	
	
	if(!resourceListTopics){
		return false;
	}
	
	resourceListTopics.each(function(listTopic){
		listTopic.toggleClassName('closed');
		listTopic.next('p').toggle();
		
		listTopic.observe('click', function(event){
		
		
		listTopic.toggleClassName('closed');
		listTopic.next('p').toggle();
		});
		
	});
}

//Issue List Expansion
function issueListHelper(){
	var issueListTopics = $$('ul.issues li h2');
	
	
	if(!issueListTopics){
		return false;
	}
	
	issueListTopics.each(function(listTopic){
		listTopic.toggleClassName('closed');
		listTopic.next('div').toggle();
		
		listTopic.observe('click', function(event){
		
		
		listTopic.toggleClassName('closed');
		listTopic.next('div').toggle();
		});
		
	});
}


//Contribution Form JS Insertion

function formInsert(){

	var placeHolder = $('formPlaceholder');
	var formMarkup = $('contribution');

	
	if(!placeHolder){
		return false;
	}
	
	placeHolder.insert({after:formMarkup});
	placeHolder.remove();
}

//PayPal Custom Variable Combinator

function customCombine(){

	var contributeForm = $('contribution');
	
	if(!contributeForm){
		return false;
	}
	
	contributeForm.observe('submit', function(event){
		customVariable = $('customVariable');
		firstName = $('firstName').value;
		lastName = $('lastName').value;
		employer = $('employer').value;
		jobTitle = $('jobTitle').value;
		
		//alert(firstName + lastName + employer + jobTitle);
		customVariable.value = 'Name:'+ firstName + ' ' + lastName + ',' + 'Employer:' + employer + ' Title:' + jobTitle;
		event.stop;
	});
	 
}

//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;
			}
		});
	});
}

// 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() {
	
	formInsert();
	//dynamicShadow('/images/global/shadow.png', 'page-container', 16, 0);
	liFirstLast(); // Adds classes 'first' and 'last' to respective LIs
	customCombine();
	homeHelper();
	resourceListHelper();
	issueListHelper();
});