// hic sunt leones
Download = Class.create();
Download.prototype = {
	initialize: function(){
        this.pages = $("pages");
        this.interval = 5000;
        this.allpages = this.pages.getElementsByClassName("xpage");
        
        // load the images
        for (var i = 0; i < this.allpages.length; i++){
            this.allsplashes = this.allpages[i].getElementsByClassName("splashes");
            if (this.allsplashes.length > 0){
                this.allsplashes = this.allsplashes[0].getElementsByTagName("div");
                for(var j = 0; j < this.allsplashes.length; j++){
                    var count = j + 1;
                    var path = "images/";
                    path += Element.classNames(this.allsplashes[j]);
                    path += count;
                    this.allsplashes[j].innerHTML = '';
                    this.allsplashes[j].insert({ top: "<img src=\""+path+".jpg\" width=\"620\" height=\"400\">" });
                    
                }
            }
        }
        
        // hide all the pages
        for(i = 0; i < this.allpages.length; i++){
            Element.hide(this.allpages[i]);
        }
        
        // set the page to an appropriate value
        this.page = false;
        var hash = document.location.hash;
        switch (hash){
            case '#mac':
            case '#mac':
                this.page = "mac";
                break;
            case '#iphone':
                this.page = "iphone";
                break;
            case '#windows':
                this.page = "windows";
                break;
            case '#thanks':
                this.page = "thanks";
                break;
            default:
                switch(navigator.platform){
                    case 'MacPPC':
                    case 'MacIntel':
                        this.page = "mac";
                        break;
                    default:
                        this.page = "windows";
                }
        }
        
        this.setPage(this.page);
    },

    setPage : function(pg){
        if (this.fadeIn != undefined) this.fadeIn.cancel();
        if (this.fadeOut != undefined) this.fadeOut.cancel();
        if (this.fadeTimer != undefined) clearTimeout(this.fadeTimer);
        clearInterval(this.timer);
        if (pg) this.page = pg;
        for (var i = 0; i < this.allpages.length; i++){
            Element.hide(this.allpages[i]);
        }
        Element.show($(""+this.page+"page"));
        pageTracker._trackPageview("/unity/download/"+this.page);
        var splshs = $(""+this.page+"page").getElementsByClassName("splashes");
        if (splshs.length > 0){
            this.allsplashes = splshs[0].getElementsByTagName("div");
            this.currentsplash = 0;
            this.previoussplash = this.allsplashes.length - 1;
            for (i = 0; i < this.allsplashes.length; i++){
                Element.hide(this.allsplashes[i]);
            }
            this.showSplash();
            this.timer = setInterval(this.showSplash.bind(this), this.interval);
        }
    },
    showSplash: function(){
        this.fadeIn = Effect.Appear(this.allsplashes[this.currentsplash]);
        this.fadeTimer = setTimeout(this.fadeSplash.bind(this), this.interval-750);
        if (this.currentsplash < this.allsplashes.length-1){
            this.previoussplash = this.currentsplash;
            this.currentsplash = this.currentsplash + 1;
        } else {
            this.currentsplash = 0;
            this.previoussplash = this.allsplashes.length - 1;
        }
    },
    fadeSplash: function(){
            this.fadeOut = Effect.Fade(this.allsplashes[this.previoussplash], {duration: 0.5});
      
    },
    getDownload: function(plat){
        var form = document.forms[0];
        var okToSubmit = false;
        switch(plat){
            case 'windows':
                form.os.value = 'Win';
                okToSubmit = true;
                break;
            case 'mac':
                form.os.value = 'Mac';
                okToSubmit = true;
                break;
            case 'iphone':
                var approved = $("iphonedev");
                form.os.value = 'iPhone';
                if (approved.checked){
                    okToSubmit = true;
                } else {
                    alert("You must check the box indicating that you are an approved Apple Developer for the iPhone and that you have installed the paid version of the iPhone SDK before downloading Unity iPhone.");
                }
                break;
        }
        if (okToSubmit){
            this.setPage("thanks");
            form.submit();
        }
    }
};


		
Event.observe(window, 'load', function() {
	window.DownloadPage = new Download();

}, false);
