﻿var site = new Events();
var projectSlider = new Class({
    Binds : ["_inputClicked","_gotoProject","_showImg","_goNext","_openPop"],
    initialize: function() {
           $("menu").getElements("input").each(function(inputElt){
                var projectId = inputElt.get("rel")
                inputElt.addEvent("click", this._inputClicked.pass(projectId));
                $("projet_"+projectId).addEvents({"mouseenter" : this._projectHover.pass([projectId, true]),
                                                 "mouseleave" : this._projectHover.pass([projectId, false])});
                $("projet_"+projectId).getElements("div.controls input.projectButton").each(function(subinputElt) {
                    subinputElt.addEvent("click", function(imgId, pId) {this._activateProject(pId);
                                                                        this._showImg(imgId, pId)}.pass([subinputElt.get("rel"),projectId], this) );
                },this);
                $("projet_"+projectId).getElements("div.controls input.projectLink").each(function(subinputElt) {
                    subinputElt.addEvent("click", this._openPop.pass(subinputElt.get("rel")) );
                },this);
           }, this);
           this.highestZ = 10;
           site.addEvent("goNext", this._goNext);
           this.lastProj=1;
           this.lastImg=0;
//           window.addEvent("load", this._inputClicked.pass("1"));
    },
    _openPop : function(url){
        window.open(url,"down");
    },
    _goNext : function() {
        var nextprojet = this.lastProj;
        var nextimage = this.lastImg + 1;
        if(!$("p_"+nextprojet + "_" +nextimage)) {
                nextprojet++;
                nextimage = 1;
                if(!$("p_"+nextprojet + "_"+nextimage)) {
                    nextprojet = 1
                }
        }
        confir
        this._showImg("p_"+nextprojet + "_"+nextimage, nextprojet);
    },
    _projectHover :  function(projectId, bool) {
        if(bool)
            $("button_"+projectId).addClass("hover");
        else 
            $("button_"+projectId).removeClass("hover");
    },
    _inputClicked: function(inputRel) {
        site.fireEvent("openProject", inputRel);
        this._gotoProject(inputRel);
    },
    _gotoProject : function(projectId) {
        if(!$("menu").retrieve("fx"))
            $("menu").store("fx", new Fx.Scroll(window, {"offset" : {'x' : -450, 'y' : -150}}))
        $("menu").retrieve("fx").toElement($("projet_"+projectId));
        $("button_"+projectId).addClass("visited");
        this._activateProject(projectId);
    },
    _activateProject : function(projectId) {
        $("projet_"+projectId).addClass("active").setStyle("z-index", this.highestZ++);
    },
    _showImg : function(imgId, pId) {
        
        this._gotoProject(pId);
        if(imgId.test(/_info$/))
            this._getInfos(pId);
        else if(imgId.test(/_next$/)) {
            var nextint = 1;
            if($("projet_"+pId).retrieve("i"))
                nextint = $("projet_"+pId).retrieve("i") + 1;
            next= pId + "_" + nextint;
            if($("p_"+next)) {
                $("projet_"+pId).store("i",nextint);
                if($("p_"+next).getStyle("display") != "inline") {
                    $("p_"+next).setStyles({"display":"inline"});
                    if($("p_"+next).getParent().retrieve("left"))
                        $("p_"+next).getParent().store("left",$("p_"+next).getParent().getSize().x);
                }
                $("p_"+next).getParent().tween("margin-left", -$("p_"+next).getPosition($("p_"+next).getParent()).x);   
            }
        }
        else if(imgId.test(/_prev$/)) {
            var prevint = 1;
            if($("projet_"+pId).retrieve("i"))
                prevint = $("projet_"+pId).retrieve("i") - 1;
            prev= pId + "_" + prevint;
            if($("p_"+prev)) {
                $("projet_"+pId).store("i",prevint);
                if($("p_"+prev).getStyle("display") != "inline") {
                    $("p_"+prev).setStyles({"display":"inline"});
                    if($("p_"+prev).getParent().retrieve("left"))
                        $("p_"+prev).getParent().store("left",$("p_"+prev).getParent().getSize().x);
                }
                $("p_"+prev).getParent().tween("margin-left", -$("p_"+prev).getPosition($("p_"+prev).getParent()).x);   
            }
        }
        else if($("p_"+imgId))
        {
            if($("p_"+imgId).getStyle("display") != "inline") {
                $("p_"+imgId).setStyles({"display":"inline"});
                if($("p_"+imgId).getParent().retrieve("left"))
                    $("p_"+imgId).getParent().store("left",$("p_"+imgId).getParent().getSize().x);
            }
            $("p_"+imgId).getParent().tween("margin-left", -$("p_"+imgId).getPosition($("p_"+imgId).getParent()).x);
        }
    },
    _getInfos : function(projectId) {
        if($("projet_"+projectId).getElement("div.projInfo2").getStyle("display") != "block") {
            if(!$("projet_"+projectId).getElement("div.projContent").retrieve("left"))
                $("projet_"+projectId).getElement("div.projContent").store("left",$("projet_"+projectId).getElement("div.projContent").getSize().x);
            $("projet_"+projectId).getElement("div.projContent").tween("margin-left", -$("projet_"+projectId).getElement("div.projContent").retrieve("left"));
            $("projet_"+projectId).getElement("div.projInfo2").setStyle("display","block");
        } else {
            $("projet_"+projectId).getElement("div.projInfo2").setStyle("display","none");    
        }
    }
});
window.addEvent("domready", function() {new projectSlider();});