$(document).ready(function() {

	$.blockUI.defaults.css.border = '2px solid #fff'; 
	$.blockUI.defaults.css.top = '20%'; 

	if ( qUser==1 ) {
		$('#recipe-rating').rating('/recipe_view?node_id=' + qNodeID, {maxvalue:5, increment:.5, curvalue: qRating });
	}

	$('#thumbImage').click(function() { 
		var html = '<a href="' + qNodeURL + '" onclick="$.unblockUI(); return false;"><img src="' + qNodeImage + '"/></a>';
		$.blockUI({ css: { left: ( (screenWidth()/2) - 320), width: '640px', height: '480px' }, message: html }); 
	}); 

	$('#SendToFriend').click(function() { 

		var s = '';
		s += '<div id="SendDialog">';
		s += '<p>By filling out the box below and pressing send,<br />your friend will receive the following message.</p> ';
		s += '<p>\'Hi there, I\'ve sent you this recipe from the SunRice RecipeClub.com.au site.<br /> ';
		s += 'Click on this link to have a look at this and other great recipes.\'</p> ';
		s += '<br /> ';
		s += '<p>Please enter your friend\'s email address.</p> ';
		s += '<form>';
		s += '<input type="text" id="FriendEmailAddress" name="FriendEmailAddress" size="32" value="" /> ';
		s += '<p>';
		s += '<input type="image" src="/images/promo-send-button.png" id="FriendSendSubmit" value="Send" onclick="SendToFriendProcess(); return false;" />';
		s += '<input type="image" src="/images/promo-cancel-button.png" id="FriendSendCancel" value="Cancel" onclick="$.unblockUI(); return false;" />';
		s += '</p>';
		s += '</form>';
		s += '</div>';

		$.blockUI( { css: { left: ( (screenWidth()/2) - 240), width: '480px', height: '240px' }, message: s } ); 
		return false;
	}); 

	$('#PrintRecipe').click(function() { 
			window.open ( qNodeURLprint, 'print', 'width=740, height=640' );
			return false;
			// 		var s = '<img src="' + qNodeImagePrint + '" />';
			// 		$('#recipe-meta-image').css({background:"none"});
			// 		$('#recipe-meta-image').html( s );
			// 		window.print();
		
	}); 

	$('#SignInLink').click( function() { 
 		$('#target').val('recipe_view' );
 		$('#target_node_id').val( qNodeID );
  		addFade();
		return false;
	}); 

});

function SendToFriendProcess() {
	var e = ( $('#FriendEmailAddress').val() );

	var s = '';
	s += '<h3>Sent!</h3> ';
	s += '<form>';
	s += '<input type="button" id="FriendSendCancel" value="OK" onclick="$.unblockUI();" />';
	s += '</form>';
	$.ajax({ type: 'get', url: '/index.php', data: 'm=recipe_view&send_friend_id=' + qNodeID + '&friend_email=' + e, complete: function() { $('#SendDialog').html(s); return false; } }); 
	return false;
}

function CookbookAdd() {
	var s = '<a href="/recipe_view?cookbook_remove=' + qNodeID + '" id="RemoveFromCookbook" onclick="CookbookRemove(); return false;"><span>Remove from Cookbook</span></a>';
	$.ajax({ url: '/recipe_view?cookbook_add=' + qNodeID, complete: function() { $('#CookbookControl').html(s); } }); 
	return false; 
}

function CookbookRemove() {
	var s = '<a href="/recipe_view?cookbook_add=' + qNodeID + '" id="AddToCookbook" onclick="CookbookAdd(); return false;"><span>Add to Cookbook</span></a>';
	$.ajax({ url: '/recipe_view?cookbook_remove=' + qNodeID, complete: function() { $('#CookbookControl').html(s); } }); 
	return false; 
}

function screenWidth() {
    if ( window.innerWidth ) {
        return window.innerWidth;
	} else {
	return document.documentElement.clientWidth;
	}
}

function addFade() {
	doBGFade($("#username"),[255,255,100],[255,255,255],[255,255,255],75,20,4);
}

function doBGFade(elem,startRGB,endRGB,finalColor,steps,intervals,powr) {
	if (elem.bgFadeInt) { 
		window.clearInterval(elem.bgFadeInt);
	}
	var actStep = 0;
	elem.bgFadeInt = window.setInterval(
										function() {
											document.getElementById("username").style.backgroundColor = "rgb("+
											easeInOut(startRGB[0],endRGB[0],steps,actStep,powr)+","+
											easeInOut(startRGB[1],endRGB[1],steps,actStep,powr)+","+
											easeInOut(startRGB[2],endRGB[2],steps,actStep,powr)+")";
											actStep++;
											if (actStep > steps) {
												document.getElementById("username").style.backgroundColor = finalColor;
												window.clearInterval(elem.bgFadeInt);
												document.getElementById("username").value='';
												document.getElementById("username").focus();
											}
										}
										,intervals);
}

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp);
}

