var RPi;

function RaspberryPi(model, stage3d)
{
    this.model = $( model );
    this.rotateX = 70;
    this.rotateY = 0;
    this.rotateZ = 40;


    this.traqball = new Traqball({
        stage: stage3d,
        //axis: [0.5,1,0,0.25],
        prespective: 1000
    });
    
    this.components = {
        ethernet:   new HWComponent("ethernet", this),
        usb:        new HWComponent("usb", this),
        cpu:        new HWComponent("cpu", this),
        ram:        new HWComponent("ram", this),
        sd:         new HWComponent("sd", this)
    }
    this.initSelf();
}

RaspberryPi.prototype.initSelf = function()
{
    var thisObj = this;
    
    $("#default_button").click(function(){
        thisObj.defaultPosition();
    });   
};

RaspberryPi.prototype.defaultPosition = function()
{
    //this.traqball.disable();
    this.model.addClass( "picontainer_mover" );
    this.model.css( "-webkit-transform", "rotateX(58deg) rotateY(0deg) rotateZ(45deg)");
    this.model.css( "transform", "rotateX(58deg) rotateY(0deg) rotateZ(45deg)" );
    var thisObj = this;
    setTimeout(function(){
        thisObj.model.removeClass( "picontainer_mover" );
        //thisObj.traqball.activate();
    },500);
    

};

RaspberryPi.prototype.hideAll = function()
{
    for(var key in this.components)
    {
        var comp = this.components[key];
        if(comp.out)
        {
            comp.animateIn();
        }
    }
}

$(".sd").click(function(){
    this.addClass("sdout");
    alert("Hello");
});

function HWComponent( id, rpi )
{
    var thisObj = this;
    this.parentRPi = rpi;
    this.element = $("."+id );
    this.moverClassOut = id + "-mover-out";
    this.moverClassIn = id + "-mover-in";
    this.moverClassExt = id + "-ext";
    this.out = false;
    
    this.element.click(function () 
    {
        if (thisObj.out === false)
        {
            thisObj.parentRPi.traqball.disable();
            thisObj.parentRPi.defaultPosition();
            setTimeout(function() {
                thisObj.animateOut();
            }, 500);
        }
        else
        {
            thisObj.animateIn();
            setTimeout(function() {
                thisObj.parentRPi.traqball.activate();
                console.log("Activated")
            }, 500);
        }
    });
}

HWComponent.prototype.animateOut = function()
{
    var thisObj = this;
    this.element.addClass(this.moverClassOut);
    this.parentRPi.hideAll();
    this.element.bind("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd", function(){
        thisObj.element.addClass(thisObj.moverClassExt);
        thisObj.element.removeClass(thisObj.moverClassOut);
        thisObj.element.unbind("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd");
    });
    this.out = true;    
};    

HWComponent.prototype.animateIn = function()
{
    var thisObj = this;
    this.element.removeClass(this.moverClassExt);
    this.element.addClass(this.moverClassIn);
    
    this.element.bind("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd", function(){
        thisObj.element.removeClass(thisObj.moverClassIn);
        thisObj.element.unbind("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd");
    });
    this.out = false;
};

$(document).ready(function()
{
    RPi = new RaspberryPi(".pi", "stage");
});

@@ -38,24 +38,41 @@
  stage3d)%0A%7B%0A
+    'use strict'%0A
     this.mod
@@ -566,24 +566,25 @@
  this)%0A    %7D
+;
 %0A    this.in
@@ -629,37 +629,55 @@
 tSelf = function
+ 
 ()%0A%7B%0A
+    'use strict'%0A
     var thisObj 
@@ -720,32 +720,33 @@
 ).click(function
+ 
 ()%7B%0A        this
@@ -827,29 +827,47 @@
 n = function
+ 
 ()%0A%7B%0A
+    'use strict'%0A
     //this.t
@@ -1315,24 +1315,41 @@
 unction()%0A%7B%0A
+    'use strict'%0A
     for(var 
@@ -1503,87 +1503,8 @@
 %0A%7D%0A%0A
-$(%22.sd%22).click(function()%7B%0A    this.addClass(%22sdout%22);%0A    alert(%22Hello%22);%0A%7D);%0A
 %0Afun
@@ -1530,24 +1530,41 @@
 id, rpi )%0A%7B%0A
+    'use strict'%0A
     var this
@@ -2344,32 +2344,49 @@
  = function()%0A%7B%0A
+    'use strict'%0A
     var thisObj 
@@ -2865,24 +2865,41 @@
 unction()%0A%7B%0A
+    'use strict'%0A
     var this

add strict