//////////////////// scroll ////////////////////////////////////////////////////////////////////////////
jQuery.getPos = function (e)
{
	var l = 0;
	var t  = 0;
	var w = jQuery.intval(jQuery.css(e,'width'));
	var h = jQuery.intval(jQuery.css(e,'height'));
	var wb = e.offsetWidth;
	var hb = e.offsetHeight;
	while (e.offsetParent){
		l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
		t += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
		e = e.offsetParent;
	}
	l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
	t  += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
	return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};
jQuery.getClient = function(e)
{
	if (e) {
		w = e.clientWidth;
		h = e.clientHeight;
	} else {
		w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
		h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
	}
	return {w:w,h:h};
};
jQuery.getScroll = function (e) 
{
	if (e) {
		t = e.scrollTop;
		l = e.scrollLeft;
		w = e.scrollWidth;
		h = e.scrollHeight;
	} else  {
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
			l = document.documentElement.scrollLeft;
			w = document.documentElement.scrollWidth;
			h = document.documentElement.scrollHeight;
		} else if (document.body) {
			t = document.body.scrollTop;
			l = document.body.scrollLeft;
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
	}
	return { t: t, l: l, w: w, h: h };
};

jQuery.intval = function (v)
{
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};

jQuery.fn.ScrollTo = function(s) {
	o = jQuery.speed(s);
	return this.each(function(){
		new jQuery.fx.ScrollTo(this, o);
	});
};

jQuery.fx.ScrollTo = function (e, o)
{
	var z = this;
	z.o = o;
	z.e = e;
	z.p = jQuery.getPos(e);
	z.s = jQuery.getScroll();
	z.clear = function(){clearInterval(z.timer);z.timer=null};
	z.t=(new Date).getTime();
	z.step = function(){
		var t = (new Date).getTime();
		var p = (t - z.t) / z.o.duration;
		if (t >= z.o.duration+z.t) {
			z.clear();
			setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
		} else {
			st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
			sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
			z.scroll(st, sl);
		}
	};
	z.scroll = function (t, l){window.scrollTo(l, t)};
	z.timer=setInterval(function(){z.step();},13);
};


$(document).ready(function(){
	$(".nav")
	.superfish({
		animation : {opacity:"show",height:"show"},
		delay : 40
	});
	
	$('a[rel*=link]').click(function(){
		window.open(this.href);
        return false;
    });
	
	$('.back_to_top a').click(function(){
		$('#wrapper').ScrollTo(800);
		return false;
	});

});


jQuery.fn.reset = function() {
	this.each(function(){
		if($(this).is('form')) {
			var button = jQuery(jQuery('<input type="reset" />'));
			button.hide();
			$(this).append(button);
			button.click().remove();
		} else if($(this).parent('form').size()) {
			var button = jQuery(jQuery('<input type="reset" />'));
			button.hide();
			$(this).parent('form').append(button);
			button.click().remove();
		} else if($(this).find('form').size()) {
			$(this).find('form').each(function(){
				var button = jQuery(jQuery('<input type="reset" />'));
				button.hide();
				$(this).append(button);
				button.click().remove();
			});
		}
	})
	return this;
};

function ajaxSubmitForm(){
								
	$(".error").remove();
	var hasError = false;
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var phoneReg = /\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/;
	
	var fullNameVal = $("#ClientName").val();
	if(fullNameVal == '') {
		$("#ClientName").after('<strong class="error">Name required</strong>');
		$("#ClientName").focus();
		hasError = true;
	}
	
	var cityVal = $("#City").val();
	if(cityVal == '') {
		$("#City").after('<strong class="error">City required</strong>');
		$("#City").focus();
		hasError = true;
	}
	
	
	var emailAddressVal = $("#Email").val();
	if(!emailReg.test(emailAddressVal)) {	
		$("#Email").after('<strong class="error">Invalid email address</strong>');
		$("#Email").focus();
		hasError = true;
	}
	
	var phoneVal = $("#CompanyPhone").val();
	if(phoneVal==''||!phoneReg.test(phoneVal)) {
		$("#CompanyPhone").after('<strong class="error">Invalid phone number</strong>');
		$("#CompanyPhone").focus();
		hasError = true;
	}
	
	
	if(hasError == false) {
		$("#ajaxWrapper").slideDown("slow", function(){
			//$(this).hide();
			//$("#sendEmail li.buttons").append('<img src="/images/universal/loading.gif" alt="Loading" id="loading" />');
			
			$(":checkbox, :radio").click(showValues);
			$("select").change(showValues);
			var formVARS = showValues();
			
			$.get("/scripts/injury_request.php?"+formVARS,
					function(data, textStatus){					
						$("#ajaxWrapper").html(data+'<br /><a href="#" id="closeButton">Close</a>');
						if(textStatus=='success'){
							$("form").reset();
						}						
						$('#closeButton').click(function(e){
    						e.preventDefault();
    						closeAjax();
						});
					}
			 );
		})
	};
	
	return false;
}

function ajaxContactForm(){
								
	$(".error").remove();
	var hasError = false;
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	
	var fullNameVal = $("#formName").val();
	if(fullNameVal == '') {
		$("#formName").after('<strong class="error">Name required</strong>');
		hasError = true;
	}
	
	
	var emailAddressVal = $("#formEmail").val();
	if(!emailReg.test(emailAddressVal)||emailAddressVal=='') {	
		$("#formEmail").after('<strong class="error">Invalid email address</strong>');
		hasError = true;
	}
	
	var messVal = $("#formMessage").val();
	if(messVal=='') {
		$("#formMessage").after('<strong class="error">Message required</strong>');
		hasError = true;
	}
	
	
	if(hasError == false) {
		$("#ajaxWrapper").slideDown("slow", function(){
			
			$(":checkbox, :radio").click(showValues);
			$("select").change(showValues);
			var formVARS = showValues();
			
			$.get("/scripts/contact_form.php?"+formVARS,
					function(data, textStatus){					
						$("#ajaxWrapper").html(data+'<br /><a href="#" id="closeButton">Close</a>');
						if(textStatus=='success'){
							$("form").reset();
						}						
						$('#closeButton').click(function(e){
    						e.preventDefault();
    						closeAjax();
						});
					}
			 );
		})
	};
	
	return false;
}

function closeAjax(){
	if($("#ajaxWrapper")) $("#ajaxWrapper").slideUp("slow",function(){$(this).empty()});
	return false;
}

function showValues() {
	var str = $("form").serialize();
	return str;
}