/* =Load Functions
-----------------------------------------------------------------------------*/
var URL;
jQuery(document).ready(function(){
	URL = getURL();
	doNewWin();
	listHover();
	if(document.body.className.match("form")){
		ajaxForm();
	}
	if($("#resources").length>0){
		$("#resources").find("h3").each(function(i){
			$(this).prepend("<span>&raquo;</span> ");
			$(this).addClass("drops");
			$(this).next("ul").hide();
			$(this).toggle(function(){
				$(this).next("ul").slideDown(500);
			},function(){
				$(this).next("ul").slideUp(500);
			});
		});
	}
});


/* =Setup 
-----------------------------------------------------------------------------*/
function getURL(){
	var http = (("https:" == document.location.protocol) ? "https://" : "http://");
	var wloc = window.location+'';
	var locarr = wloc.split("/");
	var locLeng = locarr.length-1;
	var loc = '';
	for(var i=0; i<locLeng; i++){
		loc += locarr[i]+"/";
	}
	if(!empty(locarr[locLeng])){
		var l = locarr[locLeng];
		l = l.split("?")[0].split("#")[0];
		var file = l;
	}else{ var file = 'index.php'; }
	return {
		cur: http+loc.split("://")[1],
		domain: document.domain,
		base: http+document.domain+"/",
		file: file
	}
}

/* =Open a new window
-----------------------------------------------------------------------------*/
function doNewWin() {
	$("#main a[href^='http://']").addClass("newWin");
	$("#main a[href*='"+URL.domain+"']").removeClass("newWin");
	$("a.newWin").each(function(){
		var x = $(this);
		var linkTitle = "(This link opens a new window)";
		var newTitle = (empty(x.attr("title"))) ? linkTitle : x.attr("title")+" "+linkTitle;
		x.attr("title", newTitle);
		x.removeClass("newWin").addClass("ext");
		x.click(function(){window.open(x.attr("href"));return false;});
	});
}


/* =Add class "hover to all li's and buttons
-----------------------------------------------------------------------------*/
function listHover(){
	$("li, button")
		.mouseover(function(){$(this).addClass("hover")})
		.mouseout(function(){$(this).removeClass("hover")});
}

/* =Ajax Form & Interface
-----------------------------------------------------------------------------*/
function ajaxForm(){
	var $form = $("form:first");
	var formID = $form.attr("id");
	var $formID = '#'+formID;
	var formClass = $form.attr("class");
	alert($formID);
	if(empty(formClass)) return false;
	var clArr = formClass.split(' ');
	alert(formClass);
	for(var i=0; i<clArr.length; i++){
		var w = clArr[i].match("do_");
		if(w){break;}
	}
	if(w != 'do_') return false;
	var sendto = clArr[i].split("_")[1];
	alert(sendto);
	sendto = (sendto == 'sendForm') ? 'includes/'+sendto : sendto;
	var options = {
		url:			sendto+'.php?ajax=true',
		target: 		'#formCanvas',
		beforeSubmit: 	function(formData, jqForm, options){ validateForm(formData, jqForm, options); },
		success: 		function(){ 
			$("label.error:first").children("input:first, textarea:first, select:first, radio:first, checkbox:first").focus();
			$.getScript(URL['cur']+"/js/behavior.js");
		}
	};
	$($formID).ajaxForm(options);
	UserFriendlyForm($formID);
	
}
function validateForm(formData, jqForm, options){
	var queryString = $.param(formData);
	//alert('About to submit: \n\n' + queryString);
	return true;
}
function UserFriendlyForm($formID){
	var $form = $($formID);
	// payment type
	var $payment = $form.find("select[name*=Payment_Method]");
	var payVal = $payment.val();
	paymentFN(payVal);
	if(payVal != undefined){
		paymentFN(payVal);
		// add on change event to pulldown
		$payment.change(function(){
			var payVal = $(this).val();
			paymentFN(payVal);
		});
	}
	$("form").find("button").click(function(){
		var b = $(this);
		var bwidth = b.width();
		var bhtml = b.html();
		b.after("<span class='loading' style='display:block; width:"+bwidth+"px;'>"+bhtml+"</span>");
		b.parent().children("button").hide();
		return true;
	});
}
function paymentFN(payVal){
	if(
	   payVal == 'Visa' ||
	   payVal == 'MasterCard' ||
	   payVal == 'Discover'
	   ){$("#creditInfo").show("fast");}
	else{$("#creditInfo").hide("fast");}
	if(payVal == 'check'){$("#checkInfo").show("fast");}
	else{$("#checkInfo").hide("fast");}
}
/* =Helper Functions
-----------------------------------------------------------------------------*/
function empty(x){	return ((x == '' || x == null) ? true : false);}
