$(document).ready(function() {
	// Ghost Text
    $(".populate input").each(function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
        }
    });
    $(".populate input").focus(function() {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
        }
    });
    $(".populate input").blur(function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
        }
    });


	// Encrypt Email Addresses
	$(".mask").each(function(){
		var ats, dots, address, i;
		ats = [ ' at ', ' (at) ', ' [at] ' ];
		dots = [ ' dot ', ' (dot) ', ' [dot] ' ];
		address = $(this).html();
		for ( i = 0; i < ats.length; i++ ) {
			address = address.replace(ats[i], '@');
		}
		for ( i = 0; i < dots.length; i++ ) {
			address = address.replace(dots[i], '.');
		}
		$(this).html('<a href="mailto:' + address + '">' + address + '</a>');
	});
	
	
	// Google Analytics Click Tracking
	$(".lightbox").click( function() {
		_gaq.push( ['_trackPageview', this.href] );
	});
	$(".external").click( function() {
		_gaq.push( ['_trackEvent', 'External Links', this.href] );
		setTimeout('document.location = "' + this.href + '"', 100);
		return false;
	});
	
	
	// Lightbox
	$('.lightbox').fancybox({
		'overlayColor'		: '#000',
		'autoDimensions'	: true,
		'padding'			: 0
	});
	
	
	// Calculator
	$('#numAthletes, #numWorkouts').bind('change keyup', function() {
		var numAthletes = $("#numAthletes").val();
		var numWorkouts = $("#numWorkouts").val();
		
		if( numAthletes > 0 && numWorkouts > 0 ) {
			var casesNeeded = Math.ceil( ( numAthletes * numWorkouts ) / 12 );
			$("#cases-needed").html( casesNeeded );
		} else {
			$("#cases-needed").html("");
		}
	});
	
	
	// Product Information Tabs
	$('#products').tabs();
	
	
	// Prevents Anchor Links for Scrolling Page in IE
	$('.rr_cont a[href="#"]').click( function(event) {
		event.preventDefault();
	});
	
	
	// Why Rockin' Refuel Rollovers
	$('#why li').hover( function() {
		$(this).children('div').toggle();
	});
	
	
	// Product Locator
	$('#retailers .toggle').click( function(event) {
		event.preventDefault();
		$(this).next('ul').slideToggle();
	});
	
	
	// Buy Button Tracking
	// Prevents default link action, inserts an iframe into the dom with tracking code, waits half a second, then redirects to original URL
	$('#buyBtn').click( function(event) {
		event.preventDefault();
		
		var url = $(this).attr('href');
		var axel = Math.random() + "";
		var a = axel * 10000000000000;
		$(this).append('<iframe src="http://fls.doubleclick.net/activityi;src=2535741;type=2011r293;cat=buyro660;ord=1;num=' + a + '?" width="1" height="1" frameborder="0"></iframe>');

		setTimeout( function(){ window.location = url }, 500 );
	});
	
	// Contest Rules Link for Gravity Form
	$('.contest-rules li label').html('I have read the <a href="/wp-content/themes/rockinrefuel/pdfs/contest-rules.pdf" target="_blank">Contest Rules</a>');
	
	// Quote Rotater
	$('.quotes .quote:first-child').show();
	setInterval(function(){
		$('.quotes .quote').filter(':visible').fadeOut(500,function(){
			if($(this).next('.quote').size()){
				$(this).next().fadeIn(500);
			}
			else{
				$('.quotes .quote').eq(0).fadeIn(500);
			}
		});
	},10000);
});
