/*
 * @package WorldCup
 * @version $Id: wcp.js 233639 2010-04-25 23:10:45Z ianhaycox $
 * @author ian
 * Copyright Ian Haycox, 2010
 */

jQuery(document).ready(function($) {
	
	/*
	 * Save current form
	 */
	$('form.wcp2010_user_form').submit(function() {
		var data = $(this).serialize();
		var msg_id = $(this).find("input[name='msg_id']");  // get element id for message
		
		$.ajax({
			type:"POST",
			cache:false,
			url: WorldCupPredictorAjax.blogUrl + "/wp-admin/admin-ajax.php",
			dataType:"json",
			data:"action=worldcuppredictor_ajax&"+data,
			success: function(msg){
				// Using the hidden field for this form display message and scroll
				$('#'+msg_id.val()).html(msg.notice);
				$('.wcp2010_user_pred_widget').replaceWith(msg.preds);
				$('html, body').animate({scrollTop: $('#'+msg_id.val()).offset().top}, 500);
			},
			error: function(xml, text, error) {
				alert("Error" + xml + text + error);
			}
		});
		
		return false;
	});
	
	$('.zebra tr.row:even').addClass('even');
	$('.zebra tr.row:odd').addClass('alt');
		
});


/*
Author:		Robert Hashemian (http://www.hashemian.com/)
Modified by:	Munsifali Rashid (http://www.munit.co.uk/)
*/

/*
Modified by: Ian Haycox
Countdown timer to next prediction deadline
*/
function wcp2010_countdown(obj)
{
	this.obj		= obj;
	this.Div		= "clock1";
	this.BackColor		= "white";
	this.ForeColor		= "black";
	this.TargetDate		= "12/31/2020 5:00 AM";
	this.ServerDate		= "12/31/2020 5:00 AM";
	this.DisplayFormat	= "%%D%%d, %%H%%h, %%M%%m, %%S%%s.";
	this.FinishStr      = "Too Late";
	this.CountActive	= true;
	
	this.DisplayStr;
	
	this.Calcage		= wcp2010_cd_Calcage;
	this.CountBack		= wcp2010_cd_CountBack;
	this.Setup		= wcp2010_cd_Setup;
}

function wcp2010_cd_Calcage(secs, num1, num2)
{
	s = ((Math.floor(secs/num1))%num2).toString();
	if (s.length < 2) s = "0" + s;
	return (s);
}
function wcp2010_cd_CountBack(secs)
{
	if (secs < 0) {
	  if (document.getElementById(this.Div) != null) {
		  document.getElementById(this.Div).innerHTML = this.FinishStr;
	  }
	  return;
	}
	
	this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g,	this.Calcage(secs,86400,100000));
	this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(secs,3600,24));
	this.DisplayStr = this.DisplayStr.replace(/%%M%%/g,		this.Calcage(secs,60,60));
	this.DisplayStr = this.DisplayStr.replace(/%%S%%/g,		this.Calcage(secs,1,60));
	
	if (document.getElementById(this.Div) != null) {
	  document.getElementById(this.Div).innerHTML = this.DisplayStr;
	}
	if (this.CountActive) setTimeout(this.obj +".CountBack(" + (secs-1) + ")", 990);
}
function wcp2010_cd_Setup()
{
	var dthen	= new Date(this.TargetDate);
		var dnow	= new Date(this.ServerDate);
	ddiff		= new Date(dthen-dnow);
	gsecs		= Math.floor(ddiff.valueOf()/1000);
	this.CountBack(gsecs);
}
