var hideArrs;
var visibleObjects = new Array();

if ( window.addEventListener ){
	window.addEventListener('load', getCalLinks, false);
	window.addEventListener('load', getLinks, false);
	window.addEventListener('load', getInputs, false);
	window.addEventListener('load', getArrows, false);
} else {
	window.attachEvent('onload', getCalLinks, false);
	window.attachEvent('onload', getLinks, false);
	window.attachEvent('onload', getInputs, false)
	window.attachEvent('onload', getArrows, false);
}



function getArrows(){
	if ( document.getElementById('main_image') ){

		var featured = document.getElementById('featured');
		if ( featured.addEventListener ){
			featured.addEventListener('mouseover', showArrows, false);
			featured.addEventListener('mouseout', function (){ hideArrs = setTimeout('hideArrows()', 500) }, false);
		} else {
			featured.attachEvent('onmouseover', showArrows, false);
			featured.attachEvent('onmouseout', function (){ hideArrs = setTimeout('hideArrows()', 500) }, false);
		}
		
		var next = document.getElementById('next');
		next.addEventListener('mouseover', showArrows, false);
		next.addEventListener('mouseout', function (){ hideArrs = setTimeout('hideArrows()', 500) }, false);
		
		var prev = document.getElementById('prev');
		prev.addEventListener('mouseover', showArrows, false);
		prev.addEventListener('mouseout', function (){ hideArrs = setTimeout('hideArrows()', 500) }, false);
		
	}
}


function showArrows()
{
	window.clearTimeout(hideArrs);
	var next = document.getElementById('next');
	next.style.visibility = 'visible';
	document.getElementById('prev').style.visibility = 'visible';
}

function hideArrows()
{
	var next = document.getElementById('next');
	next.style.visibility = 'hidden';
	document.getElementById('prev').style.visibility = 'hidden';
}


document.onclick = function(e){
	hidemenus();
}

function getCalLinks()
{
	var sidebar = document.getElementById('sidebar');
	var links = sidebar.getElementsByTagName('a');
	var link_num = links.length;
	
	for ( var x=0; x < link_num; x++ ){
		if ( links[x].className == 'event'){
			links[x].onclick = function(e){
				if (!e) var e = window.event;
				e.cancelBubble = true;
				if (e.stopPropagation) e.stopPropagation();
				return getEvent(this);
			}
			links[x].parentNode.onclick = function(e){
				e.cancelBubble = true;
				if (e.stopPropagation) e.stopPropagation();
				getEventTD(this);
			}
			
		}
	}
	
	var cal_prev = document.getElementById('cal_prev');
	var cal_next = document.getElementById('cal_next');
	
	getCurrentDate();
	
	cal_prev.onclick = function(){
		return showCalendar('prev');
	}
	
	cal_next.onclick = function(){
		return showCalendar('next');
	}
}

function getEventTD(cell)
{
	var links = cell.getElementsByTagName('a');
	var calid = links[0].getAttribute('data-calid');
	getEvent(links[0], calid);
}

function getCurrentDate(date_array)
{
	if ( date_array == null ){
		
		var next_link = document.getElementById('cal_next');
		var link_text = next_link.getAttribute('href');

		var query_array = link_text.split('?');
		var query_string = query_array[1];
		var items_array = query_string.split('&');

		var month = items_array[0].split('=')[1];
		var year = items_array[1].split('=')[1];
		
		if ( month == 1 ){
			cur_month = 12;
			cur_year = (year*1) - 1;
		} else {
			cur_month = (month*1) - 1;
			cur_year = year;
		}
		
	}
}

function showCalendar(dir)
{

	var url = ''
	var new_class = '';
	if ( dir == 'next' ){
		
		if ( cur_month == 12 ){
			var new_month = 1;
			var new_year = (cur_year*1) + 1;
		} else {
			var new_month = (cur_month *1)+ 1;
			var new_year = cur_year;
		}
		
	} else {
		
		if ( cur_month == 1 ){
			var new_month = 12;
			var new_year = (cur_year*1) - 1;
		} else {
			var new_month = (cur_month*1) - 1;
			var new_year = cur_year;
		}
		
	}
	
	cur_month = new_month;
	cur_year = new_year;
	
	url = 'calendar.php?month='+new_month+'&year='+new_year+'&a=1';
	var req = makeRequest(url);
	
	req.open('GET', url, true);
	req.send();
	
	req.onreadystatechange = function() { 
		if (req.readyState == 4) {
			if (req.status == 200) {
				
				var newcal = req.responseText;
				var sidebar = document.getElementById('sidebar');
				var holders = sidebar.getElementsByClassName('cal_holder');
				
				var cur_cal = holders[0];
				removeChildren(cur_cal);
				cur_cal.innerHTML= newcal;
				
			}
		}
	}
	
	
	return false;
}

function getInputs()
{
	var inputs = document.getElementsByTagName('input');
	var input_num = inputs.length;
	
	for ( var x=0; x < input_num; x++ ){
		
		var input_type = inputs[x].getAttribute('type');
		var input_class = inputs[x].getAttribute('class');
		
		if ( input_type == 'text' && input_class == 'no_focus' ){
			
			if ( inputs[x].value == '' ){
				inputs[x].value = inputs[x].getAttribute('title');
			}
			
			inputs[x].onfocus = function(){
				clearField(this);
			}
		}
	}
	
	var textareas = document.getElementsByTagName('textarea');
	var text_num = textareas.length;
	for ( var x=0; x < text_num; x++ ){
		var text_class = textareas[x].getAttribute('class');
		
		if ( text_class == 'no_focus' ){
			
			if ( textareas[x].value == '' ){
				textareas[x].value = textareas[x].getAttribute('title');
			}
			
			textareas[x].onfocus = function(){
				clearField(this);
			}
		}
	}
}

function getLinks()
{
	
	if (document.getElementById('navigation')){
		var nav = document.getElementById('navigation');
		var links = nav.getElementsByTagName('a');
		var link_num = links.length;
		for ( var x=0; x < link_num; x++ ){
			if ( links[x].className == 'menu' ){
				links[x].onclick = function(e){
					if (!e) var e = window.event;
					e.cancelBubble = true;
					if (e.stopPropagation) e.stopPropagation();
					return openClose(this);
				}
			}
		}
	}
	
	if ( document.getElementById('shop_div') ){

		var shopDiv = document.getElementById('shop_div');
		var links = shopDiv.getElementsByTagName('a');
		var link_num = links.length;
		for ( var x=0; x < link_num; x++ ){
			if ( links[x].className == 'showDesc' ){
				links[x].onclick = function(e){
					if (!e) var e = window.event;
					e.cancelBubble = true;
					if (e.stopPropagation) e.stopPropagation();
					return showDesc(this);
				}
			}
		}
	}
	
	if ( document.getElementById('newsletter') ){
		var mlist = document.getElementById('newsletter');
		mlist.onclick = function(e){
			if (!e) var e = window.event;
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
			showList();
			return false;
		}
	}
}

function showList()
{
	if ( document.getElementById('mailing_list') ){
		var mlist = document.getElementById('mailing_list');
		
		var disp_status = mlist.style.display;
		
		if ( disp_status == '' || disp_status == 'none' ){
			hidemenus();
			mlist.style.display = 'block';
			visibleObjects.push(mlist);
		} else {
			hidemenus();
		}
		
		
		mlist.onclick = function(e)
		{
			if (!e) var e = window.event;
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
		}
		
		return false;	
	}
}

function hideList()
{
	var mlist = document.getElementById('mailing_list');
	mlist.style.display = 'none';
	return false;	
}

function getEvent(link, eventId)
{
	
	hidemenus();
	
	if ( eventId == null ){
		var eventId = link.getAttribute('data-calid');
	}
	
	var url = 'event.php?id='+eventId;
	var req = makeRequest(url);
	
	req.onreadystatechange = function() { 
		if (req.readyState == 4) {
			if (req.status == 200) {
				
				var cell = link.parentNode;
				var posArray = findPos(cell);
				var leftpos = posArray[0] - 300;
				var	toppos = posArray[1] - 58;
				
				if ( document.getElementById('cal_date') ){
					var cal_date = document.getElementById('cal_date');

					
					cal_date.innerHTML = req.responseText;
					cal_date.style.left = leftpos+'px';
					cal_date.style.top = toppos+'px';
					
					var point = document.createElement('img');
					point.setAttribute('src', 'images/point.png');
					point.setAttribute('alt', '');
					point.setAttribute('id', 'point');
					cal_date.appendChild(point);
					
					var close_link = document.createElement('a');
					close_link.setAttribute('src', '#');
					close_link.onclick = function(){
						closeDate();
						return false
					}
					
					var close_button = document.createElement('img');
					close_button.setAttribute('src', 'images/close.png');
					close_button.setAttribute('class', 'close');
					
					close_link.appendChild(close_button);
					cal_date.appendChild(close_link);
					cal_date.style.display = 'block';
					
				} else {
					
					var point = document.createElement('img');
					point.setAttribute('src', 'images/point.png');
					point.setAttribute('alt', '');
					point.setAttribute('id', 'point');
					
					var cal_date = document.createElement('div');
					cal_date.setAttribute('id', 'cal_date');
					cal_date.innerHTML = req.responseText;
					cal_date.style.left = leftpos+'px';
					cal_date.style.top = toppos+'px';
					
					cal_date.appendChild(point);
					
					var close_link = document.createElement('a');
					close_link.setAttribute('src', '#');
					close_link.onclick = function(){
						closeDate();
						return false
					}
					
					var close_button = document.createElement('img');
					close_button.setAttribute('src', 'images/close.png');
					close_button.setAttribute('class', 'close');
					
					close_link.appendChild(close_button);
					cal_date.appendChild(close_link);
					
					cal_date.onclick = function(e)
					{
						if (!e) var e = window.event;
						e.cancelBubble = true;
						if (e.stopPropagation) e.stopPropagation();
					}
					
					document.body.appendChild(cal_date);
					
				}
				visibleObjects.push(cal_date);
				
			} else {
				//alert('There was a problem with the request.');
			}
		}
	};
	req.open('GET', url, true);
	req.send(null);
	return false;
	
	return false;
}

function closeDate()
{
	if ( document.getElementById('cal_date') ){
		var cal_date = document.getElementById('cal_date');
		document.body.removeChild(cal_date);
	}
}

function makeRequest(url) {
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		/*if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}*/
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	return http_request;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		return [curleft,curtop];
	}
}

function hidemenus()
{

	var menus = document.getElementsByTagName('ul');
	var menu_num = menus.length;

	for ( var n=0; n < menu_num; n++ ){
		if ( menus[n].className == 'menu' ){
			menus[n].style.display = 'none';
		}
	}
	
	var stop = visibleObjects.length;

	for ( var x=0; x < stop; x++ ){		
		visibleObjects[x].style.display = 'none';
	}
	visibleObjects.length = 0;
}

function hideVisibleObjects()
{
	var stop = visibleObjects.length;

	for ( var x=0; x < stop; x++ ){		
		visibleObjects[x].style.display = 'none';
	}
	visibleObjects.length = 0;
}

function hideothers(obj)
{
	var menus = document.getElementsByTagName('ul');
	var menu_num = menus.length;

	for ( var n=0; n < menu_num; n++ ){
		if ( menus[n].className == 'menu' && menus[n] != obj ){
			menus[n].style.display = 'none';
		}
	}
}


function openClose(obj){
	hideVisibleObjects();
	var parent = obj.parentNode;
	var childList = parent.getElementsByTagName("ul");

	if (childList[0].style.display == "none" || childList[0].style.display == "" ){
		childList[0].style.display = "block";
	} else {
		childList[0].style.display = "none";
	}
	hideothers(childList[0]);
	
	return false;
}

function clearField(input)
{
	
	var title = input.getAttribute('title');
	
	if ( input.value.toLowerCase() == title.toLowerCase() ){
		input.value = '';
	}
	input.onblur = function(){
		return restoreText(input, title);
	}
	input.setAttribute('class', 'has_focus');
	input.className = 'has_focus';
}

function restoreText(input, defText){
	if ( input.value == '' ){
		input.value = defText;
		input.setAttribute('class', 'no_focus');
		input.className = 'no_focus';
	}
	
}

function doSignup(form)
{

	//collect values
	var first_name = form.first_name.value;
	var last_name = form.last_name.value;
	var email = form.email.value;
	var country = form.country.value;
	var state = form.state.value;
	var phone = form.phone.value;
	var city = form.city.value;
	
	var params = 'first_name='+first_name+'&last_name='+last_name+'&email='+email+'&state='+state+'&country='+country+'&city='+city+'&phone='+phone;
	
	var url = 'quick_signup.php';
	var req = makeRequest(url);
	
	req.open('POST', url, true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.send(params);
	
	req.onreadystatechange = function() { 
		if (req.readyState == 4) {
			if (req.status == 200) {
				
				if ( document.getElementById('mailing_list') ){
					var specsheet = document.getElementById('mailing_list');
					specsheet.innerHTML = req.responseText;
					getInputs();
					
					var close_button = document.createElement('img');
					close_button.setAttribute('src', 'images/close.png');
					close_button.setAttribute('class', 'close');
					
					var close_link = document.createElement('a');
					close_link.setAttribute('href', '#');
					close_link.appendChild(close_button);
					
					close_link.onclick = function(){
						hideList();
						return false;
					}
					
					specsheet.appendChild(close_link);
					
					
				}
				
			}
		}
	};
	
	return false;
}

function makeRequest(url) {
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		/*if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}*/
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	return http_request;
}

function removeChildren(obj)
{
	while(obj.hasChildNodes())
	{
		obj.removeChild(obj.lastChild);
	}
}

function showDesc(obj)
{
	var item_div = obj.parentNode.parentNode;
	var descs = getElementsByClassName('item_description', 'p', item_div);
	var description = descs[0];
	description.onclick = function(e){
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	}
	
	var display_status = description.style.display;
	
	if ( display_status == '' || display_status == 'none' ){
		hidemenus();
		description.style.display = 'block';
		visibleObjects.push(description);
	} else {
		hidemenus();
	}
	
	
	return false;
}

function hideDesc(obj)
{
	obj.parentNode.style.display = 'none';
	return false;
}

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/	
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

function addToCart(form)
{
	var itemID = form.item_id.value;
	var sid = getCookie('PHPSESSID');
	
	var url = 'cart_add.php';
	var req = makeRequest(url);
	var params = 'id='+itemID+'&sid='+sid;
	
	req.open('POST', url, true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.send(params);
	
	req.onreadystatechange = function() { 
		if (req.readyState == 4) {
			if (req.status == 200) {
				
				var answer = parseInt(req.responseText);
				
				var conf = document.createElement('div');
				conf.setAttribute('id', 'cart_confirmation');
				var div = form.parentNode;
				var names = getElementsByClassName('item_name', 'span', div);
				var item_name = names[0].firstChild.data;
				
				if ( answer == 1 ){
					
					/*var buttons = div.getElementsByClassName('submit_btn', 'input', div);
					buttons[0].value = 'Remove From Cart';
					form.onsubmit = function(){
						removeFromCart(this);
						return false;
					}*/
					
					var cart_count = document.getElementById('cart_count');
					var links =  cart_count.getElementsByTagName('span');
					var cart_link = links[0];
					var new_num = parseInt(cart_link.firstChild.data) + 1;
					removeChildren(cart_link);
					if ( new_num == 1 ){
						var new_text = new_num+' item';
					} else {
						var new_text = new_num+' items';
					}
					
					var cart_count = document.getElementById('cart_count2');
					var links2 =  cart_count.getElementsByTagName('span');
					var cart_link2 = links2[0];
					removeChildren(cart_link2);
					
					cart_link2.appendChild(document.createTextNode(new_text))
					cart_link.appendChild(document.createTextNode(new_text))
					
					
					conf.innerHTML = '<h2>'+item_name+'</h2><p>This item has been added to your cart</p><p><a href="cart.php">View Shopping Cart</a></p>';
					
				} else if ( answer == 2 ){
					
					conf.innerHTML = '<h2>'+item_name+'</h2><p>This item is already in your cart</p><p><a href="cart.php">View Shopping Cart</a></p>';
					
				} else if ( answer == 3 ){
					
					conf.innerHTML = '<h2>'+item_name+'</h2><p>This item could not be added to your cart.  Please try again later</p><p><a href="cart.php">View Shopping Cart</a></p>';
					
				}
				
				var point = document.createElement('img');
				point.setAttribute('src', 'images/point_left.png');
				point.setAttribute('alt', '');
				point.setAttribute('id', 'point');
				conf.appendChild(point);
				
				var close_button = document.createElement('img');
				close_button.setAttribute('src', 'images/close.png');
				close_button.setAttribute('class', 'close');
				
				var close_link = document.createElement('a');
				close_link.setAttribute('href', '#');
				close_link.appendChild(close_button);
				
				close_link.onclick = function(){
					removeThis(this);
					return false;
				}
				
				conf.appendChild(close_link);
				
				conf.onclick = function(e){
					if (!e) var e = window.event;
					e.cancelBubble = true;
					if (e.stopPropagation) e.stopPropagation();
				}
				visibleObjects.push(conf);
				form.parentNode.appendChild(conf);
				
			}
		}
	};
	
	return false;
}

function removeFromCart(form)
{
	alert('this will remove stuff');
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function removeThis(obj)
{
	var div = obj.parentNode;
	div.parentNode.removeChild(div);
}


