// display current time at author location
// =======================================
// Muy Modificado por Eric S. Auchterberge at Menttes - Mayo de 2005
// so now, it also display an initial date and update it, and doesn't use the client's 
// machine minutes because they could be wrong!.
// copyright Stephen Chapman, Felgall Pty Ltd, 11 July 2001, 25th November 2004
// http://www.felgall.com/ and http://javascript.about.com/
// permission is given to use this script
// provided that all comment lines in the script are retained

function myTime(startdate, timezone_nz, language) {

var dst = 0; // set to 1 for daylight savings time
             // update this as you go on and off daylight saving time

var locpg = 'Nova Zelândia'; // set to your location
var loc = 'Nueva Zelanda'; // set to your location

var mtz = 12 + timezone_nz; // set to your local timezone (hours ahead of UTC, negative if behind)
                            // NZ es UTC+12 y Resin es UTC-4
var stdz = 'NZST'; // standard time indicator
var dayz = 'NZDT'; // daylight saving time indicator (blank if you dont have daylight saving)

// do not alter anything below this line
var initialdate = setAndGetDspInit(mtz,dst,stdz,dayz,startdate);

if (language == 'pt') {
    document.writeln('Na ' + locpg + ' são <span id="time">' + showDate(initialdate, 'pt') + '<\/span>');
} else {
    document.writeln('En ' + loc + ' es la hora <span id="time">' + showDate(initialdate, 'es') + '<\/span>');
}
if (DOMsupported) setTimeout('updDsp("'+ initialdate.toGMTString()+'")',5000);
}

var DOMsupported = 0;var standardDOMsupported = 0;var ieDOMsupported = 0;
if (document.getElementById) {standardDOMsupported = 1; DOMsupported = 1;}
else { if (document.all) {ieDOMsupported = 1; DOMsupported = 1;}
}
function findDOM(objectId) {
if (standardDOMsupported) {return (document.getElementById(objectId));}
if (ieDOMsupported) {return (document.all[objectId]);}
}

//Inicia un objeto fecha a la hora del server. 
function setAndGetDspInit(mtz,dst,stdz,dayz,inicial) {
    var dayname = new Array ('Domingo','Lunes','Martes','Miércoles','Jueves','Viernes','Sábado','Domingo');
    var daynamepg = new Array ('domingo','segunda-feira','terça-feira','quarta-feira','quinta-feira','sexta-feira','sábado','domingo');
    var now = new Date(inicial);
    var serverCorrectHour = 0 ; //Para corregir la hora del server (resin esta dos horas 55 min atrasado)
    var serverCorrectMin = 0; //Para corregir los minutos en el server  
now.setHours(now.getHours() + mtz + dst + serverCorrectHour);
now.setMinutes(now.getMinutes() + serverCorrectMin);
return (now);
}

//Adelanta la fecha que le pasamos cada 1 segundos.
function updDsp(adate) {
var obj = findDOM('time');
var newdate = new Date(adate);
newdate.setSeconds(newdate.getSeconds() + 1);
obj.innerHTML = showDate(newdate);
setTimeout('updDsp("'+ newdate.toGMTString()+'")',1000);
}

//Muestra lo que queremos y como queremos una fecha dada, devuelve un string.
function showDate(thedate, language) {
var dayname = new Array ('Domingo','Lunes','Martes','Miércoles','Jueves','Viernes','Sábado','Domingo');
var daynamepg = new Array ('domingo','segunda-feira','terça-feira','quarta-feira','quinta-feira','sexta-feira','sábado','domingo');
var dow = thedate.getDay();
var minute = thedate.getMinutes();
var second = thedate.getSeconds();
var hour = thedate.getHours();
if (hour > 11) {ampm = 'PM'; hour -= 12;} else {ampm = 'AM'}
if (hour == 0) {hour = 12;} 
if (minute < 10) {padm = ':0';} else {padm = ':';}
if (second < 10) {pads = ':0';} else {pads = ':';}
var txt = hour + padm + minute + pads + second + ' ' + ampm + ' ';
/*if (dst) txt += dayz; else txt += stdz;  No mostrar NZDT*/ 
if (language == 'pt') {
    txt += ' da ' + daynamepg[dow];
} else {
    txt += ' del día ' + dayname[dow];
}
return (txt);
}


function setDST() {
    /* 
    Daylight Time commences at 2.00am Standard Time on the first Sunday in
    October each year. It ends at 2.00am Standard Time on the third Sunday in
    March of the following year.
    http://javascript.about.com/library/bltime4.htm
    http://www.statoids.com/tnz.html
    http://webexhibits.org/daylightsaving/newZealand.html
    http://www.rotoruanz.com/information/about_nz/index.asp
    http://www.timeanddate.com/worldclock/city.html?n=264
    http://javascript.about.com/library/bltime3.htm
    http://www.javascript-page.com/clock.html
    */

    var gmt = new Date;
    var lsm = new Date;
    var lso = new Date;
    //yes
    lsm.setMonth(2); // March
    lsm.setDate(31);
    var day = lsm.getDay(); // day of week of 31st
    lsm.setDate(31-day);  // last Sunday
    
    lso.setMonth(9); // October
    lso.setDate(31);
    day = lso.getDay();
    lso.setDate(31-day);
    
    if (gmt < lsm || gmt >= lso) dst = 1;
}
