/*
 Slimbox v1.31 - The ultimate lightweight Lightbox clone
 by Christophe Beyls (http://www.digitalia.be) - MIT-style license.
 Inspired by the original Lightbox v2 by Lokesh Dhakar.
*/

var Lightbox = {
	
	init: function (fileLoadingImage, fileBottomNavCloseImage, nextLinkImage, previousLinkImage, resizeDuration, resizeTransition, imageNrDesc, imageNrSep, nextKeys, prevKeys, closeKeys) {
    this.options = {};
    this.options.resizeDuration = resizeDuration;
    this.options.resizeTransition = resizeTransition;
    this.options.initialWidth = 450;
    this.options.initialHeight = 337;
    this.options.animateCaption = false;
    this.options.previousLinkImage = previousLinkImage;
    this.options.nextLinkImage = nextLinkImage;
    this.options.closeLinkImage = fileBottomNavCloseImage;
    this.options.loadingImage = fileLoadingImage;
    this.options.imageNrDesc = imageNrDesc;
    this.options.imageNrSep = imageNrSep;
    this.options.nextKeys = nextKeys;
    this.options.prevKeys = prevKeys;
    this.options.closeKeys = closeKeys;
    this.anchors = [];
    $each(document.links, function (el) {if (el.rel && el.rel.test(/^lightbox/i)) {el.onclick = this.click.pass(el, this);this.anchors.push(el);}}, this);
    this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
    this.eventPosition = this.position.bind(this);
    this.generalContainer = (new Element("div")).addClass("contentContainer").injectInside(document.body);
    this.center = (new Element("div")).setProperty("id", "lbCenter").setStyles({width:this.options.initialWidth + "px", height:this.options.initialHeight + "px", display:"none", background:"#fff url(" + this.options.loadingImage + ") no-repeat center"}).injectInside(this.generalContainer);
    this.image = (new Element("div")).setProperty("id", "lbImage").injectInside(this.center);
    this.prevLink = (new Element("a")).setProperties({id:"lbPrevLink", href:"#"}).setStyle("display", "none").injectInside(this.image);
    this.nextLink = this.prevLink.clone().setProperty("id", "lbNextLink").injectInside(this.image);
    this.prevLink.onclick = this.previous.bind(this);
    this.prevLink.setProperty("onmouseover", "this.style.backgroundImage=\"url(" + this.options.previousLinkImage + ")\"; this.style.backgroundRepeat=\"no-repeat\"; this.style.backgroundPosition=\"left 15%\";");
    var previousLinkImage = this.options.previousLinkImage;
    this.prevLink.onmouseover = function () {this.style.backgroundImage = "url(" + previousLinkImage + ")";this.style.backgroundRepeat = "no-repeat";this.style.backgroundPosition = "left 15%";};
    this.prevLink.setProperty("onmouseout", "this.style.backgroundImage=\"\";");
    this.prevLink.onmouseout = function () {this.style.backgroundImage = "";};
    this.nextLink.onclick = this.next.bind(this);
    this.nextLink.setProperty("onmouseover", "this.style.backgroundImage=\"url(" + this.options.nextLinkImage + ")\"; this.style.backgroundRepeat=\"no-repeat\"; this.style.backgroundPosition=\"right 15%\";");
    var nextLinkImage = this.options.nextLinkImage;
    this.nextLink.onmouseover = function () {this.style.backgroundImage = "url(" + nextLinkImage + ")";this.style.backgroundRepeat = "no-repeat";this.style.backgroundPosition = "right 15%";};
    this.nextLink.setProperty("onmouseout", "this.style.backgroundImage=\"\";");
    this.nextLink.onmouseout = function () {this.style.backgroundImage = "";};
    this.bottomContainer = (new Element("div")).setProperty("id", "lbBottomContainer").setStyle("display", "none").injectInside(this.generalContainer);
    this.bottom = (new Element("div")).setProperty("id", "lbBottom").injectInside(this.bottomContainer);
    this.closeLink = (new Element("a")).setProperties({id:"lbCloseLink", href:"#"}).injectInside(this.bottom);
    this.closeLink.onclick = this.close.bind(this);
    this.closeLink.setStyle("background", "transparent url(" + this.options.closeLinkImage + ") no-repeat center");
    this.caption = (new Element("div")).setProperty("id", "lbCaption").injectInside(this.bottom);
    this.number = (new Element("div")).setProperty("id", "lbNumber").injectInside(this.bottom);
    (new Element("div")).setStyle("clear", "both").injectInside(this.bottom);
    
	
	var nextEffect = this.nextEffect.bind(this);
    this.fx = {resize:this.center.effects({duration:this.options.resizeDuration, transition:this.options.resizeTransition, onComplete:nextEffect}), image:this.image.effect("opacity", {duration:500, onComplete:nextEffect}), bottom:this.bottom.effect("margin-top", {duration:400, onComplete:nextEffect})};
    this.preloadPrev = new Image;
    this.preloadNext = new Image;
},
click: function (link) {
    if (link.rel.length == 8) {
        return this.show(link.href, link.title);
    }
    var j, imageNum, images = [];
    this.anchors.each(function (el) {if (el.rel == link.rel) {for (j = 0; j < images.length; j++) {if (images[j][0] == el.href) {break;}}if (j == images.length) {images.push([el.href, el.title]);if (el.href == link.href) {imageNum = j;}}}}, this);
    
	/*
	 *   alexia
	 */
	var menuParent = link.getParent().getParent().getParent().getProperty("class");
    var who = link.rel;
    //var numGalerie = who.substring(12, 14);
	var numGalerie = who.match(/\d+/,numGalerie); //recupere lID de la gallerie 
	//alert(numGalerie);
		
	if (menuParent == "childscontainer") {/*if links are in the menu find the right div to open*/
        var destinationPrincipale = "containerBig" + numGalerie;
        var destination = "containerImg" + numGalerie;
        var containerPrincipal = $(destinationPrincipale).getParent();
        var containerImg = $(destination);
        var containerBttm = containerImg.getNext();
    } else {
        var containerPrincipal = link.getParent().getParent().getParent();
        var containerImg = link.getParent().getParent().getNext().getFirst();
        var containerBttm = containerImg.getNext();
		$$('div.childscontainer a.smenu').setStyles({'background-color': '#ffffff','color': '#000000'});
		var menuActif = "smenu" + numGalerie;
		var selectMenu = $(menuActif).setStyles({
		'background-color': '#000000',
		'color': '#ffffff'
	     });
	/*
	 *   end alexia
	 */
    }
    return this.open(images, imageNum, containerPrincipal, containerImg, containerBttm);
},
show:function (url, title) {
    return this.open([[url, title]], 0);
},
open:function (images, imageNum, containerPrincipal, containerImg, containerBttm) {
    this.images = images;
    this.setup(true);
	/* 
	 * alexia injection galleries
	 */
    this.containerImage = containerImg;
    this.containerBottom = containerBttm;
    this.containerPrinc = containerPrincipal;
    this.containerPrinc.setStyle("z-index", indexLevel + 1);
    this.center.injectAfter(this.containerImage).setStyles({display:"", width:"450px", position:"relative"});
    this.bottom.injectAfter(this.containerBottom);
    this.bottom.setStyle("display", "");
	/*
	 * end alexia
	 */
	return this.changeImage(imageNum);
},
position:function () {

},
setup: function (open) {
    var elements = $A(document.getElementsByTagName("object"));
    elements.extend(document.getElementsByTagName(window.ie ? "select" : "embed"));
    elements.each(function (el) {if (open) {el.lbBackupStyle = el.style.visibility;}el.style.visibility = open ? "hidden" : el.lbBackupStyle;});
    var fn = open ? "addEvent" : "removeEvent";
    window[fn]("scroll", this.eventPosition)[fn]("resize", this.eventPosition);
    document[fn]("keydown", this.eventKeyDown);
    this.step = 0;
},
keyboardListener: function (e) {
    if (e == null) {
        var keycode = e.keyCode;
    } else {
        var keycode = e.which;
    }
    var key = String.fromCharCode(keycode).toLowerCase();
    var i = 0;
    for (i = 0; i < this.options.nextKeys.length; i++) {
        if (this.options.nextKeys[i] == key) {
            this.next();
        }
    }
    for (i = 0; i < this.options.prevKeys.length; i++) {
        if (this.options.prevKeys[i] == key) {
            this.previous();
            break;
        }
    }
    for (i = 0; i < this.options.closeKeys.length; i++) {
        if (this.options.closeKeys[i] == key) {
            this.close();
            break;
        }
    }
},
previous:function () {
    return this.changeImage(this.activeImage - 1);
},
next: function () {
    return this.changeImage(this.activeImage + 1);
},
changeImage:function (imageNum) {
    if (this.step || imageNum < 0 || imageNum >= this.images.length) {
        return false;
    }
    this.step = 1;
    this.activeImage = imageNum;
    this.center.style.backgroundColor = "";
    this.bottomContainer.style.display = this.prevLink.style.display = this.nextLink.style.display = "none";
    this.fx.image.hide();
    this.center.className = "lbLoading";
    this.preload = new Image;
    this.preload.onload = this.nextEffect.bind(this);
    this.preload.src = this.images[imageNum][0]; 
	
    return false;
},


nextEffect: function () {
    switch (this.step++) {
      case 1:
        this.center.className = "";
        this.image.style.backgroundImage = "url(" + this.images[this.activeImage][0] + ")";
        this.image.style.width = this.preload.width + "px";
        this.image.style.height = this.prevLink.style.height = this.nextLink.style.height = this.preload.height + "px";
        this.caption.setHTML(this.images[this.activeImage][1] || "");
        this.number.setHTML(this.images.length == 1 ? "" : this.options.imageNrDesc + " " + (this.activeImage + 1) + " " + this.options.imageNrSep + " " + this.images.length);
        if (this.activeImage) {
            this.preloadPrev.src = this.images[this.activeImage - 1][0];
        }
        if (this.activeImage != this.images.length - 1) {
            this.preloadNext.src = this.images[this.activeImage + 1][0];
        }
        this.step++;
      case 2:
        this.step++;
     case 3:
        //this.bottomContainer.setStyles({top:this.top + "px", height:"0px", marginLeft:this.center.style.marginLeft, display:""}); // error here on ie 6 !!
        this.fx.image.start(1);
        break;
      
	  case 4:
        this.center.style.backgroundColor = "#000";
        if (this.options.animateCaption) {
            this.fx.bottom.set(- this.bottom.offsetHeight);
            this.bottomContainer.style.height = "";
            this.fx.bottom.start(0);
            break;
        }
        this.bottomContainer.style.height = "";
      case 5:
        if (this.activeImage) {
            this.prevLink.style.display = "";
        }
        if (this.activeImage != this.images.length - 1) {
            this.nextLink.style.display = "";
        }
        this.step = 0;
      default:;
    }
},
close:function () {
    if (this.step < 0) {
        return;
    }
    this.step = -1;
    if (this.preload) {
        this.preload.onload = Class.empty;
        this.preload = null;
    }
    for (var f in this.fx) {
        this.fx[f].stop();
    }
    this.center.style.display = this.bottom.style.display = "none";
	
	/*
	 * alexia - reset menu highlight
	 */
	$$('div.childscontainer a.smenu').setStyles({
                            'background-color': '#ffffff',
                            'color': '#000000'
                        });
	/*
	 *  end alexia
	 */
    return false;
	}
};

Window.onDomReady(Lightbox.init.pass(new Array(fileLoadingImage, fileBottomNavCloseImage, nextLinkImage, previousLinkImage, resizeDuration, resizeTransition, imageNrDesc, imageNrSep, nextKeys, prevKeys, closeKeys), Lightbox)); //changed by doze
