function loaddsplimg(direction, id){
	var connect = Post();
	connect[0].onreadystatechange = function(){
		if(connect[0].readyState == 4){
			try{
				var np = new Array();
				var th = new Array();
				th['next'] = new Array();
				th['prev'] = new Array();
				th['curr'] = new Array();
				var book = document.getElementById('photoBook');
				eval(connect[0].responseText);
				window.location = '#v=photos&id='+np['id'];
				clocation = '#v=photos&id='+np['id'];
				var photo = document.getElementById('viewedPhoto');
				photoID = np['id'];
				photo.src = 'http://img.publicsize.com/photo/'+np['dir']+'/original/'+np['file']+'.jpg';
				photoHeight = np['height'];
				photoWidth = np['width'];
				var i, file, dir, id;
				if(np['caption']!=''){
					document.title = np['caption']+' | PublicSize';
				}else{
					document.title = 'No Caption | PublicSize';
				}
				try{
					albumCoverStatus(np['is_cover'], np['id']);
				}catch(e){}
				try{
					ProPicStatus(np['is_profile'], np['id']);
				}catch(e){}
				ProPicCpation(np['caption'], np['owns']);
				getPhotoComments();
				book.innerHTML = "";
			}catch(e){
				loadBody('photos');
			}
		}
	};
	if(allowNext){
		var va = '';
		connect[0].open("POST", '/?p=prevnext&dir='+escape(direction)+'&id='+escape(id)+'&album='+albumID, true);
		connect[0].setRequestHeader("Content-Type", connect[1]);
		connect[0].send(va);
	}
}

function setAlbumFilter(status){
	var divs = document.getElementById('listofalbums').getElementsByTagName('div');
	var i;
	for(i=0;i<divs.length;i++){
		var clss = divs[i].getAttribute('class');
		if(clss!=null){
			if(status!='all'){
				if(clss.split(' ')[1]==status){
					divs[i].style.display = 'block';
				}else{
					divs[i].style.display = 'none';
				}
			}else{
				divs[i].style.display = 'block';
			}
		}
	}
	var optg = document.getElementById('headOptionGroup').getElementsByTagName('span');
	for(i=0;i<optg.length;i++){
		if(optg[i].getAttribute('id')=='f'+status){
			optg[i].setAttribute('class', 'headOptionsSel');
		}else{
			optg[i].setAttribute('class', 'headOptions');
		}
	}
}


function getPhotoComments(){
	var connect = Post();
	connect[0].onreadystatechange = function(){
		if(connect[0].readyState == 4){
			document.getElementById('photoCommentBlock').innerHTML = connect[0].responseText;
		}
	};
	var va = '';
	connect[0].open("POST", '/?p=photoComments&id='+escape(photoID), true);
	connect[0].setRequestHeader("Content-Type", connect[1]);
	connect[0].send(va);
}
function deletePhoto(){
	var connect = Post();
	var img = photoID;
	connect[0].onreadystatechange = function(){
		if(connect[0].readyState == 4){
			eval(connect[0].responseText);
			loaddsplimg('next', img);
		}
	};
	var va = '';
	connect[0].open("POST", '/?p=deletePhoto&id='+escape(img), true);
	connect[0].setRequestHeader("Content-Type", connect[1]);
	connect[0].send(va);
}
function postPhotoComment(id){
	var connect = Post();
	var message = document.getElementById('photoCommentBox').value;
	connect[0].onreadystatechange = function(){
		if(connect[0].readyState == 4){
			document.getElementById('postCBlock').innerHTML = connect[0].responseText;
			/*document.getElementById('postCBlock').innerHTML = message;*/
		}
	};
	var va = 'message='+escape(message);
	connect[0].open("POST", '/?p=postPhotoComment&id='+escape(id), true);
	connect[0].setRequestHeader("Content-Type", connect[1]);
	connect[0].send(va);
}
function setPhotoCaption(e, obj){
	var connect = Post();
	connect[0].onreadystatechange = function(){
		if(connect[0].readyState == 4){
			var good = false;
			eval(connect[0].responseText);
			if(good){
				obj.value = obj.value;
				obj.style.backgroundImage = 'url(/images/checkbox.png)';
				obj.style.backgroundRepeat = 'no-repeat';
				obj.blur();
				document.title = obj.value+' | PublicSize';
				setTimeout(function(){removeCheck(obj)}, 3000);
			}
		}
	};
	if(e.keyCode == 13 && obj.value.length>2){
		var va = 'cap='+obj.value;
		connect[0].open("POST", '/?p=setPhotoCaption&id='+escape(photoID), true);
		connect[0].setRequestHeader("Content-Type", connect[1]);
		connect[0].send(va);
	}
}
function nextprevimg(e, id){
	if(allowNext){
		var kc = e.keyCode;
		document.getElementById('viewedPhoto').removeAttribute('onclick');
		if(kc == 39 || e.type=='click'){
			document.getElementById('viewedPhoto').setAttribute('style', 'opacity:0.5;filter:alpha(opacity=50)');
			loaddsplimg('next', id);
		}else if(kc == 37){
			document.getElementById('viewedPhoto').setAttribute('style', 'opacity:0.5;filter:alpha(opacity=50)');
			loaddsplimg('prev', id);
		}
	}
}
function imgloaded(){
	if(photoID!=undefined){
		document.getElementById('viewedPhoto').setAttribute('style', 'cursor:pointer;opacity:1;filter:alpha(opacity=100)');
		var bod = document.getElementsByTagName('body')[0];
		bod.setAttribute('onkeyup', 'nextprevimg(event, '+photoID+')');
		var photo = document.getElementById('viewedPhoto');
		photo.setAttribute('onclick', 'nextprevimg(event, '+photoID+')');
		if(photoHeight != undefined){
			photo.height = photoHeight;
			photo.width = photoWidth;
		}
	}
}
function ProPicStatus(is_profile, id){
	var tpp = document.getElementById('txtProPic');
	if(is_profile==1){
		tpp.removeAttribute('onclick');
		tpp.removeAttribute('class');
		tpp.setAttribute('class', 'bold block5');
	}else{
		tpp.setAttribute('onclick', 'setProfile('+id+');');
		tpp.setAttribute('class', 'linknormal block5');
	}
}
function ProPicCpation(cap, is_owner){
	if(!is_owner || is_owner == undefined){
		document.getElementById('piccation').innerHTML = cap;
	}else{
		document.getElementById('piccation').innerHTML = '<input type="text" id="myComment" value="'+((cap==null||cap=='')?'Add Caption':cap)+'" onkeyup="setPhotoCaption(event, this);" onfocus="allowNext=false;if(this.value==\'Add Caption\'){this.value=\'\';}" onblur="allowNext=true;if(this.value==\'\'){this.value=\'Add Caption\'}" />';
	}
}
function removeCheck(obj){
	obj.style.backgroundImage = '';
}
function showProfileImage(mw,mh,th){
	mainw = mw;
	mainh = mh;
	thumbh = th;
	var thumb = document.getElementById('proImgThumb');
	var main = document.getElementById('proImgMain');
	var feed = document.getElementById('feed');
	thumb.style.display = 'none';
	with(main.style){
		display = 'block';
		position = 'absolute';
	}
	with(feed.style){
		marginTop = (mainh+20)+'px';
	}
	timer = setInterval('moveImage()',30);
}
function reSetProfileImage(w, h){
	var thumb = document.getElementById('proImgThumb');
	var main = document.getElementById('proImgMain');
	var feed = document.getElementById('feed');
	thumb.style.display = 'block';
	with(main.style){
		display = 'none';
		position = 'static';
	}
	main.style.left = thumb.offsetLeft+'px';
	main.style.height = h+'px';
	main.style.width = w+'px';
	with(feed.style){
		marginTop = '0px';
	}
}
function moveImage(){
	var main = document.getElementById('proImgMain');
	var con = document.getElementById('contentWide');
	var l=false, w=false, h=false;

	var center = Math.round(((con.offsetWidth - mainw) / 2) + con.offsetLeft);

	var intervalw = Math.round((mainw * mainh) / (180 * thumbh));
	/*var intervalh = Math.ceil((thumbh / mainh) * 10);*/
	var intervalh = Math.round(mainh / (mainw / intervalw));
	if(main.offsetLeft+20 < center)
		main.style.left = main.offsetLeft+5+'px';
	else
		l=true;
	if(main.offsetWidth < mainw)
		main.style.width = main.offsetWidth+intervalw+'px';
	else{
		main.style.width = mainw+'px';
		w=true;
	}
	if(main.offsetHeight < mainh)
		main.style.height = main.offsetHeight+intervalh+'px';
	else{
		main.style.height = mainh+'px';
		h=true;
	}
	if(l && w && h){
		clearInterval(timer);
	}
}

