/*
 * @package WorldCup
 * @version $Id: wcp.js 248088 2010-06-02 12:16:21Z ianhaycox $
 * @author ian
 * Copyright Ian Haycox, 2010
 */

jQuery(document).ready(function($) {

	var wcp_d = new Date();
	var wcp_offset = -wcp_d.getTimezoneOffset();
	
	/*
	 * Format UTC to SAST or Client time using browsers locale.
	 */
	function sast_time(utc, offset_mins, date_only) {
		var d = new Date();
		var year = parseInt(utc.substr(0,4));
		var month = parseInt(utc.substr(4,2));
		month = month - 1;
		var date = parseInt(utc.substr(6,2));
		var hour = parseInt(utc.substr(8,2));
		var min = parseInt(utc.substr(10,2));
		min = min + offset_mins;
		d.setUTCFullYear(year, month, date);
		d.setUTCHours(hour, min, 0, 0);  // To SAST time
		var str = '';
		if (date_only) {
			str = d.toLocaleDateString();
		} else {
			str = d.toLocaleDateString() + ' ' + d.toLocaleTimeString();
			// remove seconds
			if (str.length > 3 &&  str.substr(str.length-3, 3) == ':00') {
				str = str.substr(0, str.length-3 );
			} 
			if (str.length > 6 &&  str.substr(str.length-6, 3) == ':00') {
				str = str.substr(0, str.length-6 ) + str.substr(str.length-3, 3 );
			} 
		}
		return str;
	}
	
	/*
	 * Set each kickoff to localized date/time format in SAST time
	 */
	$('.wcp2010_kickoff_time').each(function() { 
		var utc = $(this).attr('utc');  // YYYYMMDDHHMM format
		if (utc == undefined) return;
		var date_only = $(this).is('.date_only');
		
		$(this).html(sast_time(utc, wcp_offset-120, date_only));  // Note: toLocale...() adds the TZ offset,
															      // so subtract the offset between UTC and SAST (120 mins)
	});

	/*
	 * Toggle between Local (SAST = UTC+2) and Client time.
	 */
	$('#tzLocal').click(function() {
		$('.wcp2010_kickoff_time').each(function() {
			var utc = $(this).attr('utc');
			if (utc == undefined) return;
			
			$(this).addClass('tzClient');
			var date_only = $(this).is('.date_only');
			$(this).html(sast_time(utc, wcp_offset-120, date_only));  // As above
		});
		$(this).hide();
		$('#tzClient').show();
	});
	
	$('#tzClient').click(function() {
		$('.wcp2010_kickoff_time').each(function() { 
			var utc = $(this).attr('utc');
			if (utc == undefined) return;
			$(this).removeClass('tzClient');
			var date_only = $(this).is('.date_only');
			$(this).html(sast_time(utc, 0, date_only));  // No offset display UTC in browser local time
		});
		$(this).hide();
		$('#tzLocal').show();
	});

	/*
	 * 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);
}
