/* (c) Triton Showers 2008 */

$(document).ready(function() {
	$('a[@rel="external"]').attr('target', '_blank');
});

$(document).ready(writeUpCalculatorSelects).ready(clearBlankULs).ready(wireUpFeatureShowHideLinks).ready(setTabs);

function wireUpFeatureShowHideLinks(){

	$('.showHideButton').each(function(index){
		
		$(this).click(function(){
			var rowArray = this.title.split("-");
			hideShow(Number(rowArray[1]),Number(rowArray[2]));
			return false;
		});	
	});
}

function hideShow(startRow,endRow){
   var row=".row-";
   var showImage="/images/show.png";
   var hideImage="/images/hide.png";
   var showText="Show";
   var hideText="Hide";
   var headerStyle="background:#999999 url(/images/arrow_closed.png) no-repeat ;border-color:#333333;color:#FFFFFF;";
   var headerDiv = "#product-0-row-" + (startRow-1).toString();

  for(var r=startRow;r<endRow;r++){
	var rowClass=row + r;
	$(rowClass).slideToggle("slow");
	
	if(r==startRow){
                var first = '.row-' + (r-1).toString() + ' > .feature';
		var icon = '.row-' + (r-1).toString() + ' > .icon';	
		$(first).slideToggle("slow");
		$(icon).slideToggle("slow");
		
                var showHideImagePath = '.row-' + (r-1).toString() + ' a.showHideButton img.panelImage';	               
                if($(showHideImagePath).attr('src').indexOf(showImage)!=-1){
                   $(showHideImagePath).attr('src',hideImage);
                   //$('.row-' + (r-1).toString() + ' a.showHideButton').text(hideText);
                }
                else{
                   $(showHideImagePath).attr('src',showImage);	
		  // $(headerDiv).attr('style',headerStyle);	   
                   //$('.row-' + (r-1).toString() + ' a.showHideButton').text(showText);
                }
	}
  }
	  
}

function setTabs(){
   $('#featuresBox-2 a.showHideButton').each(function(index){
       if(index!=0){
        $(this).trigger('click');
       }
   });
}


/*
on calculator savings page on click of select
we prefill litre per min for shower and calculate
*/
function writeUpCalculatorSelects(){

	 $('#Product_1_select').change(function(){
	      // alert("in select");
	       $('#Product_1_flow').val($('#Product_1_select option:selected').val());
	       doCaculations();
	 });	

	 $('#no-uses_1').change(function(){
	       doCaculations();
	 });	

	 $('#running-time_1').change(function(){
	       doCaculations();
	 });	
	
	 $('#Product_2_select').change(function(){
	       $('#Product_2_flow').val($('#Product_2_select option:selected').val());
	       doCaculations();
	 });	

	 $('#no-uses_2').change(function(){
	       doCaculations();
	 });	

	 $('#running-time_2').change(function(){
	       doCaculations();
	 });	

	 $('#Product_3_select').change(function(){
	       $('#Product_3_flow').val($('#Product_3_select option:selected').val());
	       doCaculations();
	 });	

	 $('#no-uses_3').change(function(){
	       doCaculations();
	 });	

	 $('#running-time_3').change(function(){
	       doCaculations();
	 });	


}

function doCaculations(){
	calculateTritonTotal();	
	calculateStandardTotal();
	calculateLitresSaved();
	calculateSustainable();
}

function calculateTritonTotal(){
	var total1 = calculate_total($('#Product_1_flow').val(),$('#no-uses_1').val(),$('#running-time_1').val());
	$('#total-water-used_1').val(total1);

	var total2 = calculate_total($('#Product_2_flow').val(),$('#no-uses_2').val(),$('#running-time_2').val());
	$('#total-water-used_2').val(total2);

	var total3 = calculate_total($('#Product_3_flow').val(),$('#no-uses_3').val(),$('#running-time_3').val());
	$('#total-water-used_3').val(total3);
	$('#triton_total').val(total1+total2+total3);		
}

function calculateStandardTotal(){
	var standard_const = $('#conventional-l-p-m_1').val();
	var total1 = calculate_total(standard_const,$('#no-uses_1').val(),$('#running-time_1').val());
	$('#mixer-l-p-m_1').val(total1);

	var total2 = calculate_total(standard_const,$('#no-uses_2').val(),$('#running-time_2').val());
	$('#mixer-l-p-m_2').val(total2);

	var total3 = calculate_total(standard_const,$('#no-uses_3').val(),$('#running-time_3').val());
	$('#mixer-l-p-m_3').val(total3);
	$('#mixer-l-p-m_total').val(total1+total2+total3);		
}

function calculateLitresSaved(){
	var saved = $('#mixer-l-p-m_1').val() - $('#total-water-used_1').val();
	$('#water-saved-l-p-m_1').val(saved);
	saved = $('#mixer-l-p-m_2').val() - $('#total-water-used_2').val();
	$('#water-saved-l-p-m_2').val(saved);
	saved = $('#mixer-l-p-m_3').val() - $('#total-water-used_3').val();
	$('#water-saved-l-p-m_3').val(saved);
	saved = $('#mixer-l-p-m_total').val() - $('#triton_total').val();
	$('#water-saved-l-p-m_total').val(saved);	
}

function calculateSustainable(){
	var use_const = $('#no-uses-sustain_1').val();
	var run_time_const = $('#running-time_1').val();
	var total1 = Math.round(calculate_total($('#Product_1_flow').val(),use_const,run_time_const));
	$('#water-used_1').val(total1);
	
	var total2 = Math.round(calculate_total($('#Product_2_flow').val(),use_const,run_time_const));
	$('#water-used_2').val(total2);

	var total3 = Math.round(calculate_total($('#Product_3_flow').val(),use_const,run_time_const));
	$('#water-used_3').val(total3);
	$('#total').val(total1+total2+total3);

}

function calculate_total(flow,users,time){
	return flow*users*time;
}
function wireUpCalculateButton(){
	$('#calculate').click(function(){
		$('#calculatorForm').submit();
	});
}

function clearBlankULs(){
   $('ul:empty').hide();
}

var initShadowbox = function() {
	Shadowbox.init({
		loadingImage: 'img/shadowbox-loading.gif',
		handleOversize: 'drag'
	});
}
	Shadowbox.loadSkin('classic', '/scripts/shadowbox/skin');
	Shadowbox.loadLanguage('en', '/scripts/shadowbox/lang');
window.onload = initShadowbox;