﻿var ourCompany = new Events();
var criteriasSelect = new Class({
    Binds : ["_showHide","_showRoll","_toggleCat","_showRollDelay","_showRollIE","_getProject"],
    initialize : function(){
        this.curCatId = false;
        
        var listTable = $("criterias").getElements("div.list");
        //the initial class name
        this.initC = $("content").get("rel");
        // a link is actie in one of the category :
        var activeLink = $("criterias").getElement("div.list div.catCont div a.active");
        // set the content class so it shows only the right links
        if(activeLink) this.c = activeLink.get("id");
        else this.c = this.initC;
        
        var isMorethanOne = listTable.length > 1;
        listTable.each(function(curCat,i) {
            var title = curCat.getElement("div.title");
            if(isMorethanOne) {
                // toogle categeries accordeon
                title.addEvent("click", this._toggleCat.pass("jCat_"+i));
                
                var cont = curCat.getElement("div.catCont");
                
                
                cont.set({"id" :"jCat_"+i, "rel" : title.get("text")});
                // get the height :
                cont.store("h",cont.getSize().y);
                
                var fx = new Fx.Tween(cont, {"property" : "height", transition : Fx.Transitions.linear});
                
                cont.store("fx",fx);
                
                if(activeLink) {
                    var insactiveLink = curCat.getElement("div.catCont div a.active");
                    if(!insactiveLink) fx.start(0);
                    else this.curCatId = "jCat_"+i;
                } else if(i != 0) {
                    fx.start(0);
                } else {
                    this.curCatId = "jCat_"+i;
                }
            }
            // set the links roll overs :
            curCat.getElements("div.catCont div a").each(function(aElt) {
                // get rid of the href => we go ajax
                aElt.set("href", "javascript:void(0)").addEvent("click",this._showHide.pass(aElt.get("id")));
                if(Browser.Engine.trident) {
                    aElt.addEvent("mouseenter", this._showRollIE.pass([aElt.get("id"), true]));
                    aElt.addEvent("mouseleave", this._showRollIE.pass([aElt.get("id"), false]));    
                } else {
                    aElt.addEvent("mouseenter", this._showRoll.pass([aElt.get("id"), true]));
                    aElt.addEvent("mouseleave", this._showRoll.pass([aElt.get("id"), false]));    
                }
            }, this);
        }, this);
        // get rid of the tool tips and prepare links for the exchanger
        $("content").getElements("a.lkcloud").each(function(a){
                a.set({"href" : "javascript:void(0)", "title" : ""}).addEvent("click", this._getProject.pass(a.get("id")));
        }, this);
    },
    _toggleCat : function(newCat) {
      if(newCat != this.curCatId) {
        if(this.curCatId  && $(this.curCatId)) {
            $(this.curCatId).retrieve("fx").cancel();
            $(this.curCatId).retrieve("fx").start(0);
            $("content").set("class", this.initC);
            this.c = this.initC;
        }
        if(newCat && $(newCat)) {
            this.curCatId = newCat;
            $(newCat).retrieve("fx").cancel();
            $(newCat).retrieve("fx").start($(newCat).retrieve("h"));
        }
      } else {
         $(this.curCatId).retrieve("fx").cancel();
         $(this.curCatId).retrieve("fx").start(0);
         $("content").set("class", this.initC);
         $(this.curCatId).getElements("div a.active").removeClass("active");
         this.curCatId = "";
         this.c = this.initC;
      }
    },
    _showHide : function(inputId) {
        var input = $(inputId);
        
        if(!input.hasClass("active")) {
            //the category is not selected
           input.addClass("active");
           $("content").addClass(inputId);
           
           if(this.c) {
                var newstore = this.c.split(" ").erase("all").include(inputId).join(" ");
                this.c = newstore;
            }
        } else {
           //the category is selected
           input.removeClass("active");
           $("content").removeClass(inputId);
           
           if(this.c) {
                var newstore = this.c.split(" ").erase(inputId).join(" ");
                this.c = newstore == "" ? "all" : newstore;
           }
        }
        
        // todo : isn't it done before ?
        if($("content").get("class") != "") $("content").removeClass("all");
        else $("content").addClass("all");
        
        // change the links :
//        if(!$("content").hasClass("all")) {
//            var s = this.c.split(" ").map(function(e){return e.substr(2)}).join(".");
//            var curcat = $(this.curCatId).get("rel");
//            
//        }
    },
    _getProject : function(linkId) {
        var url = linkId.substr(3);
        if(!$("content").hasClass("all")) {
            var s = this.c.split(" ").map(function(e){return e.substr(2)}).join(".");
            var curcat = $(this.curCatId).get("rel");
            url = url+"/c/"+curcat+"/v/"+s;
        }
        window.location = "/projects/"+url;
    },
    _showRollDelay : function(inputId, bool) {
        if(this._showRollInt) $clear(this._showRollInt);
        this._showRollInt = this._showRoll.delay(10, this,[inputId, bool]);
    },
    _showRoll : function(inputId, bool) {
//        if(this._showRollInt) $clear(this._showRollInt);
        var contentdiv = $("content");
        if(bool) {
            //this.curC = contentdiv.get("class");
            contentdiv.removeClass("all");
            contentdiv.addClass(inputId);
            ourCompany.fireEvent("title.change", inputId);
        } else {
            contentdiv.set("class", this.c);
            ourCompany.fireEvent("title.hide", "");
        }
    }, 
    _showRollIE : function(inputId, bool) {
        var contentdiv = document.getElementById("content");
        if(bool) {
            var curclassArr = contentdiv.getAttribute("className").split(" ");
            var found = false;
            for(var i = 0; i < curclassArr.length; i++) {
                if(curclassArr[i] == "all"){
                    curclassArr[i] = inputId;
                    found = true;
                }
            }
            if(!found) curclassArr.push(inputId);   
            
            contentdiv.setAttribute("className", curclassArr.join(" "));
            ourCompany.fireEvent("title.change", inputId);
        } else {
            contentdiv.setAttribute("className", this.c);
            
            ourCompany.fireEvent("title.hide", "");
        }
    }
});
var criteriasChartSelect = new Class({
    Binds : ["_showHide","_showRoll","_drawChart"],
    initialize : function(){
        this.curCatId = false;
        var listTable = $("criterias").getElements("div.list");
        listTable.each(function(curCat,i) {
            var title = curCat.getElement("div.title");
            if(listTable.length > 1) {
                title.addEvent("click", this._toggleCat.pass("jCat_"+i));
                var cont = curCat.getElement("div.catCont");
                cont.set("id", "jCat_"+i);
                cont.store("h",cont.getSize().y);
                var fx = new Fx.Tween(cont, {"property" : "height"});
                cont.store("fx",fx);
                fx.start(0);
            }
            curCat.getElements("div.catCont div a").each(function(aElt,i) {
                aElt.addEvent("click", this._showHide.pass(aElt.get("id")));
                aElt.addEvent("mouseenter", this._showRoll.pass([aElt.get("id"), true]));
                aElt.addEvent("mouseleave", this._showRoll.pass([aElt.get("id"), false]));
            }, this);
        }, this);
        $("criterias")
        $("content").store("c", "all");
    },
    _toggleCat : function(newCat) {
      if(newCat != this.curCatId) {
        if(this.curCatId  && $(this.curCatId)) {
            $(this.curCatId).retrieve("fx").cancel();
            $(this.curCatId).retrieve("fx").start(0);
            $("content").set("class", "all").store("c", "all");
        }
        if(newCat && $(newCat)) {
            this.curCatId = newCat;
            $(newCat).retrieve("fx").cancel();
            $(newCat).retrieve("fx").start($(newCat).retrieve("h"));
        }
      } else {
         $(this.curCatId).retrieve("fx").cancel();
         $(this.curCatId).retrieve("fx").start(0);
         $("content").set("class", "all").store("c", "all");
      }
    },
    _showRoll : function(inputId,bool) {
        if(bool) {
            $("content").store("c", $("content").get("class"));
            $("content").removeClass("all");
            $("content").addClass(inputId);
        } else {
            $("content").set("class", $("content").retrieve("c"));
            $("content").store("c", false);
        }   
    },
    _showHide : function(inputId) {
        var input = $(inputId);
        if(!input.hasClass("active")) {
           input.addClass("active");
           $("content").addClass(inputId);
           if($("content").retrieve("c")) {
                var newstore = $("content").retrieve("c").split(" ").erase("all").include(inputId).join(" ");
                $("content").store("c", newstore);
            }
        } else {
           input.removeClass("active");
           $("content").removeClass(inputId);
           if($("content").retrieve("c")) {
                var newstore = $("content").retrieve("c").split(" ").erase(inputId).join(" ");
                $("content").store("c", newstore == "" ? "all" : newstore);
           }
        }
        if($("content").get("class") != "") $("content").removeClass("all");
        else $("content").addClass("all");   
    }
});
var titleSwitch = new Class ({
    Binds : ["_switchTitle","_switchTitleIE"],
    initialize:function() {
        $("content").getElements("a.lkcloud, div.lkThumb, div.paragraphText").each(function(aElt) {
            aElt.addEvent("mouseenter", this._switchTitle.pass([aElt.get("id"), false, true]));
            aElt.addEvent("mouseleave", this._switchTitle.pass([aElt.get("id"), false, false]));
        },this);
         if(Browser.Engine.trident) {
                ourCompany.addEvent("title.change", function(e){this._switchTitleIE(e, true, false);}.bind(this));
                ourCompany.addEvent("title.hide", function(e){this.t = "";this._switchTitleIE("", false, false);}.bind(this));
            } else {
                ourCompany.addEvent("title.change", function(e){this._switchTitle(e, true, false);}.bind(this));
                ourCompany.addEvent("title.hide",function(e){this.t = "";this._switchTitle("", false, false);}.bind(this));
            }
        this.t = "";
        this.c = $("mainTitle").get("text");
    },
    _switchTitle: function(linkId, constant, over ) {
        var link = linkId != "" ? $(linkId) : null;
        
        var linkrel = link && $chk(link.get("rel")) ? link.get("rel") : "";
        if(constant) {
            $("mainTitle").set("text",this.c+ " "+ linkrel);
            this.t = " " + linkrel;
        } else {
            if(over){
                $("mainTitle").set("text",this.c + " " + linkrel);
            } else {
                $("mainTitle").set("text", this.c + this.t);   
            }
        }
    },
    _switchTitleIE: function(linkId, constant, over ) {
        var rel = linkId != "" ? document.getElementById(linkId).getAttribute("rel") : "";
        var mainTitle = document.getElementById("mainTitle");
        if(constant) {
            mainTitle.innerText = this.c+ " "+ rel;
            this.t = " " + rel;
        } else {
            if(over){
                mainTitle.innerText = this.c + " " + rel;
            } else {
                mainTitle.innerText = this.c + this.t;
            }
        }
    }
});
var contactSlider = new Class ({
    initialize : function() {
        var sliderAdd = $("sliderAddress").setStyle("overflow","hidden");
        var fx = new Fx.Scroll(sliderAdd);
        $("contactFrance").addEvent("mouseenter", function() {
            $("contactFrance").addClass("active");
            $("contactSwiss").removeClass("active");
            $("contactPortugal").removeClass("active");
            
            $("sliderAddress").retrieve("fx").cancel();
            $("sliderAddress").retrieve("fx").start(0,125);
        });
        $("contactSwiss").addEvent("mouseenter", function() {
            $("contactFrance").removeClass("active");
            $("contactPortugal").removeClass("active");
            $("contactSwiss").addClass("active");
            $("sliderAddress").retrieve("fx").cancel();
            $("sliderAddress").retrieve("fx").start(0,0);
        });
        $("contactPortugal").addEvent("mouseenter", function () {
            $("contactFrance").removeClass("active");
            $("contactPortugal").addClass("active");
            $("contactSwiss").removeClass("active");
            $("sliderAddress").retrieve("fx").cancel();
            $("sliderAddress").retrieve("fx").start(0, 250);
        });
        sliderAdd.store("fx",fx);
        fx.set(0, 0);
        $("contactPortugal").removeClass("active");
        $("contactFrance").removeClass("active");
        $("contactSwiss").addClass("active");
        //fx.start(0,0);
    }
});
var visitors = new Class({
    Binds : ["_setNum","_sendReq"],
    initialize : function() {
        this.req = new Request({"url" : "/ajax/getVisitors.aspx" });
        this.req.addEvent("complete", this._setNum);
        this._sendReq.periodical(30000);
        this._sendReq();
        $("lottery").store("fx",new Fx.Tween($("lottery"), {"property" : "opacity"}));
    },
    _setNum : function(a,b,c) {
        $("lottery").retrieve("fx").start(0).chain(function(){$("lottery").set("html",a); this.callChain()},function(){this.start(1)});
    },
    _sendReq : function () {
        this.req.send();
    }
});
function chartDrawer () {
    $("content").getElements("div.chartCat").each(function(chartDiv){
        var newDiv = new Element("div").inject(chartDiv);
        var data = new google.visualization.DataTable();
        data.addColumn('string', chartDiv.getElement("div.chartCatTitle").get("text"));
        data.addColumn('number', "percent");
        var colorArr = ["#009600","#0096FF", "#FF9600", "#FF0096", "#C80000"];
        //var colorArr = ["#E5F4E5","#E5F4FF", "#FFF4E5", "#FFE5F4", "#FAEAE2"];
        var newcolArray = [];
        chartDiv.getElements("div.chartBar").each(function(curPer) {
             var infoarr = curPer.get("rel").split("|");
             data.addRow([infoarr[0], infoarr[1].toInt()]);
             newcolArray.include(colorArr[infoarr[2]]);
        });
        var chart = new google.visualization.PieChart(newDiv);
        //, colors : 
        
        chart.draw(data, {width: 120, height: 120, is3D: false, title: '', legend : 'none', colors : newcolArray});
    });
}
var getNews = new Class({
    Binds : ["_slideIn"],
    initialize : function() {
        if($("newsContent")) {
            $("newsContent").setStyles({"display" : "none"});
            var req = new Request.HTML({"url" : "/ajax/getNews.aspx", update :  $("newsContent")}).send();
            req.addEvent("complete", this._slideIn);
        }
    },
    _slideIn : function() {
        var h = $("newsContent").getDimensions(true).height;
        $("newsContent").setStyles({"overflow" : "hidden", height : 0, "display" : "block", opacity:0, "visibility" : "visible"});
        var myFx = new Fx.Tween('newsContent', {property: 'height'});
        myFx.start(h).chain(function(){$("newsContent").tween("opacity",1)});
        
    }
}); 
var getNewsContent = new Class({
    Binds : ["_slideIn"],
    initialize : function() {
        if($("bignewsContent")) {
            $("bignewsContent").setStyles({"display" : "none"});
            var req = new Request.HTML({"url" : "/ajax/getNews.aspx?isDetail=true", update :  $("bignewsContent")}).send();
            req.addEvent("complete", this._slideIn);
        }
    },
    _slideIn : function() {
        var h = $("bignewsContent").getDimensions(true).height;
        $("bignewsContent").setStyles({"display" : "block", opacity:0, "visibility" : "visible"});
        $("bignewsContent").tween("opacity",1);
        $("bignewsContent").getElements("div.paragraphText a.addthis_button").each(function(aElt,i) {aElt.set("id", "post_th_"+i);  addthis.button("#post_th_"+i, {}, {url:aElt.get("rel")});});
        
        //addthis.button('.sharing-button');
    }
}); 
window.addEvent("domready", function() {new contactSlider(); new visitors();});