/*
 * Copyright (C) 2008 Hendrik Brandt, hsf studentenradio e.V.
 * 
 * Redistribution and modification of this code is allowed under the terms
 * of GNU General Public License, Version 3. See LICENSE file for more information.
 * 
 * IN NO EVENT SHALL THE HSF STUDENTENRADIO E.V. BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
 * HSF STUDENTENRADIO E.V. HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * HSF STUDENTENRADIO E.V. SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE HSF STUDENTENRADIO E.V. HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
 * */

var Countdown = new Class(
{
	mContainer: null,
	mText: "",
	mTimer: null,
	mStart: 0,
	mStop: 0,
	mShort: false,
	
	initialize: function( pContainer, pText, pShort )
	{
		this.mContainer = pContainer;
		this.mText = pText;
		this.mShort = pShort;
	},
	
	setStart: function( pStart )
	{
		this.mStart = pStart;
	},

	setStop: function( pStop )
	{
		this.mStop = pStop;
	},

	update: function()
	{
		var tCurrent = new Date().getTime();
		
		if ( tCurrent > this.mStart && tCurrent < this.mStop )
		{
			//this.mStop = tCurrent + 3600000
			// Millis in Sekunden umrechnen
			var tDiff = parseInt( ( this.mStop - tCurrent ) / 1000 );
			
			// Tage herausfinden
			var tDays = parseInt( tDiff / 86400 );
			
			// Stunden
			var tHours = parseInt( ( tDiff - tDays * 86400 ) / 3600 );
			
			// Minuten
			var tMinutes = parseInt( ( tDiff - ( tDays * 86400 + tHours * 3600 ) ) / 60 );
			
			// Sekunden
			var tSeconds = tDiff - ( tDays * 86400 + tHours * 3600 + tMinutes * 60 );
			
			var tTime = "";
			if ( tDays > 0 )
			{
				tTime = tDays > 1 ? tDays + " Tage, " : tDays + " Tag ";
			}
			
			if ( this.mShort )
			{
				tTime += tHours < 10 ? "0" + tHours : tHours;
				tTime += ":"
				tTime += tMinutes < 10 ? "0" + tMinutes : tMinutes;
				tTime += ":"
				tTime += tSeconds < 10 ? "0" + tSeconds : tSeconds;
			}
			else
			{
				tTime += tHours;
				tTime += tHours > 9 || tHours < 1 ? " Stunden, ": " Stunde, ";
				tTime += tMinutes;
				tTime += tMinutes > 9 || tMinutes < 1 ? " Minuten und ": " Minute und ";
				tTime += tSeconds;
				tTime += tSeconds > 9 || tSeconds < 1 ? " Sekunden": " Sekunde";
			}
			
			this.mContainer.set( 'html', this.mText.replace( "{0}", tTime ) );
		}
		else
		{
			this.mContainer.set( 'html', '<!-- countdown ausgelaufen oder aktuelle Zeit ausserhalb von start/stop -->' );
		}
    },

    stop: function()
    {
    	$clear( this.mTimer );
    },
	
	run: function()
	{
		this.mTimer = this.update.periodical( 333, this );
	}
	
});

