YAHOO.namespace("simpleLightbox");
var myLightbox = function () {
	var Event = YAHOO.util.Event, Dom = YAHOO.util.Dom, currentThumb, slbActive = false, slbDisplay;
	YAHOO.simpleLightbox.slblinks = Dom.getElementsByClassName('slbLink', 'a', 'container');
	
	var fadeIn = function() {
		var ani = new YAHOO.util.Anim(slbDisplay , { opacity: {from: 0, to: 1 } }, .25, YAHOO.util.Easing.easeOut);
		ani.animate();
	}

	var showImage = function (obj,header) {
		var vpw = Dom.getViewportWidth() - 150;
		var vph = Dom.getViewportHeight() - 100;
		if (obj.width > vpw || obj.height > vph){
			var objRatio = obj.width / obj.height;
			var vpRatio = vpw / vph;
			
			if (objRatio <= vpRatio) {
				obj.width = obj.width * (vph / obj.height);
				obj.height = vph;
			} else {
				obj.height = obj.height * (vpw / obj.width);
				obj.width = vpw;
			}
		}
		YAHOO.simpleLightbox.photoViewer.cfg.setProperty('width', (obj.width + 20)  + 'px');
		YAHOO.simpleLightbox.photoViewer.cfg.setProperty('zindex', '10000');
		YAHOO.simpleLightbox.photoViewer.setHeader(unescape(header));
		Dom.setStyle(slbDisplay,'width', obj.width+'px');
		Dom.setStyle(slbDisplay,'height', obj.height+'px');
		YAHOO.simpleLightbox.photoViewer.setBody( '<img src="' + obj.src + '" width="' + (obj.width) + '" height="' + (obj.height) + '" onclick="javascript: YAHOO.simpleLightbox.photoViewer.hide();" />' );
		YAHOO.simpleLightbox.photoViewer.center();
		if (slbActive == false) {
			slbActive = true;
			YAHOO.util.Dom.setStyle(slbDisplay, 'opacity', '1');
			YAHOO.simpleLightbox.photoViewer.show();
			YAHOO.util.Dom.setStyle( YAHOO.util.Dom.get('photoViewer_mask'), 'zIndex', '50' );
			YAHOO.util.Dom.setStyle( YAHOO.util.Dom.get('photoViewer_c'), 'zIndex', '55' );
		} else {
			fadeIn();
		}
	}

	var fadeOut = function(obj,header) {
		if (slbActive == true) {
			var ani = new YAHOO.util.Anim(slbDisplay , { opacity: {from: 1, to: 0 } }, .25, YAHOO.util.Easing.easeOut);
			ani.animate();
			ani.onComplete.subscribe(function(){showImage(obj,header)});
		} else {
			showImage(obj,header);
		}
	}
	
	YAHOO.simpleLightbox.loadImage = function(el) {
		if (el.src && el.src != '') {
			currentThumb = el.src;
			var imageSrc = el.parentNode.href;
			var header = (el.alt != null) ? el.alt : '';
			var objImage = new Image();
			Event.on(objImage, 'load', function(){fadeOut(objImage,header);});
			objImage.src = imageSrc;
		}
	}
	
	var prevThumb = function() {
		for (i=0; i<YAHOO.simpleLightbox.slblinks.length; i++) {
			if (YAHOO.simpleLightbox.slblinks[i].firstChild.src == currentThumb) {
				if (i == 0) {
					i = YAHOO.simpleLightbox.slblinks.length-1;
				} else {
					i = i - 1;
				}
				YAHOO.simpleLightbox.loadImage(YAHOO.simpleLightbox.slblinks[i].firstChild);
				break;
			}
		}
	}
	
	var nextThumb = function() {
		
		for (i=0; i<YAHOO.simpleLightbox.slblinks.length; i++) {
			if (YAHOO.simpleLightbox.slblinks[i].firstChild.src == currentThumb) {
				if (i == YAHOO.simpleLightbox.slblinks.length-1) {
					i = 0;
				} else {
					i = i + 1;
				}
				//alert(i);
				YAHOO.simpleLightbox.loadImage(YAHOO.simpleLightbox.slblinks[i].firstChild);
				break;
			}
		}
	}
	
	var hideMe = function() {
		slbActive = false;
		this.hide();
	}
	
	YAHOO.simpleLightbox.photoViewer = new YAHOO.widget.SimpleDialog("photoViewer",{
		width: "300px",
		//fixedcenter: true,
		visible: false,
		draggable: true,
		close: true,
		modal: true,
		text: '<div id="slbDisplay"></div>',
		constraintoviewport: true,
		effect: [ {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25} ],
		buttons: [ { text:"Prev", handler:prevThumb },{ text:"Next", handler:nextThumb },{ text:"Close", handler:hideMe, isDefault:true } ]
	});
	
	
	
	YAHOO.simpleLightbox.photoViewer.cfg.setProperty('zindex', '10000');
	YAHOO.simpleLightbox.photoViewer.setHeader('Simple Lightbox');
	YAHOO.simpleLightbox.photoViewer.render("container");
	
	YAHOO.simpleLightbox.photoViewer.beforeHideEvent.subscribe(function () {
		slbActive = false;
		return true;
	});
	
	slbDisplay = Dom.get('slbDisplay');
	
	YAHOO.Bubbling.addDefaultAction('slbLink',
		function (layer, args) {
			YAHOO.simpleLightbox.loadImage(args[1].target);
			return true;
		}
	);
}
YAHOO.util.Event.addListener(window, "load", myLightbox);