var switchList = new Array;
var switchCount=0;
var autoswitch_blocked;
var switchTime = 5000;
var switchSpeed= 100;

var albumcount = 0;
var albums = '';
albums = new Array;
window.onload = bodyload;

function bodyload(){
	new_windows();
	albumload();
	albumanchors();
	setup_switches();
	autoSwitchItem();
}

function albumload(){
	if(albumcount > 0){
		for(i=1;document.getElementById('thumb_container_' + i);i++){
			trg = document.getElementById('thumb_container_' + i);
			trg.innerHTML = albums[i];
		}
	}
}

function albumanchors(){
	var album = document.getElementById('singlealbum');
	if(!album) { return false; }
	var img_anchors = album.getElementsByTagName('a');
	var l = img_anchors.length;
	for(var i = 0; i < l ; i++){
		var thisanchor = img_anchors[i];
		thisanchor.onclick = function(){ open_image(this);return false;};
	}
}
function open_image(img_anchor){
	img = img_anchor.getElementsByTagName('img');
	thisimg = img[0];
	img_target = document.getElementById('bigimage');
	new_src = thisimg.src.replace(/_h[0-9]+_w[0-9]+_f\.jpg$/,'_h285_w450_a.jpg');
	img_target.src = new_src;
	document.getElementById('bigimage_desc').innerHTML = thisimg.alt;
	return false;
}

function new_windows(){
	var bl_anchors = document.getElementsByTagName('a');
	if(!bl_anchors) return false;
	var l = bl_anchors.length;
	var re = /\bnewwindow\b/;
	for(var i = 0; i < l; i++){
		var thisanchor = bl_anchors[i];
		if(thisanchor.className.match(re)){
			thisanchor.onclick = function(){opennew(this.href);return false;}
		}
	}
}
function opennew(uri){
	newwindow = window.open(uri,'_blank');
	if(!newwindow){return true;}
	if (window.focus) {newwindow.focus()}
	return false;
}


function setup_switches()
{
	var tList = document.getElementsByTagName('a');
	var scnt=0;
	for( cnt=0; cnt<tList.length; cnt++ )
	{
		tItem = tList[cnt];
		if(tItem.className == 'switch_normal')
		{
			switchList.push(tItem);
			tItem.onclick = function(){autoswitch_blocked=true;switchItem(this,0);return false;};
			//tItem.onmouseover = function(){autoswitch_blocked=true;switchItem(this,0);return false;};
			tItem.onmouseout = function(){autoswitch_blocked=false;return false;};
		}
	}
}

function autoSwitchItem()
{
	if( ! autoswitch_blocked )
	{
		switchCount--;
		if( switchCount <0)
		{
			switchCount = switchList.length-1;
		}
		switchItem(switchList[switchCount],true);
	}
	setTimeout("autoSwitchItem()",switchTime);
}

function switchItem(obj,auto_switch)
{
	if( auto_switch && autoswitch_blocked ) // blocked autoswitch
	{
		return false;
	}
	else if (! auto_switch ) // setup manual switch
	{

	}

	// setup_switch
	relAtt=obj.getAttribute('rel');
	if( relAtt && (target=document.getElementById(relAtt) ) )
	{
		for( cnt=switchList.length; cnt;cnt--)
		{
			if( sItem = switchList[cnt-1] )
			{
				if( relAtt=sItem.getAttribute('rel'))
				{
					if(tItem=document.getElementById(relAtt) )
					{
						sItem.className='switch_normal';
						tItem.style.display='block';
						tItem.style.position='absolute';
						tItem.style.zIndex--;
					}
				}
				if( sItem==target )
				switchCount=cnt;
			}
		}
		obj.className='switch_selected';
		target.style.display = 'block';
		target.style.position= 'absolute';
		target.style.zIndex = switchList.length;

		crossFade(target.id,0,100);
	}
}

function crossFade(id,cur,end)
{
	cur=end-((end-cur)*0.80);

	//cur += 10;
	if( cur < (end-1) )
	{
		setTimeout('crossFade(\''+id+'\','+cur+','+end+')',60);
	}
	else cur=100;

	if(target=document.getElementById(id) )
	{
		target.style.opacity=cur/100;
		target.style.filter='alpha(opacity='+cur+')';
	}
}

