var next=1;
var ready=1;
var needBg=0;
var isIE7=(navigator.appVersion.indexOf('MSIE 7.')==-1) ? false : true;
var cnt_img_loaded=0;

var fixed_height=0;

function incrementImgLoad(){
	cnt_img_loaded++;
}

function picChange(side){
	if(ready){
		ready=0;
		var parent = document.getElementById('main_img');
		var photoD = document.getElementById('photoDiv');
		var nameD = document.getElementById('nameDiv');
		var quoteD = document.getElementById('quoteDiv');
		var textD = document.getElementById('textDiv');
		var counter = document.getElementById('counter');
	
		if(!(fixed_height>0))document.getElementById('backLink').style.display='none';
		
		var picsS = counter.innerHTML;
		var picsArr = picsS.split('/');
		var curPic = parseInt(picsArr[0]);
		var maxPic = parseInt(picsArr[1]);
		var nextPic='';
		
		if(side=='prev')nextPic=(curPic==1)?maxPic:(curPic-1);
		if(side=='next')nextPic=(curPic==maxPic)?1:(curPic+1);
		if(side=='new')nextPic=1;
		if(parseInt(side)>0)nextPic=parseInt(side);
	
		if(nextPic){
			next = nextPic-1;
			photoD.innerHTML=photo_author[next]?((noPSign?'':'Photographer<br/>')+photo_author[next]):'';
			nameD.innerHTML=photo_name[next];
			quoteD.innerHTML=photo_quote[next]?(noQuotes?photo_quote[next]:('"'+photo_quote[next]+'"')):'';
			textD.innerHTML=photo_text[next];
			counter.innerHTML=nextPic+'/'+maxPic;
			
			if(roundChange){
				var roundD = document.getElementById('roundDiv');
				var roundD1 = document.getElementById('roundDiv1');
				roundD.style.display='none';
				roundD1.style.display='none'
				roundD.innerHTML=photo_round[next];
				roundD1.innerHTML=photo_round[next];
			}
			if(typeof(cgiChange)!='undefined'){
				document.getElementById('cgiPic').style.display='none';
				document.getElementById('cgiPic').innerHTML='';
			}
			
			parent.innerHTML='';
			parent.style.height='300px';
			need_bg=1;
			setTimeout("setBG()",300);
	
			var newImg = new Image();
			
			if(isIE7) newImg.onload=function(){imageLoaded();}
			else newImg.onload=imageLoaded;
			
			newImg.src = '/uploads/'+folder+'/'+photo_file[next];
		}
	}
	return false;
}

function setBG(){
	if(need_bg)document.getElementById('main_img').style.background='url(/img/loading.gif) no-repeat center 150px';
}

function imageLoaded(){
	var parent = document.getElementById('main_img');
	var sizes = screenSize();
	var screenH= sizes[1];
	var maxImgH = screenH-170;
	if(fixed_height>0)maxImgH=fixed_height;
	var newH = (photo_height[next]>maxImgH)?('height="'+maxImgH+'"'):'';

	parent.innerHTML='<img src="/uploads/'+folder+'/'+photo_file[next]+'" alt="'+photo_name[next]+'" '+newH+' />';
	need_bg=0;
	parent.style.background='none';
	parent.style.height='auto';

	document.getElementById('backLink').style.display='';
	if(roundChange){
		if(fixed_height>0){
			document.getElementById('roundDiv').style.display='';
		}else{
			if((maxImgH-photo_height[next])<50){
				document.getElementById('roundDiv1').style.display='';
			}else{
				document.getElementById('roundDiv').style.display='';
			}
		}
	}
	if(typeof(cgiChange)!='undefined'){
		if(photo_cgism[next]){
			document.getElementById('cgiPic').style.marginTop='20px';
			cur_h=photo_height_sm[next];
			document.getElementById('allbutsmall').scrollHeight;
			main_image_height=(photo_height[next]>maxImgH)?maxImgH:photo_height[next];
			space_left=main_image_height-document.getElementById('allbutsmall').scrollHeight-20;
			if(space_left>cur_h)document.getElementById('cgiPic').style.marginTop=20+(space_left-cur_h)+'px';
			if(space_left<cur_h)cur_h=space_left;
			if(cur_h<30)cur_h=photo_height_sm[next];
			document.getElementById('cgiPic').innerHTML='<img src="/uploads/cgi/side/'+photo_cgism[next]+'" alt="'+photo_name[next]+'" height="'+cur_h+'" />';
			///////document.getElementById('cgiPic').innerHTML='<img src="/uploads/cgi/side/'+photo_cgism[next]+'" alt="'+photo_name[next]+'" />';
			document.getElementById('cgiPic').style.display='block';
		}
	}
	ready=1;
}
function preload(src){
	var newImg = new Image();
	newImg.src=src;
	if(isIE7)newImg.onload=function(){}
}

function okd(e){
	var keynum;
	var keychar;

	if(!e){
		e=window.event;
		keynum = e.keyCode;
	}else keynum = e.which;
	if(keynum==37)picChange('prev');
	if(keynum==39)picChange('next');
}

function toggleTitle(elemObj){
	var spanObj = elemObj.getElementsByTagName('span')[0];
	spanObj.style.display=(spanObj.style.display != 'block')?'block':'';
	
}

function toggleTitle1(elemObj){
	var spanObj = elemObj.getElementsByTagName('div')[0];
	spanObj.style.display=(spanObj.style.display != 'block')?'block':'';
	
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


sliding=false;
slided_obj=null;
slided_obj_container=null;
slided_obj_container_id=null;
cur_sliding_obj=null;
cur_sliding_obj_container=null;
slide_step=10;

function slideNav(id){
	if(cnt_img_loaded<4){
		alert('Please wait while the page loads');
		return false;
	}
	if((!sliding)&&(id!=slided_obj_container_id)){
		if(slided_obj_container_id!=null){
			slided_obj_container.style.display='none';
			slided_obj=null;
			slided_obj_container=null;
			slided_obj_container_id=null;
		}
		sliding=true;
		containerObj=document.getElementById(id);
		bodyObj=document.getElementById('thecontainer');
		contentObj=document.getElementById(id+'_content');
		xy=findPos(bodyObj);
		containerObj.style.left=xy[0]+1000-parseInt(contentObj.style.width)+'px';
		containerObj.style.top='22px';
		contentObj.style.left=contentObj.style.width;
		containerObj.style.display='';
		cur_sliding_obj=contentObj;
		cur_sliding_obj_container=containerObj;
		win_width=window.innerWidth-20;
		if(!win_width)win_width=document.body.clientWidth;
		document.getElementById('mouse_catcher').style.width=win_width+'px';
		document.getElementById('mouse_catcher').style.height=bodyObj.scrollHeight+'px';
		document.getElementById('mouse_catcher').style.display='';
		doSlideNav();
	}
	return true;
}

function doSlideNav(){
	new_x=parseInt(cur_sliding_obj.style.left)-slide_step;
	//alert(new_x);
	if(new_x<0)new_x=0;
	cur_sliding_obj.style.left=new_x+'px';
	if(new_x==0){
		slided_obj=cur_sliding_obj;
		slided_obj_container=cur_sliding_obj_container;
		slided_obj_container_id=cur_sliding_obj_container.id;
		sliding=false;
	}else{
		window.setTimeout("doSlideNav()",10);
	}
}

function slideNavOut(){
	if(!sliding){
		if(slided_obj_container_id!=null){
			sliding=true;
			doSlideNavOut();
		}
	}
	return false;
}

function doSlideNavOut(){
	new_x=parseInt(slided_obj.style.left)+slide_step;
	cur_sliding_obj.style.left=new_x+'px';
	if(new_x>(parseInt(slided_obj.style.width))){
		slided_obj_container.style.display='none';
		slided_obj=null;
		slided_obj_container=null;
		slided_obj_container_id=null;
		sliding=false;
		document.getElementById("mouse_catcher").style.display='none';
	}else{
		window.setTimeout("doSlideNavOut()",10);
	}
}

files_cnt=0;

function addFile(tmpname, realname, filesize){
	files_cnt++;
	theObj=document.getElementById('uploaded_files');
	theObj.innerHTML=theObj.innerHTML+'<div id="fl_'+files_cnt+'_div" style="padding-bottom:5px;overflow:hidden;"><div style="width:155px;float:left;">'+realname+' ('+filesize+')</div><div style="width:50px;float:left;"><a href="#" onclick="return delFile('+files_cnt+')">Delete</a></div><input type="hidden" name="fl_'+files_cnt+'" value="'+tmpname+'|'+realname+'"></div>';
}

function delFile(num){
	theObj=document.getElementById('fl_'+num+'_div');
	theObj.innerHTML='';
	theObj.style.display='none';
	return false;
}

function openFTP(){
	srvr=document.ftp_form.server[document.ftp_form.server.selectedIndex].value;
	if(srvr=='cl'){
		document.cl_archive_form.u.value=document.ftp_form.RumpusLoginUserName.value;
		document.cl_archive_form.p.value=document.ftp_form.RumpusLoginPassword.value;
		document.cl_archive_form.submit();
	}else{
		document.ftp_form.action="http://"+srvr+':8000/Rumpus.clogin'
		document.ftp_form.submit();
	}
}

function screenSize(){
	var viewportwidth;
	var viewportheight;
 
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined'){
    	viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}
 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
    	viewportwidth = document.documentElement.clientWidth,
    	viewportheight = document.documentElement.clientHeight
	}
 
	// older versions of IE
	else{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	return Array(viewportwidth,viewportheight);
}