//-------------------------------------------------------------------
// tbg.js - Thickbox Gallery 2
// Copyright (c) 2006-2007 Yuji Takenaka <webmaster@davilin.com>
// Licensed under the MIT:
// http://www.opensource.org/licenses/mit-license.php
//-------------------------------------------------------------------

var tbgReq = false;
var tbgPrev = "";
var tbgEdit = false;
var tbgKey = "";

//-------------------------------------------------------------------
// call XMLHttpRequest
//-------------------------------------------------------------------
if(window.XMLHttpRequest && !(window.ActiveXObject)){
	try{
		tbgReq = new XMLHttpRequest();
	}catch(e){
		tbgReq = false;
	}
}else if(window.ActiveXObject){
	try{
		tbgReq = new ActiveXObject("MXSML2.XMLHTTP");
	}catch(e){
		try {
			tbgReq = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			tbgReq = false;
		}
	}
}
//-------------------------------------------------------------------
// catch keydown event
//-------------------------------------------------------------------
document.onkeydown = function(e){
	if(document.all){
		tbgKey = event.keyCode;
	}else if(document.getElementById){
		tbgKey = (e.keyCode!=0) ? e.keyCode : e.charCode;
	}else if(document.layers){
		tbgKey = e.which;
	}
}
//-------------------------------------------------------------------
// focus when username field exists
//-------------------------------------------------------------------
window.onload = function(){
	if(document.getElementById("username")){
		document.getElementById("username").focus();
	}
}
//-------------------------------------------------------------------
// show/hide function icons
//-------------------------------------------------------------------
function display_functions(obj){
	var target = obj.nextSibling;
	var value = "none";
	var icon = obj.src;
	if(icon.indexOf("funcp")!=-1){
		value = "block";
	}
	target.style.top = obj.offsetTop + 9;
	target.style.left = obj.offsetLeft;
	target.style.display = value;
	if(value=="none"){
		icon = icon.replace(/funcm/, "funcp");
	}else{
		icon = icon.replace(/funcp/, "funcm");
	}
	obj.src = icon;
}
//-------------------------------------------------------------------
// save description
//-------------------------------------------------------------------
function saveComment(obj, id, target, album, filename, noajax){
	if(tbgKey==13){
		if(tbgReq){
			var value = obj.value;
			var url = target + "?a=" + album;
			var query = "z=description&v=" + filename + "&v1=" + value;
			if(tbgReq.readyState!=0 && tbgReq.readyState!=4){
				tbgReq.abort();
			}
			tbgReq.open("POST", url, true);
			tbgReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			tbgReq.send(query);
			tbgReq.onreadystatechange = function(){
				if(tbgReq.readyState == 4){
					if(tbgReq.responseText==""){;
						tbgPrev = "";
						restoreComment(obj);
						var elem = document.getElementById("tbg-anchor" + id);
						if(elem){
							elem.setAttribute("title", value);
						}
						elem = document.getElementById("tbg-thumbnail" + id);
						if(elem){
							elem.setAttribute("alt", value);
							elem.setAttribute("title", value);
						}
						elem = document.getElementById("caption-for-anchor" + id);
						if(elem){
							elem.innerText = value;
						}
					}else if(tbgReq.responseText=="0"){
						tbgPrev = "";
						restoreComment(obj);
					}else{
						restoreComment(obj);
						window.alert(tbgReq.responseText);
					}
				}
			}
		}else{
			window.alert(noajax);
		}
	}else if(tbgKey==27){
		restoreComment(obj);
	}
}
//-------------------------------------------------------------------
// restore description
//-------------------------------------------------------------------
function restoreComment(obj){
	if(tbgPrev!=""){
		obj.value = tbgPrev;
		tbgPrev = "";
	}
	obj.style.backgroundColor = "#FFFFD3";
	obj.style.fontStyle = "italic";
	tbgEdit = false;
	obj.blur();
}
//-------------------------------------------------------------------
// change font and background-color of obj
//-------------------------------------------------------------------
function activateInput(obj){
	if(!tbgEdit){
		obj.style.backgroundColor = "#FFFFFF";
		obj.style.fontStyle = "normal";
		tbgPrev = obj.value;
		tbgEdit = true;
		obj.select();
	}
}
//-------------------------------------------------------------------
// output confirm message
//-------------------------------------------------------------------
function confirming(message){
	if(window.confirm(message)){
		return true;
	}else{
		return false;
	}
}
