$(document).ready(AddListen);

var GalleryArray;

function AddListen()
	{
	var popups = $("img.popup_image");
	popups.css({cursor:"pointer"}).click(LoadImg);
	GalleryArray = new Array();
	popups.each( function(i)
		{
		im = new Image();
		im.src = getLargeImage(this.src);
		GalleryArray[i] = im;
		});
	PreloadImages();
	$("#fade").click(CloseEnlarged);
	$("li.pup-li a").click(function(e){e.preventDefault();});
	$("a.div_prev").click(PrevGallery);
	$("a.div_next").click(NextGallery);
	$("a.div_close").click(CloseEnlarged);
	$("#presentation a").click(Presentation);
	
	if($.browser.msie && $.browser.version == '6.0')
		{
		FixPng();
		}
	}
function FixPng()
	{
	$("img").each(function(i){
		if(this.src.substring(this.src.length-3, this.src.length) == 'png')
			{
			this.style.height = this.scrollHeight;
			this.style.width = this.scrollWidth;
			this.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+this.src+'", sizingMethod="scale")';
			this.src = "/images/spacer.gif";
			this.style.visibility = 'visible';
			}
	});
	}
function getElement(event)
	{
	if (event.srcElement)
		return event.srcElement;
	else if (event.target)
		return event.target;
	}
function PreloadImages()
	{
	var image = new Array(
		'controls.png',
		's00.png',
		's01.png',
		's10.png',
		's11.png',
		'st.png',
		'sb.png',
		'sr.png',
		'sl.png'
		);
	var oImage = new Array();
	for(var i in image)
		{
		oImage[i] = new Image();
		oImage[i].src = '/images/'+image[i];
		}
	}
function getLargeImage(oUrl)
	{
	var what=oUrl.split("/");
	var wh="";
	for(i=0;i<what.length-1;i++)
		wh+=what[i]+'/';
	wh+='__large/'+what[i];
	return wh;
	}
function LoadImg(event)
	{
	el = getElement(event);
	
	oUrl=el.src;
	wh = getLargeImage(oUrl);
		
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i].src == wh)
			{
			Img = GalleryArray[i];
			setTimeout('_fill()',200);
			return;
			}
		}
	}
function NextGallery(event)
	{
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i] == Img)
			{
			if( i == GalleryArray.length - 1 )
				{
				i = -1;
				}
			Img = GalleryArray[i + 1];
			setTimeout('_fill()',10);
			return;
			}
		}
	}
function PrevGallery(event)
	{
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i] == Img)
			{
			if( i == 0 )
				{
				i = GalleryArray.length;
				}
			Img = GalleryArray[i - 1];
			setTimeout('_fill()',10);
			return;
			}
		}
	}
function isImageOk(img)
	{
	if(!img.complete)
		return false;
	if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
        	return false;
	return true;
	}
function _fill()
	{
		if(!isImageOk(Img))
			{
			setTimeout('_fill()',10);
			return;
			}
		
		shadow = $("#div_shadow");
		container = $("#div_enlarged");
		ofade = $("#fade");
		
		container.empty().append(Img);
		
		if(jQuery.browser.opera)
			shadow.show();
		
		var w = Img.naturalWidth || Img.width;
		var h = Img.naturalHeight || Img.height;
		
		if(jQuery.browser.opera)
			shadow.hide();
		
		shadow.css({margin:"-"+Math.round(h/2)+"px 0 0 -"+Math.round(w/2)+"px"});
		if(!jQuery.browser.msie)
			{
			shadow.fadeIn(300);
			ofade.fadeIn(300);
			}
		else
			{
			shadow.show();
			ofade.show();
			}
	}
function CloseEnlarged()
	{
	if(!jQuery.browser.msie)
		{
		$("#div_shadow").fadeOut(300);
		$("#fade").fadeOut(300);
		}
	else
		{
		shadow.hide();
		ofade.hide();
		}
	}
function run_movie(arg, w, h) 
	{ 
	document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="'+w+'" HEIGHT="'+h+'" id="'+arg+'" ALIGN=""><PARAM NAME=movie VALUE="'+arg+'.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=wmode VALUE=transparent> <EMBED src="'+arg+'.swf"  quality=high wmode=transparent  WIDTH="'+w+'" HEIGHT="'+h+'" NAME="'+arg+'" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>'); 
	}

function random(what)
	{
	var ranNum= Math.floor(Math.random()*what);
	return ranNum;
	}

function Presentation(event)
	{
	var w=500, h=400, arg='/i/flash';
	shadow = $("#div_shadow");
	container = $("#div_enlarged");
	ofade = $("#fade");
	shadow.css({margin:"-"+Math.round(h/2)+"px 0 0 -"+Math.round(w/2)+"px"});
	shadow.show();
	ofade.show();
	container.html('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="'+w+'" HEIGHT="'+h+'" id="'+arg+'" ALIGN=""><PARAM NAME=movie VALUE="'+arg+'.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=wmode VALUE=transparent> <EMBED src="'+arg+'.swf"  quality=high wmode=transparent  WIDTH="'+w+'" HEIGHT="'+h+'" NAME="'+arg+'" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>');
	}
	
