/**
 * jQuery's Countdown Plugin
 *
 * display a countdown effect at given seconds, check out the following website for further information:
 * http://heartstringz.net/blog/posts/show/jquery-countdown-plugin
 *
 * @author Felix Ding
 * @version 0.1
 * @copyright Copyright(c) 2008. Felix Ding
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 * @date 2008-03-09
 * @lastmodified 2008-03-09 17:48    		 
 * @todo error & exceptions handling
*/
jQuery.fn.countdown = function(options) {
	/**
	 * app init
	*/	
	if(!options) options = '()';
	if(jQuery(this).length == 0) return false;
	var obj = this;	

	/**
	 * break out and execute callback (if any)
	 */
	if(options.seconds < 0 || typeof(options.seconds) == 'undefined')
	{
		jQuery(this).html("已结束");
		return null;
	}
	
	function right(str,count)
	{
	    return str.substring(str.length-2,str.length);
	}
	
    function intToTime(number)
    {
        var hour = Math.floor(number / 3600);
        //hour = right("0" + hour,2)
        var minute = Math.floor((number % 3600) / 60)
        minute = right("0" + minute,2);
        var second = number % 60;
        second = right("0" + second,2);
        return hour + ":" + minute + ":" + second;
    }
    
    function timeToInt(time)
    {
        var ary = time.split(":");
        return parseInt(ary[0]) * 3600 + parseInt(ary[1]) * 60 + parseInt(ary[2]);
    }
	/**
	 * recursive countdown
	 */
	window.setTimeout(
		function() {
			jQuery(obj).html(intToTime(options.seconds));
			--options.seconds;
			jQuery(obj).countdown(options);
		}
		, 1000
	);	

	/**
     * return null
     */
    return this;
};
$(document).ready(function(){

    $(".countdown").each(function(){
        if($(this).html().split(":").length == 3)
        {
            var second = timeToInt($(this).html());
            $(this).countdown({seconds: second,callback: 'timeover()'})
        }
    });
});


function timeover()
{}

function timeToInt(time)
{
    var ary = time.split(":");
    return parseInt(ary[0]) * 3600 + parseInt(ary[1]) * 60 + parseInt(ary[2]);
}

function intToTime(number)
{
    return Math.floor(number / 3600) + ":" + Math.floor((number % 3600) / 60) + ":" + number % 60;
}
