$(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);
	
	//StartRotatePhotos();
	StartJaluzee();
	//StartRound();
	LogoGlare();
	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 StartRotatePhotos()
	{
	$("#photo1").animate({opacity: 1.0}, 10000, 0, RotatePhotos);
	}

var backgroundPosition = 0;
function RotatePhotos()
	{
	backgroundPosition = -256 * random(13);
	$("#photo2").css({backgroundPosition: "0 "+backgroundPosition+"px"}).fadeIn(4000, HidePhoto2);
	}
function HidePhoto2()
	{
	$("#photo1").css({backgroundPosition: "0 "+backgroundPosition+"px"});
	$("#photo2").hide().animate({opacity: 1.0}, 8000, 0, RotatePhotos);;
	}

function StartJaluzee()
	{
	var photo3 = $("#photo3");
	for(var y=0; y<16; y++)
		photo3.append("<div></div>");
	$("#photo1").animate({opacity: 1.0}, 18000, 0, Jaluzee);
	}

function Jaluzee()
	{
	backgroundPosition = -256 * random(13);
	$("#photo3 div").each(function(i){
		var duration = 100*(((i > 8) ? i-8 : 8-i)+1);
		$(this).hide().css({backgroundPosition: "0 "+(backgroundPosition - i*16)+"px", top: i*16+"px"}).animate({opacity: 1.0}, duration).fadeIn(1500, i == 15 ? HidePhoto3 : '');
		});
	$("#photo3").show();

	}

function HidePhoto3()
	{
	$("#photo1").css({backgroundPosition: "0 "+backgroundPosition+"px"});
	$("#photo3").hide().animate({opacity: 1.0}, 10000, 0, Jaluzee);;
	}

function LogoGlare()
	{
	$("#logo").css({backgroundPosition : "-234px 0"}).animate({backgroundPosition : 234}, 3000).animate({opacity: 1.0}, 20000, 0, LogoGlare);
	}
function StartRound()
	{
	var photo3 = $("#photo3");
	for(var y=0; y<16; y++)
		for(var x=0; x<70; x++)
			photo3.append("<div style='left:"+x*14+";top:"+y*16+"px;background-position:-"+x*14+"px -"+y*16+"px;'></div>");
	//$("#photo1").animate({opacity: 1.0}, 18000, 0, Jaluzee);
	}

