var DEBUG = false;

$(document).ready(function(e) {
	// add debugging if needed first
	if (DEBUG) {
		$('body').append('<div id="debug" class="box" ><h3>Debugging:</h3><div id="debugContent"></div></div>');	
		$("body").append('<div id="error" class="box"></div>');
	}

	if ($.browser.msie) {
		dbug('IE!  Ewwwwww....');
		$("#fn").css('padding', '2px');
	}
	
	$('#uform').bind('submit', checkitall);
});

function checkitall() {
	// when they click Upload is when the magic happens
	$('#fn').blur();
	$('#uploadButton').blur();
	
	// verify & submit
	var f = $('#uform');
	
	// disable buttons while this is going on
	disableForm();
	
	// undefined if absolutely nothing
	var fn = ($('#uform input:file').attr('value')) ? $.trim($('#uform input:file').attr('value')) : '';
	
	// check to see if it's empty (nothing / all spaces)
	if (fn === '') {
		// it's empty
		error('Filename is empty.  Please click the "Browse" button and select an image file.');
		enableForm();
		$('#uform input:file').focus();	// focus on the Browse button just to make the point
		return false;
	} else if (!(/(\.png|\.gif|\.jpg)$/i).test(fn)){
		// filename needs to end in .gif, .jpg, or .png
		dbug('File "' + fn + '" is not an image');
		error('File "' + $('#uform input:file').attr('value') + '" is not an image.  This page only accepts images of type GIF, PNG, or JPEG.');
		enableForm();
		return false;
	} else {
		// process image
		return processImage();
	} 	
	// enableForm();
}

function success($imgUrl) {
	dbug('<h1>Success</h1>Image URL is ' + $imgUrl);	
}

function processImage() {
		// looks okay, so treat it like a valid image file
	dbug('File value == ' + $('#uform input:file').attr('value'));
	
	// first, set work area to general-purpose uploading throbber
	throbberSetup();
	enableForm();
	
	// have to (re?)set the filename in the iframe
	
	// now, set up a very basic pinging mechanism to retrieve the image's URL
	// OR if there was an error
	$("#hider").fadeIn("fast");
	
	dbug('submitting form');
	
	// return true; 
	document.forms[0].submit();
}

function throbberSetup() {
	if (!DEBUG) {
		window.parent.throbberSetup();
	}
}

function timedMsg (msg, target) {
	msgCount++;
	var box = target ? $(target) : $('#error');	// assume it's an error 
	
	// add an initially-hidden message div to the box
	var mbox = $('<div class="msg hidden" id="msg' + msgCount + '">' + msg + '</div>');
	box.append(mbox);
	
	// grow & fade in the message
	$("#msg" + this.msgCount).fadeIn("normal");
	
	if (!target) {	// no target means it was an error
		// add a timeout call that, when it goes off, will hide & fade-out the message
		setTimeout(new Function('$("#msg" + ' + msgCount + ').animate({height: "hide", opacity: "hide"}, "slow");')
			//function() {	//$("#msg" + msgCount).animate({height: 'hide', opacity: 'hide'}, 'slow'); }
		, 4000);
	} else if (target == '#debugContent') {
		$("#msg" + msgCount).prepend('<div class="stamp">' + (new Date()).toTimeString() + '</div>');
	}
	
	return this;
}

var msgCount = 0;

function error(msg) {
	// wrapper for timedMsg
	if (DEBUG) {
		timedMsg(msg);
	} else {
		window.parent.error(msg);
	}
	return this;
}

function dbug (msg) {
	if (DEBUG) {
		timedMsg(msg, '#debugContent');
	} else {
		window.parent.dbug(msg);
	}	
	return this;
}

function disableForm() {
	$('#uform input,#uform button').each(function (){
		$(this).attr({disabled: 'disabled'});
	});
	
	return this;
}

function enableForm() {
	$('#uform input,#uform button').each(function (){
		$(this)[0].removeAttribute('disabled');
	});
}	
