/**
 * Author: Melvyn Hills (@melvynhills)
 * Date: 11.01.2011
 * 
 *             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
 *                     Version 2, December 2004 
 * 
 *  Copyright (C) 2011 Melvyn Hills
 * 
 *  Everyone is permitted to copy and distribute verbatim or modified 
 *  copies of this license document, and changing it is allowed as long 
 *  as the name is changed. 
 * 
 *             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
 *    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 
 * 
 *   0. You just DO WHAT THE FUCK YOU WANT TO. 
 *
 */

var segundosCuenta=500;

now = new Date(); 
// ñapa para que siempre veamos una cuenta regresiva de 5 segundos...
//Pasamos la fecha local a milisegundos y le restamos el número de miliegundos de la diferencia horaria(DATE.UTC devuelve la hora sin
//contar franja horaria.
diferenciaMsTimezone =  1000 * 60 * now.getTimezoneOffset(); //la función devuelve la diferencia de tiempo en min según franja horaria.

//fechaLocalMs = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds()) + diferenciaMsTimezone;
fechaLocalMs = Date.UTC(2011, 5, 17, 20, 30, 0) + diferenciaMsTimezone;
//msFechaFutura= fechaLocalMs + segundosCuenta * 1000; 
msFechaFutura = fechaLocalMs;
//msFechaFutura = Date.UTC(2011, 04, 23, 00, 00, 00) 

//future = new Date(msFechaFutura);
future = new Date(msFechaFutura);

var diferenciaReal;

function getRemainingTime() { // Object

	// 17 february
	// 30 march, 29 to fix February 28-day-month 
	
	// recordar que enero es el mes 0;

	var now = new Date();
	

	//quito esta fecha para ñapear el tema y que siempre veamos una cuenta regresiva de 5 segundos...
	//var future = new Date(2011, 3, 7, 13, 12, 5);	
	
	
	
	//alert("now="+now);
	//alert("future="+future);
	
	return getDateDifference(now, future);
}


/* Esta funcion no se usa ahora
function getRemainingTimeOriginal() { // Object
	var now = new Date();
	// 17 february
	// 30 march, 29 to fix February 28-day-month 
	//var future = new Date(2011, 3-1, 30, 0, 0, 0);
	
	var future = new Date(2011, 3-1, 30, 0, 0, 0);
	
	
	return getDateDifference(now, future);
}

// esta función tampoco se usa. Servía para unos calculos que no tienen nada que ver ahora con nuestro objetivo.

function getTotalTime(y, m, d) { // Object

	// alert("fecha1=("+d+" / "+m+"/"+y+")");

	// 13 june
	
	y = y || 2010;
	m = m || 5;
	d = d || 13;
		
	alert("fecha2=("+d+" / "+m+"/"+y+")");
	
	var now = new Date();
	var past = new Date(y, m, d, 0, 0, 0); //yr_num, mo_num(0-11), day_num(1-31) [hr_num, min_num, sec_num]
	alert("devuelvo:"+getDateDifference(past, now));
	return getDateDifference(past, now);
}
*/

function getDateDifference(date1, date2) { // Object
	var diff = (Date.UTC(date1.getFullYear(), date1.getMonth(), date1.getDate(), date1.getHours(), date1.getMinutes(), date1.getSeconds()) - Date.UTC(date2.getFullYear(), date2.getMonth(), date2.getDate(), date2.getHours(), date2.getMinutes(), date2.getSeconds())) / 1000; // in seconds
	// Diferencia real
	diferenciaReal = diff;
	
	diff = Math.abs(diff);
	
	var days = Math.floor(diff/(24 * 60 * 60));
	diff -= days * (24 * 60 * 60);
	var hours = Math.floor(diff/(60 * 60));
	diff -= hours * (60 * 60);
	var minutes = Math.floor(diff/60);
	diff -= minutes * 60;
	var seconds = Math.floor(diff);
	
	//alert("getDateDifference("+date1+","+date2+") = ("+days+" - "+hours+"/"+minutes+"/"+seconds+")");
	//alert("getDateDifference() = ("+days+" - "+hours+"/"+minutes+"/"+seconds+")");
	
	return {d:days, h:hours, m:minutes, s:seconds};
//	return {d:0, h:2, m:58, s:36};
}

function formatNumber(number) { // String
	return (number < 10 ? '0' : '') + number;
}

function isIOS() { // Boolean
	return navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i);
}


