//<!--

var pos = 0;
var timePerPhoto = 4000;
var photoTimer;
var running = false;
var waiting;
var back;

function replaceURLWithHTMLLinks(text) {
	var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;//[\(]*([A-Z]*)[\)]*
	return text.replace(exp,"<a href='$1'>$1</a>");
}

function nextImg(){
	if(waiting)return;
	back = false;
	pos++;
	if(pos == (imageArray.length)) pos = 0;
	$.history.load(Number(pos)+1);
}

function prevImg(){
	if(waiting)return;
	back = true;
	pos--;
	if(pos == -1) pos = imageArray.length-1;
	$.history.load(Number(pos)+1);
}
$(document).ready(function(){

	$.history.init(function(url) {
			if(url=='')return;
			pos = url-1;
			fwd();
	});

	if($('#image').width()>720){
		$('#image').css({'width':720});
	}
	else{
		$('#image').css({'width':'auto'});
	}
	if($('#imageB').width()>720){
		$('#imageB').css({'width':720});
	}
	else{
		$('#imageB').css({'width':'auto'});
	}
	$('.dotFive').css({
		  'opacity': '0.5',
		  'filter': 'alpha(opacity=50)',
		  '-moz-opacity': '0.5',
		  '-khtml-opacity': '0.5'
	});
	$('.shadow2').css({
		  'opacity': '0.65',
		  'filter': 'alpha(opacity=65)',
		  '-moz-opacity': '0.65',
		  '-khtml-opacity': '0.65'
	});
	$('.shadow').css({
		  'opacity': '0.65',
		  'filter': 'alpha(opacity=65)',
		  '-moz-opacity': '0.65',
		  '-khtml-opacity': '0.65'
	});
	$('#waiter').css({'background-position':+($(document).width()/2-120)+'px '+Math.floor($('#image').height()/2-30)+'px'});
	//h = $('#imageHolder').height();
	//$('#backwd').css({'top': h/2-35/2});
	//$('#forwd').css({'top': h/2-35/2});
	//$('#topMenu').css({'left': ($(document).width()/2-$('#topMenu').width()/2) + "px"});
	//$('#tags').css({'left': ($(document).width()/2-$('#tags').width()/2) + "px"});
	
/*	$(window).resize(function() {
		$(".image").attr('height',$(window).height());
		h = $('#imageHolder').height();
		$('#backwd').css({'top': h/2-35/2});
		$('#forwd').css({'top': h/2-35/2});
		$('#topMenu').css({'left': ($(document).width()/2-$('#topMenu').width()/2) + "px"});
		//$('#tags').css({'left': ($(document).width()/2-$('#tags').width()/2) + "px"});
	});*/
	//if(imageArray!=null)
	//	photoTimer = setInterval(fwd,timePerPhoto);
});

function addEvent(id,e,fn){
	if (document.getElementById(id).attachEvent)
		document.getElementById(id).attachEvent("on"+e, fn)
	else if (document.addEventListener)
		document.getElementById(id).addEventListener(e, fn, false)
}

function pause(){
	if(running){
		clearInterval(photoTimer);
		running = false
	}else{
		photoTimer = setInterval(fwd,timePerPhoto);
		running = true
	}
}
var openMenus = [];
function showChild(id,event){
	if(document.getElementById("child"+id))
	if(document.getElementById("child"+id).style.display == "none" || document.getElementById("child"+id).style.display == ""){
		document.getElementById("child"+id).style.display = "block";
		for(i=0;i<openMenus.length;i++){
			oid = openMenus.pop();
			$("#child"+oid).animate({opacity:'0'},300,function(){document.getElementById("child"+oid).style.display = "none";});
		}
		$("#child"+id).animate({opacity:'1'},300,function(){openMenus.push(id);});
	}
	else
	{
		$("#child"+id).animate({opacity:'0'},300,function(){document.getElementById("child"+id).style.display = "none";openMenus.pop();});
	}
	else;
}

function outOfA(id,event){
	var toElement = null;
	if (event.relatedTarget)
	toElement = event.relatedTarget;
	else if (event.toElement)
	toElement = event.toElement;
	if(toElement && toElement.tagName=="DIV")
		hideChild(id,event);
}

function hideChild(id,event){
	var toElement = null;
	if (event.relatedTarget)
	toElement = event.relatedTarget;
	else if (event.toElement)
	toElement = event.toElement;
	if(toElement && toElement.tagName!="A")
	if(document.getElementById("child"+id)){
		document.getElementById("child"+id).style.display = "none";$("#child"+id).fadeTo(1,0);
	}
}

function wait(flag){
	if(flag){
		waiting = true;
		$('#waiter').css({'background-position':+($(document).width()/2-120)+'px '+Math.floor($('#image').height()/2)+'px'});
		document.getElementById('waiter').style.display = "block";
	}
	else{
		waiting = false;
		document.getElementById('waiter').style.display = "none";
	}
}



function infoShow(){
	if(document.getElementById('infoDiv').style.display!='block'){
		$('#infoDiv').css({'opacity':0});
		document.getElementById('infoDiv').style.display='block';
		document.getElementById('aCont').style.textDecoration = 'underline';
		$('#infoDiv').animate({opacity: 1},800);
	}
	else{
		$('#infoDiv').stop(true,true);
		document.getElementById('infoDiv').style.display='none';
		document.getElementById('aCont').style.textDecoration = 'none';
	}
}

function fwd(){
	if(imageArray.length==1)
		return;
	if ($('#imageHolder:animated').size() != 0){
		$('#imageHolder').stop(true,true);
		return;
	}


	var ima = new Image();
	wait(true);
	ima.onload = function(){
		wait(false);
		if(back===true)
			pspos = pos - 1;
		else pspos = pos + 1;
		
		var tags; tags = tagArray[pos]; if(tags!=''){tags = tags.split(",");document.getElementById('tags').style.display='block'}else document.getElementById('tags').style.display='none'; tt = "";
		for(j = 0;j<tags.length; j++){
			tt+="<a ";
			ttt = tags[j].split('`');
			if(selectedTag == ttt[0])tt+="style='color:#7db610;'";
			tt+=" href='?tag="+ttt[1]+"'>"+ttt[0]+"</a>";
			if(j<(tags.length-1)) tt+= " , ";
		}
		info = replaceURLWithHTMLLinks(infoArray[pos]);
		document.getElementById('infoCont').innerHTML = info;
		document.getElementById('tagLinks').innerHTML = "<span style='color:#888;margin-right:3px'>TAGS : </span>"+tt;
		if(trim(info)=='' &&  trim(tt)=='')
			document.getElementById('infoandtags').style.display = 'none';
		else document.getElementById('infoandtags').style.display = 'block';
		
		$('#imageB').attr('src',imageArray[pos]);
		$('#imageHolder').animate({
			opacity: 0
		}, timePerPhoto/6,
		function() {
			document.getElementById('fnumber').innerHTML = pos+1;
			$('#image').css({'width':'auto'});
			$('#image').attr('src',imageArray[pos]);
			if($('#image').width()>720){
				$('#image').css({'width':720});
			}
			$('#infoandtags').css({width:$('#image').width()+'px'});
			$('#imageHolder').fadeTo('fast', 1);
			$('#imageHolderB').css({display:'none'});
			$('#imageHolderB').css({opacity:0});
			$('#imageHolderB').css({display:'block'});
			$('#imageHolderB').fadeTo('fast', 1);
			var src;
			if(!back){
				if(pos+1!=imageArray.length)
					src = imageArray[pos+1];
				else src = imageArray[0];
			}
			else{
				if(pos-1!=-1)
					src = imageArray[pos-1];
				else src = imageArray[imageArray.length-1];
			}
			$('#imageB').attr('src',src);
			$('#imageHolderB').css({display:'none'});
			$('#imageHolderB').fadeTo(0, 1);
			$('#waiter').css({'background-position':($(document).width()/2-120)+'px 50%'});
		});
	};
	ima.src = imageArray[pos];
}

function trim(S){
	S=S.replace(/ /g,' ');
	return S.replace(/(^\s+)|(\s+$)/g, '');
}
//-->