// JavaScript Document

var lastthumb = "thumb0";

function openLarge(url) {
	popup = window.open(url,'detail','width=800, height=585, menubar=no, location=no, status=no');
	if (window.focus) {popup.focus()}
	
}

// AJAX functions

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sndReq(path) {
    http.open('get', path);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('][');
            document.getElementById(update[0]).innerHTML = update[1];
        }
    }
}

function adminPopup(path) {
	sndReq(path);
	var gray = document.getElementById('gray');
	gray.style.visibility = 'visible';
	
	Slide('admin').down();
}


	

function cancel() {
	Slide('admin').up();
}

// Slide functions

var slideInUse = new Array();

function Slide(objId, options) {
	this.obj = document.getElementById(objId);
	this.duration = 1;
	this.height = parseInt(this.obj.clientHeight);

	if(typeof options != 'undefined') { this.options = options; } else { this.options = {}; }
	if(this.options.duration) { this.duration = this.options.duration; }
		
	this.up = function() {
		this.curHeight = this.height;
		this.newHeight = '1';
		if(slideInUse[objId] != true) {
			var finishTime = this.slide();
			window.setTimeout("Slide('"+objId+"').finishup("+this.height+");",finishTime);
		}
	}
	
	this.down = function() {
		this.newHeight = this.height;
		this.curHeight = '1';
		if(slideInUse[objId] != true) {
			this.obj.style.height = '1px';
			this.obj.style.visibility = 'visible';
			this.obj.style.display = 'block';
			this.slide();
		}
	}
	
	this.slide = function() {
		slideInUse[objId] = true;
		var frames = 30 * duration; // Running at 30 fps

		var tIncrement = (duration*1000) / frames;
		tIncrement = Math.round(tIncrement);
		var sIncrement = (this.curHeight-this.newHeight) / frames;

		var frameSizes = new Array();
		for(var i=0; i < frames; i++) {
			if(i < frames/2) {
				frameSizes[i] = (sIncrement * (i/frames))*4;
			} else {
				frameSizes[i] = (sIncrement * (1-(i/frames)))*4;
			}
		}
		
		for(var i=0; i < frames; i++) {
			this.curHeight = this.curHeight - frameSizes[i];
			window.setTimeout("document.getElementById('"+objId+"').style.height='"+Math.round(this.curHeight)+"px';",tIncrement * i);
		}
		
		window.setTimeout("delete(slideInUse['"+objId+"']);",tIncrement * i);
		
		if(this.options.onComplete) {
			window.setTimeout(this.options.onComplete, tIncrement * (i-2));
		}
		
		return tIncrement * i;
	}
	
	this.finishup = function(height) {
		this.obj.style.visibility = 'hidden';
		this.obj.style.height = height + 'px';
		var gray = document.getElementById('gray')
		gray.style.visibility = 'hidden';
		this.obj.innerHTML = '<h2><img src="../spinner.gif" /></h2>';
	}
	
	return this;
}

// FORM VALIDATION FUNCTIONS

var W3CDOM = (document.getElementsByTagName && document.createElement);

/*window.onload = function () {
	document.forms[0].onsubmit = function () {
		return validate();
	}
}*/

function toggleForm(state){
  var elems = document.forms[0].elements;
  for(i = 0; i < elems.length; i++){
    elems[i].disabled = state;
  }
}

function validate() {
	validForm = true;
	firstError = null;
	errorstring = '';
	var fields = document.forms[0].elements;
	for (var i=0; i < fields.length; i++) {
		if (fields[i].value.length == 0)
			writeError(fields[i],'*required');
	}
	if (!W3CDOM)
		alert(errorstring);
	if (firstError)
		firstError.focus();
	if (validForm)
		spinner();
	return validForm;
}

function writeError(obj,message) {
	validForm = false;
	if (obj.hasError) return;
	if (W3CDOM) {
		obj.className += ' error';
		obj.onchange = removeError;
		var sp = document.createElement('span');
		sp.className = 'error';
		sp.appendChild(document.createTextNode(message));
		obj.parentNode.appendChild(sp);
		obj.hasError = sp;
	}
	else {
		errorstring += obj.name + ': ' + message + '\n';
		obj.hasError = true;
	}
	if (!firstError)
		firstError = obj;
}

function removeError()
{
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.hasError);
	this.hasError = null;
	this.onchange = null;
}

function adminMsg(msg) {
	document.getElementById('msg').innerHTML = msg;
}

function uploadComplete(msg) {
	adminMsg(msg);
	Slide('admin').up();
	setTimeout("location.reload(true)", 1000);
}

function spinner() {
	document.getElementById('msg').innerHTML = '<img src="../spinner.gif" />';
}

function date_onchange() {
	var y = document.forms[0].date_y.value;
	var m = document.forms[0].date_m.value;
	var d = document.forms[0].date_d.value;
	
	document.forms[0].date.value = y+'-'+m+'-'+d;
}