/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 450;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.name +"' alt='Image preview' />"+ c +"</p>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});



/*
 * Image slideshow script 
 */

function slideSwitch(target) {
    var current = $(target + ' .slide:eq(0)');
    var next = $(target + ' .slide:eq(1)');
    $(target).append(current);
    next.fadeIn(1100);
    current.fadeOut(1100);
}
$(function() {
    setInterval("slideSwitch('#slideshow')", 5000 );
});


/*
 * 
 * Volume Calculator
 * 
*/
// convert 1000 to 1,000
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;
}
//Round number to decimal plaes - num is the number - dec is the amount of decimal places
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

//calculate the volume or weight
function cal() {
	
	//Check which shape is selected
	var S = null;
	var radioButtons = document.getElementsByName("shape");
		for (var x = 0; x < radioButtons.length; x ++) {
		if (radioButtons[x].checked) {
			var S = radioButtons[x].value;
		}
	}
    
    var L = document.getElementById("length").value;
    var W = document.getElementById("width").value;
    var D = document.getElementById("depth").value;
    var R = document.getElementById("radius").value;
    var P = document.getElementById("product").value; 
    var pName = null;
    var T = "null"; // var for product type
    var M = 0; // multiplier for weight products
    
    //document.getElementById("debug").innerHTML = S;
    
    D = D/1000; //convert depth from mm into metres
    
    //convert the chosen product into the T var used in the formula
    switch(P) {
    	case "sand":
    	T = "weight";
    	M = 1.5;
    	pName = "sand.";
    	break;
    	case "screening":
    	T = "weight";
    	M = 1.5;
    	pName = "screening products.";
    	break;
    	case "gravel":
    	T = "weight";
    	M = 1.5;
    	pName = "gravel products.";
    	break;
    	case "soil":
    	T = "weight";
    	M = 1.5;
    	pName = "soil.";
    	break;
    	case "loam":
    	T = "weight";
    	M = 1.5;
    	pName = "loam.";
    	break;
    	case "mulch":
    	T = "volume";
    	pName = "mulch.";
    	break;
    	case "bark":
    	T = "volume";
    	pName = "bark.";
    	break;
    	case "compost":
    	T = "volume";
    	pName = "compost.";
    	break;
    	case "recover":
    	T = "volume";
    	pName = "Jeffries Recover";
    	break;
    }
    
    if(S == "circle") {
    	var result = (R * R * 3.141) * D;
    } else if (S == "rectangle") {
    	var result = L * W * D;
    } else if (S == "triangle") {
    	var result = (L * W)/2 * D;
    }
    result = roundNumber(result,2);
    
    if(T == "volume"){        	
        	//result0 = Math.round(10*result)/10; // Round up the number
        	result0 = roundNumber(result,2);
        	document.getElementById("result").innerHTML = "You will require approx " + result0 + "m<sup>3</sup> of " + pName;
        }
	else if(T == "weight"){			      	
        	var result1 = result * M;
        	result2 = roundNumber(result1,2);
        	//result1 = Math.round(10*result1)/10; //this will round up the number
        	var result3 = result2 * 1000; //convert tonnes into KG
        	var result4 = addCommas(result3); // add comma if KG over 1000
        	//document.getElementById("debug").innerHTML = result1;
        	
        	document.getElementById("result").innerHTML = "You will require approx " + result2 + " Tonne or " + result4 + " Kilograms of " + pName;
        	
        }
        
	//document.getElementById("debug").innerHTML = result + " debugger";
}

            
