/*
 * 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 jur to work with
    self.display = $(this);    
    self.target = ( typeof( params.date) !== "Object" ? new Date(params.date) : params.date );        
    self.message = params.message ? params.message : "Nous y sommes!";
    self.addZeros = params.addZeros ? params.addZeros : false;
	self.displayMinutes = params.displayMinutes ? params.displayMinutes : true;
	self.displaySeconds = params.displaySeconds ? params.displaySeconds : true;
            
    //Events
    //----------------------------------------------
    self.onTick = params.onTick ? params.onTick:function() { return true; };
    self.onFinish = params.onFinish ? params.onFinish:function() { return true; };
            
    //Images
    //----------------------------------------------
    self.path = "/_Lib/jQuery/CountDown/images/";
	
	//Preload Images
    //----------------------------------------------
	for(i=0; i<=9; i++) {
		$('<img/>').attr('src', self.path + 'b' + i + '.jpg');
		$('<img/>').attr('src', self.path + 'w' + i + '.jpg');
	}
	
    //Methods
    //----------------------------------------------   
    //Updates the text for the countdown timer
    self._tick = function() {

        //get the time difference
        var now = (self.target - new Date());
        
        //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
            self.display.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) {
            //day = (day != 0 , (day+"").length<2?"0"+day:day, "");
            day = (day+"").length<2?"0"+day:day;
            hrs = (hrs+"").length<2?"0"+hrs:hrs;
            min = (min+"").length<2?"0"+min:min;
            sec = (sec+"").length<2?"0"+sec:sec;
        };
            
		// create day graphic
		day = day.toString();
		var dgfx = "";
		for (i=0;i<day.length;i++){
			var dgfx=  dgfx + "<img src='"+ self.path +"b"+ day.charAt(i) +".jpg' />";
		}
		
		//create hours graphic
		hrs = hrs.toString();
		var hgfx = "";
		for (i=0;i<hrs.length;i++){
			var hgfx = hgfx + "<img src='"+ self.path +"w"+ hrs.charAt(i) +".jpg' />";
		}
		
		//create minutes graphic
		min = min.toString();
		var mgfx = "";
		for (i=0;i<min.length;i++){
			var mgfx = mgfx + "<img src='"+ self.path +"b"+ min.charAt(i) +".jpg' />";
		}
		
		//create seconds graphic
		sec = sec.toString();
		var sgfx = "";
		for (i=0;i<sec.length;i++){
			var sgfx = sgfx + "<img src='"+ self.path +"w"+ sec.charAt(i) +".jpg' />";
		}
			
        //display the new time
        self.display.html(
            dgfx + (dgfx.length > 0, "", "<img src='"+ self.path +"days_en.png' />") +
            hgfx + "<img src='"+ self.path +"hours_en.png' />" + 
			( self.displayMinutes ? mgfx + "<img src='"+ self.path +"minutes_en.png' />" : "" ) +
			( self.displaySeconds ? sgfx + "<img src='"+ self.path +"seconds_en.png' />" : "" )
        );
    };
    
    
    //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;

};