/*
 * 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;

    //Properties
    //----------------------------------------------            
    //set the time and day to work with
    self.display = $(this);    
    self.target = params.date;
    self.overtime = params.overtime;
    self.message = params.message?params.message:"Time is up!";
    self.addZeros = params.addZeros?params.addZeros:true;
    self.isExtra = 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() {

        //get the time difference
		var today = new Date();
		today.setTime(today.getTime()+today.getTimezoneOffset()*60*1000);		
        var now = self.target;
        self.target--;
        //make sure success hasn't been reached
        if (now < 0) {
        	if(self.overtime>0){
        		self.isExtra=true;
        		self.target=self.overtime-1;
        		now=self.target;
        		self.target--;
        		self.display.css('color','red');
        		self.overtime=0;
        	}
        	else{
        		self.display.css('color','black');
        	    //clear the interval and run the event
	            window.clearInterval(self._interval);
	            if (!self.onFinish(self.display)) { return; }
	            
	            //display the finish message
	            self.display.html(self.message);
	            return;
        	}
            
        };
        
        //update the values
        var seconds = now;        
        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(self.isExtra){
            self.display.text("Extra Time "+hrs+":"+min+":"+sec);
        }
        else{
            self.display.text(hrs+":"+min+":"+sec);
        }
    };
    
    
    //Setup Routine
    //----------------------------------------------
    self._interval = window.setInterval(
        self._tick,
        params.interval?params.interval:1000
        );
        
    //run immediately by default

    //return itself
    return this;

};