/*
 *	Application.js
 *		This is the main application that drives the website. It also serves to 
 *		keep the global namespace un-cluttered.
 *
 */

if (YAHOO) {
	if (YAHOO.util) {
		if (!YAHOO.util.Dom) {
			throw new Error("YAHOO.util.Dom does not exist!");
		}
		if (!YAHOO.util.Anim) {
			throw new Error("YAHOO.util.Anim does not exist!");
		}
		if (!YAHOO.util.Easing) {
			throw new Error("YAHOO.util.Easing does not exist!");
		}
		if (!YAHOO.util.Connect) {
			throw new Error("YAHOO.util.Connect does not exist!");
		}
	} else {
		throw new Error("YAHOO.util does not exist!");
	}
	if (YAHOO.lang) {
		if (!YAHOO.lang.JSON) {
			throw new Error("YAHOO.lang.JSON does not exist!");
		}
	} else {
		throw new Error("YAHOO.lang does not exist!");
	}
} else {
	throw new Error("YAHOO does not exist!");
}
 
Application = function () {
	this.failures = 0;
/*	
	this.small_window = function (target) {
		var small_win = window.open(target, "SmallWin", "toolbar=no,directories=no,status=no,scrollbars=no,menubar=no,width=430,height=207");
		small_win.window.focus();
	};
*/
	this.on_dom_ready = function () {
		images["current"]["home"].onmouseover = function () {
			images["current"]["home"].src = images["mouse_over"]["home"].src;
		};
		images["current"]["home"].onmouseout = function () {
			images["current"]["home"].src = images["mouse_out"]["home"].src;
		};
		images["current"]["corporate"].onmouseover = function () {
			images["current"]["corporate"].src = images["mouse_over"]["corporate"].src;
		};
		images["current"]["corporate"].onmouseout = function () {
			images["current"]["corporate"].src = images["mouse_out"]["corporate"].src;
		};
		images["current"]["promo"].onmouseover = function () {
			images["current"]["promo"].src = images["mouse_over"]["promo"].src;
		};
		images["current"]["promo"].onmouseout = function () {
			images["current"]["promo"].src = images["mouse_out"]["promo"].src;
		};
		images["current"]["narration"].onmouseover = function () {
			images["current"]["narration"].src = images["mouse_over"]["narration"].src;
		};
		images["current"]["narration"].onmouseout = function () {
			images["current"]["narration"].src = images["mouse_out"]["narration"].src;
		};
		images["current"]["trailers"].onmouseover = function () {
			images["current"]["trailers"].src = images["mouse_over"]["trailers"].src;
		};
		images["current"]["trailers"].onmouseout = function () {
			images["current"]["trailers"].src = images["mouse_out"]["trailers"].src;
		};
	};

	this.grow = function (id, size, easing) {
		var element = document.getElementById(id);
		if (element) {
			if (element.anim) {
				element.anim.stop(true);
				element.anim = null;
			}
			element.anim = new YAHOO.util.Anim(element, {height: {to: size}}, 0.5, easing); 
			element.anim.animate();
		}
	};
	
	this.move = function (id, y, easing) {
		var element = document.getElementById(id);
		if (element) {
			if (element.anim) {
				element.anim.stop(true);
				element.anim = null;
			}
			element.anim = new YAHOO.util.Anim(element, {top: {to: y}}, 0.5, easing); 
			element.anim.animate();
		}
	};
	
	this.show_login = function () {
		var box = document.getElementById("transfer_box");
		this.grow("main", 665);
		this.grow("main_bg", 665);
		this.grow("trim_left", 544);
		this.grow("trim_right", 544);
		this.grow("transfer_box", 75);		// +50
		this.move("main_text_02", 365);
		this.move("wma_logo", 395);
		this.move("wma", 470);
		this.move("abrams_logo", 395);
		this.move("abrams", 470);
		this.move("main_text_03", 615);
		this.move("trim_bottom", 665);
		box.anim.onComplete.subscribe(function (b, this_obj) { 
			return function () {
				b.innerHTML = "<p>&nbsp;</p>";
				var form = document.createElement("form");
				form.setAttribute("id", "login");
				var el = document.createElement("input");
				el = document.createElement("label");
				el.innerHTML = "User login: ";
				form.appendChild(el);
				var user = document.createElement("input");
				user.setAttribute("type", "text");
				user.setAttribute("name", "user");
				form.appendChild(user);
				el = document.createElement("br");
				form.appendChild(el);
				el = document.createElement("label");
				el.innerHTML = "Password: ";
				form.appendChild(el);
				var pass = document.createElement("input");
				pass.setAttribute("type", "password");
				pass.setAttribute("name", "password");
				form.appendChild(pass);
				el = document.createElement("br");
				form.appendChild(el);
				var login = document.createElement("a");
				login.setAttribute("href", "#");
				login.innerHTML = "ENTER";
				form.appendChild(login);
				b.appendChild(form);
				YAHOO.util.Event.on(login, "click", (function () {
					return function () { 
						YAHOO.util.Connect.setForm(form);
						YAHOO.util.Connect.asyncRequest('POST','login.php', this_obj.login);
					};
				}()));
			}; 
		}(box, this));
		return false;
	};

	this.hide_login = function () {
		this.grow("main", 650);
		this.grow("main_bg", 650);
		this.grow("trim_left", 529);
		this.grow("trim_right", 529);
		this.grow("transfer_box", 60);
		this.move("main_text_02", 350);
		this.move("wma_logo", 380);
		this.move("wma",455);
		this.move("abrams_logo", 380);
		this.move("abrams", 455);
		this.move("main_text_03", 600);
		this.move("trim_bottom", 650);
	}

	this.login = {
		success: function (o) { this.handle_login(o); },
		failure: function(o) { },
		timeout: 5000,
		scope: this
	};
	
	this.handle_login = function (o) {
		var box = document.getElementById("transfer_box");
		var response = YAHOO.lang.JSON.parse(o.responseText);
		switch (response.status) {
			case "admin":
				this.show_admin();
				break;
			case "guest":
				this.show_guest();
				break;
			default:
				this.failures++;
				alert(response.message);
				if (this.failures >= 3) {
					box.innerHTML = "<p>&nbsp</p><p>Sorry, you have too many failed login attempts.</p>";
					this.hide_login();
				}
				break;
		}
	};
	
	this.show_admin = function () {
		var box = document.getElementById("transfer_box");
		box.innerHTML = "Welcome, Tito.<br />";
		this.grow("main", 825);
		this.grow("main_bg", 825);
		this.grow("trim_left", 704);
		this.grow("trim_right", 704);
		this.grow("transfer_box", 240);	// + 215
		this.move("main_text_02", 525);
		this.move("wma_logo", 555);
		this.move("wma", 630);
		this.move("abrams_logo", 555);
		this.move("abrams", 630);
		this.move("main_text_03", 775);
		this.move("trim_bottom", 825);
		YAHOO.util.Connect.asyncRequest('POST','list_files.php', this.get_admin_file_list);
	};
	
	this.show_guest = function () {
		var box = document.getElementById("transfer_box");
		box.innerHTML = "Welcome, guest.<br />";
		this.grow("main", 825);
		this.grow("main_bg", 825);
		this.grow("trim_left", 704);
		this.grow("trim_right", 704);
		this.grow("transfer_box", 240);	// + 215
		this.move("main_text_02", 525);
		this.move("wma_logo", 555);
		this.move("wma", 630);
		this.move("abrams_logo", 555);
		this.move("abrams", 630);
		this.move("main_text_03", 775);
		this.move("trim_bottom", 825);
		YAHOO.util.Connect.asyncRequest('POST','list_files.php', this.get_file_list);
	};
	
	this.get_file_list = {
		success: function (o) { this.show_files(o); },
		failure: function(o) { },
		timeout: 5000,
		scope: this
	};
	
	this.show_files = function (o) {
		var box = document.getElementById("transfer_box");
		var new_div = document.getElementById("download_box");
		if (new_div) {
			new_div.innerHTML = "";
		} else {
			new_div = document.createElement("div");
			new_div.setAttribute("id", "download_box");
			box.appendChild(new_div);
		}
		var response = YAHOO.lang.JSON.parse(o.responseText);
		var length = response.data.length;
		var i;
		for (i = 0; i < length; i++) {
			new_div.innerHTML += response.data[i].file_name;
			var el = document.createElement("a");
			el.setAttribute("href", "javascript: app.small_window('download.php?file=" + response.data[i].file_name + "');");
			el.setAttribute("class", "download_guest");
			el.innerHTML = "Download";
			new_div.appendChild(el);
			el = document.createElement("br");
			new_div.appendChild(el);
		}
		new_div = document.getElementById("upload_box");
		if (new_div) {
			new_div.innerHTML = "";
		} else {
			new_div = document.createElement("div");
			new_div.setAttribute("id", "upload_box");
			box.appendChild(new_div);
		}
		var form = document.createElement("form");
		form.setAttribute("id", "upload");
		var el = document.createElement("input");
		el = document.createElement("label");
		el.innerHTML = "To Upload file";
		el.setAttribute("class", "bold");
		form.appendChild(el);
		el = document.createElement("br");
		form.appendChild(el);
		el = document.createElement("hr");
		el.setAttribute("style", "margin-left: -10px; height: 1px;");
		el.setAttribute("noshade", "noshade");
		form.appendChild(el);
		el = document.createElement("input");
		el.setAttribute("type", "file");
		el.setAttribute("name", "file");
		el.setAttribute("size", "35");
		form.appendChild(el);
		var upload = document.createElement("span");
		upload.setAttribute("class", "upload");
		upload.innerHTML = "Upload";
		form.appendChild(upload);
		new_div.appendChild(form);
		YAHOO.util.Event.on(upload, "click", (function (this_obj) {
			return function () { 
				YAHOO.util.Connect.setForm(form, true);
				YAHOO.util.Connect.asyncRequest('POST','upload.php', this_obj.upload);
			};
		}(this)));	};

	this.upload = {
		upload: function (o) { this.show_files(o); },
		scope: this
	};

	this.get_admin_file_list = {
		success: function (o) { this.show_admin_files(o); },
		failure: function(o) { },
		timeout: 5000,
		scope: this
	};
	
	this.do_delete = function (file) {
		YAHOO.util.Connect.asyncRequest('POST','delete.php', this.get_admin_file_list, "file=" + file);
		return false;
	};
	
	this.show_admin_files = function (o) {
		var box = document.getElementById("transfer_box");
		var new_div = document.getElementById("download_box");
		if (new_div) {
			YAHOO.util.Event.purgeElement(new_div, true)
			new_div.innerHTML = "";
		} else {
			new_div = document.createElement("div");
			new_div.setAttribute("id", "download_box");
			box.appendChild(new_div);
		}
		var response = YAHOO.lang.JSON.parse(o.responseText);
		var length = response.data.length;
		var i;
		var el;
		for (i = 0; i < length; i++) {
			new_div.innerHTML += response.data[i].file_name;
			el = document.createElement("a");
			el.setAttribute("href", "javascript: app.small_window('download.php?file=" + response.data[i].file_name + "');");
			el.setAttribute("class", "download_admin");
			el.innerHTML = "Download";
			new_div.appendChild(el);
			el = document.createElement("a");
			el.setAttribute("href", "#");
			el.setAttribute("onclick", "app.do_delete('" + response.data[i].file_name +"');");
			el.setAttribute("class", "delete");
			el.innerHTML = "Delete";
			new_div.appendChild(el);
			el = document.createElement("br");
			new_div.appendChild(el);
		}
		new_div = document.getElementById("upload_box");
		if (new_div) {
			new_div.innerHTML = "";
		} else {
			new_div = document.createElement("div");
			new_div.setAttribute("id", "upload_box");
			box.appendChild(new_div);
		}
		var form = document.createElement("form");
		form.setAttribute("id", "upload");
		el = document.createElement("input");
		el = document.createElement("label");
		el.setAttribute("class", "bold");
		el.innerHTML = "To Upload file";
		form.appendChild(el);
		el = document.createElement("br");
		form.appendChild(el);
		el = document.createElement("hr");
		el.setAttribute("style", "margin-left: -10px; height: 1px;");
		el.setAttribute("noshade", "noshade");
		form.appendChild(el);
		el = document.createElement("input");
		el.setAttribute("type", "file");
		el.setAttribute("name", "file");
		el.setAttribute("size", "35");
		el.setAttribute("class", "upload_input");
		form.appendChild(el);
		var upload = document.createElement("span");
		upload.setAttribute("class", "upload");
		upload.innerHTML = "Upload";
		form.appendChild(upload);
		new_div.appendChild(form);
		YAHOO.util.Event.on(upload, "click", (function (this_obj) {
			return function () { 
				YAHOO.util.Connect.setForm(form, true);
				YAHOO.util.Connect.asyncRequest('POST','upload.php', this_obj.admin_upload);
			};
		}(this)));
	};

	this.admin_upload = {
		upload: function (o) { this.show_admin_files(o); },
		scope: this
	};
	
	this.small_window = function (target) {
		var small_win = window.open(target, "SmallWin", "toolbar=no,directories=no,status=no,scrollbars=no,menubar=no,width=200,height=200");
		small_win.window.focus();
	};};

var app = new Application();
var images = [];

(function () {
	YAHOO.util.Event.onDOMReady(function () {
		app.on_dom_ready();
	});
	images["current"] = [];
	images["current"]["home"] = document.getElementById("home");
	images["current"]["corporate"] = document.getElementById("corporate");
	images["current"]["promo"] = document.getElementById("promo");
	images["current"]["narration"] = document.getElementById("narration");
	images["current"]["trailers"] = document.getElementById("trailers");
	images["mouse_over"] = [];
	images["mouse_over"]["home"] = new Image();
	images["mouse_over"]["home"].src = "images/buttons/home.png";
	images["mouse_over"]["corporate"] = new Image();
	images["mouse_over"]["corporate"].src = "images/buttons/corporate.png";
	images["mouse_over"]["promo"] = new Image();
	images["mouse_over"]["promo"].src = "images/buttons/promo.png";
	images["mouse_over"]["narration"] = new Image();
	images["mouse_over"]["narration"].src = "images/buttons/narration.png";
	images["mouse_over"]["trailers"] = new Image();
	images["mouse_over"]["trailers"].src = "images/buttons/trailers.png";
	images["mouse_out"] = [];
	images["mouse_out"]["home"] = new Image();
	images["mouse_out"]["home"].src = "images/buttons/home2.png";
	images["mouse_out"]["corporate"] = new Image();
	images["mouse_out"]["corporate"].src = "images/buttons/corporate2.png";
	images["mouse_out"]["promo"] = new Image();
	images["mouse_out"]["promo"].src = "images/buttons/promo2.png";
	images["mouse_out"]["narration"] = new Image();
	images["mouse_out"]["narration"].src = "images/buttons/narration2.png";
	images["mouse_out"]["trailers"] = new Image();
	images["mouse_out"]["trailers"].src = "images/buttons/trailers2.png";
})();

