var ONE_MIN = 1000 * 60;

var CLOCK_MONTH_NAMES = new Array("January", "February", "March",
        "April", "May", "June", "July", "August", "September",
        "October", "November", "December");


function Clock(/*String*/ startDate, /*Number*/ logic, /*Element*/ targetPlace, /*Number*/ tzShift) {

    this.logic = logic;
    this.targetPlace = targetPlace;
    this.tzShift =  tzShift;
    var newstartdate = this.timeStampXMLToDate( startDate,tzShift);
    this.startDate = newstartdate;
    
}

function ClockList(/*Hash*/ elementsDesc, /*Number*/ ticks, /*Number*/ tzShift) {
    this.elementName = elementsDesc.elementName;
    this.startArrName = elementsDesc.startArrName;
    this.logicArrName = elementsDesc.logicArrName;
    this.ticks = parseInt(ticks, 10);
    this.tzShift = parseInt(tzShift, 10);

    this._elements = [];

    var timeToHandleMethodName = "updateTime";
    var obj = this;
    var timeToHandleCallback = function() {
        try {
            obj[timeToHandleMethodName]();
        } catch(e) {
        }
    };

    this.timedHanlderExecutor = new PeriodicalExecuter(timeToHandleCallback, this.ticks);
}

ClockList.prototype.rebuild = function() {
    var e = document.getElementsByName(this.elementName);
    this._elements = [];

    for (var i = 0; i < e.length; i++) {

        var targetParent = e[i];
        var targetPlace = undefined;

        var nbh = e[i].parentNode.childNodes;

        for (var j = 0; j < nbh.length; j++) {
            if (nbh[j].tagName == 'SPAN') {
                targetPlace = nbh[j];
                continue;
            }
        }

        var startDateString = (targetParent.getAttribute(this.startArrName));
        var logic = targetParent.getAttribute(this.logicArrName);

        if (targetParent && targetPlace) {
            this._elements.push(new Clock(startDateString, logic, targetPlace,this.tzShift));
        }
    }

    this.updateTime();
};

ClockList.prototype.updateTime = function() {
    var cd = new Date();
    for (var i = 0; i < this._elements.length; i++) {
        var c = this._elements[i];
        c.sampleTime(cd);
    }
};

Clock.prototype.sampleTime = function(currDate) {
    this.targetPlace.innerHTML = this.timeLabel(this.logic, this.calcDiff(this.startDate, currDate));
};


Clock.prototype.calcDiff = function(startDate, currDate) {
    return Math.ceil((currDate.getTime() - startDate.getTime()) / (ONE_MIN));
};


Clock.prototype.timeLabel = function(logic, diff) {
    var result = "";
    var delay = 0;
    var period = 0;

    if (logic == "100" || logic == "150" || logic == "200") {
        delay = 15;
        period = 45;
    } else if (logic == "105" || logic == "155" || logic == "205") {
        delay = 15;
        period = 45;
    } else {
        result = result + "&nbsp;"
    }

    if (period > 0) {
        if (diff > 0) {
            if (diff >= (period + 4) && diff < (period + delay)) { //ie 49
                result += " HT&nbsp;&nbsp;";
            } else    if (diff >= period && diff < (period + delay)) { //ie 45
                result += " " + period + "'+";
            }
            if (diff == (period + delay)) {
                result += " " + (period + 1) + "'&nbsp;&nbsp;";
            }
            if (diff > (period + delay)) {
                if ((diff - delay) > 90 && (logic == "100" || logic == "150" || logic == "200")) {
                    result += " 90'+";
                } else {
                    result += " " + (diff - delay) + "'&nbsp;&nbsp;";
                }
            }
            if (diff < (period)) {
                result += " " + (diff) + "'&nbsp;&nbsp;";
            }
        } else {
            result += " 1'&nbsp;&nbsp;";
        }
    }
    return result;

};
Clock.prototype.timeStampXMLToDate = function(/*String*/ xmlDate,tzShift) {
    var dateStr = this.timeStampXMLToDateString(xmlDate);
    var result = new Date(Date.parse(this.timeStampXMLToDateString(xmlDate)));
    //result.setHours(result.getHours() + tzShift);
    return result;
};

Clock.prototype.timeStampXMLToDateString = function(xmlDate) {
    var y = xmlDate.substring(0, 4);
    var m = xmlDate.substring(5, 7);
    var d = xmlDate.substring(8, 10);
    var h = xmlDate.substring(11, 13);
    var mm = xmlDate.substring(14, 16);
    var s = xmlDate.substring(17, 19);
    var o1 = xmlDate.substring(19, 22);
    var o2 = xmlDate.substring(23, 25);
    //October 18, 2011 15:30:00 GMT+2000 works on IE as well as iPhone safari
    return  "" +
            CLOCK_MONTH_NAMES[parseInt(m, 10) - 1] + " " + d + ", " + y + " " +
            h + ":" + mm + ":" + s + " " + "GMT" + o1 + o2;
};

