$(document).ready(function()
{
	
	// external links
    $('a[rel="external"]').click(function(event) 
	{
        event.preventDefault();
		pageTrack("/external/" + $(this).attr('href'));
		window.open($(this).attr('href'));
    }); 
	
	// anchor links
	$("a[rel=\"anchor\"]").click(function(event)
	{
		pageTrack("/anchor/" + $(this).attr('href'));
	});
	
	// promo links
	$("#check-it-endless").click(function(event) { pageTrack("/promo/checkitoutButton"); });
	$("#deen-promo").click(function(event) { pageTrack("/promo/deen-promo"); });
	$("#bap-promo").click(function(event) { pageTrack("/promo/bap-promo"); });
	$("#latest-blog-post").click(function(event) { pageTrack("/promo/latest-blog-post"); });
	$("#promo-submit-recipe").click(function(event) { pageTrack("/button/submit-your-recipe"); });
	$("#endless-sandwich-promo").click(function(event) { pageTrack("/promo/endless-sandwich-promo"); });
	$("#calculator-promo").click(function(event) { pageTrack("/promo/all-to-grain-calculator"); });
	$("#submit-recipe-via-email").click(function(event) { pageTrack("/button/submit-recipe-via-email"); });
	$("#recipeSubmitFB").click(function(event) { pageTrack("/button/submit-recipe-via-fb"); });
	$("#recipeSubmit").click(function(event) { pageTrack("/button/submit-via-email"); });
	
	// flash events
	$("#flash-most-liked").click(function(event) { pageTrack("flash-most-liked"); })
	$("#flash-most-commented").click(function(event) { pageTrack("flash-most-commented"); })
	$("#flash-most-viewed").click(function(event) { pageTrack("flash-most-viewed"); })
	$("#flash-gff-favorite").click(function(event) { pageTrack("flash-gff-favorite"); })
	
	
	// mailto links
	$("a[href^=\"mailto:\"]").click(function(event)
	{
		pageTrack("/email/" + $(this).attr('href'));
	});
	
	  $('a[rel="download"]').click(function(event) 
	{
        event.preventDefault();
		pageTrack("/download/" + $(this).attr('href'));
		window.open($(this).attr('href'));
    }); 
	
	
	// print recipe
	$("a[rel=\"print\"]").click(function(event)
	{
		pageTrack("/printRecipe/" + $(this).attr('href'));
	});
	
	
	 // Floodlight Track Click events
	$("#deen-promo").click(function(event){ trackFloodlight("deanb772"); });
 	$("#bap-promo").click(function(event){ trackFloodlight("bapti504"); });
	$("#check-it-endless").click(function(event){ trackFloodlight("endle294"); });
	$("#latest-blog-post").click(function(event){ trackFloodlight("lates828"); });
	$("#promo-submit-recipe").click(function(event){ trackFloodlight("endle507"); });
	$("#searchSubmit").click(function(event){ trackFloodlight("sumbi730"); });	
	$("#endless-sandwich-promo").click(function(event) { trackFloodlight("recip303"); });
	$("#recipeSubmit").click(function(event) { trackFloodlight("sumbi370"); });
	$("#recipeSubmitFB").click(function(event) { trackFloodlight("sumbi110"); })
	
	//flash events
	$("#flash-most-liked").click(function(event) { trackFloodlight("endle004"); })
	$("#flash-most-commented").click(function(event) { trackFloodlight("endle170"); })
	$("#flash-most-viewed").click(function(event) { trackFloodlight("endle981"); })
	$("#flash-gff-favorite").click(function(event) { trackFloodlight("endle549"); })
	
// Validate Recipe Submission Form
	$("#add-recipe").validate();			
		
// thank you header effect		
	setTimeout(function()
	{
		$('h2.collapsible').slideUp(1000);
	}, 5000);	
				

	//form fields	
	
			$('input[type="text"]').addClass("idleField");
       		$('input[type="text"]').focus(function() {
       			$(this).removeClass("idleField").addClass("focusField");
    		    if (this.value == this.defaultValue){ 
    		    	this.value = '';
				}
				if(this.value != this.defaultValue){
	    			this.select();
	    		}
    		});
    		$('input[type="text"]').blur(function() {
    			$(this).removeClass("focusField").addClass("idleField");
    		    if ($.trim(this.value) == ''){
			    	this.value = (this.defaultValue ? this.defaultValue : '');
				}
    		});	
 });
 
function facebook_submit() {
    $("#name").removeClass("required");
    $("#email").removeClass("required");
    $('#add-recipe').submit();
}

function getQueryVariable(variable) { 
	var query = window.location.search.substring(1); 
	var vars = query.split("&"); 
	for (var i=0;i<vars.length;i++) { 
		var pair = vars[i].split("="); 
		if (pair[0] == variable) { 
			return pair[1]; 
		} 
	} 
}

$(document).ready(function() {
    flashTrack('test');
});
 /* ********************************************************** */
function flashTrack(filter)
{
    var url = '/flash/filter/' + filter + '/';
    pageTrack(url);

    if(filter == 'most-liked') {
        trackFloodlight("endle004");
	} else if(filter == 'most-recent') {
        trackFloodlight("endle499");	
    } else if(filter == 'most-commented') {
        trackFloodlight("endle170");
    } else if(filter == 'most-viewed') {
        trackFloodlight("endle981");
    } else if(filter == 'gff-favorites') {
        trackFloodlight("endle549");
    } else if(filter == 'deen') {
        trackFloodlight("endledeen");
    }
}
 /* ********************************************************** */

 
 
 /* ********************************************************** */
function pageTrack(url)
{
	if(pageTracker){ pageTracker._trackPageview(url); }
}
 /* ********************************************************** */
 
 
 
 /* ************************************************************************** */
// This function replicates the functionality of a Floodlight tag as an event-based
// tracking mechanism.
/* ************************************************************************** */
function trackFloodlight(cat)
{
	var src = "1929705";
	var type = "year7277";
	
	var axel = Math.random() + "";
	var a = axel * 10000000000000;
	
	// Create a new iframe
	var iframe = document.createElement("iframe");
	iframe.src = "http://fls.doubleclick.net/activityi;src=" + src + ";type=" + type + ";cat=" + cat + ";ord=" + a + "?";
	
	// Make it as small as possible
	iframe.width = "1";
	iframe.height = "1";
	iframe.frameBorder = "0";
	
	// Append it
	document.body.appendChild(iframe);
}
/* ************************************************************************** */

