$(document).ready(function() {
$('img.captify').captify({
		// all of these options are... optional
		// ---
		// speed of the mouseover effect
		speedOver: 'fast',
		// speed of the mouseout effect
		speedOut: 'normal',
		// how long to delay the hiding of the caption after mouseout (ms)
		hideDelay: 500,	
		// 'fade', 'slide', 'always-on'
		animation: 'slide',		
		// text/html to be placed at the beginning of every caption
		prefix: '',		
		// opacity of the caption on mouse over
		opacity: '0.7',					
		// the name of the CSS class to apply to the caption box
		className: 'caption-bottom',	
		// position of the caption (top or bottom)
		position: 'bottom',
		// caption span % of the image
		spanWidth: '100%'
	});

	if ($("#slideshow").length) {
		$('#slideshow').cycle({ 
			timeout:  4000,
			next:   '#next', 
			prev:   '#previous' 
		}); 
	}
	
	$('.eq').equalHeights();

	$('#pstandard').click(function() {
		$('#technical').hide();
		$('#standard').show();
	})
	$('#ptech').click(function() {
		$('#standard').hide();
		$('#technical').show();
	})

	$(".col1").find("a[href!='"+window.location.pathname.replace("/","")+"']:not(li > a)").each(function(){
		$(this).parent().css("display","inherit")
	})

	if ($("dt").length) {
		$("dd").hide();
		$('dt').click(function() {
		var answer = $(this).next();
		if (answer.is(':visible')) {
			answer.hide();
			$(this).css("background-image","url(/assets/images/bg-plus.gif)");
		} else {
			answer.show();
			$(this).css("background-image","url(/assets/images/bg-minus.gif)");
		}
		});
	}
	$("a.fancybox").fancybox({
		'overlayColor':		'#000',
		'overlayOpacity':	0.8
	});
	 $('#height,#width,#depth').keyup(function(){
        formatted=stripNonNumeric($(this).val());
		$(this).val(formatted);
		if($("#width").val()!=""&&$("#height").val()!=""&&$("#depth").val()!="") {
			$("#selector .col3").fadeTo(200,1);
			$("input:radio[name=insulation]").attr('disabled', '');
		}
    });

	$("#calculate").click(function(){
		pass=true;
		if(typeof($("input:radio[name=units]:checked").val())=="undefined") { 
			pass=false;
			alert("Please select your measurement units"); 
		}
		if($("#width").val()==""||$("#height").val()==""||$("#depth").val()=="") {
			pass=false;
			alert("Please enter your measurements"); 
		}
		if(typeof($("input:radio[name=insulation]:checked").val())=="undefined") { 
			pass=false;
			alert("Please select your room's insulation"); 
		}
		if (pass) {
			setvars();
			calculateit();
		}
	});
	if($("#selector").length){
		if($("#width").val()==""&&$("#height").val()==""&&$("#depth").val()=="") {
			$("#selector .col2,#selector .col3").fadeTo(0,0.5);
			$("#height,#width,#depth,input:radio[name=insulation]").attr('disabled', 'disabled');
		} else {
			setvars();	
			calculateit();
		}
		$("input:radio[name=units]").click(function(){ 
			$("#selector .col2").fadeTo(200,1);
			$("#height,#width,#depth").attr('disabled', '');
		});
	}
	
	
});

function setvars() {
	st_width=$('#width').val(); 
	st_depth=$('#depth').val(); 
	st_height=$('#height').val(); 
	if ($('#units_0').attr("checked")) { st_units='metres'; }
	if ($('#units_1').attr("checked")) { st_units='feet'; }
	if ($('#insulation_0').attr("checked")) { st_insul='poor'; }
	if ($('#insulation_1').attr("checked")) { st_insul='average'; }
	if ($('#insulation_2').attr("checked")) { st_insul='good'; }
}
function calculateit() {
	units=$("input:radio[name=units]:checked").val();
	volume=$("#width").val()*$("#height").val()*$("#depth").val();
	if(units=="feet"){volume=($("#width").val()*0.3048)*($("#height").val()*0.3048)*($("#depth").val()*0.3048)  ;}
	//alert(volume);
	kw=volume/$("input:radio[name=insulation]:checked").val();
	kw=Math.round(kw*10)/10;
	$("#kw").html(kw);
	$("#result").fadeIn();	
	if($("#stoves").length){
		returnurl="?width="+st_width+"&height="+st_height+"&depth="+st_depth+"&units="+st_units+"&insulation="+st_insul;
		url="/stove-results.html?kw="+kw;
		//alert(url);
		$("#stoves").load(url, function(response, status, xhr) {
		  if (status == "error") {
			var msg = "Sorry but there was an error: ";
			$("#error").html(msg + xhr.status + " " + xhr.statusText);
		  }
		})
	
	}
}
function stripNonNumeric( str )
{
  str += '';
  var rgx = /^\d|\.|-$/;
  var out = '';
  for( var i = 0; i < str.length; i++ )
  {
    if( rgx.test( str.charAt(i) ) ){
      if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) ||
             ( str.charAt(i) == '-' && out.length != 0 ) ) ){
        out += str.charAt(i);
      }
    }
  }
  return out;
}
function stripAlphaChars(pstrSource) 
{ 
var m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[^0-9]\./g, ''); 

    return m_strOut; 
}
function IsNumeric(strString) {
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	  {
	  strChar = strString.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1)
		 {
		 blnResult = false;
		 }
	  }
	return blnResult;
}


(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			//$(this).height(tallest).css("overflow","auto");
			$(this).height(tallest);
		});
	}
})(jQuery);


// jQuery Captify Plugin
// Copyright (C) 2009 Brian Reavis
// Licenced under the MIT license
// $Date: 2009-11-30 [Mon, 30 Nov 2009] $
jQuery.fn.extend({captify:function(n){var a=$.extend({speedOver:"fast",speedOut:"normal",hideDelay:500,animation:"slide",prefix:"",opacity:"0.45",className:"caption-bottom",position:"bottom",spanWidth:"100%"},n);$(this).each(function(){var c=this;$(this).load(function(){if(c.hasInit)return false;c.hasInit=true;var i=false,k=false,e=$("#"+$(this).attr("rel")),g=!e.length?$(this).attr("alt"):e.html();e.remove();e=this.parent&&this.parent.tagName=="a"?this.parent:$(this);var h=e.wrap("<div></div>").parent().css({overflow:"hidden",
padding:0,fontSize:0.1}).addClass("caption-wrapper").width($(this).width()).height($(this).height());$.map(["top","right","bottom","left"],function(f){h.css("margin-"+f,$(c).css("margin-"+f));$.map(["style","width","color"],function(j){j="border-"+f+"-"+j;h.css(j,$(c).css(j))})});$(c).css({border:"0 none"});var b=$("div:last",h.append("<div></div>")).addClass(a.className),d=$("div:last",h.append("<div></div>")).addClass(a.className).append(a.prefix).append(g);$("*",h).css({margin:0}).show();g=jQuery.browser.msie?
"static":"relative";b.css({zIndex:1,position:g,opacity:a.animation=="fade"?0:a.opacity,width:a.spanWidth});if(a.position=="bottom"){e=parseInt(b.css("border-top-width").replace("px",""))+parseInt(d.css("padding-top").replace("px",""))-1;d.css("paddingTop",e)}d.css({position:g,zIndex:2,background:"none",border:"0 none",opacity:a.animation=="fade"?0:1,width:a.spanWidth});b.width(d.outerWidth());b.height(d.height());g=a.position=="bottom"&&jQuery.browser.msie?-4:0;var l=a.position=="top"?{hide:-$(c).height()-
b.outerHeight()-1,show:-$(c).height()}:{hide:0,show:-b.outerHeight()+g};d.css("marginTop",-b.outerHeight());b.css("marginTop",l[a.animation=="fade"||a.animation=="always-on"?"show":"hide"]);var m=function(){if(!i&&!k){var f=a.animation=="fade"?{opacity:0}:{marginTop:l.hide};b.animate(f,a.speedOut);a.animation=="fade"&&d.animate({opacity:0},a.speedOver)}};if(a.animation!="always-on"){$(this).hover(function(){k=true;if(!i){var f=a.animation=="fade"?{opacity:a.opacity}:{marginTop:l.show};b.animate(f,
a.speedOver);a.animation=="fade"&&d.animate({opacity:1},a.speedOver/2)}},function(){k=false;window.setTimeout(m,a.hideDelay)});$("div",h).hover(function(){i=true},function(){i=false;window.setTimeout(m,a.hideDelay)})}});if(this.complete||this.naturalWidth>0)$(c).trigger("load")})}});
