$(document).ready(function() { 

	$('#SignInLink').click( function() { 
		$('#target').val('promo');
		addFade();
		return false;
	}); 

	$('#username').click( function() { 
		this.select();
	}); 

	$('#PromoSendToFriend').click(function() { 

	$.blockUI.defaults.css.border = '2px solid #fff'; 
	$.blockUI.defaults.css.top = '20%'; 
	$.blockUI( { css: { top: 24, left: ( (screenWidth()/2) - 290), width: '640px', height: '580px' }, message: $('#SendDialog') } ); 
	return false;

// 		var s = '';
// 		s += '<div id="SendDialog">';
// 		s += '<p>Please enter your friend\'s email address, send and if they join they get a chance to enter.</p> ';
// 		s += '<form>';
// 		s += '<input type="text" id="FriendEmailAddress" name="FriendEmailAddress" value="" /> ';
// 		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 += '</form>';

// 		s += '<p style="text-align: left; margin-left: 32px;">Preview</p> ';
// 		s += '<div id="SendDialogPreview">';
// 		s += '<div id="SendDialogPreviewInner">';
// 		s += '<p>Hi There!</p> ';
// 		s += '<p>I though you\'d like to enter this free competition. All you have to do is join the SunRice Recipe Club - a free site with recipes, articles and competitions.</p> ';
// 		s += '<img src="http://recipeclub.modus.com.au' + qImageURL + ' />';
// 		s += '<p>Click this link, join and you\'ll get 30 chances to win.</p> ';
// 		s += '<p>Kind Regards,<br />Recipe Club</p> ';
// 		s += '</div>';
// 		s += '</div>';

// 		s += '</div>';

// 		$.blockUI( { css: { top: 24, left: ( (screenWidth()/2) - 320), width: '640px', height: '580px' }, message: s } ); 
// 		return false;
	}); 

 	$('#promo-legal-link').click( function() { 
 		var w = window.open("/promo/legal/" + qNodeID , "pollPopup", 'toolbar=no,resizable=yes,scrollbars=yes,width=640,height=480');
		w.focus();
		return false;
 	}); 

}); 

function PromoEntryProcess() {
	var points = ( $('#EntryPoints').val() );
	$.ajax({ type: 'post', url: '/index.php', data: 'm=promo&rpc=enter&node_id=' + qNodeID + '&points=' + points, success: function(s) { $('#PromoEntry').html(s);} }); 
	return false;
}

function SendToFriendReset() {
	$.unblockUI();
	var s = '';
	s += '<h1>Please enter your friend\'s email address.</h1> ';
	s += '<form>';
	s += '<input type="text" id="FriendEmail" value="" /> ';
	s += '<input type="button" id="FriendSendSubmit" value="Send" />';
	s += '<input type="button" id="FriendSendCancel" value="Cancel" />';
	s += '</form>';
	$('#PromoEntry').html(s);
}

function SendToFriendProcess() {
	var e = ( $('#FriendEmailAddress').val() );

	var s = '';
	s += '<h3>Sent!</h3> ';
	s += '<form>';
	s += '<input type="image" src="/images/ok-button.png" id="FriendSendCancel" value="OK" />';
	s += '</form>';
	$.ajax({ type: 'get', url: '/index.php', data: 'm=promo&send_friend=' + qNodeID + '&email=' + e, complete: function() { $('#SendDialog').html(s); } }); 
}

function screenWidth() {
    if ( window.innerWidth ) {
        return window.innerWidth;
	} else {
		return document.documentElement.clientWidth;
	}
}

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);
}

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);
}
