/*ImageBrowser (c) 2007-2008 derosso.net*/
function ImageBrowser()
	{
		this.aImages = new Array();
		this.add = function()
			{
			this.aImages[this.aImages.length] = arguments;
			}
			
		this.fadeToBlack = function()
			{
			if(!$('fadeToBlack'))
				{
				var oBlind = new Element('div',{id:"fadeToBlack", style:"height:"+this.iScreenHeight+"px"});
				document.getElementsByTagName('body')[0].appendChild(oBlind);
				}
			}
		this.fadeUp = function()
			{
				Element.remove('fadeToBlack');
			}
			
		this.startShow = function()
			{
			var iScreenWidth;
			var iScreenHeight;
				
			if (window.innerWidth || window.innerHeight){ 
				this.iScreenWidth = window.innerWidth; 
				this.iScreenHeight = window.innerHeight; 
			}
			else if(document.documentElement.clientWidth) {
				this.iScreenWidth = document.documentElement.clientWidth;
				this.iScreenHeight = document.documentElement.clientHeight;
				}
			else
				{
				this.iScreenWidth = document.body.clientWidth;
				this.iScreenHeight = document.body.clientHeight;
				}

			this.iWidthLimit = this.iScreenWidth-90;		
			this.iHeightLimit= this.iScreenHeight-70;	
			
			this.fadeToBlack();
			setTimeout("ImageBrowser.openWindow("+arguments[0]+")", 200);
			}
		this.stopShow = function()
			{
			ImageBrowser.closeWindow();
			setTimeout("ImageBrowser.fadeUp();", 500);
			}
		this.closeWindow = function()
			{
			new Effect.Opacity('imagePreviewWindow', {duration:0.4, from:1.0, to:0.01});
			setTimeout("Element.remove('imagePreviewWindow');", 500);
			}
			
		this.buildNavigationbar = function()
			{
			var oNavigationBar = new Element('div', {id:"imageNavigationBar"});
			var oNaviPrevious = new Element('div', {id:"imageNavigationBarPrevious"}).update(this.iImagePosition<=0 ? "\u00A0" : "Previous");
			if(!(this.iImagePosition<=0))
				oNaviPrevious.observe('click', ImageBrowser.previousImage);
			var oNaviNext = new Element('div', {id:"imageNavigationBarNext"}).update(this.iImagePosition >=this.aImages.length-1 ? "\u00A0" : "Next");
			if(!(this.iImagePosition >=this.aImages.length-1))
				oNaviNext.observe('click', ImageBrowser.nextImage);
					
			var oNaviTitle = new Element('div', {id:"imageNavigationBarTitle"}).update(this.aImages[this.iImagePosition][3] == '' ? "\u00A0" : this.aImages[this.iImagePosition][3]);
			
			oNavigationBar.appendChild(oNaviPrevious);
			oNavigationBar.appendChild(oNaviTitle);
			oNavigationBar.appendChild(oNaviNext);
			return oNavigationBar;
			}
			
		this.naviPleaseWait = function()
			{
			$('imageNavigationBarNext').update("\u00A0");	
			$('imageNavigationBarPrevious').update("\u00A0");	
			$('imageNavigationBarTitle').update("Loading. Please wait...");	
			}
			
		this.nextImage = function()
			{
			ImageBrowser.openWindow(ImageBrowser.iImagePosition+1);
			}
			
		this.previousImage = function()
			{
			ImageBrowser.openWindow(ImageBrowser.iImagePosition-1);
			}	
		
		this.showCached = function()
			{
			if(!this.iImageCache.complete)
				{
				setTimeout("ImageBrowser.showCached();", 100);
				}
			else
				{	
				$('imagePreviewImage').setStyle({width:ImageBrowser.iCurrentWidth+"px", height:ImageBrowser.iCurrentHeight+"px"});
				$('imagePreviewImage').src = this.iImageCache.src;
				new Effect.Opacity('imagePreviewImage', {duration:0.3, from:0.01, to:1.0});
				$('imageNavigationBar').remove();
				var oNavigationBar = this.buildNavigationbar();
				$('imagePreviewWindow').appendChild(oNavigationBar);
				}
			}
		
		this.openWindow = function()
			{
			this.iImagePosition = arguments[0];
			this.iCurrentWidth = this.aImages[this.iImagePosition][1];
			this.iCurrentHeight = this.aImages[this.iImagePosition][2];			
			iRatio = (this.iCurrentWidth/this.iCurrentHeight);
			
			if(this.iCurrentWidth >= this.iWidthLimit)
				{
				this.iCurrentWidth =this.iWidthLimit;
				this.iCurrentHeight = Math.round(this.iCurrentWidth / iRatio);
				}

			if(this.iCurrentHeight >= this.iHeightLimit)
				{
				this.iCurrentHeight= this.iHeightLimit;
				this.iCurrentWidth = Math.round(this.iCurrentHeight*iRatio);
				}
			var iPosX = Math.round((this.iScreenWidth - this.iCurrentWidth)/2);
			if($('imagePreviewWindow')!=null)
				{
				this.iImageCache = new Element('img', {'src':this.aImages[this.iImagePosition][0], 'alt':""});
				new Effect.Opacity('imagePreviewImage', {duration:0.3, from:1.0, to:0.01});
				setTimeout("new Effect.Morph('imagePreviewWindow',{style:'left:"+iPosX+"px; top:20px;height:"+(this.iCurrentHeight+20)+"px;width:"+this.iCurrentWidth+"px', duration:0.25});", 400);
				setTimeout("ImageBrowser.showCached();", 400);
				ImageBrowser.naviPleaseWait();

				
				}
			else
				{
				var oWindow = new Element('div',{id:"imagePreviewWindow", style:"background-color:#000; height:1px; width:1px; left:"+iPosX+"px;display:block"});
				var oContent = new Element('div',{id:"imagePreviewContent"});
				var oImage = new Element('img',{id:"imagePreviewImage", src:this.aImages[this.iImagePosition][0], alt:"", style:"display:block; visiblity:visible; width:"+this.iCurrentWidth+"px; height:"+this.iCurrentHeight+"px;"});
				
				var oNavigationBar = this.buildNavigationbar();
				
				var oExit = new Element('div', {id:"imageExitButton", style:"cursor:pointer", onclick:"ImageBrowser.stopShow();"}).update('Exit');
				oExit.observe('click', ImageBrowser.stopShow);

				oExit.hide();
				oWindow.appendChild(oExit);
				oContent.appendChild(oImage);
				oContent.hide();
				oWindow.appendChild(oContent);
				oNavigationBar.hide();
				oWindow.appendChild(oNavigationBar);
	//			oWindow.hide();
				$('fadeToBlack').appendChild(oWindow);
	//			$('imagePreviewImage').show();
				$('imagePreviewWindow').style.top=Math.round(this.iScreenHeight/2)+"px";
				
				//let the cyna begin...
				new Effect.Morph('imagePreviewWindow',{style:'width:'+this.iCurrentWidth+'px', duration:0.5});
				setTimeout("new Effect.Morph('imagePreviewWindow',{style:'top:20px;height:"+(this.iCurrentHeight+20)+"px', duration:0.7});", 500);
				setTimeout("$('imagePreviewContent').show();", 1200);
				setTimeout("$('imageNavigationBar').show();", 1200);			
				setTimeout("$('imageExitButton').show();", 1200);						
				$('imagePreviewWindow').appendChild(oContent);
				$('imagePreviewWindow').appendChild(oNavigationBar);
				}
			}
	}

ImageBrowser = new ImageBrowser();

function externalLinks()
	{
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++)
		{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && (anchor.getAttribute("rev") == "external"))
		anchor.target = "_blank";
		}
	}
	
function addEvent(element, eventType, doFunction, useCapture){
		if (element.addEventListener) 
		{
			element.addEventListener(eventType, doFunction, useCapture);
			return true;
		} else if (element.attachEvent) {
			var r = element.attachEvent('on' + eventType, doFunction);
			return r;
		} else {
			element['on' + eventType] = doFunction;
		}

	}

addEvent(window, 'load', externalLinks, false);