function rotatePhoto(imgname,r){
	var id = "wb_"+imgname;
	var obj = document.getElementById(id);
	var url = "/mason/components/photo/pic_rotate.html?imgname="+imgname+"&rotate="+r;
    
	if (obj){
		ajaxLoader(url,id);

		var tm = new Array();
		for (var i=0; i<4;i++){
			tm[i] = document.getElementById("th"+i+"_"+imgname).href;
		}

		for (var i=0; i<4;i++){
			document.getElementById("th"+i+"_"+imgname).href = tm[(i+(4-r))%4];
		}
		instant_popuphide("pop"+imgname+"pop");
	}
}

function flagPhoto(imgname,c){
	ajaxLoader("/mason/components/photo/save_photos_options.html?img_"+imgname+"=flag_"+c);
}

function setDefaultPhoto(imgname,sectionId,propkey){
	var element = document.getElementById(sectionId);
	ajaxLoader("/mason/components/photo/pic_default.html?imgname="+imgname+"&section="+sectionId.substring(0,5)+"&propkey="+propkey+"&set="+element.checked);
}

function setDefaultUserPhoto(imgname,type,propkey){
	ajaxLoader("/mason/components/photo/pic_default.html?imgname="+imgname+"&propkey="+propkey+"&type="+type);
}

function loadDefaultUserPhoto(imgname,type,imageId,linkId){
	// Used for reloading the "default user photo" in reports
	// when a user changes it
	var size = "preview";
	
	var imageLink = document.getElementById(linkId);
	imageLink.href = "/mason/Photos/view.html?photo="+ imgname +"&type="+ type;
	
	var imageObj = document.getElementById(imageId);
	imageObj.src = "/mason/components/photo/pic_view.html?imgname="+imgname+"&type="+type+"&size="+size;
}

function showPhoto(id,imgname,n,type,size){
	var obj = document.getElementById(id);
	var size = size || "thumb";
	if (obj){
		// Load selected photo
		obj.src = "/mason/components/photo/pic_view.html?imgname="+imgname+"&type="+type+"&size="+size;

		// Reset selected photo
		var tbody = obj.parentNode.parentNode.parentNode;
		var squares = tbody.getElementsByTagName("a");
		for (var i=0; i<squares.length; i++){
			squares[i].className = "";
		}

		// Mark selected photo box
		var photo = document.getElementById(id+"_"+window.current_photo);
		if (photo) photo.className = "";

		window.current_photo = n;

		var photo = document.getElementById(id+"_"+window.current_photo);
		if (photo) photo.className = "current";
	}
}

function showHint(popper, text) {
	var poppee = document.createElement("div");
	with (poppee){
		id = popper.id + "_hint";
		style.width = text.length*7 + "px";
		style.position = "absolute";
		style.fontSize = "10px";
		style.zIndex = "99999999";
		style.padding = "3px";
		style.border = "solid #5A7EA4 1px";
		style.backgroundColor = "#FFFFFF";
		style.color = "#000000";
		style.margin = "0px";
		innerHTML = text;
	}

	document.body.appendChild(poppee);
//	popper.appendChild(poppee);
//	_UTIL_setIFrameBackground(poppee.id);

	var left = _UTIL_getPosleft(popper);
	var top = _UTIL_getPostop(popper);
	var offsetheight = poppee.offsetHeight+22;
	var offsetwidth = poppee.offsetWidth;
	var leftOffset = popper.offsetWidth+2;
	var topOffset = 1;
	var availWidth = _UTIL_getAvailWidth();
	var availHeight = _UTIL_getAvailHeight();
	var scrOfX = _UTIL_getScrollLeft();
	var scrOfY = _UTIL_getScrollTop();
	
	if ((left+offsetwidth+leftOffset)<(availWidth+scrOfX)){
		poppee.style.left = (left + leftOffset) + "px";
	}else{
		poppee.style.left = Math.max(scrOfX,(left - offsetwidth-4)) + "px";
	}

	if ((top+offsetheight+topOffset)<(availHeight+scrOfY)){
		poppee.style.top = (top+topOffset) + "px";
	}else{
		poppee.style.top = Math.max(scrOfY,(availHeight+scrOfY+topOffset - offsetheight)) + "px";
	}
}

function hideHint(popper) {
	var poppee = document.getElementById(popper.id+"_hint");
//	popper.removeChild(poppee);
	document.body.removeChild(poppee);
}

function openPanoViewer(imgname) {
	var w = window.open("/mason/PanoViewer/?image="+imgname, "Panorama", "menubar=1, scrollbars=0, width=100, height=100, resizable=0, location=0");
	w.moveTo(0,0);
	w.resizeTo(screen.availWidth,screen.availHeight);
}

function addPictureField (maxPics) {
    var i = 2;
    for (i;i<=maxPics;i++) {
        var element = document.getElementById("block_" + i);
        if(element.style.display == 'none') {
            element.style.display = 'block';
            break;
        }
    }
}

function toggleVisibility(id) {
    var element = document.getElementById(id);
    if(element.style.display == 'none')
        element.style.display = 'block';
    else
        element.style.display = 'none';
}

function checkFiles() {
	var result = false;
	
	for (var index = 0; index < document.uploadFiles.elements.length; index++) {
		var currentElement = document.uploadFiles.elements[index];
		if (currentElement.type == "file") {
			result = (currentElement.value != "");
			if (result) break;
		}
	}
	
	if(!result) {
		alert("No files selected! Please use the 'Browse...' button to find photo files on your computer.");
	}
	return result;
}

function popupReposition(img){
	img.style.visibility = 'visible';
	var el = img;
	while (true){
		if (el.id.match(/^popup[0-9]+pop$/)){
			el.style.top = _UTIL_getScrollTop()+(_UTIL_getAvailHeight()-el.clientHeight)/2 + "px";
			break;
		}else{
			el = el.parentNode;
		}
	}
	_UTIL_setIFrameBackground(el.id);
}
