/*
 * Shadowbox initialisation, for the our-recipes / send to a friend section 
 *
 * 
 */


// initialise shadowbox immediately
Shadowbox.init({
    skipSetup: true,
    players: ["html"],
	language: "en",
	enableKeys: false
});



// on dom load functionality
$(document).ready(function(){
	
	// send to a friend functionality
	$('a.email').bind('click', function(e){
		e.preventDefault();
		this.blur();
		openSendToAFriend(this);
	})

	.bind('keypress', function(e){
		if (e.keyCode == 13) {
			e.preventDefault();
			this.blur();
			openSendToAFriend(this);
		}
	});
	
	// open terms and conditions page
	$('a.terms').bind('click', function(e){
		e.preventDefault();
		this.blur();
		openTerms(this);
	})

	.bind('keypress', function(e){
		if (e.keyCode == 13) {
			e.preventDefault();
			this.blur();
			openTerms(this);
		}
	});
});



/*
 * request the 'send to a friend' functionality, and create the shadowbox overlay
 */
var openSendToAFriend = function(linkElement) {
	
	// get the ID of the current link
	var id = linkElement.href.split('/').pop();

	// set up the ajax call
	var ajaxPath = "/plum-mums/our-recipes/send-to-a-friend/";
	var content = "";
	
	// request the form content
	var response = $.post(
		ajaxPath, 
		{ 'random' : Math.random(), 'format' : 'html', 'id' : id },
		function(data, textStatus){
			
			// condition : if the data has been retrieved successfully, save it
			if (textStatus == 'success') {
				content = data;
				
			// an error has occurred, prepare to display it
			} else {
				content = '<p>Sorry, there was an error retrieving this content, please refresh the page</p>';
			}
			
			// create the overlay!
			Shadowbox.open({
			    player:     	"html",
			    title:      	"Send to a friend",
				content: 		content,
			    width:      	400,
				initialWidth: 	400,
				height: 		300
			});
			
			
			// google analytics custom call
			if (!!window.pageTracker) {
				pageTracker._trackPageview("/_overlay/send-to-a-friend/" + id);
			}
	});
};



/*
 * request the 'send to a friend' functionality, and create the shadowbox overlay
 */
var openTerms = function(linkElement) {
	
	// set up the ajax call
	var ajaxPath = "/terms-and-conditions/";
	var content = "";
	
	// request the form content
	var response = $.post(
		ajaxPath, 
		{ 'random' : Math.random(), 'format' : 'html' },
		function(data, textStatus){
			
			// condition : if the data has been retrieved successfully, save it
			if (textStatus == 'success') {
				content = data;
				
			// an error has occurred, prepare to display it
			} else {
				content = '<p>Sorry, there was an error retrieving this content, please refresh the page</p>';
			}
			
			// create the overlay!
			Shadowbox.open({
			    player:     	"html",
			    title:      	"Terms &amp; Conditions",
				content: 		content,
			    width:      	400,
				initialWidth: 	400,
				height: 		500
			});
			
			// google analytics custom call
			if (!!window.pageTracker) {
				pageTracker._trackPageview("/_overlay/terms/");
			}
	});
};
// Shadowbox.close()