$(document).ready(function() {
	$('#dialogbackground').click(function() {
		closedialog();
	});
	
	// Help dialogs
	$('a.help').click(function() {
		$('#dialog').load($(this).attr('href') + ' #ajax_help');
		opendialog('Help', '', 400, 0, 400);
		return false;
	});
	
	// Browser-Update.org
	if ($buoop.ol) $buoop.ol();
	var e = document.createElement("script");
	e.setAttribute("type", "text/javascript");
	e.setAttribute("src", "http://browser-update.org/update.js");
	document.body.appendChild(e);
	
	//$('object embed[allowfullscreen!=true]').attr('allowfullscreen', 'true');
	//$('object param[name=allowfullscreen]').attr('value', 'true');
	//$('object param[name=allowFullScreen]').attr('value', 'true');
	//$('object:not(:has(param[name=allowfullscreen]))').append('<param name="allowfullscreen" value="true" />');
});

// Browser-Update.org
var $buoop = {
	reminder: 168, // after how many hours should the message reappear (0 = show all the time)
	newwindow: true
};
$buoop.ol = window.onload;


// Dialogs
function opendialog(title, content, width, height, maxheight) {
	if (typeof width != 'number') {
		width = 100;
	} else if (width < 100 && width != 0) {
		width = 100;
	}
	if (typeof height != 'number') {
		height = 100;
	} else if (height < 100 && height != 0) {
		height = 100;
	}
	if (width != 0) {
		$('#dialogtitle').css('width', width+'px');
		$('#dialog').css('width', width+'px');
	}
	if (height != 0) {
		$('#dialog').css('height', height+'px');
	} else if (maxheight != null && typeof maxheight == 'number') {
		$('#dialog').css( { maxHeight: '400px',	overflowY: 'scroll' } );
	}
	$('#dialogtitle').html(title);
	if (content != '') {
		$('#dialog').html(content);
	}
	$('#dialogcontainer').show();
}
function closedialog() {
	$('#dialogcontainer').hide();
	$('#dialogtitle').html('G.I.G. Entertainment');
	$('#dialog')
		.html('')
		.css( { maxHeight: '', overflowY: '', height: '' } );
}

function login() {
	$.get(
		'login.ajax.php',
		{},
		function(data, textStatus) {
			opendialog('Log in', data, 156, 0);
			//$('form#login').live('submit', function() {
			//	$.post(
			//		'login.ajax.php', 
			//		{ a: 'login', 'data[User][username]': $('form#login #uid').val(), 'data[User][password]': $('form#login #pwd').val() }, 
			//		function(data, textStatus) {
			//			if (data == 'logged') {
			//				loc = document.location;
			//				if (loc == 'http://www.greatestingenerations.com/login') {
			//					loc = 'http://www.greatestingenerations.com/';
			//				}
			//				document.location.href = loc;
			//			} else {
			//				$('#dialog').html(data);
			//			}
			//		},
			//		"html"
			//	);
			//	return false;
			//});
		},
		"html");
}

function dumpObj(obj, name, indent, depth) {
	if (depth > 2) {
		return indent + name + ": <Maximum Depth Reached>\n";
	}
	
	if (name == 'undefined' || typeof name == 'undefined') {
		name = name;
	}
	if (indent == 'undefined' || typeof name == 'undefined') {
		indent = '';
	}
	
	if (typeof obj == "object") {
		var child = null;
		var output = indent + name + "\n";
		indent += "\t";
		
		for (var item in obj) {
			try {
				child = obj[item];
			} catch (e) {
				child = "<Unable to Evaluate>";
			}
			
			if (typeof child == "object") {
				output += dumpObj(child, item, indent, depth + 1);
			} else {
				output += indent + item + ": " + child + "\n";
			}
		}
		return output;
	} else {
		return obj;
	}
}

function getfiletype(filename) {
	if (filename.lastIndexOf('.') == -1) {
		return 'unknown';
	} else {
		filetype = filename.substr(filename.lastIndexOf('.')+1);
	}
	
	switch(filetype) {
		case 'mp3': case 'mp4': case 'wav': case 'ogg': case 'm4a': case 'aac': case 'midi':
			return 'audio';
			break;
		case 'mpeg': case 'mpg': case 'wmv': case 'mov': case 'avi': case 'divx':
			return 'video';
			break;
		case 'jpeg': case 'jpg': case 'gif': case 'png': case 'bmp': case 'tif': case 'tiff':
			return 'image';
			break;
		case 'swf': case 'flv':
			return 'flash';
			break;
		case 'html': case 'htm':
			return 'web';
			break;
		case 'txt':
			return 'text';
			break;
		case 'sql':
			return 'sql';
			break;
		default:
			return 'unknown';
	}
}

function shorten(input, maxlength) {
	if (typeof input == 'string') {
		if (input.length > 30) {
			return input.substr(0, 27) + '...';
		} else {
			return input;
		}
	} else {
		return input;
	}
}

function previewFile(filename, filesize) {
	filetype = getfiletype(filename);
	if (filetype == 'audio') {
		opendialog(
			shorten(filename, 30) + ' - Preview',
			"<iframe src=\"http://www.greatestingenerations.com/data/downloads/" + filename + "\" style=\"border: 0px; width: 300px; height: 50px;\"></iframe>",
			300, // width
			0); // height (0 = auto)
	} else if (filetype == 'video') {
		opendialog(
			shorten(filename, 30) + ' - Preview',
			"<iframe src=\"http://www.greatestingenerations.com/data/downloads/" + filename + "\" style=\"border: 0px; width: 400px; height: 267px;\"></iframe>",
			400, // width
			267); // height (0 = auto)
	} else if (filetype == 'image') {
		opendialog(
			shorten(filename, 30) + ' - Preview',
			"<img src=\"/data/downloads/" + filename + "\" style=\"border: 0px; width: 400px;\" />",
			400, // width
			0); // height (0 = auto)
	} else if (filetype == 'text' || filetype == 'sql') {
		opendialog(
			shorten(filename, 30) + ' - Preview',
			"<iframe src=\"/showfilecontents.php?f=/data/downloads/" + filename + "\" style=\"border: 0px; width: 800px; height: 500px;\"></iframe>",
			800, // width
			500); // height (0 = auto)
	} else if (filetype == 'web') {
		opendialog(
			shorten(filename, 30) + ' - Preview',
			"<iframe src=\"/data/downloads/" + filename + "\" style=\"border: 0px; width: 800px; height: 500px;\"></iframe>",
			800, // width
			500); // height (0 = auto)
	} else if (typeof filesize == 'number') {
		if (filesize < 10000) {
			opendialog(
				shorten(filename, 30) + ' - Preview',
				"<iframe src=\"/showfilecontents.php?f=/data/downloads/" + filename + "\" style=\"border: 0px; width: 800px; height: 500px;\"></iframe>",
				800, // width
				500); // height (0 = auto)
		} else {
			opendialog(
				shorten(filename, 30) + ' - Preview',
				"<iframe src=\"http://www.greatestingenerations.com/data/downloads/" + filename + "\" style=\"border: 0px; width: 800px; height: 500px;\"></iframe>",
				800, // width
				500); // height (0 = auto)
		}
	} else {
		opendialog(
			shorten(filename, 30) + ' - Preview',
			"<iframe src=\"http://www.greatestingenerations.com/data/downloads/" + filename + "\" style=\"border: 0px; width: 800px; height: 500px;\"></iframe>",
			800, // width
			500); // height (0 = auto)
	}
}
