//------------------------------------
//	RPM.JS
//	Author: 	Engage Interactive
//	Requires:	jQuery 1.5
//                      Cufon
//                      Whitney Semibold font
//                      Whitney Book font
//------------------------------------



////////////////////////////
// CUFON

Cufon.replace('#content h1, #content h2, #content h3, #content h4, #content h5, .feature h3, header #quote #customer', {
	fontFamily: 'whitney'
});
Cufon.replace('header #quote p', {
	fontFamily: 'whitney-book'
});

$(function(){
	//BEGIN jQuery

	// TARGET BLANK REPLACEMENT
	$(".external").attr("target","_blank");
        
        
	// NAVIGATION
	// $('nav ul').children('li:not(.contact)').append('<span class="arrow"><!-- # --></span>');
        
	// NAVIGATION - LEVEL 1
	$('nav ul li.drop_down').mouseenter(function(){
		$(this).addClass('hover').children('ul').show();
	}).mouseleave(function(){
		$(this).removeClass('hover').children('ul').hide();    
	});
		
	$('nav ul li a').click(function(){
		if($(this).attr('href') == '#') return false;
	});

	// NAVIGATION - MAGAZINE FEATURE
	$('.magazine_feature select').change(function() {
		if($(this).val().length) window.open($(this).val());
	});
        
	// ODD & EVEN/HOVER CLASSES
	$('#content section#services p:odd').addClass('odd');
	$('table tbody tr:odd').addClass('odd');
        
	$('table tbody tr td').live({
		mouseenter: function(){
			$(this).parent().addClass('hover')
		},

		mouseleave: function(){
			$(this).parent().removeClass('hover')
		},

		click: function(){
			
			if($(this).siblings('td:first').children('input[type=checkbox]').length){
			
				if($(this).siblings('td:first').children('input[type=checkbox]').is(':checked')){
					$(this).siblings('td:first').children('input[type=checkbox]').attr('checked', false);
					$('p#total').calculateTotal();
				}else{
					$(this).siblings('td:first').children('input[type=checkbox]').attr('checked', true);
					$('p#total').calculateTotal();
				}
				
			}
		}
	});
	
	$('table tbody tr td.col1 input').live('click', function() {
		
		$('p#total').calculateTotal();
		
	});

        
        
	$.fn.calculateTotal = function(){

		$total = 0.00;

		$('table#'+$form).find('input#items:checked').each(function(){
			$total += parseFloat($(this).parent().siblings('td:last').html().substr(1), 10)
		})

		$total = $(this).roundNumber($total, 2);

		$(this).html('&pound;'+$total);

		if($total == 0.00){
			$('#model_selector button').fadeOut();
		}else{
			$('#model_selector button').fadeIn();
		}
                
	}


	$.fn.roundNumber = function(number,decimals) {
		var newString;// The new rounded number
		decimals = Number(decimals);
		if (decimals < 1) {
			newString = (Math.round(number)).toString();
		} else {
			var numString = number.toString();
			if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
				numString += ".";// give it one at the end
			}
			var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
			var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
			var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
			if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
				if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
					while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
						if (d1 != ".") {
							cutoff -= 1;
							d1 = Number(numString.substring(cutoff,cutoff+1));
						} else {
							cutoff -= 1;
						}
					}
				}
				d1 += 1;
			} 
			if (d1 == 10) {
				numString = numString.substring(0, numString.lastIndexOf("."));
				var roundedNum = Number(numString) + 1;
				newString = roundedNum.toString() + '.';
			} else {
				newString = numString.substring(0,cutoff) + d1.toString();
			}
		}
		if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
			newString += ".";
		}
		var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
		for(var i=0;i<decimals-decs;i++) newString += "0";
		//var newNumber = Number(newString);// make it a number if you like
		return newString; // Output the result to the form field (change for your purposes)
	}
        
    $("[placeholder]").focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr('placeholder')) {
	    input.val("");
	    input.removeClass('placeholder');
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == "" || input.val() == input.attr('placeholder')) {
	    input.addClass("placeholder");
	    input.val(input.attr('placeholder'));
	  }
	}).blur();
});
//END jQuery




/*
 * jQuery Timer Plugin
 * http://www.evanbot.com/article/jquery-timer-plugin/23
 *
 * @version      1.0
 * @copyright    2009 Evan Byrne (http://www.evanbot.com)
 */

jQuery.timer = function(time,func,callback){
	var a = {
		timer:setTimeout(func,time),
		callback:null
	}
	if(typeof(callback) == 'function'){
		a.callback = callback;
	}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){
		a.callback();
	};
	return this;
};

//EASING
jQuery.extend(jQuery.easing,{
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;
		var p=0;
		var a=c;
		if (t==0) return b;
		if ((t/=d)==1) return b+c;
		if (!p) p=d*.3;
		if (a < Math.abs(c)) {
			a=c;
			var s=p/4;
		}
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutExpo: function(x,t,b,c,d){
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	}
});
