var orig_img_width = 1160;
var orig_img_height = 812;
var new_img_width, new_img_height
var doc_width, doc_height
var doc_orient, img_orient // l = landscape, p = portrait (squares treated like portrait)
var ul_num = 1;


$(document).ready(function(){
	
	$("#contact").validate({
		
		rules: {
					name: "required",
					email: {
						required: true,
						email: true
					}
				},
				messages: {
					name: " Please enter your name",
					email: " Please enter a valid email address"
				},
				errorElement: "em",
				errorPlacement: function(error, element) {
					var id_is = element.attr("id");
					// alert(id_is);
					error.appendTo( $('#fs-'+id_is+' label') );
			   	}
		
	});
	$("#relocate").validate({
		
		rules: {
					name: "required",
					address1: "required",
					city: "required",
					state: "required",
					zip: "required",
					email: {
						required: true,
						email: true
					}
				},
				messages: {
					name: " Please enter your name",
					address1: " Please enter your address",
					city: " Please enter your city",
					state: " Please enter your state",
					zip: " Please enter you zip code",
					email: " Please enter a valid email address"
				},
				errorElement: "em",
				errorPlacement: function(error, element) {
					var id_is = element.attr("id");
					// alert(id_is);
					error.appendTo( $('#fs-'+id_is+' label') );
			   	}
		
	});
	
	
	/* jquery commands here */
	//Background Image Sizing
	// If true, set to portrait, all else treated as landscape
	img_orient = (orig_img_height > orig_img_width) ? 'p' : 'l';

	// Get the browser window dimensions
	doc_width  = $(window).width();
	doc_height = $(window).height();

	// Do the background image resizing
	$(window).bind("resize", function() {
		calc_and_set_scale_bg_img();
	});
	
	// Wanna do this on page load too
	calc_and_set_scale_bg_img();
	// open external links in new window/tab
		$('a[rel="external"]').click(function(){
			window.open($(this).attr('href'));
			return false;
		});
		
	// open close the uni nav :)

	$('.sms-btn a').click(function(){
		if($.browser.msie && $.browser.version.substr(0,1)<8){
			$('#sms-wrap').toggle();
		}
		else{
			$('#sms-wrap').slideToggle("slow");
		}
		return false;
	});

	// search results returned
	$('#searchsiteform').submit(function(){
		$('#search-load').addClass('loading');
		$('#search-content ul, #search-content p, #search-load h1, #search-load ul, #search-load p').fadeOut('slow');
		var act  = $('#searchsiteform').attr('action');
		var vars = $('#searchsiteform').serialize();
		var cc = $('#searchsite').val();
		$.ajax({
			type: 'POST',
			url: act,
			data: vars,
			cache: false,
			dataType: 'html',
			success: function(searchResults){
				setTimeout(function(){
					$('#search-load').removeClass('loading').addClass('loaded').html(searchResults).fadeIn('slow');
					$('#search-load').hide().fadeIn('slow');
					}, 3000);
				}					
			});

		return false;
	});

	// inline search results returned
	$('#searchinlineform').submit(function(){
		$('#search-il-load').addClass('loading');
		$('#search-il-content ul, #search-il-content p, #search-il-load h1, #search-il-load ul, #search-il-load p').fadeOut('slow');
		var act  = $('#searchinlineform').attr('action');
		var vars = $('#searchinlineform').serialize();
		var cc = $('#searchinline').val();
		$.ajax({
			type: 'POST',
			url: act,
			data: vars,
			cache: false,
			dataType: 'html',
			success: function(searchResults){
				setTimeout(function(){
					$('#search-il-load').removeClass('loading').addClass('loaded').html(searchResults).fadeIn('slow');
					$('#search-il-load').hide().fadeIn('slow');
					}, 3000);
				}					
			});

		return false;
	});
	
	// clear search field
	$('#searchsite, #searchinline').focus(function(){
		if($(this).val() == $(this).attr('title')){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val($(this).attr('title'));
		}
	});
	
	// Call this on page load to get the carousel fade going
	setTimeout("fade_thru_carousels()", 5000);
});


// Fade through the carousel ul's
function fade_thru_carousels() {
	$('#carousel-'+ul_num).fadeOut(250, function() {
		ul_num++;
		if($('#carousel-'+ul_num).length == 0) { // element doesn't exist
			ul_num = 1;
		}
		$('#carousel-'+ul_num).fadeIn(250, function() {
			setTimeout("fade_thru_carousels()", 5000);
		});
	});
}

//Function for Image Sizing

function calc_and_set_scale_bg_img() {
	// Get the browser window dimensions
	doc_width  = $(window).width();
	doc_height = $(window).height();
	//is_doc_height = $(document).height();
	
	// If true, set to portrait, all else treated as landscape
	doc_orient = (doc_height > doc_width) ? 'p' : 'l';

	if(doc_orient == 'p') {

		new_img_width   = doc_width;
		new_img_height  = Math.floor(orig_img_height * (doc_width / orig_img_width));
	
		if(new_img_height < doc_height) {
			new_img_height = doc_height;
			new_img_width  = Math.floor(orig_img_width * (doc_height / orig_img_height));
		}

	} else if(doc_orient == 'l') {

		new_img_height = doc_height;
		new_img_width  = Math.floor(orig_img_width * (doc_height / orig_img_height));
	
		if(new_img_width < doc_width) {
			// alert('here')
			new_img_width = doc_width;
			new_img_height  = Math.floor(orig_img_height * (doc_width / orig_img_width));
		}

	}

	// Set the CSS properties for the image div
	// $('#img-scale-bg').css( { 'width' : new_img_width+'px', 'height' : new_img_height+'px' } );
	$('#img-scale-bg img').css( { 'width' : new_img_width+'px', 'height' : new_img_height+'px' } );

	return false;
}

