//$.noConflict();
$(document).ready(function() {
	//Preload images
	var imgNames= [ "smallcubicle","largecubicle","standardcubicle","smalloffice","largeoffice"
	,"standardoffice","executiveoffice","smallreception","largereception","SmallMeetingRoom"
	,"ConferenceRoom","BoardRoom","BreakRoom","serverroom","CopyRoom"];
	var imgObjects=[];
	for (var i=0; i<imgNames.length; i++){
		imgObjects[i]=new Image();
		imgObjects[i].src='images/space_cal' + imgNames[i] + '.jpg';
	}

	//Bind keyup and focus events to all space calculator inputs
	$("#spaceCalculator input").keyup(function() {
		//To calculate square footage, take the ceiling function of the quantity. If you need 1.5 offices, 1 wont' cut it, you need 2
		var sqft = Math.ceil($(this).val()) * parseInt($(this).attr("class"));
		//Make sure sqft is a number and is greater than 0
		if (isNaN(sqft) || sqft < 0)
			sqft=0;
		$(this).parent().siblings(".rowResult").html(sqft);
		calculateTotal();
	}).focus(function() {
		updateImage($(this).parent().next().next().children());
	});
	
	//fire the keyup method in case any values were saved in the form.
	$("#spaceCalculator input").keyup();	
	
	//Bind click event to room names
	$("#spaceCalculator td a").click(function() {
		updateImage(this);
		return false;
	});
	
});

/*----------------------------------------------------------------------------------------------------------------
function: calculateTotal
	calculateTotal adds up the totals for each room type and updates the grand total as well
	as the square foot input field for the get rates form.

	input:none
	output:none
--------------------------------------------------------------------------------------------------------------------*/
function calculateTotal(){
	var newTotal=0;
	$("td.rowResult").each(function(){
		newTotal+=parseInt($(this).html());
	});
	if(newTotal == 0)
		document.getElementById("ratesForm").style.display = 'none';
	else
		document.getElementById("ratesForm").style.display = '';
	$("#circulation").html(Math.ceil(newTotal*.20));
	$("#columnTotal").html(Math.ceil(newTotal*1.20));
	$("#sqftInput").val(Math.ceil(newTotal*1.20));
	validateCalc("sqftInput");
}


/*----------------------------------------------------------------------------------------------------------------
function: updateImage
	updateImage is an event handler function that takes the html of the caller and uses it
	to update the space calculator image and title.

	input:Pointer to text node that identifies the type of room
	output:none
--------------------------------------------------------------------------------------------------------------------*/
function updateImage(that){ 
	
	var spaceIMG = $(that).attr('id');
	var spaceTitle = $(that).html();
	
	//Update the image title
	$("#room img").attr('alt',spaceTitle);
	//Update the image
	$("#room img").attr('src',"images/space_cal/" + spaceIMG + ".jpg");

}