﻿/*
* Copyright (C) 2009-2012 Solmead Productions
*
* == BEGIN LICENSE ==
*
* Licensed under the terms of any of the following licenses at your
* choice:
*
*  - GNU General Public License Version 2 or later (the "GPL")
*    http://www.gnu.org/licenses/gpl.html
*
*  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
*    http://www.gnu.org/licenses/lgpl.html
*
*  - Mozilla Public License Version 1.1 or later (the "MPL")
*    http://www.mozilla.org/MPL/MPL-1.1.html
*
* == END LICENSE ==
*/


if (System.SevenHills.CountDownClock == undefined) {
    System.SevenHills.CountDownClock = function (DivArea, Name, EndTime) {
        var OnShow = null;
        var OnHide = null;
        var Obj = {
            EndTime: "1/1/2001 1:00 PM",
            Shown: false,
            Name: '',
            Div: "",
            SetOnShow: function (TheOnShow) {
                OnShow = TheOnShow;
            },
            SetOnHide: function (TheOnHide) {
                OnHide = TheOnHide;
            },
            SetEndTime: function (EndTime) {
                Obj.EndTime = EndTime;
            },
            Init: function (DivArea, Name, EndTime) {
                Obj.EndTime = EndTime;
                Obj.Div = DivArea;
                Obj.Name = Name;
                Obj.CreateHTML();
                Obj.CallBack();
            },
            CreateHTML: function () {
                var tstr = '';
                tstr = tstr + '<div class="CountDownClock" style="display:none;">\n\r';
                tstr = tstr + '<div class="ClockArea">\n\r';
                tstr = tstr + '<div class="DayArea Area">00\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '<div class="DayWord Word">\n\r';
                tstr = tstr + 'DAYS\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '<div class="ClockArea">\n\r';
                tstr = tstr + '<div class="HourArea Area">00\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '<div class="HourWord Word">\n\r';
                tstr = tstr + 'HOURS\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '<div class="ClockArea">\n\r';
                tstr = tstr + '<div class="MinArea Area">00\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '<div class="MinWord Word">\n\r';
                tstr = tstr + 'MINS\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '<div class="ClockArea">\n\r';
                tstr = tstr + '<div class="SecArea Area">00\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '<div class="SecWord Word">\n\r';
                tstr = tstr + 'SECS\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '<div class="ClockArea ClockInfoArea">\n\r';
                tstr = tstr + '<div class="Until">\n\r';
                tstr = tstr + 'UNTIL\n\r';
                tstr = tstr + '<span class="UntilWord">' + Obj.Name + '</span>\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '</div>\n\r';
                tstr = tstr + '</div>';
                $(Obj.Div).html(tstr);
                return;
            },
            CallBack: function () {
                var NewLocStartTime = Date.parse(Obj.EndTime);
                Now = new Date();
                Now.setTime(Now.getTime() + System.Offset);

                var StartInSeconds = parseInt((NewLocStartTime - Now.getTime()) / 1000);
                if (StartInSeconds < 0) {
                    if (Obj.Shown) {
                        //System.DebugWrite("Hide Count Down Clock");
                        //$(Obj.Div).fadeOut("slow");
                        $(Obj.Div).find(".CountDownClock").fadeOut("slow");
                        Obj.Shown = false;
                        if (OnHide != null) {
                            OnHide();
                        }
                    }
                    System.Later(1000, Obj.CallBack);
                    return;
                } else {
                    if (!Obj.Shown) {
                        //System.DebugWrite("Show Count Down Clock");
                        //$(Obj.Div).fadeIn("slow");
                        $(Obj.Div).find(".CountDownClock").fadeIn("slow");
                        Obj.Shown = true;
                        if (OnShow != null) {
                            OnShow();
                        }
                    }
                }
                Diff = StartInSeconds;
                var Secs = Diff % 60;
                Diff = parseInt(Diff / 60); //minutes
                var Mins = Diff % 60;
                Diff = parseInt(Diff / 60); //hours
                var Hours = Diff % 24;
                Diff = parseInt(Diff / 24); //days
                var Days = Diff;

                var dtn = parseInt(Days / 10);
                var don = Days % 10;
                var htn = parseInt(Hours / 10);
                var hon = Hours % 10;
                var mtn = parseInt(Mins / 10);
                var mon = Mins % 10;
                var stn = parseInt(Secs / 10);
                var son = Secs % 10;

                $(Obj.Div).find(".DayArea").html("" + dtn + don);
                $(Obj.Div).find(".HourArea").html("" + htn + hon);
                $(Obj.Div).find(".MinArea").html("" + mtn + mon);
                $(Obj.Div).find(".SecArea").html("" + stn + son);

                System.Later(1000, Obj.CallBack);
                //setTimeout(Obj.CallBack, 1000);
                return;
            }
        };
        $(function () {
            Obj.Init(DivArea, Name, EndTime);
        });
        return Obj;
    }
}
