// var alb contains all a tags
var alb = new Array();
//alb_ind contains indexes of the correct a tags.
var alb_ind = new Array();


var isIE = 0;
if (navigator.appName.indexOf('Explorer')!=-1){isIE=1;}


function load_albums(){
	alb = document.getElementsByTagName('span');
	count = 0;
	for (i=0;i<alb.length;i++){
		if(alb[i].parentNode.id == 'albums'){
		alb_ind[count]=i;
		count++;
	}}
	
	var i=0;
	while (i<alb_ind.length){
		var offset = 0;
		var parId = i;
		do{
			obj = alb[alb_ind[i]];
			y=parseInt(offset/4);
			switch ((offset+1)%4){
				case (1):create_item(obj,0,y*220);
				break
				case(2):create_item(obj,200,y*220);
				break
				case(3):create_item(obj,400,y*220);
				break
				case(0):create_item(obj,600,y*220);
				break
				default:
					alert('badness');
				break
			}
			i++;
			offset++;
			if (i==alb_ind.length){break;}
		}
		while( alb[alb_ind[i]].parentNode == alb[alb_ind[parId]].parentNode );
		alb[alb_ind[parId]].parentNode.parentNode.style.height=parseInt((y+1)*220)+'px';
	}
}

// Function for debugging
function print_items(obj){
	print = ' ';
	for (i=0;i<obj.length;i++){print += obj[i] + "\n ";}
	alert(print);
}


function create_item(obj,x,y){
	//obj.style.left=x;
	//obj.style.top=y;
	add_thumb(obj);
	new Effect.Move(obj,{x:x,y:y,mode:'absolute'});
	add_display(obj);
}

function add_thumb(obj){
	var y = document.createTextNode(obj.title);
	var d = document.createElement('div');
	var x = document.createElement('img');
	var z = document.createElement('div');
	x.src=obj.id;
	x.className='reflect';
	x.width=150;
	x.height=100;
	z.appendChild(x);
	d.appendChild(y);
	z.appendChild(d);
	obj.appendChild(z);
	x.style.cursor='pointer';
	z.style.cursor='default';
	z.style.width='150px';
	//z.style.border='1px solid white';

	d.style.fontSize='12px';
	d.style.width='150px';
	d.style.padding='2px';
	//d.style.position='absolute';
	d.style.top='0px';
	//d.style.border='1px solid blue';
	d.style.height='50px';

	window.setTimeout(function (x) {
		addReflections(x);
	},500,x);
	addReflections(x);

}

function add_display(obj){
	obj.onclick=show_item;
}

function show_item(){

	var div = document.createElement('div');
	div.id='show_item';
	div.onclick=close_item;
	div.style.width='100%';
	div.style.height='100%';
	var frame = document.createElement('iframe');
	frame.id='show_item_frame';


	frame.src=this.className;
	//frame.backgoundColor='black';
	//frame.style.backgroundColor='black';
	//alert(this.className + ' =? ' + frame.src);
	frame.style.border='2px solid white';
	frame.style.opacity='1.0';
	//frame.scrolling='no';
	
	var close_window = document.createElement('img');
	close_window.id = 'close_window';
	close_window.style.opacity='1.0';	
	close_window.style.position = 'absolute';
	close_window.onclick=close_item;
	close_window.style.display='block';
	close_window.style.width='26px';
	close_window.style.height='26px';
	close_window.style.padding='0px';
	close_window.style.margin='0px';
	close_window.style.marginTop='0px';
	close_window.src='images/icons/close.png';
	close_window.style.cursor='hand';
	close_window.onmouseover=function hov(){
		document.getElementById('close_window').src = 'images/icons/close_over.png';
		//document.body.style.cursor='wait';
	};
	close_window.onmouseout=function hov2(){
		document.getElementById('close_window').src = 'images/icons/close.png';
	};
	//close_window.style.fontSize='16px';
	//close_window.style.color='#ffffff';
	//close_window.style.backgroundColor='#000';
	close_window.style.border='2px solid white';
	//close_window.textAlign = 'center';
	
	frame.style.position = 'absolute';
	if(navigator.appVersion.indexOf('Apple')!=-1){
		sTop=document.body.scrollTop;
		sLeft = document.body.scrollLeft;
	}
	else{
		sTop=document.documentElement.scrollTop;
		sLeft = document.documentElement.scrollLeft;
	}
	
	try{
		frame.height=parseInt(0.9*parseInt(window.innerHeight));
		frame.width=parseInt(0.9*parseInt(window.innerWidth));
		frame.style.top = (parseInt(sTop) + parseInt(window.innerHeight)/2 - parseInt(frame.height)/2 -3) +"px";
		frame.style.left = (parseInt(sLeft) + parseInt(window.innerWidth)/2 - parseInt(frame.width)/2 -3) + "px" ;
		div.style.top = parseInt(sTop)+'px';
		div.style.left = (parseInt(sLeft))+'px';
	}
	catch(e){
		frame.height=parseInt(0.9*parseInt(document.documentElement.clientHeight));
		frame.width=parseInt(0.9*parseInt(document.documentElement.clientWidth));
		frame.style.top = (parseInt(sTop) + parseInt(document.documentElement.clientHeight)/2 - parseInt(frame.height)/2 -3) +"px";
		frame.style.left = (parseInt(sLeft) + parseInt(document.documentElement.clientWidth)/2 - parseInt(frame.width)/2 -3) + "px" ;
		div.style.width=parseInt(document.documentElement.clientWidth)+'px';
		div.style.height=parseInt(document.documentElement.clientHeight)+'px';
		div.style.top = parseInt(sTop)+'px';
		div.style.left = (parseInt(sLeft))+'px';
	}
	
	close_window.style.top = frame.style.top;
	close_window.style.left = (parseInt(frame.style.left) - 28) + "px";
	
	
	var framebg = document.createElement('div');
	framebg.style.display="block";
	framebg.style.position = 'absolute';
	framebg.style.top = frame.style.top;
	framebg.style.left = frame.style.left;
	framebg.style.height = frame.height+'px';
	framebg.style.width = frame.width+'px';
	framebg.style.backgroundColor='white';
	framebg.style.border='2px solid white';
	framebg.id='framebg';

	document.body.appendChild(close_window);
	document.body.appendChild(frame);
	document.body.appendChild(framebg);
	document.body.appendChild(div);
	div.style.zIndex="0";
	framebg.style.zIndex="1";
	frame.style.zIndex="2";
	close_window.style.zIndex="3";
	try{
		new Effect.Opacity(div,{from:0.0,to:.5});
	}
	catch(e){
		div.style.opacity='0.5';
	}
	if(!isIE){
		window.addEventListener('scroll',update_item,false);
		window.addEventListener('resize',update_item,false);
	}
	else{
		window.onresize=update_item;
		window.onscroll=update_item;

	}
	

	
}

function close_item(){
	fobj=document.getElementById('show_item_frame');
	obj=document.getElementById('show_item');
	cobj = document.getElementById('close_window');
	fbgobj=document.getElementById('framebg');
	//fobj.src='videos/blank.htm';
	try{
		new Effect.Opacity(fobj,{from:0.5,to:0.0});
		new Effect.Opacity(obj,{from:0.5,to:0.0});
		new Effect.Opacity(cobj,{from:0.5,to:0.0});
		new Effect.Opacity(fbgobj,{from:0.5,to:0.0});
	}
	catch(e){
		fobj.style.opacity='0.0';
		obj.style.opacity='0.0';
		cobj.style.opacity='0.0';
		fbgobj.style.opacity='0.0';
	}
	if(!isIE){
		window.removeEventListener('scroll',update_item,false);
		window.removeEventListener('resize',update_item,false);
	}
	else{
		window.onresize=dummy;
		window.onscroll=dummy;
	}
	
	setTimeout("document.body.removeChild(document.getElementById('show_item_frame'))",500);
	setTimeout("document.body.removeChild(document.getElementById('show_item'))",700);
	setTimeout("document.body.removeChild(document.getElementById('close_window'))",500);
	setTimeout("document.body.removeChild(document.getElementById('framebg'))",500);

}

function update_item(){

	fobj = document.getElementById('show_item_frame');
	fbgobj = document.getElementById('framebg');
	div = document.getElementById('show_item');
	fobj.style.position = 'absolute';
	
	if(navigator.appVersion.indexOf('Apple')!=-1){
		sTop=document.body.scrollTop;
		sLeft = document.body.scrollLeft;
	}
	else{
		sTop=document.documentElement.scrollTop;
		sLeft = document.documentElement.scrollLeft;
	}
	
	try{
		fobj.height=parseInt(0.9*parseInt(window.innerHeight));
		fobj.width=parseInt(0.9*parseInt(window.innerWidth));
		fobj.style.top = (parseInt(sTop) + parseInt(window.innerHeight)/2 - parseInt(fobj.height)/2 -3) +"px";
		fobj.style.left = (parseInt(sLeft) + parseInt(window.innerWidth)/2 - parseInt(fobj.width)/2 -3) + "px" ;
		div.style.top = parseInt(sTop)+'px';
		div.style.left = parseInt(sLeft)+'px';
	}
	catch(e){
		fobj.height=parseInt(0.9*parseInt(document.documentElement.clientHeight));
		fobj.width=parseInt(0.9*parseInt(document.documentElement.clientWidth));
		fobj.style.top = (parseInt(sTop) + parseInt(document.documentElement.clientHeight)/2 - parseInt(fobj.height)/2 -3) +"px";
		fobj.style.left = (parseInt(sLeft) + parseInt(document.documentElement.clientWidth)/2 - parseInt(fobj.width)/2 -3) + "px" ;
		div.style.top = parseInt(sTop)+'px';
		div.style.left = parseInt(sLeft)+'px';
	}
	
	fbgobj.style.top = fobj.style.top;
	fbgobj.style.left = fobj.style.left;
	fbgobj.style.height = fobj.height+'px';
	fbgobj.style.width = fobj.width+'px';
	
	document.getElementById('close_window').style.left = (parseInt(fobj.style.left) - 28) + "px";
	document.getElementById('close_window').style.top = fobj.style.top;

}

function dummy(){
	
}

/**
 * reflection.js v1.7
 *
 * Contributors: Cow http://cow.neondragon.net
 *               Gfx http://www.jroller.com/page/gfx/
 *               Sitharus http://www.sitharus.com
 *               Andreas Linde http://www.andreaslinde.de
 *               Tralala, coder @ http://www.vbulletin.org
 *
 * Freely distributable under MIT-style license.
 */
 
/* From prototype.js */
document.getElementsByClassName = function(className) {
	var children = document.getElementsByTagName('*') || document.all;
	var elements = new Array();
  
	for (var i = 0; i < children.length; i++) {
		var child = children[i];
		var classNames = child.className.split(' ');
		for (var j = 0; j < classNames.length; j++) {
			if (classNames[j] == className) {
				elements.push(child);
				break;
			}
		}
	}
	return elements;
}

var Reflection = {
	defaultHeight : 0.5,
	defaultOpacity: 0.5,
	
	add: function(image, options) {
		Reflection.remove(image);
		
		doptions = { "height" : Reflection.defaultHeight, "opacity" : Reflection.defaultOpacity }
		if (options) {
			for (var i in doptions) {
				if (!options[i]) {
					options[i] = doptions[i];
				}
			}
		} else {
			options = doptions;
		}
	
		try {
			var d = document.createElement('div');
			var p = image;
			
			var classes = p.className.split(' ');
			var newClasses = '';
			for (j=0;j<classes.length;j++) {
				if (classes[j] != "reflect") {
					if (newClasses) {
						newClasses += ' '
					}
					
					newClasses += classes[j];
				}
			}

			var reflectionHeight = Math.floor(p.height*options['height']);
			var divHeight = Math.floor(p.height*(1+options['height']));
			
			var reflectionWidth = p.width;
			
			if (document.all && !window.opera) {
				/* Fix hyperlinks */
                if(p.parentElement.tagName == 'A') {
	                var d = document.createElement('a');
	                d.href = p.parentElement.href;
                }  
                    
				/* Copy original image's classes & styles to div */
				d.className = newClasses;
				p.className = 'reflected';
				
				d.style.cssText = p.style.cssText;
				p.style.cssText = 'vertical-align: bottom';
			
				var reflection = document.createElement('img');
				reflection.src = p.src;
				reflection.style.width = reflectionWidth+'px';
				
				reflection.style.marginBottom = "-"+(p.height-reflectionHeight)+'px';
				reflection.style.filter = 'flipv progid:DXImageTransform.Microsoft.Alpha(opacity='+(options['opacity']*100)+', style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy='+(options['height']*100)+')';
				
				d.style.width = reflectionWidth+'px';
				d.style.height = divHeight+'px';
				p.parentNode.replaceChild(d, p);
				
				d.appendChild(p);
				d.appendChild(reflection);
			} else {
				var canvas = document.createElement('canvas');
				if (canvas.getContext) {
					/* Copy original image's classes & styles to div */
					d.className = newClasses;
					p.className = 'reflected';
					
					d.style.cssText = p.style.cssText;
					p.style.cssText = 'vertical-align: bottom';
			
					var context = canvas.getContext("2d");
				
					canvas.style.height = reflectionHeight+'px';
					canvas.style.width = reflectionWidth+'px';
					canvas.height = reflectionHeight;
					canvas.width = reflectionWidth;
					
					d.style.width = reflectionWidth+'px';
					d.style.height = divHeight+'px';
					p.parentNode.replaceChild(d, p);
					
					d.appendChild(p);
					d.appendChild(canvas);
					
					context.save();
					
					context.translate(0,image.height-1);
					context.scale(1,-1);
					
					context.drawImage(image, 0, 0, reflectionWidth, image.height);
	
					context.restore();
					
					context.globalCompositeOperation = "destination-out";
					var gradient = context.createLinearGradient(0, 0, 0, reflectionHeight);
					
					gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");
					gradient.addColorStop(0, "rgba(255, 255, 255, "+(1-options['opacity'])+")");
		
					context.fillStyle = gradient;
					if (navigator.appVersion.indexOf('WebKit') != -1) {
						context.fill();
					} else {
						context.fillRect(0, 0, reflectionWidth, reflectionHeight*2);
					}
				}
			}
		} catch (e) {
	    }
	},
	
	remove : function(image) {
		if (image.className == "reflected") {
			image.className = image.parentNode.className;
			image.parentNode.parentNode.replaceChild(image, image.parentNode);
		}
	}
}

function addReflections() {
	var rimages = document.getElementsByClassName('reflect');
	for (i=0;i<rimages.length;i++) {
		var rheight = null;
		var ropacity = null;
		
		var classes = rimages[i].className.split(' ');
		for (j=0;j<classes.length;j++) {
			if (classes[j].indexOf("rheight") == 0) {
				var rheight = classes[j].substring(7)/100;
			} else if (classes[j].indexOf("ropacity") == 0) {
				var ropacity = classes[j].substring(8)/100;
			}
		}
		
		Reflection.add(rimages[i], { height: rheight, opacity : ropacity});
	}
}

//var previousOnload = window.onload;
//window.onload = function () { if(previousOnload) previousOnload(); addReflections(); }