//Initialize Shadowbox
//Shadowbox.loadSkin('dickey-john', '/_lib/js/shadowbox-2.0/src/skin');
//Shadowbox.loadLanguage('en', '/_lib/js/shadowbox-2.0/build/lang');
//Shadowbox.loadPlayer(['flv', 'html', 'iframe', 'img', 'qt', 'swf', 'wmp'], '/_lib/js/shadowbox-2.0/build/player');
//window.onload = Shadowbox.init;
$(window).load(function(){
	var options = {
		flvPlayer:'/_lib/js/shadowbox-2.0/flvplayer.swf',
		counterType:'skip'
	};
    Shadowbox.init(options);
});

//preload images
image1 = new Image();
image1.src = "/_img/arrow_down.gif";

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;}

//savings calculator
function calculateSavings(){
	input1=Number($("#input1").val());
	input2=Number($("#input2").val());
	input3=$("#input3").val();	
	bushels=Math.round(input1*input2);	
	switch (input3){		case "15.1-15.5%": penalty=0.06; break;
		case "15.6-16.0%": penalty=0.12; break;
		case "16.1-16.5%": penalty=0.18; break;
		case "16.6-17.0%": penalty=0.24; break;
		case "17.1-17.5%": penalty=0.30; break;
		case "17.6-18.0%": penalty=0.36; break;
		case "18.1-18.5%": penalty=0.43; break;
		case "18.6-19.0%": penalty=0.50; break;
		case "19.1-19.5%": penalty=0.57; break;
		case "19.6-20.0%": penalty=0.64; break;
		case "20.1-20.5%": penalty=0.71; break;
		case "20.6-21.0%": penalty=0.78; break;
		case "21.1-21.5%": penalty=0.86; break;
		case "21.6-22.0%": penalty=0.94; break;
		case "22.1-22.5%": penalty=1.02; break;
		case "22.6-23.0%": penalty=1.10; break;
		case "23.1-23.5%": penalty=1.18; break;
		case "23.6-24.0%": penalty=1.26; break;
		case "24.1-24.5%": penalty=1.34; break;
		case "24.6-25.0%": penalty=1.42; break;
		default: 
			penalty="undefined"; 
			$(".savingsResults").addClass("inactive").addClass("error"); 
			return;	}
	formula = (bushels*penalty).toFixed(2);
	if(formula==0){ result="0.00"; }else{ result = addCommas(formula); }
	$(".savingsResults").removeClass("inactive").removeClass("error");
	$("#resultNum").html(result);
	$("#output1").html(input3);
	$("#output2").html("$"+penalty.toFixed(2)+" penalty per bushel");
	$("#output3").html(addCommas(bushels)+" bushels of corn.");
	if(isNaN(formula)){ $(".savingsResults").addClass("inactive").addClass("error"); }
}

$(document).ready(function(){
	
	//tabs
	 /*$("ul#tabMenu").tabs();
	 $("ul#tabMenu").tabs("select", '#allResults');*/
	
	//sliding content over main content
	var contentHeight = $("div.contentArea.shadowRight .content").height();
	var inProgress = false;
	var sliderOpen = false;
	$("div.slideMainContent").fadeTo(1, 0.95);
	function toggleMainSlider(){
		$("div.slideMainContent").animate({ "width": "toggle"}, 400, complete);
	} 
	function resetMainContentToAuto(){
		$("div.contentArea.shadowRight .content").css({height:"auto"});	
		complete();			
	}
	function resetMainContent(){
		$("div.contentArea.shadowRight .content").animate({height:contentHeight},300,resetMainContentToAuto);
	}
	function complete(){
		inProgress=false;
	}
	resetMainContent();
	$("a.mainContent").click(function()	{
		if(!inProgress){					
			inProgress=true;
			//$("a.dropLink").removeClass("open").removeClass("closeThis").next("div.dropdownSlider").slideUp(200);
			if(!sliderOpen){
				contentHeight = $("div.contentArea.shadowRight .content").height();
				if($("div.slideMainContent").height() < contentHeight){
					$("div.slideMainContent").height(contentHeight);
					toggleMainSlider();							
				}else{							
					//$("div.contentArea.shadowRight").animate({height:$("div.slideMainContent").height()},300,toggleMainSlider);	
					$("div.contentArea.shadowRight .content").animate({height:$("div.slideMainContent").height()},300,toggleMainSlider);				}
				sliderOpen=true;
			}else if(sliderOpen){
				$("div.slideMainContent").animate({ "width": "toggle"}, 350,resetMainContent);
				sliderOpen=false;
			}					
		}
	});
	
	//accordian menus with dropdown content	
	$("a.dropLink").click(function()	{
		$(this).parent().children("a.open").addClass("closeThis");
		$(this).next("div.dropdownSlider").slideToggle(300).siblings("div.dropdownSlider").slideUp("slow");
		$(this).addClass("open");
		$(this).siblings().removeClass("open");					
		$(this).parent().children("a.closeThis").removeClass("open").removeClass("closeThis");
		$(this).parent().children("a.deselect").removeClass("open");
	});	
});