/*=======================================================
	"root" is a variable that is defined by the template.
	Please use "root" before you call any image directly.
  =======================================================*/

/*calculations for tank calcuators */

/* getHopperHeight
 * xsl formula: =ROUND((TAN('HOPPER:'*PI()/180))*((+'DIAMETER:'-(+'OUTLET:'/12))/2),2)
 */
function getHopperHeight(hopper, diameter, outlet)
{
	return ( Math.tan(hopper * (Math.PI/180)) ) * ((diameter - (outlet / 12))/2 );
}

/* getHopperVolume (also used for deckVolume)
 * xsl formula: =ROUND((PI()*(+'HOPPER HEIGHT:'/12))*('DIAMETER:'^2+('DIAMETER:'*('OUTLET:'/12))+((+'OUTLET:'/12)^2)),1)
 * xsl formula: =ROUND((PI()*(+'DECK HEIGHT:'/12))*('DIAMETER:'^2+('DIAMETER:' *(+ 'CENTER DOME:'/12))+((+'CENTER DOME:'/12)^2)),1)
 * end result should round to the tenth
 */
function getHopperVolume(hopperHeight, diameter, outlet)
{
	return (Math.PI * (hopperHeight / 12)) * ( Math.pow(diameter,2) + (diameter * (outlet/12)) + (Math.pow((outlet/12), 2)) );
}

/* calc Shell Volume
 * xsl formula: =ROUND(+'STRAIGHT WALL H'*(PI()*('DIAMETER:'/2)^2),1)
 * end result should round to the tenth
 */
function getShellVolume(straightWall, diameter)
{
	return (straightWall * (Math.PI * Math.pow( (diameter / 2), 2 ) ));
}

/* getWorkingCapacity
 * xsl formula: =ROUND(+'LEVEL FULL:'-((2/3)*PI()*(('DIAMETER:'/2)^2)*((TAN('REPOSE:'*PI()/180))*('DIAMETER:'/2))),1)
 * end result should round to be an int
 */
function getWorkingCapacity(levelFull, diameter, repose)
{
	return (levelFull - ( (2/3) * Math.PI * ( Math.pow((diameter/2),2) * (Math.tan((repose*Math.PI)/180) ) * (diameter/2)  ) ));
}

/* calc Level full Capacity
 * xsl formula: ='HOPPER VOLUME:'+'SHELL VOLUME:'
 * end result should round to be an int
 */
function getLevelFull(hopperVolume, shellVolume)
{
	return (hopperVolume + shellVolume);
}

/* ROUNDING FUNCTIONS */
function roundToInt(num) { return Math.round(num); }
function roundToTenth(num) { return Math.round( num * 10) / 10; }
function roundToHundredth(num) { return Math.round( num * 100) / 100; }

/* Change measurement units */
function changeMeasurementUnit()
{
	//alert("Trying to change measurement unit");
	$(".tankCalculator .unit").each(function()
	{
		curText = $(this).text();
		switch(curText)
		{
			case "(feet)":
				$(this).text("(meters)"); break;
			case "(inches)":
				$(this).text("(millimeters)"); break;
			case "(meters)":
				$(this).text("(feet)"); break;
			case "(millimeters)":
				$(this).text("(inches)"); break;
			case "feet": 
				$(this).text("meters"); break;
			case "meters":
				$(this).text("feet"); break;
			default: break;
		}
	});
}

// events for selecting a new measurment unit in the calculators
function measurementUnitChange()
{
	if( $("#unitSelector").length )
	{
		var previousUnit = $("#unitSelector").val();
				
		$("#unitSelector").change(function() 
		{
			//do not switch eachtime the change() function is called to update the calc, instead
			//make sure the previous input is different from this one.
			//then update fields in the "calcInput"
			if(previousUnit != $(this).val())
			{
				//alert("previousUnit = "+previousUnit+"\ncurrentUnit = "+$(this).val())
				previousUnit =  $(this).val();
				changeMeasurementUnit();
				convertInputs();				
			}
		});
	}
}
//events for changing the tank type (skirted tanks)
function tankTypeChange()
{
	//change type of tank (bolted or welded)
	if( $("#tankType").length ){
	$("#tankType").change( function()
	{	
		if($("#tankType").val() == "bolted")
		{	
			//update diameter
			$(".diameter option").each(function(){ $(this).remove(); });
			$(".diameter").append(""+
			"<option value='8'>8</option>\n"+
			"<option value='8.5'>8.5</option>\n"+
			"<option value='9'>9</option>\n"+
			"<option value='9.5'>9.5</option>\n"+
			"<option value='10'>10</option>\n"+
			"<option value='10.5'>10.5</option>\n"+
			"<option value='11'>11</option>\n"+
			"<option value='11.5'>11.5</option>\n"+
			"<option value='11.92'>11.92</option>\n"+
			"<option value='12.5'>12.5</option>\n"+
			"<option value='12.92'>12.92</option>\n"+
			"<option value='13.5'>13.5</option>\n"+
			"<option value='13.92'>13.92</option>\n"+
			"<option value='14.5'>14.5</option>\n"+
			"<option value='14.92'>14.92</option>\n");
			//update eaveHeight
			$(".eaveHeight option").each(function(){ $(this).remove(); });
			$(".eaveHeight").append(''+
			'<option value="5.92">5.92</option>\n' +
			'<option value="11.84">11.84</option>\n' +
			'<option value="17.76">17.76</option>\n' +
			'<option value="23.68">23.68</option>\n' +
			'<option value="29.60">29.60</option>\n' +
			'<option value="35.52">35.52</option>\n' +
			'<option value="41.44">41.44</option>\n' +
			'<option value="47.36">47.36</option>\n' +
			'<option value="53.28">53.28</option>\n' +
			'<option value="59.20">59.20</option>\n' +
			'<option value="65.12">65.12</option>\n' +
			'<option value="71.04">71.04</option>\n' +
			'<option value="76.96">76.96</option>\n' +
			'<option value="82.88">82.88</option>\n');
		}
		if($("#tankType").val() == "welded")
		{
			$(".diameter option").each(function(){ $(this).remove(); });
			$(".diameter").append(""+
			"<option value='9'>9</option>\n"+
			"<option value='12'>12</option>\n"+
			"<option value='15'>15</option>\n"+
			"<option value='18'>18</option>\n"+
			"<option value='21'>21</option>\n"+
			"<option value='26'>26</option>\n"+
			"<option value='29'>29</option>\n"+
			"<option value='32'>32</option>\n"+
			"<option value='38'>38</option>\n"+
			"<option value='41'>41</option>\n"+
			"<option value='47'>47</option>\n"+
			"<option value='55'>55</option>\n");
			//update eaveHeight
			$(".eaveHeight option").each(function(){ $(this).remove(); });
			$(".eaveHeight").append(''+
			'<option value="16.12">16.12</option>\n' +
			'<option value="24.18">24.18</option>\n' +
			'<option value="32.24">32.24</option>\n' +
			'<option value="40.30">40.30</option>\n' +
			'<option value="48.36">48.36</option>\n' +
			'<option value="56.42">56.42</option>\n' +
			'<option value="64.48">64.48</option>\n' +
			'<option value="72.54">72.54</option>\n' +
			'<option value="80.60">80.60</option>\n' +
			'<option value="88.66">88.66</option>\n' +
			'<option value="96.72">96.72</option>\n' +
			'<option value="104.78">104.78</option>\n' +
			'<option value="112.84">112.84</option>\n' +
			'<option value="120.90">120.90</option>\n' +
			'<option value="128.99">128.99</option>\n' +
			'<option value="137.02">137.02</option>\n');
		}
		
		if( measureUnit == "metric")	
			convertInputs();
		else
			calcTank();
	});
	}
}

/* Random images loader for site template (in header) */
function randomImage()
{
	var imgArr = ['3.jpg','5.jpg','6.jpg','8.jpg','10.jpg','11.jpg'];
	var rndNum = Math.floor( Math.random() * imgArr.length );
	
	$("#image_holder").fadeOut("fast", function () {
		$("#image_holder").css("background-image","url("+root+"img/img_rotator/" + imgArr[rndNum] + ")");
		$("#image_holder").fadeIn(3000);
	});
}

/*
	Flash rotation for home page
*/
function flashRotator()
{
	if($("#flashRotator").length)
	{
		$('#flashRotator').cycle({ 
			fx: 'fade',
			timeout: 8000,
			speed: 1000
		});
	}
}

//elegant Select
function elegantSelect()
{
	if( $(".elegantSelect").length )
	{		
		if( $(".appExamples").length )
		{
			$(".appExamples li").not(":first").hide();
			$("#appContent .appExamples li").css("borderWidth", "0px")
		}
		//click event
		$(".elegantSelect a").live("click", function()
		{
			var name = $(this).attr("href");
			name = name.substring( 1, (name.length) );
			
			//fadeout what is showing, when complete, find the correct
			// element and fade it in.
			$(".appExamples li").fadeOut(1000, function() {
				setTimeout ( function(){$(".appExamples li a[name='"+name+"']").parent().fadeIn("medium");}, 1200);
			});			
			return false;
		});
	}
}

// change the plus image
function Touch(myGraphic)
{
	x = document.getElementById(myGraphic).src;
	x = x.split('/');
	i = x.length-1;
	y = x[i];

	if (y == 'plus.gif'){
		document.getElementById(myGraphic).src = 'images/minus.gif';
	} else {
		document.getElementById(myGraphic).src = 'images/plus.gif';
	}
}


//DOM Loaded
$(document).ready(function () 
{
	//start random images
	randomImage();
	
	//init chrome menu
	cssdropdown.startchrome("main-nav");
	
	//init chrome menu for app pages
	if( $("#app_nav").length )
		cssdropdown.startchrome("app_nav");
		
	//init custom dropdown for app pages
	if( $(".elegantSelect").length )
		elegantSelect();
	
	//flash rotator for homepage
	flashRotator();
	
	//style scroll pane
	if( $('#pane1').length )
		$('#pane1').jScrollPane({showArrows:true, scrollbarWidth: 15, arrowSize: 16});
	
	if($("#info_frm").length){
		$("#info_frm").validate();
	}
	if($("#liq_req_frm").length){
		$("#liq_req_frm").validate();
	}
	if($("#frm").length){
		$("#frm").validate();
	}
	
	//calculator actions
	tankTypeChange();
	measurementUnitChange();

	
	//for submitting to the 'quick quote form'
	if($(".calculator_submit").length)
	{
		$(".calculator_submit").click(function(){
			//$(form) is assumed if there is a calculator_submit button
			$('form').submit();
		});
				
		$(".calculator_submit").mouseenter(function(){
			//$(form) is assumed if there is a calculator_submit button
			$(this).addClass("hover");
		}).mouseleave(function(){
			$(this).removeClass("hover");
		});
	}
	
	
});
