﻿///<reference path="mootools-1.2.js" />
var textSlider = new Class({
    Implements: Events,
    initialize: function(infosObj, curProject){
        var oldCenter = $$("#middle div.center").dispose();
        var oldRight = $$("#middle div.right").dispose();
        var oldLeft = $$("#middle div.left").dispose();
        $$("form").set("styles",{"position" : "absolute","z-index":"1","width":"100%","top" : 0, "left": 0});
        this.centeredContainer = new Element("div").inject(document.body).set("styles",{"position" : "absolute","z-index":"5","min-width":"950px", "top":159,"left" : 0, "width" : "100%", "height" : 450, "overflow" : "hidden"})
        this.sepStr = " | ";
        this.locked = false;
        this.mainDataObj = infosObj;
        this.numPages = infosObj.length;
        
        this.setProjects(curProject);
        this.setMenu(curProject);
        window.addEvent("resize", this.placeProjects.bind(this));
        this.mainDataObj[curProject].pageElt = new textpage(this.mainDataObj[curProject],this,oldCenter);
        if(this.previousPrj >= 0) this.mainDataObj[this.previousPrj].pageElt = new textpage(this.mainDataObj[this.previousPrj],this,oldLeft);
        if(this.nextPrj < this.mainDataObj.length) this.mainDataObj[this.nextPrj].pageElt = new textpage(this.mainDataObj[this.nextPrj],this,oldRight);
        this.createPage(curProject);
        if(this.previousPrj > 0) this.createPage(this.previousPrj);
        if(this.nextPrj < this.mainDataObj.length) this.createPage(this.nextPrj);
      
    },
    createPage : function(objNum) {
        var obj = this.mainDataObj[objNum];
        if(!$chk(obj.pageElt)) {
            var pageElt = new textpage(obj,this, null);
            obj.pageElt = pageElt;
        }
        var objNumVar = objNum;
        obj.pageElt.container.addEvent("click", function(ev) {
                if(this.currentPrj != objNumVar) this.movePage(objNumVar);
                }.bind(this));
        if(this.currentPrj == objNumVar) {
            obj.pageElt.fx.set('left',150);
            obj.pageElt.container.setStyle("display", "block");
            
        } else if(this.nextPrj == objNumVar) {
            obj.pageElt.fx.set('left',this.miniWidth()-100);
            obj.pageElt.container.setStyle("display", "block");
        } else if(this.previousPrj == objNumVar) {
            obj.pageElt.fx.set('left',-550);
            obj.pageElt.container.setStyle("display", "block");
        } else {
            obj.pageElt.container.setStyle("display", "none");
        }
    },
    placeProjects : function() {
        
        if(window.getWidth() < this.miniWidth()) $$("form").set("styles" , {"width" :  this.miniWidth()});
        else $$("form").set("styles" , {"width" :  "100%"});
    },
    setMenu : function(menuPos) {
        var menufound, submenufound, isSubMenu;
        menufound = submenufound = isSubMenu = false;
        var tableProjects  = [];
        var numPages = this.numPages;
        for(var i = 0; i < numPages; i++) {
            var isPage = (menuPos == i);
            submenufound = submenufound || isPage;
            tableProjects.push([(i+1).toString(), (i).toString()]);
        }
        this.trimMenus(tableProjects,$("projectsImg"), menuPos);
    },
    trimMenus : function (theTable, container, curNum) {
        container.empty();
        var counter = 0;
        var i = 0;
        var finalStr = "";        
        while(counter <= 70 && i < theTable.length) {
            new Element("a").set('text',theTable[i][0].trim()).addClass(i ==  curNum? "active":"link").addEvent("click",this.movePage.pass(theTable[i][1],this)).inject(container);
            counter  += theTable[i][0].length;
            if(i != theTable.length - 1 && counter <= 70) new Element("span").addClass("separator").set('text',this.sepStr).inject(container);
            i ++;
        }
        if(counter > 70) new Element("span").set("text","…").inject(container);
        return finalStr;
    },
    getPage : function (pos) {
        if(this.locked) return;
        if(pos == this.currentPage) return;
        this.currentPage = pos % this.pages.length;
        this.showImage();
    },
    movePage : function(newPos) {
        newPos = newPos.toInt();
        switch(newPos) {
         case this.nextPrj.toInt() : 
            if(this.previousPrj >= 0) {
                var prevPrj = this.getProject(this.previousPrj);
                prevPrj.pageElt.fx.start(-1000);  
            }
            if(this.nextPrj < this.mainDataObj.length) {
                var nextProj = this.getProject(this.nextPrj);
                nextProj.pageElt.fx.start(150);
            }
            var curPrj = this.getProject(this.currentPrj);
            
            curPrj.pageElt.fx.start(-550);
            if(this.nextnextPrj < this.mainDataObj.length) {
                var nextnextPrj = this.getProject(this.nextnextPrj);
                nextnextPrj.pageElt.fx.set('left',window.getWidth() + 500);
                nextnextPrj.pageElt.container.setStyle("display","block");
                nextnextPrj.pageElt.fx.start(this.miniWidth() - 100);
            }
         break;
         case this.previousPrj.toInt() :
            if(this.nextPrj < this.mainDataObj.length) {
                var nextProj = this.getProject(this.nextPrj);
                nextProj.pageElt.fx.start(window.getWidth() + 500);
            }
            
            if(this.previousPrj >= 0) {
                var prevPrj = this.getProject(this.previousPrj)
                prevPrj.pageElt.fx.start(150);
            }
            
            var curPrj = this.getProject(this.currentPrj)
            curPrj.pageElt.fx.start(this.miniWidth() - 100);
            
            if(this.prevprevPrj >= 0) {
                var preprevPrj = this.getProject(this.prevprevPrj);
                preprevPrj.pageElt.fx.set('left',-1000);
                preprevPrj.pageElt.container.setStyle("display","block");
                preprevPrj.pageElt.fx.start(-550);
            }
         break;
         case this.currentPrj.toInt() : 
            return;
         break;
         default:
            if(this.previousPrj != (newPos + 1) && this.previousPrj >= 0) this.getProject(this.previousPrj).pageElt.fx.start(-1000);
            if(this.nextPrj != (newPos - 1) && this.nextPrj < this.mainDataObj.length) this.getProject(this.nextPrj).pageElt.fx.start(-1000);
            
            this.getProject(this.currentPrj).pageElt.fx.start(-1000);
            
            if((newPos + 1) < this.mainDataObj.length) {
                var nextproj = this.getProject(newPos + 1);
                nextproj.pageElt.fx.set('left',window.getWidth() + 500);
                nextproj.pageElt.container.setStyle("display","block");
                nextproj.pageElt.fx.start(this.miniWidth() - 100);
            }
            
            if((newPos - 1) >= 0) {
                var prevproj = this.getProject(newPos - 1);
                prevproj.pageElt.fx.set('left',window.getWidth() + 500);
                prevproj.pageElt.container.setStyle("display","block");
                prevproj.pageElt.fx.start(-550);
            }
            
            var infObj = this.getProject(newPos)
            infObj.pageElt.fx.set('left',window.getWidth() + 500);
            infObj.pageElt.container.setStyle("display","block");
            infObj.pageElt.fx.start(150);
         break;
        }
        this.setMenu(newPos);
        this.setProjects(newPos);
        this.fireEvent("onBeforeMove");
        return false;
    },
    setProjects : function (curProjNum) {
        curProjNum = curProjNum.toInt();
        if(curProjNum >= this.mainDataObj.length) curProjNum = 0;
        var curProj = this.mainDataObj[curProjNum];
        this.currentPrj = curProjNum;
        this.nextPrj = curProjNum + 1
        this.previousPrj = curProjNum - 1;
        this.nextnextPrj = curProjNum + 2;
        this.prevprevPrj = curProjNum - 2;
        
    },
    showProject : function (projectLabel) {
        this.locked = true;
        this.setProjects(projectLabel);
    },
    showImage : function() {
        var pgN = "";
        this.pages.each(function (curP, it) {
            var pageNum = it;
            curP.container.setStyle("display",pageNum == this.currentPage ? "block" : "none");
        }, this);  
    },
    getProject : function (projectNumber){
        var project = this.mainDataObj[projectNumber];
        if(!$chk(project.pageElt)) this.createPage(projectNumber);
        return project;
    },
    miniWidth:function () {
           return Math.max(window.getWidth(), 950);
    }
    
});
var textpage = new Class({
    initialize: function(projObj, parentClass, itemContainer){
        if($chk(itemContainer) && $chk(itemContainer[0])) this.container =  itemContainer[0].inject(parentClass.centeredContainer).erase('class').erase('styles').set('styles',this.styleObj);
        else {
            this.container = new Element("div").inject(parentClass.centeredContainer).set('styles',this.styleObj).set("html","<img src='/assets/ajax-loader.gif' alt='chargement' />");
            new Request.HTML({url:'/ajax/getPage.aspx', update:this.container}).get({'c': projObj.label, 'p' : projObj.page});
        }
        
        this.fx = new Fx.Tween(this.container, {property : 'left',duration:300})
        
        this.parentClass = parentClass;
        this.parentClass.addEvent("onBeforeMove", this.switchClasses.bind(this));
        
        this.switchClasses();
  },
  
  switchClasses : function () {
    if(this.parentClass.nextPrj == this.projLabel) this.container.addClass("nextP");
    else this.container.removeClass("nextP");
    
    if(this.parentClass.previousPrj == this.projLabel) this.container.addClass("prevP");
    else this.container.removeClass("prevP");
  },
  styleObj : {
    "position" : "absolute",
    "text-align" : "left",
    "top" : 0,
    "display" : "none",
    "height" : "450px",
    "width" : "650px",
    "font-size":"93%",
    "overflow" : "hidden"
  }
});
