// Config Options Needed for Entire Site, or Pages within site

var qtyBackgrounds = 24;
var backgroundPrefix = "http://squallco.com/";

// Functions

function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function isset(varname) {
	if(typeof( window[ varname ] ) != "undefined") return true;
	else return false;
}
$.fn.addPhone = function () {
	phoneNumber = $(this).text();
	$(this).html('<a href="tel:' + phoneNumber + '">' + phoneNumber + '</a>');
}
$.fn.resizeY = function () {
	var brY = $(window).height();
	var headerY = $("#header").height();
	var footerY = $("#footer").height();
	var contentY = $("#content").outerHeight();
	var resizeValue;
	var contentHeight = brY - (headerY + footerY);
	
	if ( contentHeight > contentY ) { resizeValue = contentHeight; } else { resizeValue = contentY; }
/*
	alert (
		   "\nwindow = " + brY + 
		   "\nheader = " + headerY +
		   "\nfooter = " + footerY +
		   "\ncontent = " + contentY +
		   "\ncontentHeight = " + contentHeight +
		   "\nresize value = " + resizeValue
	);
*/
	$(this).height(resizeValue);
}
$.fn.centerForm = function (vert,horiz) {
	if (horiz == undefined) { horiz = true; }
	if (vert == undefined) { vert = true; }
	if (vert) { this.css("top", (( $(window).height() - this.height() ) / 2 ) + "px"); }
	if (horiz) { this.css("left", ( $(window).width() - this.width() ) / 2 + "px"); }
	return this;
}
$.fn.handleSubmit = function () {
	$(this).submit(function() {
		var connect_data = $(this).serialize();
		$(this).find("button").attr("disabled","true");
		$.post ("sendmail.php",connect_data,function (msg) {
			alert (msg);
			if ( msg.indexOf('error') > 0  ) { 
				$("button").removeAttr("disabled");
			} else {
				window.location.href = '.';
			}
		});
		return false;	
	});
}
function resizeHeader() {
	brX = $(window).width();
	header_img_X = $("div#header h1#title").outerWidth();
	header_Y = $("div#header").outerHeight();
	header_intro_Y = $("div#header div#intro").height();
	header_intro_xPad = $("div#header div#intro").css("padding-left");
	$("div#header div#intro").css("padding-right",header_intro_xPad);
	header_intro_xPad = header_intro_xPad.substring(0,(header_intro_xPad.length)-2);
	header_intro_X = $("div#header div#intro").width();
	header_starthere_X = $("div#header div#starthere").outerWidth();
	
	intro_width = ( (brX - ( header_img_X + ( header_intro_xPad * 2 ) + header_starthere_X ) ));
	padding_Y = Math.floor ( ( header_Y - header_intro_Y ) / 2 );
	
	$("div#header div#intro").
	css({
		width :(intro_width)+"px",
		"padding-top": (padding_Y)+"px"
	});
}

function chooseContentPicture() {
	imageNumber=Math.floor(Math.random()*qtyBackgrounds)+1;   // Generates a number between 1 and 8
	imageUrl = backgroundPrefix+"img/background/back"+imageNumber+".jpg";
	return imageUrl;
	
}

function createBackground() {
	var imageUrl = chooseContentPicture();
	$("#page").bgStretcher({ images: [imageUrl], imageHeight: 600, imageWidth: 800, slideShow: false });
}


function whyMenu() {

// Set up Cursor changes for user notification of clickable area
	$(".menu h2").hover(function() { 
		$(this).css("cursor","pointer");
	}, function() {
		$(this).css("cursor","auto");
	});

// Set up click to load each page into appropriate container.
$(".menu h2").click(function() {
	var thisitem = $(this).parent();
	var theUrl = $(thisitem).find("a").attr("href");
	if (!$(thisitem).find(".why_explained").hasClass("selected")) {
		$(".loading").show().css("cursor","none");
		$.ajax({
			url: theUrl,
			success:function(html) {
				$(".menu").find(".why_explained").removeClass("selected").html("").hide().parent().find(".plus").html("+");
				$(thisitem).find(".why_explained").addClass("selected");
				$(thisitem).find(".why_explained").show().append(html);
				$(thisitem).find(".plus").html("&ndash;");
				resizeHeader();
				$("#backgroundwrap").resizeY();
				$(".loading").hide().css("cursor","auto");
			}
		});
		return false;
	} else { 
		$(thisitem).find(".why_explained").html("").hide().removeClass("selected");
		$(thisitem).find(".plus").html("+");
		resizeHeader();
		$("#backgroundwrap").resizeY();
		return false;
	}

	});
}


// RUN THESE AT STARTUP -- GLOBAL TO ALL PAGES
$(document).ready( function() {

	// Always run!
	if (!blog) {
		createBackground();
		$("#backgroundwrap").resizeY(); 
		resizeHeader();
	}

	// Conditional runs!

	/* MOBILE PHONE SITE CODE */
	if (mobileType) { 
		$("#footer .phone").addPhone();
		$("#content .phone").addPhone();
	};
	
	/* BASED ON PAGE TYPE */
	switch (pageType) {
		case 'home':
/*			$('div#featured').centerForm(false);	*/
			break;

		case 'contact':
			$("form#contact").handleSubmit();		
			$('div#contactform').centerForm();

			break;
		
		case 'why':	
			whyMenu();
			$(document).mousemove(function(e) {
				$(".loading").css("top",(e.pageY-16)+"px").css("left",(e.pageX-16)+"px");
			});
			break;
		
		case 'model_detail':
			equalHeight($(".threecol1"));
			equalHeight($(".threecol2"));
			$("backgroundwrap").resizeY();
			break;
			
		case 'buildone':
			$("form#buildone").handleSubmit();
			$("div#contactform").centerForm(false);
			break;
	}


});

// RUN THESE AT EACH PAGE RESIZE -- GLOBAL TO ALL PAGES

$(window).resize(function() { 
				  
	// Always run!
	if (!blog) {
		$("#backgroundwrap").resizeY();
		resizeHeader();
	}
	
	// Conditional runs!
	switch (pageType) {
		case 'contact':
			$('div#contactform').centerForm();
			break;
		case 'home':
			/* $('div#featured').centerForm(false); */
			break;
	}

});
