﻿///<reference path="mootools.v1.11.js" />
var Menu = new Class({
    initialize: function(contentObject, options){
        this.setOptions({
			orientation: "vertical",
			autoclose: true,
			closeonreclick: true,
			showvisited: true,
			menuPanelid : "myMenu",
			menuitemTag : "li",
			menuitemClass : "menuitem",
			openatstartup : '',
			getRandombackground : false,
			parentitemClass : "parentitem"
		}, options);
		this.rootElt = $(this.options.menuPanelid);
		$extend(this, { "rootElt": this.rootElt});
		if(window.ie && this.options.orientation =="horizontal") this.rootElt.setStyle("margin-top",-12);
		this.currentElement;
		this.hasStartUp = !(this.options.openatstartup == '');
		this.contentObject = contentObject;
		this.activeHover = new Array();
		this.currentRequest = new Ajax("/ajax/testAjax.aspx", {method: 'get', autoCancel : true}).request();
        this.currentRequest.addEvent('onSuccess',this.populateMenu.pass(this.rootElt,this));
        this.currentRequest.addEvent('onFailure',this.changeLocationProxy.pass(this.options.openatstartup, this))
        this.changeLinks("div#loginlink");
    },
    changeLocationProxy: function (lblstartup) {
        var d = new String(window.location.host);
        var h = new String(window.location.hash);
        var s = new String(window.location.search);
        if(!s.contains("proxy=true")){
            var lbl = "";
            if(h.contains("lbl")) lbl = "lbl=" + h.replace("lbl","");
            if(lbl=="" && s.contains("lbl=")) lbl = s.replace("?","");
            if(lbl=="" && lblstartup != "") lbl = "lbl=" + lblstartup;
            var u = "http://" + d +"?proxy=true&"+lbl;
            window.location = u;
        }
    },
    getRootElt: function () {
        return this.rootElt;
    },
    changeLinks: function (selectorStr) {
        $ES(selectorStr).each(function(el) {
            var optionsElt = el.getElement("span.options");
            if($chk(optionsElt)){
                $extend($(el), this.createOptions(optionsElt));
                var linkElt = el.getElement("a");
                if($chk(linkElt)){
                    if($(el).label != ""){
                        linkElt.addEvent('click', this.contentObject.getcontent.pass(el,this.contentObject));
                        linkElt.href = "#lbl" + $(el).label;
                    }
                }
            }
        },this);
    }, 
    populateMenu: function(element){
        menuOcc = this;
        var isHorizontal = !(this.options.orientation.contains("vertical") || this.options.orientation.contains("simpleLine"));
        element.getElementsBySelector(this.options.menuitemTag + "." + this.options.menuitemClass + "," + this.options.menuitemTag +"." + this.options.parentitemClass).each(function(el) {
            var optionsElt = el.getElement("span.options");
            $extend($(el), this.menuOcc.createOptions(optionsElt));
            var linkElt = el.getElement("a");
            if(isHorizontal) {
                if($(el).getParent() == this.menuOcc.rootElt) {
                    $(el).addClass("parentmenuBar");
                    $(el).setStyle("float","left");
                    $(el).addEvent('mouseenter', this.menuOcc.closeSub.pass(el,this.menuOcc));
                } else $(el).addClass("menuBar");
            }
            if($chk(linkElt)){
                linkElt.href = "#";
                linkElt.addEvent('click', this.menuOcc.menuclicked.pass(el,this.menuOcc));
                if($(el).label != ""){
                    linkElt.addEvent('click', this.menuOcc.contentObject.getcontent.pass(el,this.menuOcc.contentObject));
                    linkElt.href = "#lbl" + $(el).label;
                }
                
                if($(el).hasClass(this.menuOcc.options.parentitemClass) && isHorizontal){
                    $(el).addEvent('mouseenter', this.menuOcc.parentHorizontalHover.pass(el,this.menuOcc));
                    
                }
                if($(el).label == this.menuOcc.options.openatstartup && $(el).label != ""){ this.menuOcc.options.openatstartup = $(el).label;}
                
            }
        });
        if(this.hasStartUp) {var startupObj = { "label" : this.options.openatstartup };this.contentObject.getcontent(startupObj);}
    },
    parentHorizontalHover : function (el) {
        if(!$chk(el.subPosition)) {
            var childElt = $(el).getElement("ul")
            if($chk(childElt)){
                var coord = $(el).getCoordinates();
                $(childElt).setStyles({"position" : "absolute", "top" : coord.bottom, "left" : coord.left, "z-index" : 10});
                $(childElt).addEvent('mouseleave', this.closeSub.pass($(el),this))
            }
            $extend(el, {"subPosition" : true});
        }
        this.activeHover.include(el);
        while($chk(el) && el != this.rootElt){
            this.setActive(el);
            el = el.getParent()
        }
    },
    createOptions : function (elt) {
        var label = "";
        var backStr = "";
        if($chk($E("span.label",elt))) label = $E("span.label",elt).innerHTML;
        if($chk($E("span.backStr",elt))) backStr = $E("span.backStr",elt).innerHTML;
        var optobj = {
            "label" : label,
            "backStr" : backStr
        }
        return optobj;
    },
    closeSub : function (parentElt) {
        this.activeHover.each(function (thisElt) {
             var thisElttemp = thisElt;
             while($chk(thisElttemp) && thisElttemp != this.rootElt){
                    if(thisElttemp.hasClass("activeParent")) thisElttemp.removeClass("activeParent");
                    this.setInactive(thisElttemp);
                    thisElttemp = thisElttemp.getParent();
             }
         }.bind(this));
         elt = this.currentElement;
         while($chk(elt) && elt != this.rootElt){
                if(elt.hasClass("parentmenuBar")) elt.addClass("activeParent");
                elt = elt.getParent()
         }
    },
    menuclicked: function(elt){
        if(this.options.showvisited) elt.addClass("visited");
        this.update(elt);
        this.rootElt.fireEvent('onPageChange')
        if(this.options.getRandombackground) $E("body").setStyle("background-image","url(/returnbandeaux.aspx?"+$random(0,100)+")");
        if(elt.hasClass(this.options.menuitemClass)) this.fireEvent('onMenuClicked');
        if(elt.hasClass(this.options.parentitemClass)) this.fireEvent('onParentMenuClicked');
    },
    update: function(elt) {
        if(elt != this.currentElement && !elt.hasClass('active')) {
            if(this.options.autoclose){
                while($chk(this.currentElement) && this.currentElement != this.rootElt){
                    this.setInactive(this.currentElement);
                    if(this.currentElement.hasClass("activeParent")) this.currentElement.removeClass("activeParent");
                    this.currentElement = this.currentElement.getParent()
                }
            }
            elt = $(elt);
            this.currentElement = elt;
            while($chk(elt) && elt != this.rootElt){
                this.setActive(elt);
                elt = elt.getParent()
            }
        } else if(this.options.closeonreclick && elt.hasClass('active')) {
            while($chk(this.currentElement) && this.currentElement != this.rootElt && this.currentElement != elt){
                this.setInactive(this.currentElement);
                this.currentElement = this.currentElement.getParent();
            }
            this.currentElement = elt;
        }
    },
    setActive: function(elt) {
        elt.addClass('active');
    },
    setInactive: function(elt) {
        elt.removeClass('active');
    }
});
Menu.implement(new Events);
Menu.implement(new Options);
var Content = new Class({
    initialize: function(options){
        this.setOptions({
			mainpanelid: "contentPanel",
			defaultclass : "contentpage",
			transition : "fade",
			loaderIcon : "/assets/ajax-loader.gif"
		}, options);
		this.fireEvent('oninit');
		this.mainpanel = $(this.options.mainpanelid);
		this.currentelement;
		this.currentRequest;
		this.loadingDiv = new Element('div',{'id' : 'loadingIcon', 'class' : 'contentpage'}).injectInside(this.mainpanel);
		this.hideLoading();
		new Element('img',{'alt': 'loading page', 'src': this.options.loaderIcon}).injectInside(this.loadingDiv);
		new Asset.css('/styles/javascriptStyles.css', {id: 'myStyle', title: 'myStyle'});
		
    },
      
    getcontent: function(elt){
        if($chk(this.currentelement) && this.currentelement.hasClass('active')) this.currentelement.removeClass('active');
        if($chk($(elt.label))){
           this.currentelement =  $(elt.label);
           this.currentelement.setStyle('opacity',0);
           this.currentelement.addClass('active');
           Content.Transitions[this.options.transition](this.currentelement);
           this.fireEvent('onRevisit', elt.label);
        } else {
           //if($chk(this.currentRequest)) this.currentRequest.cancel();
           var newDiv = new Element('div').addClass(this.options.defaultclass).addClass('active').setProperty('id', elt.label).setStyle("opacity",0).injectInside(this.mainpanel);
           this.currentRequest = new Ajax("/ajax/ReturnPage.aspx", {method: 'post', autoCancel : true, update:newDiv, data:'label='+elt.label, evalScripts: true}).request();
           this.currentRequest.addEvent('onSuccess',function(div) {Content.Transitions[this.options.transition](div);this.hideLoading()}.pass(newDiv,this));
           this.currentRequest.addEvent('onFailure',this.hideLoading.bind(this))
           this.currentelement =  newDiv;
           this.showLoading()
        }
        if($chk(elt.backStr))this.changeBackGround(elt);
    },
    changeBackGround : function(elt){
        document.body.style.backgroundImage = "url('"+elt.backStr.split("|")[0]+"')";
        document.body.style.backgroundRepeat = (elt.backStr.split("|")[1]=="true"?"repeat":"no-repeat");
        document.body.style.backgroundPosition = elt.backStr.split("|")[2];
    },
    showLoading : function (){
        this.loadingDiv.setStyle('display','block');
    },
    hideLoading : function (){
        this.loadingDiv.setStyle('display','none');
    }
});
Content.implement(new Events);
Content.implement(new Options);
Content.Transitions = new Abstract ({
	fade: function(newDiv){
		if(newDiv.getProperty("opacity") != 1) new Fx.Style(newDiv, 'opacity', {duration:70}).start(0, 1);
	},
	none: function(newDiv) {
	    newDiv.setStyle("opacity",1);
	}
});
var News = new Class({
     initialize: function(menuOcc, mainContainerid, options){
        this.setOptions({
			autoclose: true,
			newsListContainerClass : "newsList",
			newsListItemClass : "newsListitem",
			newsItemHeaderClass : "newsHeader",
			newsItemContentClass : "newsContent",
			openFirstNews : false,
			newsMode : "inpage",
			hasIcon : false,
			backText : "page précedente"
		}, options);
		this.fireEvent('oninit');
		this.mainContainer = $(mainContainerid);
		this.ListContainer = $E("."+this.options.newsListContainerClass, mainContainerid);
		this.currentelement;
		this.populatePanel();
		this.ListContainer.addClass("active");
		menuOcc.getRootElt().addEvent('onPageChange', this.inpageReinit.pass(menuOcc,this));
		if(this.options.newsMode == "inpage"){
		    this.contentDiv = new Element('div',{'class' : 'inpageContent'}).injectInside(this.mainContainer);
		    this.backbtn = new Element('a',{'class' : 'inpageBackButton', 'href' : '#'}).injectInside(this.mainContainer);
		    this.backbtn.innerHTML = this.options.backText;
		    this.backbtn.addEvent('click', this.getBackToList.pass(this.backbtn,this));
		}
    },
   populatePanel: function () {
       $ES("."+this.options.newsListItemClass, this.mainContainer).each(function (elt) {
               var elmtObject = {"headerValue" : elt.getElement("."+this.options.newsItemHeaderClass),"contentValue" : elt.getElement("."+this.options.newsItemContentClass)};
               if(this.options.hasIcon) {
                    var iconElt = {"icon" : new Element('div',{'class' : 'newsIconClosed'}).injectTop(elmtObject.headerValue)}
                    $extend(elmtObject, iconElt);
               }
               $extend(elt, elmtObject);
               if($chk(elt.contentValue)) elt.addEvent('click', this.getNews.pass(elt,this));
       }.bind(this));
   },
   closeIcon: function(elt) {
    elt.setProperty('class','newsIconClosed')
   },
   openIcon: function(elt) {
    elt.setProperty('class','newsIconOpened')
   },
   getNews : function (elt) {
        switch(this.options.newsMode){
            case "inline" :
                this.getnewsInline(elt);
                break;
            case "inpage" : 
                this.getnewsInPage(elt);                
                break;
          }
        
   },
   getnewsInline : function (elt) {
        if($chk(this.currentelement) && (this.options.autoclose || elt.contentValue.hasClass('active'))) {this.currentelement.contentValue.removeClass('active');if($chk(this.currentelement.icon))this.closeIcon(this.currentelement.icon);}
        if($chk(this.currentelement) && elt == this.currentelement) {this.currentelement = null; return;}
        this.currentelement = elt;
        this.currentelement.contentValue.addClass('active');
        if(this.options.hasIcon) this.openIcon(this.currentelement.icon);
   },
   getnewsInPage : function (elt) {
        if($chk(this.contentDiv)) {
            this.ListContainer.removeClass('active');
            this.contentDiv.innerHTML = elt.contentValue.innerHTML;
            this.contentDiv.addClass('active');
            this.backbtn.addClass('active');
            this.currentelement = elt;
        }
   },
   getBackToList : function (elt) {
        this.contentDiv.innerHTML = "";
        elt.removeClass('active');
        this.contentDiv.removeClass('active');
        this.ListContainer.addClass('active');
        this.currentelement = null;
   },
   inpageReinit : function (menuObj) {
        if(this.options.newsMode == "inpage") this.contentDiv.innerHTML = "";
        if($chk(this.currentelement)) this.currentelement.removeClass('active');
        if(this.options.newsMode == "inpage") this.backbtn.removeClass('active');
        if(this.options.newsMode == "inpage") this.contentDiv.removeClass('active');
        if(this.options.newsMode == "inpage") this.ListContainer.addClass('active');
   }
});
News.implement(new Events);
News.implement(new Options);

var Img = new Class({
    initialize: function(element, options){
        this.setOptions({
            showThumbnails : true,
            thumbnailStyle : "right",
            linkonImage : true,
            showFirstPic : true,
            fadeDuration: 500,
            thumbWidth : 50,
            thumbHeight: 50,
            fullWidth : 400,
            fullHeight : 200,
            thumbSpacing : 5,
            fullImageWidth : 500,
            fullImageHeight : 300,
            enablediaporama : false,
            diaporamaDelay : 3000,
            legendStyle : "bottom",
            showLegends : "hover",
            legendHeight : 100,
            destroyAfterPopulate : true,
            elementSelector: "div.imageElement",
			titleSelector: "h3",
			subtitleSelector: "div",
			linkSelector: "a.open",
			imageSelector: "img.full",
			thumbnailSelector: "img.thumbnail",
			defaultTransition: "fade",
			defaultLegendTransition : "showLegend"
		}, options);
		this.fireEvent('oninit');
		this.currentIter = -1;
		this.lastIter = 0;
		this.maxIter = 0;
		this.populateFrom = $(element);
		var totalHeight = (this.options.showThumbnails && (this.options.thumbnailStyle == "top" || this.options.thumbnailStyle == "bottom") ? this.options.thumbHeight + this.options.thumbSpacing: 0) + this.options.fullHeight + (this.options.legendStyle == "top" || this.options.legendStyle == "bottom" ?  this.options.legendHeight : 0);
		var totalWidth = (this.options.thumbnailStyle == "left" || this.options.thumbnailStyle == "right" ? this.options.thumbWidth + this.options.thumbSpacing: 0) + this.options.fullWidth ;
		$(element).setStyles({'width': totalWidth , 'height': totalHeight, 'position':'absolute'})
		this.galleryData = [];
		this.populateData();
		this.galleryInit = 1;
		this.galleryElements = Array();
		this.thumbnailElements = Array();
		if(this.options.showThumbnails) {
		    var thumbwidth = this.options.thumbnailStyle == "left" || this.options.thumbnailStyle == "right" ? (this.options.thumbWidth + this.options.thumbSpacing) : '100%';
		    var thumbheight = this.options.thumbnailStyle == "top" || this.options.thumbnailStyle == "bottom" ? (this.options.thumbHeight + this.options.thumbSpacing) : '100%';
		    this.thumbnailElement = new Element('div').setStyles({'width' : thumbwidth , 'height' : thumbheight, 'position':'absolute'}).addClass('thumbContainer').injectInside($(element));
		    this.thumbnailElement.setStyle(this.options.thumbnailStyle,0);
		    this.constructThumbnails(this.thumbnailElement);
		}
		var topMain = (this.options.showThumbnails && this.options.thumbnailStyle == "top" ? (this.options.thumbHeight + this.options.thumbSpacing) : 0) + ( this.options.showLegends && this.options.legendStyle == "top" ? this.options.legendHeight : 0);
		var leftMain = this.options.showThumbnails && this.options.thumbnailStyle == "left" ? (this.options.thumbWidth  + this.options.thumbSpacing): 0
		this.galleryElement = new Element('div').setStyles({'width': this.options.fullWidth , 'height': this.options.fullHeight, 'left' : leftMain, 'top' : topMain, 'position':'absolute'}).addClass('fullContainer').injectInside($(element));
		this.constructElements();
		if(this.options.showLegends) {
		    this.legendElement = new Element('div').setStyles({'width': '100%' , 'height': this.options.legendHeight, 'position':'absolute'}).addClass('legendContainer').injectInside($(element));
		    this.createLegendPanel(this.legendElement);
		    this.legendElement.setStyle(this.options.legendStyle,0);
		}
		if ((this.galleryData.length>1)&&(this.options.linkonImage))
		{
			var leftArrow = new Element('a').setStyles({'width': this.options.fullWidth/2, 'height': this.options.fullHeight, 'position':'absolute', 'top' : topMain,'left' : leftMain, 'display':'block'}).addClass('left').addEvent(
				'click',
				this.prevItem.bind(this)
			).injectInside($(element));
			if (this.options.showLegends == "hover") leftArrow.addEvents({'mouseover' : this.showLegendPanel.bind(this), 'mouseout' : this.hideLegendPanel.bind(this)});
			var rightArrow = new Element('a').setStyles({'width': this.options.fullWidth/2, 'height': this.options.fullHeight , 'position':'absolute', 'top' : topMain,'left' : leftMain + this.options.fullWidth/2, 'display':'block'}).addClass('right').addEvent(
				'click',
				this.nextItem.bind(this)
			).injectInside($(element));
			if (this.options.showLegends == "hover") rightArrow.addEvents({'mouseover' : this.showLegendPanel.bind(this), 'mouseout' : this.hideLegendPanel.bind(this)});
		}
		if ((this.galleryData.length>0)&&(this.options.showFirstPic))
		{
		    this.goTo(0);
		}
		if(this.options.enablediaporama) this.startDiaporama();
    },
    populateData: function() {
		currentArrayPlace = this.galleryData.length;
		options = this.options;
		var data = $A(this.galleryData);
		data.extend(this.populateGallery(this.populateFrom));
		this.galleryData = data;
		this.fireEvent('onPopulated');
	},
    populateGallery: function(element) {
		var data = [];
		options = this.options;
		element.getElements(options.elementSelector).each(function(el) {
			elementDict = {
				image: el.getElement(options.imageSelector).getProperty('src'),
				number: currentArrayPlace,
				transition: this.options.defaultTransition
			};
			elementDict.extend = $extend;
			if (options.showLegends)
				elementDict.extend({
					title: el.getElement(options.titleSelector).innerHTML,
					description: el.getElement(options.subtitleSelector).innerHTML
				});
			if (options.showThumbnails)
				elementDict.extend({
					thumbnail: el.getElement(options.thumbnailSelector).getProperty('src')
				});
			data.extend([elementDict]);
			currentArrayPlace++;
			if (this.options.destroyAfterPopulate)
				el.remove();
		});
		return data;
	},
	constructElements: function() {
		el = this.galleryElement;
		this.maxIter = this.galleryData.length;
		var currentImg;
		for(i=0;i<this.galleryData.length;i++)
		{
			currentImg = new Fx.Styles(
				new Element('div').addClass('slideElement').setStyles({
					'position':'absolute',
					'left':'0px',
					'right':'0px',
					'margin':'0px',
					'padding':'0px',
					'top' : '0px',
					'bottom' : '0px',
					'backgroundPosition':"center center",
					'backgroundRepeat':'no-repeat',
					'opacity':'0'
				}).injectInside(el),
				'opacity',
				{duration: this.options.fadeDuration}
			);
			if (this.options.showLegends == "hover") currentImg.element.addEvents({'mouseover' : this.showLegendPanel.bind(this), 'mouseout' : this.hideLegendPanel.bind(this)});
			if (this.options.preloader)
			{
				currentImg.source = this.galleryData[i].image;
				currentImg.loaded = false;
				currentImg.load = function(imageStyle) {
					if (!imageStyle.loaded)	{
						new Asset.image(imageStyle.source, {
		                            'onload'  : function(img){
													img.element.setStyle(
													'backgroundImage',
													"url('" + img.source + "')")
													img.loaded = true;
												}.bind(this, imageStyle)
						});
					}
				}.pass(currentImg, this);
			} else {
				currentImg.element.setStyle('backgroundImage',
									"url('" + this.galleryData[i].image + "')");
			}
			this.galleryElements[parseInt(i)] = currentImg;
		}
		
	},
	constructThumbnails: function (element) {
		for(i=0;i<this.galleryData.length;i++)
		{
			var currentImg = new Fx.Style(new Element ('div').addClass("thumbnail").setStyles({
					"backgroundImage": "url('" + this.galleryData[i].thumbnail + "')",
					"backgroundPosition": "center center",
					"backgroundRepeat": 'no-repeat',
					"float": 'left',
					"marginRight": this.options.thumbSpacing + "px",
					"marginBottom": this.options.thumbSpacing + "px",
					"width": this.options.thumbWidth + "px",
					"height": this.options.thumbHeight + "px"
				}).injectInside(element), "opacity", {duration: 200}).set(this.options.thumbIdleOpacity);
			currentImg.element.addEvents({
				'mouseover': function (myself) {
					myself.clearTimer();
					myself.start(0.99);
					if (this.options.showCarouselLabel)
						$(this.carouselLabel).setHTML('<span class="number">' + (myself.relatedImage.number + 1) + "/" + this.maxIter + ":</span> " + myself.relatedImage.title);
				}.pass(currentImg, this),
				'mouseout': function (myself) {
					myself.clearTimer();
					myself.start(this.options.thumbIdleOpacity);
				}.pass(currentImg, this),
				'click': function (myself) {
					this.goTo(myself.relatedImage.number);
					if (this.options.thumbCloseCarousel)
						this.hideCarousel();
				}.pass(currentImg, this)
			});
			currentImg.relatedImage = this.galleryData[i];
			this.thumbnailElements[parseInt(i)] = currentImg;
		}
	},
	goTo: function(num) {
		//this.clearTimer();
		if(this.options.preloader)
		{
			this.galleryElements[num].load();
			if (num==0)
				this.galleryElements[this.maxIter - 1].load();
			else
				this.galleryElements[num - 1].load();
			if (num==(this.maxIter - 1))
				this.galleryElements[0].load();
			else
				this.galleryElements[num + 1].load();
				
		}
		this.changeItem(num);
	},
	changeItem: function(num) {
		this.fireEvent('onStartChanging');
		this.galleryInit = 0;
		if (this.currentIter != num)
		{
			for(i=0;i<this.maxIter;i++)
			{
				if ((i != this.currentIter)) this.galleryElements[i].set({opacity: 0});
			}
			Img.Transitions[this.galleryData[num].transition].pass([
				this.galleryElements[this.currentIter]?this.galleryElements[this.currentIter]:new Fx.Style(),
				this.galleryElements[num],
				this.currentIter,
				num], this)();
			this.currentIter = num;
		}
		this.fireEvent('onChanged');
	},  
	nextItem: function() {
	    
		this.fireEvent('onNextCalled');
		this.nextIter = this.currentIter+1;
		if (this.nextIter >= this.maxIter)
			this.nextIter = 0;
		this.galleryInit = 0;
		this.goTo(this.nextIter);
		if($chk(this.options.enablediaporama)) this.startDiaporama();
	},
	prevItem: function() {
		this.fireEvent('onPreviousCalled');
		this.nextIter = this.currentIter-1;
		if (this.nextIter <= -1)
			this.nextIter = this.maxIter - 1;
		this.galleryInit = 0;
		this.goTo(this.nextIter);
		if($chk(this.options.enablediaporama)) this.startDiaporama();
	},
	createLegendPanel: function(elm) {
		this.legendPanel = new Fx.Styles(elm).set({'opacity':0});
		var legendPanelTitle = new Element('h2').injectInside(this.legendPanel.element);
		var legendPanelDescription = new Element('p').injectInside(this.legendPanel.element);
		this.legendPanel.normalHeight = this.legendPanel.element.offsetHeight;
		this.legendPanel.element.setStyle('opacity',0);
	},
	showLegendPanel: function() {
		this.fireEvent('onShowLegendPanel');
		this.legendPanel.clearTimer();
		if(this.galleryData[this.currentIter]){
		    element = this.legendPanel.element;
		    element.getElement('h2').setHTML(this.galleryData[this.currentIter].title);
		    element.getElement('p').setHTML(this.galleryData[this.currentIter].description);
		    Img.Transitions[this.options.defaultLegendTransition].pass([this.legendPanel ,true], this)();
		}
		return this.legendPanel;
	},
	hideLegendPanel: function() {
		this.fireEvent('onHideLegendPanel');
		this.legendPanel.clearTimer();
		Img.Transitions[this.options.defaultLegendTransition].pass([this.legendPanel ,false], this)();
		return this.legendPanel;
	},
	startDiaporama: function () {
	    if($chk(this.diaporama)) $clear(this.diaporama);
	    this.diaporama = this.nextItem.delay(this.options.diaporamaDelay,this);
	}    
});

Img.implement(new Events);
Img.implement(new Options);

Img.Transitions = new Abstract ({
	fade: function(oldFx, newFx, oldPos, newPos){
		oldFx.options.transition = newFx.options.transition = Fx.Transitions.linear;
		oldFx.options.duration = newFx.options.duration = this.options.fadeDuration;
		if (newPos > oldPos) newFx.start({opacity: 1});
		else
		{
			newFx.set({opacity: 1});
			oldFx.start({opacity: 0});
		}
	},
	crossfade: function(oldFx, newFx, oldPos, newPos){
		oldFx.options.transition = newFx.options.transition = Fx.Transitions.linear;
		oldFx.options.duration = newFx.options.duration = this.options.fadeDuration;
		newFx.start({opacity: 1});
		oldFx.start({opacity: 0});
	},
	fadebg: function(oldFx, newFx, oldPos, newPos){
		oldFx.options.transition = newFx.options.transition = Fx.Transitions.linear;
		oldFx.options.duration = newFx.options.duration = this.options.fadeDuration / 2;
		oldFx.start({opacity: 0}).chain(newFx.start.pass([{opacity: 1}], newFx));
	},
	slideLegend: function(Fx){
	    Fx.start({'opacity': [0, this.options.slideInfoZoneOpacity], 'height': [0, this.slideInfoZone.normalHeight]});
	},
	showLegend: function(Fx, bool){
	    if(bool) Fx.element.setStyle('opacity',100);
	    else Fx.element.setStyle('opacity',0);
	},
	showOnTop : function(Fx){
	    
	}
});
var NormalImg = new Class({
    initialize: function(fullPanelid, options){
        this.setOptions({
            showThumbnails : true,
            thumbnailStyle : "right",
            linkonImage : true,
            showFirstPic : true,
            thumbSpacing : 5,
            showLegends : true,
            elementSelector: "div.imageElement",
            destroyAfterPopulate : true,
			titleSelector: "h3",
			subtitleSelector: "div",
			linkSelector: "a.open",
			imageSelector: "img.full",
			thumbnailSelector: "img.thumbnail"
		}, options);
		this.fireEvent('oninit');
		this.currentIter = -1;
		this.lastIter = 0;
		this.maxIter = 0;
		this.populateFrom = $(fullPanelid);
		if(this.options.showThumbnails) {
		    this.thumbPanel = new Element("div").setProperties({'class' : 'thumbImage'}).injectInside(this.populateFrom);
		    if(this.options.thumbnailStyle == "top"){
                this.fullPanel = new Element("div").setProperties({'class' : 'fullImage'}).injectInside(this.populateFrom);
                this.fullPanel.setStyle('float',"none");
                this.fullPanel.setStyle('clear',"left");
		    } else {
		        this.fullPanel = new Element("div").setProperties({'class' : 'fullImage'}).injectBefore(this.thumbPanel);
		        this.fullPanel.setStyle('float',"left");
		        this.thumbPanel.setStyle('float',"none");
		    }
		} else this.fullPanel = new Element("div").setProperties({'class' : 'fullImage'}).injectInside(this.populateFrom);
		this.galleryData = [];
		this.populateData();
		this.fullImage = this.createFullImage();
		if(this.options.showThumbnails) this.constructThumbnails(this.thumbPanel);
		if(this.options.showLegends) this.constructLegends(this.populateFrom);
		this.galleryInit = 1;
		if((this.galleryData.length>1)&&this.options.linkonImage) this.fullImage.addEvents({'click' : this.nextItem.bind(this)})
		if ((this.galleryData.length>0)&&(this.options.showFirstPic)) this.goTo(0);
    },
    createFullImage : function () {
        this.maxIter = this.galleryData.length;
        var fullImg = new Element('img').setProperties({'alt' : 'full size image'}).setStyles({'display' : 'block', 'opacity' : 0}).addClass('fullSizeImage').injectInside(this.fullPanel);
        return fullImg;
    },
    constructThumbnails: function (element) {
        for(i=0;i<this.galleryData.length;i++)
		{
			var currentImg = new Element ('img').addClass("thumbnail").setProperty('src', this.galleryData[i].thumbnail).setStyles( {
					"float": 'left',
					"marginRight": this.options.thumbSpacing + "px",
					"marginBottom": this.options.thumbSpacing + "px"
				}).injectInside(element);
			currentImg.addEvents({
				'click': function (myself) {
					this.goTo(myself.relatedImage.number);
					if (this.options.thumbCloseCarousel)
						this.hideCarousel();
				}.pass(currentImg, this)
			});
			currentImg.relatedImage = this.galleryData[i];
		}
    },
    constructLegends: function (element) {
        this.legendPanel = new Element ('div').addClass("legend").injectInside(element);
    },
    populateData: function() {
		currentArrayPlace = this.galleryData.length;
		options = this.options;
		var data = $A(this.galleryData);
		data.extend(this.populateGallery(this.populateFrom));
		this.galleryData = data;
		this.fireEvent('onPopulated');
	},
    populateGallery: function(element) {
		var data = [];
		
		options = this.options;
		element.getElements(options.elementSelector).each(function(el) {
			elementDict = {
				image: el.getElement(options.imageSelector).getProperty('src'),
				number: currentArrayPlace
			};
			elementDict.extend = $extend;
			if (options.showLegends)
				elementDict.extend({
					title: el.getElement(options.titleSelector).innerHTML,
					description: el.getElement(options.subtitleSelector).innerHTML
				});
			
			if (options.showThumbnails)
				elementDict.extend({
					thumbnail: el.getElement(options.thumbnailSelector).getProperty('src')
				});
			data.extend([elementDict]);
			currentArrayPlace++;
			if (this.options.destroyAfterPopulate)
				el.remove();
		});
		return data;
	},
	goTo : function(num){
		//this.clearTimer();
		if(this.options.preloader)
		{
			this.galleryElements[num].load();
			if (num==0)
				this.galleryElements[this.maxIter - 1].load();
			else
				this.galleryElements[num - 1].load();
			if (num==(this.maxIter - 1))
				this.galleryElements[0].load();
			else
				this.galleryElements[num + 1].load();
				
		}
		this.changeItem(num);
	},
	changeItem: function(num) {
		this.fireEvent('onStartChanging');
		this.galleryInit = 0;
		if (this.currentIter != num)
		{
			this.currentIter = num;
			this.fullImage.setProperty('src', this.galleryData[num].image).setStyles({'opacity' : 1});
			if(this.options.showLegends) this.legendPanel.setHTML(this.galleryData[num].description);
		}
		this.fireEvent('onChanged');
	},
	nextItem: function() {
		this.fireEvent('onNextCalled');
		this.nextIter = this.currentIter+1;
		if (this.nextIter >= this.maxIter)
			this.nextIter = 0;
		this.galleryInit = 0;
		this.goTo(this.nextIter);
	}
});
NormalImg.implement(new Events);
NormalImg.implement(new Options);


function goFull(cursrc, width, height) {
   var centerTop = (screen.availHeight-400)/2 - 200;
   var centerLeft = (screen.availWidth-400)/2 + 200;
   window.open("/popImage.aspx?url="+cursrc.replace("../",""),"full","top="+centerTop+",left="+centerLeft+",width="+width+",height="+height+",resizable,scrollbars=no,status=0");
}
function getVideo(type, width, height, url) {
    var centerTop = (screen.availHeight-400)/2 - 200;
    var centerLeft = (screen.availWidth-400)/2 + 200;
    window.open("/popVideo.aspx?url="+url+"&type="+type+"&width="+width+"&height="+height,"video","top="+centerTop+",left="+centerLeft+",width="+width+",height="+height+",resizable,scrollbars=no,status=0");
}
commentFunctions = new Abstract ({
    prepareBtn: function(formId) {
        $("sendbtn_"+formId).addEvent('click', this.sendComment.pass(formId));
    },
	sendComment: function(formid){
	    new Ajax("/ajax/blogSubmit.aspx", {method: 'post', autoCancel : true, update:$("addCommentForm_"+formid), data:$("addCommentForm_"+formid).toQueryString(), evalScripts: true}).request();
	}
});
formFunctions = new Abstract ({
    prepareBtn: function(formId) {
        new Element("div").setProperty("id","response_"+formId).injectInside($("Form_"+formId))
        $("sendbtn_"+formId).addEvent('click', this.sendForm.pass(formId));
    },
	sendForm: function(formid){
	    new Ajax("/ajax/sendForm.aspx", {method: 'post', autoCancel : true, update:$("response_"+formid), data:$("Form_"+formid).toQueryString(), evalScripts: true}).request();
	}
});