/*
 * jCountdown
 * Creates a countdown timer from a jQuery object. Allows you to format
 * the way the time is displayed.
 *
 * $(<selector>).countdown({params});
 *
 * If you want to replace the 
 *
 */
jQuery.fn.countdown = function(params) {
    var self = this;
    var now = 0;
    //Properties
    //----------------------------------------------
    //set the time and day to work with

    self.display = $(this);
    //var dtparts = params.date.split("/")
    //self.target = new Date(dtparts[0], dtparts[1], dtparts[2], dtparts[3], dtparts[4], dtparts[5]);
    //$("#valt").val(self.target);
    //var dtparts = params.targetCurrent.split("/")
    //self.targetCurrent = new Date(dtparts[0], dtparts[1], dtparts[2], dtparts[3], dtparts[4], dtparts[5]);
    self.target = new Date(params.date);
    self.targetCurrent = new Date(params.targetCurrent);
    //$("#valt").val(self.target + " - " + self.targetCurrent);
    self.message = params.message ? params.message : "";
    self.addZeros = params.addZeros ? params.addZeros : false;

    //Events
    //----------------------------------------------
    self.onTick = params.onTick ? params.onTick : function() { return true; };
    self.onFinish = params.onFinish ? params.onFinish : function() { return true; };

    //Methods
    //----------------------------------------------   
    //Updates the text for the countdown timer
    self._tick = function() {

        self.targetCurrent.setTime((self.targetCurrent.getTime()) + 1000);
        var now = (self.target - self.targetCurrent);
        //make sure success hasn't been reached

        if (now.valueOf() < 0) {
            //clear the interval and run the event
            window.clearInterval(self._interval);
            if (!self.onFinish(self.display)) { return; }

            //display the finish message
            //alert('finished');
			alert("Check back for info about next year's festival");
            //$("#countdown").html(self.message);
            return;

        };
        //update the values
        var seconds = now.valueOf() / 1000;
        var day = (Math.floor(seconds / 86400)) % 86400;
        var hrs = (Math.floor(seconds / 3600)) % 24;
        var min = (Math.floor(seconds / 60)) % 60;
        var sec = (Math.floor(seconds / 1)) % 60;

        //run the event if needed
        if (!self.onTick(self.display, day, hrs, min, sec)) { return; };

        //check for zeros
        if (self.addZeros) {
            hrs = (hrs + "").length < 2 ? "0" + hrs : hrs;
            min = (min + "").length < 2 ? "0" + min : min;
            sec = (sec + "").length < 2 ? "0" + sec : sec;
        };

        //display the new time
        if (day > 0) {
            self.display.html("<b>" + day + "</b> days &nbsp;&nbsp;<b>" + hrs + "</b> hours &nbsp;&nbsp;<b>" + min + "</b> minutes &nbsp;&nbsp;<b>" + sec + "</b> seconds to the south Florida Hanukkah festival");
        }
        else {
            self.display.html("<b>" + hrs + "</b> hours &nbsp;&nbsp;<b>" + min + "</b> minutes &nbsp;&nbsp;<b>" + sec + "</b> seconds seconds to the south Florida Hanukkah festival");
        }
    };
    //Setup Routine
    //----------------------------------------------
    self._interval = window.setInterval(self._tick, params.interval ? params.interval : 1000);

    //run immediately by default
    //if (!params.delayStart) { self.update(); };

    //return itself
    return this;

};




function startCounter(pasteddate, targetCurrent) {
    $("#countdown").countdown({ date: pasteddate, targetCurrent: targetCurrent, addZeros: 1 });
}

