
var logoPlacer = new Class({
    initialize: function(){
        this.dates = [];
        this.cols = 4;
        this.rows = 5;
        this.numDates = 5;
        this.i = -1;
        this.isChanging = false;
	    this.datesFixed = false;
        this.formatDates(this.getDates());
        this.curdate = $random(0, this.dates.length-1);
        $("mF").addEvents({"mousemove":this.changeDate.bindWithEvent(this),"mousedown":this.goDiaporama.bindWithEvent(this)}).setStyle("cursor","pointer");
        this.soundMovie = new Swiff("sounds.swf", {container : new Element("div").inject($(document.body))});
        this.diaporamaContainer = new Element("div",{"id" : "diapCont","styles" : {"width" : "100%", "height" : "100%","position" : "absolute", "display" : "none"}}).inject($("mF"));
        new Asset.images(["assets/typeface.gif"])
        this.resetDate();
        window.addEvent("diapComplete",this.reinit.bind(this));
        this.dates[this.curdate].each(function(cla,i){
                    if($("L"+(i+1))) $("L"+(i+1)).set("class", cla);
                },this);
    },
    reinit : function(){
        this.diaporamaContainer.setStyle("display","none");
        this.formatDates(this.getDates());
        this.fixTheDate.pass(false, this).delay(1000);
        //this.formatDates(this.getDates());
    },
    getDates : function (){
        var dates = [];
        var lastdate;
        for(var i =0; i < this.numDates + 1; i++){
            var curdate = $random(1958, 2058);
            lastdate = curdate;
            dates.include(curdate);
        }  
        dates.sort();
        return dates;
    },
    formatDates : function (datesarray) {
        this.dates = [];
        datesarray.each(function(date){this.dates.include(this.formatDate(date));},this);
    },
    formatDate : function(date){
        var arT = [];
        if(date == 2058) arT.push("th");
        else arT.push("thout");
        var mil = Math.floor(date/1000);
        arT.push("l"+mil);
        var cent = Math.floor((date-mil*1000)/100);
        arT.push("l"+cent);
        var diz = Math.floor((date-mil*1000-cent*100)/10);
        arT.push("l"+diz);
        var unit = date-mil*1000-cent*100-diz*10;
        arT.push("l"+unit);
        return arT;
    },
    changeDate : function(ev) {
	    if(this.datesFixed) return;
        var c = Math.round(ev.page.x/window.getSize().x*(this.rows-1))+1;
        var r = Math.round(ev.page.y/window.getSize().y*(this.cols-1))+1;
        var i = (r*this.rows + c) % this.dates.length;
        if(this.i == i || this.isChanging) return;
        this.i = i;
        this.resetDate();
        this.pickRandomDate().each(function(cla,i){
                    if($("L"+(i+1))) $("L"+(i+1)).set("class", cla);
                },this);
    },
    pickRandomDate: function(){
        var rand = $random(0, this.dates.length-1);
        this.curdate = (rand == this.curdate ? rand +1 : rand) % this.dates.length;
        Swiff.remote(this.soundMovie.toElement(), 'changeSound', this.curdate);
        return this.dates[this.curdate];
    },
    resetDate : function (){
        for(var i =1; i <= 5; i++){
            if($("L"+i)) $("L"+i).set("class", "out");
        }
    },
    fixTheDate : function(bool){
	    if(bool) {
		    this.datesFixed = true;
		    this.resetDate();
	    } else {
	        this.datesFixed = false;
	    }
    },
    goDiaporama : function(){
	    if(!this.datesFixed) {
	        if($("diapCont")) $("diapCont").destroy();
	        this.diaporamaContainer = new Element("div",{"id" : "diapCont","styles" : {"width" : "100%", "height" : "100%","position" : "absolute"}}).inject($("mF"));
	        this.fixTheDate(true);
	        Swiff.remote(this.soundMovie.toElement(), 'fixTheSound', true);
	        this.diaporamaMovie = new Swiff("Diaporama.swf", { width : "100%", 
							        height: "100%",
							        params : {'bgcolor' : '#000000', 'wMode' : 'opaque'},
							        container : this.diaporamaContainer,
							        callBacks:{"complete": this.reinit}});
        }
    }
});
function complete (){
    window.fireEvent("diapComplete");
}
window.addEvent("domready", function () {new logoPlacer()});