//Cart View
function viewCart() {
	if(!$('cartExpanded')){
		return false;
	}
	
	var cartExpanded = $('cartExpanded');
	var cartButtons = $$('h2.cartButton');
	var closeButtons = $$('p.closeButton');
	var viewCart = $('viewCart');
	
	viewCart.hide('viewCart');
	
	cartButtons.each(function(cartButton, buttonIndex){
		cartButton.observe('click', function(event){
			cartExpanded.toggleClassName('open');

		});
	});	
	
	closeButtons.each(function(closeButton, closebuttonIndex){
		closeButton.observe('click', function(event){
			cartExpanded.toggleClassName('open');

		});
	});	
}

//Result List Expansion 
//This function allows for a hide and close on results under the ul tree 'resultExpander'
function resultExpander() {
	
	if(!$$('ul.resultExpander')){
		return false;
	}
	//alert("ghsdfg");
	var resultExpanders = $$('ul.resultExpander > li');
	
	
	resultExpanders.each(function(resultExpander, expanderIndex){ 
		var resultParent = resultExpander.down('ul');
		
		resultExpanders[expanderIndex].observe('click', function(event){
			resultParent.toggleClassName('open');
			resultExpanders[expanderIndex].toggleClassName('open');
		});
		
	});
}

//Tool-tip hover
function tipHover() {
	if(!$$('ul#radioList p')){ //check to see if these even exist
		return false;
	}
		
	var toolTips = $$('ul#radioList p.tooltip'); //make an array of all the tooltips
	
	
	
	toolTips.each(function(toolTip, toolTipIndex){
		var toolTipIcon = new Element('span'); //create the tooltip icon
		var toolTipTitle = toolTip.previous('label').down('dfn').down('strong'); //set variable for title of radio selection
		var radioContainer = toolTip.up('li'); // reach up to the parent list item, needed for bypassing z-index issues in IE 6/7
		toolTipTitle.totalWidth = toolTipTitle.getWidth(); //Start to define widths so we can offset tool tip to icon.
		
		
		var toolTipInput = toolTip.previous('label').down('input'); //define the width of the radio button
		toolTipInput.totalWidth = toolTipInput.getWidth(); 
		
		var totalOffset = toolTipTitle.totalWidth + toolTipInput.totalWidth; //combine widths
		
		
		toolTipIcon.innerHTML = ''; //insert the hidden text for the tool tip icons
		
		toolTip.previous('label').down('dfn').insert({bottom:toolTipIcon}); //place the icon below the dfn in the radio list.
		
		toolTipIcon.observe('mouseenter', function(event){
			toolTips[toolTipIndex].addClassName('hover');
			toolTips[toolTipIndex].setStyle({
				left: totalOffset + 26 + 'px',
				top: '5px',
				zIndex: '100'
			});
			radioContainer.setStyle({
				zIndex: '100'
			});
			
		});
		
		toolTipIcon.observe('mouseleave', function(event){
			toolTips[toolTipIndex].removeClassName('hover');
			toolTips[toolTipIndex].setStyle({
				left: '-999em',
				zIndex: '0'
			});
			radioContainer.setStyle({
				zIndex: '0'
			});
		});
		
	});	
	
	
	
}

//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');
	});
}

//First and Last DT Selector
//Note: Prototype Driven
function dtFirstLast() {
	var firstDTs =	$$('dl > dt:first-child');
	var lastDTs = $$('dl > dt:last-child');
	
	firstDTs.each(function(dtFirst) {
		dtFirst.addClassName('first');
		});
		
	lastDTs.each(function(dtLast) {
		dtLast.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() {
		//dynamicShadow('/images/global/shadow.png', 'page-container', 16, 0);
	liFirstLast(); // Adds classes 'first' and 'last' to respective LIs
	dtFirstLast(); // Adds classes 'first and 'last' to respective DT's
	tipHover();
	viewCart();
	resultExpander();
});


