var stdBrowser = (document.getElementById) ? true : false;
var timelineObjects = new Array();
var buttonObjects = new Array();
var printLinks = new Array();
var onlineLinks = new Array();
var linkBgAnims = new Array();
var menuGearsSlides = new Array();
var closeButtonAnim;
var activeMenu = -1;
var screenOpen = 0;

function init() {

    /**********************
    * Replace pngs for IE *
    ***********************/
    if (navigator.appName=="Microsoft Internet Explorer") {
        var root = document;

        for (var i=0, obj=null; (obj=root.all[i]); i++) {
            if (obj.currentStyle.backgroundImage.match(/\.png/i) !== null) {
                url = obj.currentStyle.backgroundImage;
                img = url.substring(5,url.length-2);
                obj.style.backgroundImage = "none";
                obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='crop')";
            }

        }
    }

    /*************************
    * Init Animation Objects *
    **************************/
    var gearsAImages = new Array();
    for (i=0;i<60;i++) {
        gearsAImages[i] = "images/gearsA/gearsA"+padDigits(i,2)+".png";
    }
    var gearsAAnim = new animObj(document.getElementById("gearsA"),100,1,gearsAImages);
    gearsAAnim.activate();

    var gearBImages = new Array();
    for (i=0;i<30;i++) {
        gearBImages[i] = "images/gearB/gearB_"+padDigits(i,2)+".png";
    }
    var gearBAnim = new animObj(document.getElementById("gearB"),100,1,gearBImages);
    gearBAnim.activate();

    var spinnerImages = new Array();
    for (i=0;i<18;i++) {
        spinnerImages[i] = "images/spinner/spinner_"+padDigits(i,2)+".png";
    }
    var spinnerAnim = new animObj(document.getElementById("spinner"),50,1,spinnerImages);
    spinnerAnim.activate();

    var flywheelImages = new Array();
    for (i=0;i<49;i++) {
        flywheelImages[i] = "images/flywheel/flywheel_"+padDigits(i,2)+".png";
    }
    var flywheelAnim = new animObj(document.getElementById("flywheel"),50,2,flywheelImages);
    flywheelAnim.activate();

    /* Button Animations */
    var printButtonImages = new Array();
    for (i=0;i<5;i++) {
		printButtonImages[i] = "images/buttons/printbutton"+i+".png";
    }
    buttonObjects[0] = new animObj(document.getElementById("printButton"),50,0,printButtonImages);

    var onlineButtonImages = new Array();
    for (i=0;i<5;i++) {
		onlineButtonImages[i] = "images/buttons/onlinebutton"+i+".png";
    }
    buttonObjects[1] = new animObj(document.getElementById("onlineButton"),50,0,onlineButtonImages);
    
    /* Link BG Animations */
    var linkBgImages = new Array();
    for (i=0;i<5;i++) {
		linkBgImages[i] = "images/linkbg/linkbg"+i+".png";
    }
    linkBgAnims[0] = new animObj(document.getElementById("bonleamonlink"),50,0,linkBgImages);
    linkBgAnims[1] = new animObj(document.getElementById("indprintlink"),50,0,linkBgImages);
    linkBgAnims[2] = new animObj(document.getElementById("indweblink"),50,0,linkBgImages);
    linkBgAnims[3] = new animObj(document.getElementById("dp9weblink"),50,0,linkBgImages);
    linkBgAnims[4] = new animObj(document.getElementById("msbweblink"),50,0,linkBgImages);
    linkBgAnims[5] = new animObj(document.getElementById("nlweblink"),50,0,linkBgImages);
    linkBgAnims[6] = new animObj(document.getElementById("eccprintlink"),50,0,linkBgImages);
    linkBgAnims[7] = new animObj(document.getElementById("chopprintlink"),50,0,linkBgImages);
    
    /* Preview Screen */
    var screenImages = new Array();
    for (i=0;i<5;i++) {
        screenImages[i] = "images/screen/previewscreen"+i+".png";
    }
    var screenAnim = new animObj(document.getElementById("previewScreen"),100,0,screenImages);
    
    var closeButtonImages = new Array();
    for (i=0;i<6;i++) {
        closeButtonImages[i] = "images/screen/close"+i+".png";
    }
    closeButtonAnim = new animObj(document.getElementById("closeButton"),100,1,closeButtonImages);
    closeButtonAnim.activate();

    /*********************
    * Init Slide Objects *
    **********************/
    var slide1 = new slideObj(document.getElementById("smoke1").style,"left",0,-1815,1,50,0,0,1);
    slide1.slide();

    var slide2 = new slideObj(document.getElementById("cloud2").style,"left",-140,document.body.clientWidth,1,100,0,1,1);
    slide2.slide();
    slide2.pos = 400;

    var slide3 = new slideObj(document.getElementById("cloud1").style,"left",-125,document.body.clientWidth,1,120,0,1,1);
    slide3.slide();
    slide3.pos = 200;

    /* Link Slides */
    printLinks[0] = new slideObj(document.getElementById("indprintlink").style,"top",15,40,2,10,0,1,0);
    printLinks[1] = new slideObj(document.getElementById("bonleamonlink").style,"top",15,60,2,10,0,1,0);
    printLinks[2] = new slideObj(document.getElementById("eccprintlink").style,"top",15,80,2,10,0,1,0);
    printLinks[3] = new slideObj(document.getElementById("chopprintlink").style,"top",15,100,2,10,0,1,0);

    onlineLinks[0] = new slideObj(document.getElementById("indweblink").style,"top",15,40,2,10,0,1,0);
    onlineLinks[1] = new slideObj(document.getElementById("dp9weblink").style,"top",15,60,2,10,0,1,0);
    onlineLinks[2] = new slideObj(document.getElementById("msbweblink").style,"top",15,80,2,10,0,1,0);
    onlineLinks[3] = new slideObj(document.getElementById("nlweblink").style,"top",15,100,2,10,0,1,0);

// function slideObj(layer,property,initVal,endVal,distance,time,pause,direction,rep) {

    /* Menu Gears Slides */
    menuGearsSlides[0] = new slideObj(document.getElementById("printGears").style,"top",30,0,2,60,0,0,0);
    menuGearsSlides[1] = new slideObj(document.getElementById("onlineGears").style,"top",30,0,2,60,0,0,0);
    
    /* Preview Screen */
//     var screenSlide = new slideObj(document.getElementById("screenContainer").style,"top",-50,20,2,10,0,1,0);
    
    /* Screen Display Object */
    var screenDisplay = new displayObj(document.getElementById("screenFrame").style,"block","none","none");

    /************************
    * Init Timeline Objects *
    *************************/
    var printTimelineObjects = new Array();
    printTimelineObjects[0] = buttonObjects[0];
    printTimelineObjects[1] = menuGearsSlides[0];
    for (i=0; i<printLinks.length; i++) {
        printTimelineObjects[printTimelineObjects.length] = printLinks[i];
    }
    timelineObjects[0] = new timeline(printTimelineObjects,0,0);

    var onlineTimelineObjects = new Array();
    onlineTimelineObjects[0] = buttonObjects[1];
    onlineTimelineObjects[1] = menuGearsSlides[1];
    for (i=0; i<onlineLinks.length; i++) {
        onlineTimelineObjects[onlineTimelineObjects.length] = onlineLinks[i];
    }
    timelineObjects[1] = new timeline(onlineTimelineObjects,0,0);
    
    var screenTimelineObjects = new Array();
    screenTimelineObjects[0] = screenDisplay;
//     screenTimelineObjects[0] = screenSlide;
    screenTimelineObjects[1] = screenAnim;
    timelineObjects[2] = new timeline(screenTimelineObjects,0,0);
    
}

function toggleButtonAnim(indx) {
	buttonObjects[indx].activate();
}

function toggleLinkAnim(indx) {
    linkBgAnims[indx].activate();
}

function toggleMenuItem(indx) {
    if (activeMenu != indx) {
        // Deactivate current menu
        if (activeMenu>-1 && activeMenu != indx ) {
            buttonObjects[activeMenu].disabled=0;
            timelineObjects[activeMenu].begin();
        }
    
        activeMenu = indx;
    
        buttonObjects[indx].disabled=1;
        timelineObjects[indx].begin();
    }
}

function toggleScreen() {
	timelineObjects[2].begin();
	screenOpen = screenOpen ? 0 : 1;
}

function padDigits(n, totalDigits) {
    n = n.toString();
    pd = '';
    if (totalDigits > n.length) {
        for (i=0; i < (totalDigits-n.length); i++) {
            pd += '0';
        }
    }
    return pd + n.toString();
}

/********************
* Visibility Object *
*********************/
function displayObj(obj,onState,offState,initState) {
    this.obj = obj;
    this.state = initState;
    this.on = onState;
    this.off = offState;
    this.stat = 0;

    this.obj.display = this.state;

    this.activate = function() {
        this.state = (this.state==this.on) ? this.off : this.on;
        this.obj.display = this.state;
    }
}

/*******************
* Animation Object *
********************/
function animObj(img,spd,rep,frameList) {
    this.id = null;
    this.timer = null;
    this.drec = 0;
    this.stat = 0;
    this.pos = 0;
    this.speed = spd;
    this.frameArray = new Array();
    this.imgObj = img;
    var obj = this;
    this.repeat = rep;
    this.disabled = 0;
    this.ftype="anim";

    for (i=0;i<frameList.length;i++) {
        var img = new Image();
        img.src = frameList[i];
        this.frameArray[i] = img;
    }

    this.activate = function() {
        if (this.disabled == 0) {
		    if (this.stat == 0)
    		    obj.animate();
		    else if (this.stat == 1)
		        this.drec = this.drec ? 0:1;
        }
    }
    
    this.halt = function() {
		if (this.disabled==0) {
            clearTimeout(this.timer);
		}
    }

    this.animate = function() {
		this.stat = 1;
        if (this.pos<this.frameArray.length-1 && this.drec==0) { this.pos++; }
        else if (this.pos>0 && this.drec==1) { this.pos--; }
        else {
            if (this.repeat == 1) {
                this.pos=0;
            }
            else if (this.repeat == 2) {
                this.drec = (this.drec) ? 0:1;
            }
            else {
                this.stat=0;
                this.drec = (this.drec) ? 0:1;
                clearTimeout(this.timer);
                return;
            }
        }

        if (navigator.appName=="Microsoft Internet Explorer") {
            this.imgObj.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.frameArray[this.pos].src + "', sizingMethod='crop')";
        }
        else {
            this.imgObj.style.backgroundImage = "url('"+this.frameArray[this.pos].src+"')";
        }
        this.timer = setTimeout(function(){obj.animate()},this.speed);
    }
}

/***************
* Slide Object *
****************/
function slideObj(layer,property,initVal,endVal,distance,time,pause,direction,rep) {
    this.id = null;
    this.timer = null;
    this.stat = 0;
    this.drec = direction;
    this.startPos = initVal;
    this.endPos = endVal;
    this.pos = this.startPos;
    this.dist = distance;
    this.time = time;
    this.layerObj = layer;
    this.repeat = rep;
    var obj = this;
    this.prop = property;
    this.delay = this.time;
    this.pause = pause;
    this.disabled = 0;
    this.ftype="slide";

    if (this.prop == "left")
        this.layerObj.left = this.startPos + "px";
    else if (this.prop == "top")
        this.layerObj.top = this.startPos + "px";

    this.activate = function() {
        obj.slide();
    }

    this.slide = function() {
        this.stat=1;
        if (this.pos>this.endPos && this.drec == 0) {
            this.pos-=this.dist;
            this.delay = this.time;
        }
        else if (this.pos<this.endPos && this.drec == 1) {
            this.pos+=this.dist;
            this.delay = this.time;
        }
        else {
            if (this.repeat == 1) {
                this.pos=this.startPos;
                this.delay = this.pause;
            }
            else if (this.repeat == 2) {
                this.drec = this.drec ? 0 : 1;
                var x = this.endPos;
                this.endPos = this.startPos;
                this.startPos = x;
                this.delay = this.pause;
            }
            else {
                this.drec = this.drec ? 0 : 1;
                var x = this.endPos;
                this.endPos = this.startPos;
                this.startPos = x;
                this.stat=0;
                clearTimeout(this.timer);
                return;
            }
        }
        if (this.prop == "left") {
            this.layerObj.left = this.pos + "px";
        }
        else if (this.prop == "top") {
            this.layerObj.top = this.pos + "px";
        }

        this.timer = setTimeout(function(){obj.slide()},this.delay);
    }
}

/***************
* Timeline Object *
****************/
function timeline(objectArray,simultaneous,rep) {
    this.pos = 0;
    this.objects = new Array();
    var obj = this;
    this.timer = null;
    this.repeat = rep;
    this.drec = 0;
    this.sim = simultaneous;
    this.stat = 0;


    for (i=0;i<objectArray.length;i++) {
        this.objects[i] = objectArray[i];
    }

    this.activate = function() {
        // Fire objects simultaneously
        if (this.sim==1) {
           if (this.pos<this.objects.length-1 && this.drec == 0) {
               this.pos++;
           }
            else if (this.pos>0 && this.drec == 1) {
               this.pos--;
            }

            else {
                this.drec = this.drec ? 0 : 1;
                clearTimeout(this.timer);
                this.stat = 0;
                return;
            }

            this.objects[this.pos].activate();
        }

        // Fire objects sequentially
        else if (this.sim==0 && this.objects[this.pos].stat==0) {
			if (this.pos<this.objects.length-1 && this.drec == 0) {
                this.pos++;
            }
            else if (this.pos>0 && this.drec == 1) {
                this.pos--;
            }

            else {
                this.drec = this.drec ? 0 : 1;
                clearTimeout(this.timer);
                this.stat = 0;
                return;
            }

            this.objects[this.pos].activate();
        }

        this.timer = setTimeout(function(){obj.activate()},10);
    }

    this.begin = function() {
        this.stat = 1;
        this.objects[this.pos].activate();
        this.timer = setTimeout(function(){obj.activate()},10);
    }
}

/***************
* Grow Object *
****************/
function growObj(targetDiv,iconName,startTop,startLeft,startSize,endSize) {
    this.id = iconName;
    this.timer = null;
    var obj = this;

    this.stat = 0;
    this.drec = 0;
    this.visible = 0;
    this.topValue = startTop;
    this.leftValue = startLeft;
    this.startValue = startSize;
    this.endValue = endSize;
    this.currentValue = startSize;

    this.endImage = new Image();
    this.startImage = new Image();

    this.time = 1;
    this.layerObj = targetDiv;

    // Init values
    this.layerObj.style.marginTop = (stdBrowser) ? this.topValue + "px" : this.topValue;
    this.layerObj.style.marginLeft = (stdBrowser) ? this.leftValue + "px" : this.leftValue;
    this.layerObj.style.width = (stdBrowser) ? this.startValue + "px" : this.startValue = startSize;
    this.layerObj.style.height = (stdBrowser) ? this.startValue + "px" : this.startValue = startSize;

    this.endImage.src = "images/icons/icon_" + this.id + "_lg.png";
    this.startImage.src = "images/icons/icon_" + this.id + "_sm.png";

	this.activate = function() {
		obj.resize();
	}

    this.resize = function() {
		this.stat = 1;
		document.getElementById('cogHighlight').style.visibility="visible";
        
		if (this.currentValue<=this.endValue && this.drec==0) {
            this.currentValue+=8;
            this.topValue-=4;
            this.leftValue-=4;
        }
        else if (this.currentValue>=this.startValue && this.drec==1) {
            this.currentValue-=8;
            this.topValue+=4;
            this.leftValue+=4;
        }
        else {
            this.stat=0;
            this.drec = (this.drec) ? 0:1;
            clearTimeout(this.timer);

            if (this.drec==1) {
                this.layerObj.src = this.endImage.src;
	         	document.getElementById('cogHighlight').style.visibility="hidden";
            }
            else if (this.drec==0) {
                this.layerObj.src = this.startImage.src;
            }

            return false;
        }
        this.layerObj.style.width = (stdBrowser) ? this.currentValue + "px" : this.currentValue;
        this.layerObj.style.height = (stdBrowser) ? this.currentValue + "px" : this.currentValue;
        this.layerObj.style.marginTop = (stdBrowser) ? this.topValue + "px" : this.topValue;
        this.layerObj.style.marginLeft = (stdBrowser) ? this.leftValue + "px" : this.leftValue;
        this.timer = setTimeout(function(){obj.resize()},this.time);
    }
}

/***************
* Orbit Object *
****************/
function orbitObj (layer, centerX, centerY, offset, radius, direction, time) {
    this.id = null;
    this.stat = 0;
    this.layer = layer;
    this.cx = centerX;
    this.cy = centerY;
    this.o = offset;
    this.r = radius;
    this.d = direction;
    this.t = Math.PI / time || Math.PI / 180;
//     this.alpha = Math.PI / 2 + this.o;
    this.alpha = -1.5707963267948977;
    var obj = this;
    
    this.x = this.cx + this.r * Math.cos(this.alpha);
    this.y = this.cy + this.r * Math.sin(this.alpha);

    this.layer.marginTop = this.y + "px";
    this.layer.marginLeft = this.x + "px";
            
    document.getElementById("info").innerHTML = "Alpha: " + this.alpha + "<br />sin: " + Math.sin(this.alpha); 
    
    this.activate = function() {
		obj.resumeOrbit();
    }

    this.stopOrbit = function() {
		this.stat = 0;
        clearInterval(this.timer);
    }

    this.resumeOrbit = function() {
        this.timer = setInterval(function(){obj.orbit()},1);
    }

    this.orbit = function() {
		this.stat = 1;
        if (this.d == 1) {
            this.alpha += this.t;
        }
        else {
            this.alpha -= this.t;
        }
            
        document.getElementById("info").innerHTML = "Alpha: " + this.alpha + "<br />sin: " + Math.sin(this.alpha);    

        this.x = this.cx + this.r * Math.cos(this.alpha);
        this.y = this.cy + this.r * Math.sin(this.alpha);

        this.layer.marginTop = this.y + "px";
        this.layer.marginLeft = this.x + "px";
        
        if (Math.sin(this.alpha) == 1 || Math.sin(this.alpha) == -1) {
            this.stopOrbit();
        }
    }
}