$(document).ready(function(){
	
	var hashsearch=new RegExp(/#/);
	
	$('.page_menu li a, .scBoxIcon2 a').each(function(index) {

		if ($(this).attr('href').search(hashsearch) < 1) {
			$(this).attr('href', $(this).attr('href') + '#middle');
		};
	});
	
	$("#contact-form").submit(function(){

		var str = $(this).serialize();

		$.ajax({
			type: "POST",
			url: "http://geoshack.com/Scripts/contact.php",
			data: str,
			success: function(msg){
    
				$("#note").ajaxComplete(function(event, request, settings){

					if(msg == 'OK') {// Message Sent? Show the 'Thank You' message and hide the form
					
						result = '<div class="notification_ok">Your message has been sent. Thank you!</div>';
						$("#fields").hide();
					} else {
						result = msg;
					}

					$(this).html(result);
				});
			}
		});
		return false;
	});
	
	
	
	
	$('LI.drawer UL:not(:first)').hide(); // hide all ULs inside LI.drawer except the first one
	$('H2.drawer-handle').click(function () {
      // hide all the drawer contents
		$('LI.drawer UL:visible').slideUp().prev().removeClass('open');
      // show the associated drawer content to 'this' (this is the current H2 element)
      // since the drawer content is the next element after the clicked H2, we find
      // it and show it using this:
		$(this).addClass('open').next().slideDown();
    });



    $("#loader").fadeOut();	
	$('ul#waterwheelCarousel').fadeIn(5000);
	
	
	var interval;
	
	$("ul#waterwheelCarousel").roundabout({
		startingChild: 2,
		minOpacity: 1,
		minScale: 0.5
	}).hover(function() {
		// oh no, it's the cops!
		clearInterval(interval);
	},function() {
		// false alarm: PARTY!
		interval = startAutoPlay();
	});

	
	interval = startAutoPlay();
    
});


	var imgcounter=2;
	var imgtmparr=[];
	var descrip =[];
	var interval;
	
	imgtmparr[0]=72;
	imgtmparr[1]=22;
	imgtmparr[2]=21;
	imgtmparr[3]=23;
	imgtmparr[4]=24;
	imgtmparr[5]=26;
	imgtmparr[6]=27;
	imgtmparr[7]=73;
	imgtmparr[8]=25;
	
descrip[23]='<b>GeoShack Construction</b> offers the highest-quality support, total stations, construction lasers, layout equipment, auto levels and supplies... <a href="http://geoshack.com/construction">Learn More</a>';
descrip[24]='<b>GeoShack Survey</b> is your partner in adopting and utilizing the very best Topcon and Sokkia technology innovations for you to increase your productivity and profitability as jobsite demands change... <a href="http://geoshack.com/survey">Learn More</a>';
descrip[21]='<b>The GeoShack Machine Control Team</b> is the most experienced in the industry;  we are able provide a unique solution for your company.  Whether you require laser, sonic, robotic, GPS or mmGPS grade control;  the technology that best fits your job and your company can be installed, trained and supported by our team of experts... <a href="http://geoshack.com/machine-control">Learn More</a>';
descrip[26]='<b>GeoShack Paving</b> solutions helps you free your paving from the confines of string-lines and other linear references and move production to the fast lane... <a href="http://geoshack.com/paving">Learn More</a>';
descrip[27]='<b>GeoShack Imaging &amp; Scanning</b> Team offers the leading imaging solutions to make very accurate measurements in complicated environments. This technology is the best answer for measuring a variety of features inside and outside... <a href="http://geoshack.com/imaging-scanning/">Learn More</a>';
descrip[73]='<b>Geoshack Support</b> Team has repair centers in all 15 of our retail locations staffed by factory trained technicians, we strive to provide the highest level of service and support in the industry.  We understand that customers make money by using our products, not just owning them.  Whether in the field or in the service center, our goal is to provide the best possible solutions to our existing and future customers... <a href="http://geoshack.com/support">Learn More</a>';
descrip[72]='<b>GeoShack Precision Farming</b> offers innovative solutions to increase your productivity, such as GPS Guidance and Auto Steer, GPS based surveying and Machine Control and Laser based control for land forming, variable rate application control, spray boom section control and more - all from GeoShack... <a href="http://geoshack.com/precision-farming">Learn More</a>';
descrip[22]='<b>GeoShack DensityMAX</b> is a wireless enhanced GPS system that combines the reliability of Topcon GPS Guidance Systems and Rajant hardware with Carlson Landfill Software to maximize your landfill density... <a href="http://geoshack.com/landfill/">Learn More</a>';
descrip[25]='<b>GeoShack GIS</b> division integrates hardware, software, and data for capturing, managing, analyzing, and displaying all forms of geographically referenced information... <a href="http://geoshack.com/precision-farming">Learn More</a>';

	function add_bg( id, num  ){
		var change_item="li.menu-item-";
		$( change_item + num ).addClass('redBg');
		$('.fa_desc').html(descrip[num]);
	}
	
	function remove_bg( id, num  ){
		var change_item="li.menu-item-";
		$( change_item + num ).removeClass('redBg');
		$('.fa_desc').html(descrip[imgtmparr[imgcounter]]);
	}
	
	function add_description( id, num  ){
		imgcounter=id;
		$('.fa_desc').html(descrip[num]);						  
	}
	
		
	function startAutoPlay() {
	return setInterval(function() {
		imgcounter++;
		if( imgcounter>imgtmparr.length-1 ){
			imgcounter = 0;
		}
		tmp=imgtmparr[imgcounter];
		$("ul#waterwheelCarousel").roundabout_animateToPreviousChild();
		$('.fa_desc').html(descrip[tmp]);
		}, 3000);
	}


	function addCommas(nStr)
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}


	function airspaceCalc(){
		if( $("#waste_column").val().length > 0 )
		{
			var waste = parseInt($("#waste_column").val());
	var x = 43560*waste/27;
	x = Math.round(x*100)/100;
	x.toFixed(0);
	$("#net_airspace").val(x);
	var y = x + (x*0.1);
	$("#net_airspace2").val(y.toFixed(0));
		}
	}


	function net_airspace(){
		if( $("#net_airspace").val().length > 0 && $("#density").val().length > 0)
		{
			//Net Airspace per acre * Current Density density
			var net_airspace = parseInt($("#net_airspace").val());
			var density = parseInt($("#density").val());
			var x = density * net_airspace/2000 ;
			x = Math.round(x*100)/100;
			x.toFixed(0);
			$("#net_tons").val(x.toFixed(0));
			var x2 = x + (x*0.1);
			$("#net_tons2").val(x2.toFixed(0));
		}
	}

	function rev_acre()
	{
		if( $("#gate_rate").val().length > 0 && $("#net_tons").val().length > 0)
		{
			var gate_rate = parseInt($("#gate_rate").val());
			var net_tons1 = parseInt($("#net_tons").val());
			var x = gate_rate * net_tons1;
			x = Math.round(x*100)/100;
			x.toFixed(0);

			$("#rev_acre").val(x);
			var x3 = x + (x*0.1);
			$("#rev_acre2").val(x3.toFixed(0));
			
			var incRev = x3 - x;
			$("#inc_rev_acre").val(incRev.toFixed(0));
		}
	}

	function ebitdaAcre()
	{
		if( $("#rev_acre").val().length > 0 && $("#ebitda").val().length > 0)
		{
			var rev_acre = parseInt($("#rev_acre").val());
			var ebitda = parseInt($("#ebitda").val());
			var x = rev_acre * (ebitda/100);
			x = Math.round(x*100)/100;
			$("#ebitda_acre").val(x.toFixed(0));
			var x4 = x + (x*0.1);
			$("#ebitda_acre2").val(x4.toFixed(0));		
		}
	}

	function constructionCost()
	{
		if( $("#cost_acre").val().length > 0 && $("#net_tons").val().length > 0)
		{
			var cost_acre = parseInt($("#cost_acre").val());
			var net_tons = parseInt($("#net_tons").val());
			var x = cost_acre / net_tons;
			x.toFixed(0);
			x = Math.round(x*100)/100;
			x.toFixed(0);

			$("#construction_cost").val(x);
			$("#construction_cost2").val(x - (x*0.1));	

		}
	}
	function calculate()
	{
		airspaceCalc();
		net_airspace();
		rev_acre();
		ebitdaAcre();
		constructionCost();
		$("#finalCalc").fadeIn();
		$("#densityMax").fadeIn();
		
		var yAxis = parseInt($("#inc_rev_acre").val());
		
		$(".calc").each(function(){
			var currVal = $(this).val();
			$(this).val(addCommas(currVal));
		});
		
		
		showChart(parseInt($("#density").val()), yAxis);

	}

	function showChart(x,y)
	  {
		$("#sendResults").before('<div id="chart2"></div>');
		$("#chart2, #sendResults, .more_info").fadeIn();
		//var newY=  y + (y*0.15);
		var newY = (y/10) *10;
		newY+= y;
		var xticks = [[0,'0'], [5,'5%'],[10,'10%'], [15,'15%']];
		var yticks = [[0,'0.0'], [100000,'100k'], [200000,'200k'], [300000,'300k'], [400000,'400k'], [500000,'500k'], [600000,'600k'], [700000,'700k'], [800000,'800k'], [900000,'900k'], [1000000,'1,000k']];
		$.jqplot('chart2',  [[[0, 0],[10,y],[20,newY]]],
			{  axes:{yaxis:{min:0, max: 1000000, label: 'Incremental Revenue Per Acre', ticks:yticks},xaxis:{min:0,max:15, ticks:xticks, label: '% Density Improvement'}},
			series:[{color:'#5FAB78'}]
		});

		/*  $.jqplot.config.enablePlugins = true;
	    plot2 = $.jqplot('chart2',  [[[0, 0],[x,y]]],
				{ title:'',
				  axes:{yaxis:{min:0, max:y, label: 'Revenue per acre'},xaxis:{min:0, max:x, label: 'Density (increase)'}},
				  series:[{color:'#5FAB78'}]
			}); */
			$("#sendResults").show();
	  }
