/* =Load Functions
-----------------------------------------------------------------------------*/
var URL;
jQuery(document).ready(function(){
	URL = getURL();
	doNewWin();
	linkIcons();
	psutoClass();
	// form ajax
	if($("body.form").length>0) ajaxForm();
	// add class to current page link in nav
	$("#nav li a[href*='"+URL.file+"'], #nav2 li a[href*='"+URL.file+"']").parent().addClass("curPage");
	$("#outer a").each(function(){
		if(!empty(URL.param) && $(this).attr("href").match(URL.param)) $(this).addClass("curPage");
		if(!empty(URL.anch) && $(this).attr("href").match(URL.anch)) $(this).addClass("curPage");
		if(empty(URL.param)){
			if($(this).is(".checkPage") && $(this).attr("href").match(URL.file)) $(this).addClass("curPage");
		}
	});
	if($(".addShad").length>0) addShadows();
	if($(".caption").length>0) addCaption();
	
	if($.browser.mozilla) $(".width li").css({width:"100%"});
	if($(".photochange a").length>0){
		$(".photochange a").toggle(
			function(){
				var src = "photos/restorationBefore.jpg";
				$(".photochange").find("img").attr("src",src);
				$(this).html("View After").parent("p").find("span.baChange").html("Before Restoration");
				return false;
			},
			function(){
				var src = "photos/restorationAfter.jpg";
				$(".photochange").find("img").attr("src",src);
				$(this).html("View Before").parent("p").find("span.baChange").html("After Restoration");
				return false;
			}
		);
	}
	if($("#bar li a img").length>0){
		$("#bar li a").click(
			function(){
				var src = "photos/"+stringVar($(this).find("img").attr("class"),'src')+".jpg";
				$(".photochange").find("img").attr("src",src);
				$(".photochange a").html("Change Back to Before/After Restoration").parent("p").find("span.baChange").html("");
				return false;
			}
		);
	}
	if($("#mapLinks li a").length>0){
		$("#mapLinks li a").lightbox();
	}
});
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];
		var lArr = l.split("?");
		var param = (lArr[1]) ? lArr[1].split("#")[0] : '';
		var anch = (l.split("#")[1]) ? l.split("#")[1] : '';
		l = lArr[0].split("#")[0];
		var file = (empty(l)) ? 'index.php' : l;
	}else{ var file = 'index.php'; }
	return {
		cur:	http+loc.split("://")[1],
		domain:	document.domain,
		base:	http+document.domain+"/",
		file:	file,
		param:	param,
		anch:	anch
	}
}

/* =Open a new window
-----------------------------------------------------------------------------*/
function doNewWin() {
	$("a[href^='http://']").addClass("newWin");
	$("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;});
	});
}
function linkIcons(){
	$("#outer a[href$='.pdf']").each(function(){
		var x = $(this);
		var linkTitle = "(Download PDF)";
		var newTitle = (empty(x.attr("title"))) ? linkTitle : x.attr("title")+" "+linkTitle;
		//x.addClass("pdf").attr("title", newTitle);
		x.click(function(){window.open(x.attr("href"));return false;});
	});
	// new window icon in doNewWin()
}


/* Add Shadows
-----------------------------------------------------------------------------*/
function addShadows(){
	$(".addShad").each(function(){
		var thisFloat = $(this).css("float");
		var fl = (thisFloat=='right')?'R':'L';
		var cl = $(this).css("clear");
		var w = $(this).width()+4;
		var h = $(this).height()+4;
		var marL = $(this).css("margin-left");
		var marR = $(this).css("margin-right");
		var marT = $(this).css("margin-top");
		var marB = 	$(this).css("margin-bottom");	
		$(this).css({margin:"0px", float:"none", clear:"none"});
		$(this).wrap("<div class='imgShaddow float"+fl+"'style='"+(($.browser.msie/*IE Double margin bug fix*/)?'display:inline; ':'')+"clear:"+cl+"; margin-left:"+marL+"; margin-right:"+marR+"; margin-top:"+marT+"; margin-bottom:"+marB+"; background:url(images/sh-rTl.gif) right top repeat-y;'>"+
					 "<div style='background:url(images/sh-bTl.gif) left bottom repeat-x;'>"+
					 "<div style='background:url(images/sh-brC.gif) right bottom no-repeat;'>"+
					 "<div style='background:url(images/sh-tr.gif) right top no-repeat;'>"+
					 "<div style='width:"+w+"px; height:"+h+"px; background:url(images/sh-bl.gif) left bottom no-repeat;'>"+
					 "</div></div></div></div></div>");
	});
};

function addCaption(){
	$(".caption").each(function(){
		var title = $(this).attr("title");
		if(empty(title) || title == undefined) title = $(this).attr("alt");
		if(!empty(title) && title != undefined){
			var outer = ($(this).parents(".imgShaddow").length > 0) ? $(this).parents(".imgShaddow") : $(this);
			var thisFloat = outer.css("float");
			var fl = (thisFloat=='right')?'R':'L';
			var cl = outer.css("clear");
			var w = outer.width();
			var h = outer.height();
			var marL = outer.css("margin-left");
			var marR = outer.css("margin-right");
			var marT = outer.css("margin-top");
			var marB = outer.css("margin-bottom");
			outer.css({margin:"0px", float:"none", clear:"none"});
			outer.wrap("<div class='imgCaption float"+fl+"'style='"+(($.browser.msie/*IE Double margin bug fix*/)?'display:inline; ':'')+"clear:"+cl+"; margin-left:"+marL+"; margin-right:"+marR+"; margin-top:"+marT+"; margin-bottom:"+marB+";'></div>");
			outer.parents("div.imgCaption").append("<p class='thisImgCap'>"+title+"</p>");
		}
	});
};



/* =Add class "hover to all li's and buttons and add focus class
-----------------------------------------------------------------------------*/
function psutoClass(){
	$("li, button").hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover")});
	$("input, textarea, select")
		.focus(function(){$(this).addClass("focus");})
		.blur(function(){$(this).removeClass("focus")});
}

/* Pull a Variable wraped in "[]" from a string. 
 * EG: 
 * 		var string = "this is a string with a variable[value] wraped in brackets".
 * 		var variable = stringVar(string,'variable');
 * 		// variable == 'value';
 *
 * Works Well with Class Names
 *---------------------------------------------------------------------------*/
function stringVar(string,Var){
	var $return = ''; var findIndex = string.indexOf(Var+"[");
	if(findIndex>=0){
		var nameLeng = Var.length; var maxleng = string.length; var i = findIndex += nameLeng+1; var par = isok = true;
		while(par){var char = string.charAt(i);if(char === "]"){par = false;}else if(i>maxleng){isok = false; par = false;}i++;}
		if(isok){$return = string.substring(findIndex,i-1);}
	}
	return $return;
}

/* =Ajax Form & Interface
-----------------------------------------------------------------------------*/
function ajaxForm(){
	var $form = $("form:first");
	var formID = $form.attr("id");
	var $formID = '#'+formID;
	var formClass = $form.attr("class");
	var sendto = stringVar(formClass,'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);}
