// ************************************************************** //
// * Au dela de l'Oraison                                       * //
// ************************************************************** //
// * Copyright (c) 2007-2008                                    * //
// * Web           http://www.samantha-bailly.com               * //
// ************************************************************** //
// ************************************************************** //
// * $ Date: 2007-10-06 19:50:46 +0200 (Sat, 06 Oct 2007)     $ * //
// * $ Developper: Dietrich Christophe                        $ * //
// * $ Developper: DESPORTES Florian                          $ * //
// ************************************************************** //
// ************************************************************** //
// * $ Source: script_util_showbox.js                         $ * //
// * $ Source type: javascript with mootools framework        $ * //
// * $ Based on: ligthbox framework but auto-resize images    $ * //
// * $           to fit screen size                           $ * //
// ************************************************************** //

// Default values
var sb_default = {
	// Default URI to images
	image : {
		previous : "images/prev.png",
		next     : "images/next.png",
		waiting  : "images/waiting.gif",
		close    : "images/close.png",
		blank    : "images/blank.gif"
	},
	// Default texts
	text  : {
		prev  : "Image précédente",
		next  : "Image suivante",
		wait  : "Chargement en cours...",
		close : "Fermer"
	}
}


// ShowBox class
var showBox = new Class({

	initialize : function(selector, prev_img, next_img, wait_img, close_img) {
		var anchors = document.getElements(selector);
		//alert(this.anchors);
		if (prev_img==undefined  || prev_img=="")  { this.prev_image  = sb_default.image.previous; } else { this.prev_image  = prev_img; }
		if (next_img==undefined  || next_img=="")  { this.next_image  = sb_default.image.next;     } else { this.next_image  = next_img; }
		if (wait_img==undefined  || wait_img=="")  { this.wait_image  = sb_default.image.waiting;  } else { this.wait_image  = wait_img; }
		if (close_img==undefined || close_img=="") { this.close_image = sb_default.image.close;    } else { this.close_image = close_img; }
		this.blank_image = sb_default.image.blank;
		this.isShowing = false;
		this.imageLength = anchors.length;
		
		// full showbox container
		this.sb_container = new Element("div").set({
			"id" : "sb_container"
		});
		
		// Adding all to <body> DOM
		$(document.body).adopt(this.sb_container);

		this.currentImage = {
			index  : -1,
			width  : 0,
			height : 0
		}
		
		var sbClass = this;
		
		/*this.myKeyboardEvents = new Keyboard({
		    defaultEventType : "keyup",
			events : {
				"left"  : function() {
					if (sbClass.currentImage.index - 1 > 0) {
						sbClass.goToPrev();
					}
				},
				"right" : function() {
					if (sbClass.currentImage.index + 1 < sbClass.imageLength) {
						sbClass.goToNext();
					}
				},
				"esc"   : function() {
					sbClass.destroy();
				}
			}
		});*/
		
		for (var index=0; index<anchors.length; index++) {
			var item = anchors[index];
			item.set("id", "sb_image_" +index);
			item.addEvent("click", function(evt) {
				if (evt) { evt.stop(); }
				sbClass.display(parseInt(this.getAttribute("id").substring(this.getAttribute("id").lastIndexOf("_")+1)));
				return false;
			});
		};
	},
	
	display : function(index) {
		this.currentImage.index = index;
		if (this.isShowing == false) {
			this.construct();
		}
		this.setImage(index);
	},

	getPageSize : function() {
		return document.getSize();
	},
	getPageScrollSize : function() {
		return document.getScrollSize();
	},
	getPageScrolledSize : function() {
		return document.getScroll();
	},

	construct : function() {
		// already set
		this.isShowing = true;
		
		// this class
		var sbClass = this;
		
		window.addEvent("resize", function() {
			sbClass.updateSize();
		});
		
		//this.myKeyboardEvents.activate();
		
		// Setting default tags
		var sb_background = new Element("div").set({
			"id"   : "sb_background",
			styles : {
				"top"        : "0px",
				"padding"    : "0px",
				"margin"     : "0px",
				"position"   : "absolute",
				"width"      : this.getPageScrollSize().x,
				"height"     : this.getPageScrollSize().y,
				"text-align" : "center"
			}
		});
		var sb_content    = new Element("div").set({
			"id"   : "sb_content",
			styles : {
				"position"   : "absolute",
				"border"     : "5px solid #EEEEEE",
				"width"      : "400px",
				"height"     : "400px",
				"margin"     : "0px",
				"padding"    : "0px",
				"left"       : (this.getPageSize().x/2 - 200),
				"top"        : (this.getPageSize().y/2 + (this.getPageScrolledSize().y) - 200),
				"text-align" : "center",
				"opacity"    : 1
			}
		});
		var sb_wait       = new Element("img").set({
			"src"    : this.wait_image,
			"title"  : sb_default.text.wait,
			"alt"    : sb_default.text.wait,
			"id"     : "sb_wait",
			"border" : "0px",
			styles   : {
				"position"   : "relative",
				"left"       : "50%",
				"top"        : "50%",
				"margin-top" : "-24px"
			}
		});
		var sb_legend     = new Element("div").set({
			html      : "",
			"id"      : "sb_legend",
			"opacity" : 0,
			styles    : {
				"width"            : "100%",
				"color"            : "#000000",
				"background-color" : "#EEEEEE",
				"text-align"       : "center",
				"font-family"      : "Tahoma, Arial",
				"font-size"        : 15,
				"display"          : "none",
				"border"           : "0px"
			}
		});
		var sb_image      = new Element("img").set({
			"id"      : "sb_image",
			"border"  : "0px",
			"opacity" : 0,
			events    : {
				load : function(evt) {
					// this refers to the loaded image
					sbClass.currentImage.width  = this.width;
					sbClass.currentImage.height = this.height;
					sbClass.updateSize();
				}
			}
		});
		
		// Close tags
		var sb_close      = new Element("div").set({
			"id"   : "sb_close",
			styles : {
				"float"        : "right",
				"margin-right" : "-22px",
				"display"      : "inline",
				"top"          : "0",
				"position"     : "absolute",
				"right"        : "0"
			}
		});
		var sb_closeLink  = new Element("a").set({
			"href"   : "#",
			"id"     : "sb_closeLink",
			"title"  : sb_default.text.close,
			"alt"    : sb_default.text.close,
			styles   : {
				"text-decoration" : "none"
			},
			events   : {
				click : function(evt) {
					evt.preventDefault();
					sbClass.destroy();
				}
			}
		});
		var sb_closeImg   = new Element("img").set({
			"src"    : this.close_image,
			"id"     : "sb_closeImg",
			"border" : "0px",
			styles   : {
				"position"   : "relative",
				"margin-top" : "-20px"
			}
		});
		sb_closeLink.adopt(sb_closeImg);
		sb_close.adopt(sb_closeLink);
		
		// Prev tags
		var sb_prev = new Element("a").set({
			"id"     : "sb_prev",
			"align"  : "left",
			styles   : {
				"left"       : "0px",
				"position"   : "absolute",
				"top"        : "0px",
				"cursor"     : "pointer",
				"background" : "transparent url("+ this.blank_image +")"
			},
			events   : {
				mouseover : this.showLeftArrow,
				mouseout  : this.hideArrow,
				click     : function(evt) {
					evt.stop();
					sbClass.goToPrev();
				}
			}
		});
		var sb_prevImg = new Element("img").set({
			"src"    : this.prev_image,
			"id"     : "sb_prevImg",
			"border" : "0px",
			"title"  : sb_default.text.prev,
			"alt"    : sb_default.text.prev,
			styles : {
				"position"         : "absolute",
				"top"              : "50%",
				"left"             : "-9999px",
				"width"            : "24px",
				"height"           : "24px"
			}
		});
		sb_prev.adopt(sb_prevImg);
		
		// Next tags
		var sb_next = new Element("a").set({
			"id"    : "sb_next",
			"align" : "right",
			styles  : {
				"right"      : "0px",
				"position"   : "absolute",
				"top"        : "10px",
				"cursor"     : "pointer",
				"background" : "transparent url("+ this.blank_image +")"
			},
			events  : {
				mouseover : this.showRightArrow,
				mouseout  : this.hideArrow,
				click     : function(evt) {
					evt.stop();
					sbClass.goToNext();
					return false;
				}
			}
		});
		var sb_nextImg = new Element("img").set({
			"src"   : this.next_image,
			"id"     : "sb_nextImg",
			"border" : "0px",
			"title"  : sb_default.text.next,
			"alt"    : sb_default.text.next,
			styles   : {
				position : "absolute",
				top      : "50%",
				left     : "-9999px",
				width    : "24px",
				height   : "24px"
			}
		});
		sb_next.adopt(sb_nextImg);
		
		// Adding all to content
		sb_content.adopt(sb_close);
		sb_content.adopt(sb_wait);
		sb_content.adopt(sb_image);
		sb_content.adopt(sb_prev);
		sb_content.adopt(sb_next);
		sb_content.adopt(sb_legend);

		// Adding all to show nox container
		$(this.sb_container).adopt(sb_background);
		$(this.sb_container).adopt(sb_content);

		// Add opacity effect to background
		var mySb_background = new Fx.Morph($("sb_background"));
		mySb_background.start({
			"background-color" : "#000000",
			"opacity"          : [ 0 , 0.5 ]
		});
	},

	goToPrev : function() {
		$("sb_image_"+ (this.currentImage.index-1)).fireEvent("click");
		return false;
	},
	
	goToNext : function() {
		$("sb_image_"+ (this.currentImage.index+1)).fireEvent("click");
		return false;
	},
	
	setImage : function(imgIndex) {
		$("sb_wait").setStyle("display", "block");
		
		var src  = $("sb_image_"+ imgIndex).getAttribute("href");
		var text = $("sb_image_"+ imgIndex).getElement("img").getAttribute("alt");

		$("sb_legend").set("html", text);
		$("sb_image").set({
			"src"     : src,
			"opacity" : 0
		});
		$("sb_legend").set("opacity", 0);
		
		return false;
	},

	updateSize : function() {
		$("sb_image").setStyle("height", this.currentImage.height);
		if ($("sb_image").getStyle("height").toInt() > this.getPageSize().y) {
			$("sb_image").setStyle("height", this.getPageSize().y-34);
		}

		var myFx = new Fx.Morph($("sb_content"));
		myFx.start({
			"width"  : [ $("sb_content").getStyle("width").toInt() , $("sb_image").getStyle("width").toInt() ],
			"height" : [ $("sb_content").getStyle("height").toInt(), $("sb_image").getStyle("height").toInt() + (17) ],
			"left"   : [ $("sb_content").getStyle("left").toInt()  , (this.getPageSize().x/2 - $("sb_image").getStyle("width").toInt()/2) ],
			"top"    : [ $("sb_content").getStyle("top").toInt()   , (this.getPageSize().y/2 + this.getPageScrolledSize().y - $("sb_image").getStyle("height").toInt()/2 - (10*2) + 10) ]
		});

		$("sb_wait").setStyle("display", "none");
		
		$("sb_legend").setStyles({
			"display" : "block",
			"width"   : $("sb_image").getStyle("width").toInt()
		});

		myFx.addEvent("complete", function() {
			$("sb_image").morph({ "opacity" : 1 });
			$("sb_legend").morph({ "opacity" : 1 });
		});

		$("sb_background").setStyles({
			"width"  : this.getPageScrollSize().x,
			"height" : this.getPageScrollSize().y
		});
		
		// Hide/show prev div + prev button
		if (this.currentImage.index-1 < 0) {
			$("sb_prev").setStyle("display", "none");
		} else {
			$("sb_prev").setStyles({
				"display" : "block",
				"width"   : $("sb_image").getStyle("width").toInt()/4,
				"height"  : $("sb_image").getStyle("height").toInt()
			});
		}
		
		// Hide/show next div + next button
		if (this.currentImage.index+1 == this.imageLength) {
			$("sb_next").setStyle("display", "none");
		} else {
			$("sb_next").setStyles({
				"display" : "block",
				"width"   : $("sb_image").getStyle("width").toInt()/4,
				"height"  : $("sb_image").getStyle("height").toInt() - 10
			});
		}
		
		return false;
	},

	destroy : function() {
		this.isShowing = false;
		window.removeEvent("resize", function() {
			this.updateSize();
		});
		// Add opacity effect to background
		var mySb_background = new Fx.Morph($("sb_background"));
		mySb_background.start({
			"background-color" : "#000000",
			"opacity"          : [ 0.5 , 0 ]
		});
		$("sb_background").dispose();
		$("sb_content").dispose();
		//this.myKeyboardEvents.deactivate();
		return false;
	},

	showLeftArrow : function(evt) {
		evt.stop();
		this.getElement("img").setStyle("left", "5px");
		return false;
	},
	showRightArrow : function(evt) {
		evt.stop();
		this.getElement("img").setStyle("left", "auto");
		this.getElement("img").setStyle("right", "5px");
		return false;
	},
	hideArrow : function(evt) {
		evt.stop();
		this.getElement("img").setStyle("left", "-9999px");
		return false;
	}
});

