///<reference path="mootools.v1.11.js" />
var centeredContent = new Class({

    initialize: function(startpageArray){
        this.container = $("LinesContainer");
        this.library = new Hash();
        this.registerMenuItems();
        this.startpageArray = startpageArray;
        this.smLogo = new Asset.image('/assets/smLogo.png');
        new Asset.image('/images/startpage.png');
        this.ready = false;
        this.container.expand = new Fx.Morph(this.container,{duration: 2000, transition: Fx.Transitions.linear});
        this.addEvent("onShowMenu",this.showSmLogo.bind(this));

        this.addEvent("onShowMenu",function(){
                        $("imageTitles").setStyle("display","none");
                        $("legendcontainer").setStyle("display","none");
                        $("titlecontainer").setStyle("display","none");});
        this.deleteLogo.delay(1500,this);
        this.addEvent("onHideMenu",this.hideSmLogo.bind(this));
        this.history = new HistoryManager();
        this.history.register("lbl",[""], this.showContent.bind(this),function(values) {
						return ["lbl", '(', values[0], ')'].join('');
					}.bind(this),"lbl" + '\\((\[^)]+)\\)');
        this.history.start();
    },
    initScroll : function () {
        this.scrollHandler = $("SH");
        this.scrollHandler.setStyles({"display" : "block","opacity" : 0})
        var drag = this.scrollHandler.makeDraggable({container : this.container, modifiers:{x:'left',y:''}});
        this.addEvent("onShowMenu",this.updateScroller.pass(0,this,"menu"));
        drag.addEvent("onDrag", this.dragContent.bind(this));
    },
    dragContent : function(e) {
        if(!$chk(this.currentContent)) return;
        var leftContent = e.getStyle("left").toInt() / 790 * -(this.currentContent.getStyle("width").toInt()-840);
        this.currentContent.setStyle("left",leftContent+"px");
    },
    deleteLogo : function () {
        this.container.removeEvents("click");
        var delFx = new Fx.Tween("Logo",{property : "opacity", duration: 2000});
        delFx.start(0);
        delFx.addEvent("onComplete",this.expand.bind(this));
    },
    expand : function() {
        if(!Browser.Engine.trident4) $("Logo").destroy();
        this.container.expand.start({'top' : [212,0], 'bottom':[288,70], 'height':[70,516]});
        this.container.expand.addEvent("onComplete",this.initPage.bind(this));
    },
    initPage : function() {
        $('cH').setStyle("display","block");
        this.smLogo.injectInside(new Element("div", {'styles' : {'position': 'absolute', 'right' : 0, 'top' : 2}}).injectInside(this.container)).setStyle('opacity',0);
        this.ready = true;
        this.initScroll();
        this.initStartPage();
        this.showSubMenus();
        this.showSmLogo();  
    },
    initStartPage : function() {
        $("home").setStyles({"opacity" : 0, "display" : "block"}).setProperty("href","javascript:void(0);");
        $("m").setStyles({"opacity" : 0, "display" : "block"});
        this.startImageIndex = 0;
        var startImage;
        if(this.startpageArray && this.startpageArray.length > 0) this.startImageIndex = $random(0,this.startpageArray.length-1);
        
        else startImage = "/images/startpage.png"
        this.startContent = new Element("div",{'class' : 'content', 'styles' : {'width' : '100%', 'height' : 395,'background-image' : 'url('+($chk(this.startpageArray)?this.startpageArray[this.startImageIndex]:startImage)+')', 'opacity':0}}).injectInside($("cH"));
        this.library.set("startPage",this.startContent);
        this.showContent('startPage');
        (function () {this.changeStartImage.periodical(7000,this)}.bind(this)).delay(2000);
        $("home").addEvent("click",this.showContent.pass("startPage",this));
        this.addEvent("onShowMenu",this.hideHomeLink.bind(this));
        this.addEvent("onHideMenu",this.showHomeLink.bind(this));
    },
    changeStartImage : function()  {
        if(!$chk(this.startpageArray) || this.startpageArray.length == 0) return;
        var newDivonIt = new Element("div",{'styles' : { 'width' : '100%', 'height' : '100%','opacity' : 0, 'background-repeat' : 'no-repeat', 'background-position':'center center'}}).injectInside(this.startContent);
        this.startImageIndex = (this.startImageIndex + 1) % this.startpageArray.length;
        newDivonIt.setStyle('background-image' , 'url('+this.startpageArray[this.startImageIndex]+')');
        var showFx = new Fx.Tween(newDivonIt,{property : "opacity", duration: 1500});
        showFx.start(1);
        showFx.addEvent("onComplete",  function () {
                this.startContent.setStyle('background-image' , 'url('+this.startpageArray[this.startImageIndex]+')');
                newDivonIt.destroy.delay(500,newDivonIt);
        }.bind(this));
    },    
    showHomeLink : function() {
        var showFx = new Fx.Tween("home",{property : "opacity", duration: 750});
        showFx.start(1);
    },
    hideHomeLink : function() {
        var showFx = new Fx.Tween("home",{property : "opacity", duration: 750});
        showFx.start(0);
    },
    showSmLogo : function() {
        var showFx = new Fx.Tween(this.smLogo,{property : "opacity", duration: 750});
        showFx.start(1);
    },
    hideSmLogo : function() {
        var showFx = new Fx.Tween(this.smLogo,{property : "opacity", duration: 750});
        showFx.start(0);
    },
    showSubMenus : function() {
        var showFx = new Fx.Tween("m",{property : "opacity", duration: 750});
        showFx.start(1);
    },
    hideSubMenus : function() {
        var showFx = new Fx.Tween("m",{property : "opacity", duration: 750});
        showFx.start(0);
    },
    registerMenuItems : function () {
        $("m").getElements("a").each(function (link) {
                var target = link.get("href").replace(".aspx","").replace("/","")
                link.setProperty("href","javascript:void(0);");
                link.setStyle('opacity',0.9)
                link.addEvent("click",this.showMenuItem.pass(target,this));
                
                if(link.getParent() != $("L1")) {
                    this.addEvent("onShowMenu",function(linkElt) {linkElt.fade("in")}.pass(link));
                    this.addEvent("onHideMenu",function(linkElt) {linkElt.fade("out")}.pass(link));
                }
            },this);
        $("L1").FX = new Fx.Morph("L1", {duration: 750, transition: Fx.Transitions.linear});
        $("L1").FX.addEvent("onComplete", function () {if(this.getStyle("opacity") == "0") this.setStyle("display","none");}.bind($("L1")));
        this.addEvent("onHideMenu",function() {$("L1").FX.start({"opacity" : 0});});
        this.addEvent("onShowMenu",function() {$("L1").FX.start({"opacity" : 0.99});$("L1").setStyle("display","block");});
    },
    showMenuItem : function(target) {
        this.fireEvent("onHideMenu");
        this.showContent.pass(target,this).delay(1);
        this.history.start();
    },
    showContent : function(target) {
        if(target == "" || !this.ready) return;
        if(target != "") this.history.setValue("lbl",0, target);
        if($chk(this.currentContent)) {
            var showFx = new Fx.Tween(this.currentContent,{property : "opacity", duration: 750});
            showFx.start(0);
            showFx.addEvent("onComplete",function(curCont){curCont.setStyle("display","none");}.pass(this.currentContent,this));
            showFx.addEvent("onComplete",this.showContent.pass(target,this));
            this.currentContent = false;
        } else if($chk(this.library.get(target))) {
            this.library.get(target).setStyles({'opacity' : 0,'display':'block'});
            var showFx = new Fx.Tween(this.library.get(target),{property : "opacity",duration: 750});
            showFx.start(0.99);
            
            this.updateScroller(this.library.get(target).getStyle("width").toInt(),this.library.get(target),target);
            if(this.library.get(target).getElement("span.title")) $("titlecontainer").set('html',this.library.get(target).getElement("span.title").get('html')).setStyle("display","block");
            if(target == 'startPage') this.fireEvent("onShowMenu");
            this.currentContent = this.library.get(target);
        } else {
             var newContent = new Element("div", {'class':'content', 'styles' : {'opacity' : 0,'top':0,'left':0}}).injectInside($("cH"));
             this.library.set(target,newContent);
             new Request.HTML({url:'/ajax/returnContent.aspx', update:newContent, autoCancel : true, 'onSuccess' : function (a,b,c,d) {this.prepareandShowContent(newContent);}.bind(this)}).get({"target" : target})
             this.currentContent = newContent;
        }
    },
    prepareandShowContent : function (contentObj) {
    
       if(!$chk(contentObj.getElement("span.width"))) return;
       
       contentObj.setStyle("width", contentObj.getElement("span.width").get('html') + "px");
       
       
       $("titlecontainer").set('html',contentObj.getElement("span.title").get('html')).setStyle("display","block");
       
       var showFx = new Fx.Tween(contentObj,{property : "opacity",duration: 750});
       
       showFx.start(0.99);
       
       this.updateScroller(contentObj.getElement("span.width").get('html'),contentObj,contentObj.getElement("span.title").get('html'));
       
       contentObj.getElements("div.image").each(function (imageElt) {
                    $extend(imageElt,{"legend" : imageElt.getElement("span.legend").get('html')});
                    $extend(imageElt,{"title" : "double click for larger view"});
                    $extend(imageElt,{"titleR" : imageElt.getElement("span.title").get('html')});
                    
                    imageElt.addEvent("click",this.setImagesInfo.pass(imageElt,this));
                    
                    imageElt.addEvent("dblclick",this.getFullImage.pass(imageElt.getElement("span.fullsrc").get('html')+"|"+imageElt.getElement("span.title").get('html'),this));
                },this)
    },
    getFullImage: function (infos) {
        
        if(infos.length = 0 || infos.split("|")[0] == "") return;
        
        window.open("/popImage.aspx?url="+infos.split("|")[0]+"&title="+infos.split("|")[3],"full"+infos,"width="+infos.split("|")[1]+",height="+infos.split("|")[2]+",resizable,scrollbars=no,status=0");
    },
    updateScroller:function(width,contentObj,target) {
        var widthHandler = (width/840) > 1 ? 50 : 0;
        
        if(widthHandler > 0 && target != "newsletter") {
            this.scrollHandler.setStyles({"display" : "block", 'width' : widthHandler+'px', 'left' : 0});
            var moveFx = new Fx.Tween(contentObj,{property : "left",duration: 750});
            moveFx.start([-200,0]);
            var showFx = new Fx.Tween(this.scrollHandler,{property : "opacity",duration: 750});
            showFx.start(.99);
            var drag = contentObj.makeDraggable({'limit': {x:[-(width-860),0], y:[0,0]}, modifiers:{x:'left',y:''}})
            drag.addEvent("onDrag", function(e){this.scrollHandler.setStyles({'left' : e.getStyle("left").toInt()/( e.getStyle("width").toInt()-860)*-(800)})}.bind(this))
            //contentObj.addEvent("click", function (e){contentObj.getElements("img,a").each(function(el){el.fireEvent("click",e);})})
            
        } else {
            var showFx = new Fx.Tween(this.scrollHandler,{property : "opacity",duration: 750});
            showFx.start(0);
        }
    },
    setImagesInfo:function(imageObject) {
        var contentObj = imageObject.getParent();
        contentObj.getElements("div.focussed").each(function(elt){elt.removeClass("focussed")});
        $("imageTitles").set('text',imageObject.titleR).setStyle("display","block");
        $("legendcontainer").set('html',imageObject.legend).setStyle("display","block");
        contentObj.addClass("focussed");
        imageObject.addClass("focussed");
    }
});
centeredContent.implement(new Events);
formFunctions = new Hash ({
    prepareBtn: function(formId) {
    
        new Element("div",{"id" : "response_"+formId}).inject($("Form_"+formId))
        $("sendbtn_"+formId).addEvent('click', formFunctions.sendForm.pass(formId));
       console.log($("sendbtn_"+formId));
    },
	sendForm: function(formid){
	new Request.HTML({url:'/ajax/sendForm.aspx', update:$("response_"+formid)}).post($("Form_"+formid))
	   
	}
});