/*
---
Script: MooCountdown.js
	MooTools Countdown

License:
	MIT-style license.

Version:
	0.1

Dependencies:
	- MooTools Core 1.2.x
	- MooTools More 1.2.x

Options:
  - Container (string): ID of the HTML element which contains the countdown. Default : ‘countdown’
  - futurDate (Timestamp in milliseconds): The countdown’s start in milliseconds. Minimal value = 1. Default : empty.
  - OnlySeconds (Booleans): If the fixed value is true, there will not be a formatting days/hours/seconds. If you wish to create a countdown for only few seconds, give this value at true. Default : false.
  - dayText, hourText, minuteText, secondText (string): Text for the administration of internalization. Default, French value for day, hour, minute, second.
  - onCompleteText (string): Text displayed after the ended of the countdown. Default : null
  - startFont: starting size of the text for the tween effect. Default : ’32px’
  - finishFont: arrival size of the text for the tween effect. Default : ’16px’
  - duration (int): length of the animation and the count of seconds. Default : ‘1000’

*/

var MooCountdown = new Class({

	Implements: [Events, Options],
	
  options:    {
    //public options
		container : $empty,
		futureDate : $empty,
		onlySeconds : false,
		dayText : '',
		hourText : '',
		minuteText : '',
		secondText : '',
	        onCompleteText : '',
		duration : 1000,
		onComplete : $empty,
		
    //private options
		amount : $empty,
		amountTotal : $empty,
	        days : $empty,
	        hours : $empty,
	        minutes : $empty,
	        seconds : $empty
	      },
	
	initialize : function(options){
		this.setOptions(options);
		
		if(this.options.onlySeconds === true) {
		  this.options.amountTotal = this.options.futureDate;
		  this.startOnlySeconds();
		}
		else {
		  this.start();
		}
	},
	
	start : function(){
    this.getCount();	
  },
	
  getCount : function(){
  
    //reset de variabele
    var out = '';
    this.now = new Date();
    
    //Berekening van de resterende tijd in milliseconden
    this.options.amount = this.options.futureDate - this.now.getTime();

    //bereken de tijd in seconden
    this.options.amount = Math.floor(this.options.amount/1000);
    this.options.amountTotal = this.options.amount;
  
    //bereken dagen
    this.options.days = Math.floor(this.options.amount/86400);
    this.options.amount = this.options.amount%86400;
    
    //bereken uren
    this.options.hours = Math.floor(this.options.amount/3600);
    this.options.amount = this.options.amount%3600;
    
    //bereken minuten
    this.options.minutes = Math.floor(this.options.amount/60);
    this.options.amount = this.options.amount%60;
    
    //bereken seconden
    this.options.seconds = this.options.amount;
    
    $(this.options.container).set('text',out);
    
    var elDays = new Element('span',{
      id : "days",
      html :  "<span class='text'>Nog:</span> " +  (this.options.days < 1 ? '' : this.options.days + " <span class='text'>dag" + (this.options.days > 1 ? 'en ' : ' ') + "</span>" ) 
    });
    
    elDays.inject(this.options.container);
    
    var elHours = new Element('span',{
      id : "hours",
      html :(this.options.hours <= 9 ? '0' : '') + this.options.hours + ":"
    });
    
    elHours.inject(this.options.container)
    
    var elMinutes = new Element('span',{
      id : "minutes",
      html :(this.options.minutes <= 9 ? '0' : '') +  this.options.minutes + "."
    });
    
    elMinutes.inject(this.options.container);
       
    var elSeconds = new Element('span',{
      id : "seconds",
      html :(this.options.seconds <= 9 ? '0' : '') +  this.options.seconds + " <span class='text'>uur</span>"
    });
    
    elSeconds.inject(this.options.container); 
	
    //effecten
    
    this.options.amountTotal--; 
    var fx = new Fx.Tween($("seconds"),{
      duration: this.options.duration,
      onComplete: function() { 
        if(this.options.amountTotal >= 0) {         
          this.getCount();
        }
        else {
           $(this.options.container).set('text',this.options.onCompleteText);
           this.fireEvent('complete');
        }
      }.bind(this)
    }).start('font-size',[this.options.startFont,this.options.finishFont]); 
    
  }
});
