//<!--

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

function replaceURLWithHTMLLinks(text) {
	if(!text)return '';
	var exp = /(\b(((https?|ftp|file|):\/\/)|www)([-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]))/ig;//[\(]*([A-Z]*)[\)]*
	return text.replace(exp,"<a href='http://www$5'>$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(){
	var panePos = 1;
	var paneCount = $('.previewPane').size();
	$('.rightPrevButton').click(function(){
		panePos++;
		$('.prevSlider').animate({'left':(-750*(panePos-1))+'px'},'fast');
		if(paneCount<=panePos)
			$('.rightPrevButton').css('display','none');
		$('.leftPrevButton').css('display','block');
	});
	$('.leftPrevButton').click(function(){
		panePos--;
		$('.prevSlider').animate({'left':(-750*(panePos-1))+'px'},'fast');
		if(panePos<=1)
			$('.leftPrevButton').css('display','none');
		$('.rightPrevButton').css('display','block');
	});
	$('#prevContainer img').css('opacity','0.8').hover(
		function(){
			$(this).stop().css({'opacity':1});
		}
		,function(){
			$(this).animate({'opacity':'0.8'},500);
		}
	);
	//if(document.getElementById('thumbContainer'))
		$('#thumbContainer').css({'display':'block','width':'700px','opacity':1});//fadeTo(1000,1);
	$.history.init(function(url) {if(!document.getElementById('imageHolder'))return;
		if(!aGallery)return;
		if(url=='')url=1;
		pos = url-1;
		fwd();
	});
	console.log("1: " + $('#image').width());
	if($('#image').width()>720){
		$('#image').css({'width':720});
	}
	else{
		$('#image').css({'width':'auto'});
	}
	console.log("2: " + $('#imageB').width());
	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'
	});	
	$('.dotFiveCounter').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'
	});
	if(!previewFirst)
	$('#waiter').css({'display':'block'});
	//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;
	}
	
	if(!previewFirst){
		if($('#theSlider').css('top')=='0px')
			$('#theSlider').animate({'top':'-520px'},'fast');
	}else previewFirst = false;
		
	var ima = new Image();
	wait(true);
	ima.onload = function(){
		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+= " , ";
		}

		wait(false);
		if(imageArray[pos].search("iframe") == -1)
			$('#imageB').attr('src',imageArray[pos]);
		info = replaceURLWithHTMLLinks(infoArray[pos]);
		document.getElementById('infoandtags').style.display = 'none';
			
		$('#imageHolder').animate({
			opacity: 0
		}, timePerPhoto/6,
		function() {
			if(trim(info)!='' ||  trim(tt)!=''){
				document.getElementById('infoCont').innerHTML = info;
				document.getElementById('tagLinks').innerHTML = "<span style='color:#888;margin-right:3px'>TAGS : </span>"+tt;
				document.getElementById('infoandtags').style.display = 'block';
			}
			if(document.getElementById('fnumber'))
			document.getElementById('fnumber').innerHTML = pos+1;
			$('#image').css({'width':'auto'});
			
			$('#clipHolder').hide();
			$('#image').show();
			if(imageArray[pos].search("iframe") == -1)
				$('#image').attr('src',imageArray[pos]);
			
			console.log("3: "+ $('#image').width());
			if($('#image').width()>720){
				$('#image').css({'width':720});
			}
			if($('#imageHolder').css('display')=='none'){
				$('#imageHolder').css('display','block');
				$('#imageHolder').fadeTo('slow', 1);
			}else $('#imageHolder').fadeTo('fast', 1);
			
			if($('#image').height()>500){
				$('#image').css({'height':500,'margin-top':0});
			}else if($('#image').height()<=500){
				$('#image').css({'height':'auto','margin-top':(500-$('#image').height())/2});
			}
			
			$('#infoandtags').css({width:$('#image').width()+'px'});
			$('#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];
			}
			if(src.search("iframe") == -1)
				$('#imageB').attr('src',src);
			
			$('#imageHolderB').css({display:'none'});
			$('#imageHolderB').fadeTo(0, 1);
			//$('#waiter').css({'background-position':($(document).width()/2-120)+'px 50%'});
			$('#image').unbind('click').bind('click',nextImg);
		});
	};
	ima.onerror = function(){
		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+= " , ";
		}

		
		
		
		if( imageArray[pos].search("iframe") != -1){
//			alert('clipHolderB1');
			videoWidth = getWidth(decodeURIComponent(imageArray[pos]).substr(5));
			videoHeight = getHeight(decodeURIComponent(imageArray[pos]).substr(5));
			newHeight = parseInt((videoHeight/videoWidth)*750);
			videoStr = setWidth(decodeURIComponent(imageArray[pos]).substr(5),750);
			videoStr = setHeight(videoStr,newHeight);
			// newWidth = parseInt((videoWidth/videoHeight)*500);
			// videoStr = setWidth(decodeURIComponent(imageArray[pos]).substr(5),newWidth);
			// videoStr = setHeight(videoStr,500);
			$('#clipHolderB').css("display","block");
			$('#clipHolderB').css("height",newHeight +"px");
			$('#clipHolderB').css("width",750 +"px");
			$('#clipHolderB').css("margin-top",(500 - newHeight)/2 +"px");
			$('#clipHolderB').html(videoStr);
			$('#imageB').hide();
		}else{
			$('#clipHolderB').hide();
			$('#imageB').attr('src',imageArray[pos]);
			$('#imageB').show();
		}
		wait(false);
		
//		$('#imageB').attr('src',imageArray[pos]);
		
		info = replaceURLWithHTMLLinks(infoArray[pos]);
		document.getElementById('infoandtags').style.display = 'none';
			
		$('#imageHolder').animate({
			opacity: 0
		}, timePerPhoto/6,
		function() {
			if(trim(info)!='' ||  trim(tt)!=''){
				document.getElementById('infoCont').innerHTML = info;
				document.getElementById('tagLinks').innerHTML = "<span style='color:#888;margin-right:3px'>TAGS : </span>"+tt;
				document.getElementById('infoandtags').style.display = 'block';
			}
			if(document.getElementById('fnumber'))
			document.getElementById('fnumber').innerHTML = pos+1;
			
			$('#image').hide();
			
//			alert(imageArray[pos]);
			
			videoWidth = getWidth(decodeURIComponent(imageArray[pos]).substr(5));
			videoHeight = getHeight(decodeURIComponent(imageArray[pos]).substr(5));
			newHeight = parseInt((videoHeight/videoWidth)*750);
			videoStr = setWidth(decodeURIComponent(imageArray[pos]).substr(5),750);
			videoStr = setHeight(videoStr,newHeight);
			// newWidth = parseInt((videoWidth/videoHeight)*500);
			// videoStr = setWidth(decodeURIComponent(imageArray[pos]).substr(5),newWidth);
			// videoStr = setHeight(videoStr,500);
			
			$('#clipHolder').css("display","block");
			$('#clipHolder').css("height",newHeight +"px");
			$('#clipHolder').css("width",750 +"px");
			$('#clipHolder').css("margin-top",(500 - newHeight)/2 +"px");
			$('#clipHolder').html(videoStr);
			$('#clipHolder').show();
		
			if($('#imageHolder').css('display')=='none'){
				$('#imageHolder').css('display','block');
				$('#imageHolder').fadeTo('slow', 1);
			}else $('#imageHolder').fadeTo('fast', 1);
			
			if($('#image').height()>500){
				$('#image').css({'height':500,'margin-top':0});
			}else if($('#image').height()<=500){
				$('#image').css({'height':'auto','margin-top':(500-$('#image').height())/2});
			}
			
			$('#infoandtags').css({width:$('#image').width()+'px'});
			$('#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];
			}
			
			if( src.search("iframe") != -1){
				// alert('clipHolderB2');
				videoWidth = getWidth(decodeURIComponent(src).substr(5));
				videoHeight = getHeight(decodeURIComponent(src).substr(5));
				newHeight = parseInt((videoHeight/videoWidth)*750);
				videoStr = setWidth(decodeURIComponent(imageArray[pos]).substr(5),750);
				videoStr = setHeight(videoStr,newHeight);
				
				// newWidth = parseInt((videoWidth/videoHeight)*500);
				// videoStr = setWidth(decodeURIComponent(src).substr(5),newWidth);
				// videoStr = setHeight(videoStr,500);
				$('#clipHolderB').css("display","block");
				$('#clipHolderB').css("height",newHeight +"px");
				$('#clipHolderB').css("width",750 +"px");
				$('#clipHolderB').css("margin-top",(500 - newHeight)/2 +"px");
				$('#clipHolderB').html(videoStr);
				$('#imageB').hide();
			}else{
				$('#clipHolderB').hide();
				$('#imageB').attr('src',src);
				$('#imageB').show();
			}
			$('#imageHolderB').css({display:'none'});
			$('#imageHolderB').fadeTo(0, 1);
			//$('#waiter').css({'background-position':($(document).width()/2-120)+'px 50%'});
			$('#image').unbind('click').bind('click',nextImg);
		});
	};
//	alert(imageArray[pos]);
	ima.src = imageArray[pos];
}

function previews(){
	if($('#theSlider').css('top')=='0px')
		$('#theSlider').animate({'top':'-520px'},'fast');
	else $('#theSlider').animate({'top':'0px'},'fast');
}

function selPrev(id){
	$('#theSlider').animate({'top':'-520px'},'fast',function(){
		back = false;
		pos = id;
		if(pos == (imageArray.length)) pos = 0;
		$.history.load(Number(pos)+1);
	});
}

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