/**
 * spool-jquery.js
 * Matt Farmer for Boxkite Media
 *
 * Designed for distribution with Spool 1.8
 * Include this file after jQuery.
 * DO NOT EDIT THIS FILE TO INCLUDE CLIENT SPECIFIC FUNCTIONALITY.
 */

/* SPOOL OBJECT THAT WILL HOLD HELPFUL THINGS */
var Spool = {
	/**
	 * Show a top navigation submenu.
	**/
	showTab: function(elemid) {
		var elem = document.getElementById(elemid);
		elem.style.display = "";
	},

	/**
	 * Hide a top navigation submenu.
	**/
	hideTab: function(elemid) {
		var elem = document.getElementById(elemid);
		elem.style.display = "none";
	},

	/**
	 * Parameter for keeping track of the current section open
	 * in the side navigation.
	**/
	curSection: null,

	/**
	 * Slide open a side navigation section.
	**/
	toggleSelection: function(id) {
		if(Spool.curSection == id && $('#modules-' + curSection).first().style.display == "") {
			return;
		}

		if(Spool.curSection && Spool.curSection != id) {
			//Effect.SlideUp('modules-' + curSection, { duration: .2 });
			$('#modules-' + Spool.curSection).first().slideUp();
		}

		Spool.curSection = id;
		$('#modules-' + Spool.curSection).first().slideDown();
	},

	/**
	 * Clone some HTML and put it somewhere.
	**/
	cloneObject: function(targetId, locationId) {
		var toClone = $('#' + targetId).first();
		var newDiv = document.createElement(toClone[0].nodeName);

		//Copy the inner HTML
		newDiv.innerHTML = toClone[0].innerHTML;

		//Add the object to the target area.
		$('#' + locationId).append(newDiv);
	},

	/**
	 * Make an AJAX call (usually to a delete), then on success fade out some
	 * object.
	**/
	confirmedAjaxDelete: function(url, objectId) {
		if(confirm("Are you sure?")) {
			$.ajax({
				url: url,
				success: function(data) {
					$("#" + objectId).fadeOut();
				},
				error: function(data, status) {
					alert("An error occured: " + status);
				},
			});
		}
	},

	/*
		Use a confirm dialog to warn the user of a destructive action
	*/
	confirmNav: function(url, message) {
		if(confirm(message || "Are you sure?")) {
			if(url != "" && url != "undefined" && url != null) {
				window.location = url;
			}
			else {
				return true;
			}
		}
	},

	/**
	 * Do an AJAX popup.
	**/
	popup: function(targetDiv, file, callBack) {
		$.ajax({
			url: file,
			dataType: 'html',
			success: function(data) {
				//Set the opaque.
				Spool.setOpac();
				var scrollX = window.pageXOffset;
				var scrollY = window.pageYOffset;

				var element = document.createElement('div');
				element.setAttribute("id", targetDiv);
				element.innerHTML = data;

				WIDTH = "100%";
				element.style.width = WIDTH;
				element.style.position = "absolute";
				element.style.left = (scrollX) + "px";
				element.style.top = (scrollY + 100) + "px";
				element.style.zIndex = 1000;
				element.style.margin = "0 auto";

				document.body.appendChild(element);

				if(callBack) {
					callBack();
				}
			},
			error: function(data, status) {
				alert("There was an error retrieving that content: " + status);
			}
		});
	},

	/**
	 * This is like Spool.popup() except it pulls its HTML from the text
	 * parameter instead of from an AJAX call.
	**/
	popupText: function(targetDiv, text) {
		Spool.setOpac();
		var scrollX = window.pageXOffset;
		var scrollY = window.pageYOffset;

		var element = document.createElement('div');
		element.setAttribute("id", targetDiv);
		element.innerHTML = text;

		WIDTH = "100%";
		element.style.width = WIDTH;
		element.style.position = "absolute";
		element.style.left = (scrollX) + "px";
		element.style.top = (scrollY + 100) + "px";
		element.style.zIndex = 1000;
		element.style.margin = "0 auto";

		document.body.appendChild(element);
	},

	/**
	 * Close a popup with the specified DIV name.
	**/
	closePopup: function(divName) {
		$('#' + divName).remove();
		Spool.restoreOpac();
	},

	/**
	 * Param for tracking the current semi-transparent layer for popups.
	**/
	opacDiv: null,

	/**
	 * Set a semi-transparent layer on top of the page content.
	**/
	setOpac: function() {
		var scrollX = window.pageXOffset;
		var scrollY = window.pageYOffset;

		opacDiv = document.createElement('div');
		opacDiv.id = "opacDiv";
		opacDiv.style.position = "absolute";
		opacDiv.style.left = "0px";
		opacDiv.style.top = "0px";
		opacDiv.style.width = document.body.clientWidth + scrollX + "px";
		opacDiv.style.height = document.body.clientHeight + scrollY + "px";
		opacDiv.style.backgroundColor = "#000000";
		opacDiv.style.zIndex = "999";
		opacDiv.style.opacity = 0;
		opacDiv.style.mozOpacity = 0;
		document.body.appendChild(opacDiv);

		$("#opacDiv").animate({opacity:0.5}, 500);

		window.onresize = function() {
			opacDiv.style.width = document.body.clientWidth + window.pageXOffset + "px";
			opacDiv.style.height = document.body.clientHeight + window.pageYOffset + "px";
		};
	},

	/**
	 * Remove the semi-transparent layer on the top of content.
	**/
	restoreOpac: function() {
		$('#opacDiv').animate({opacity: 0.0}, 500, function() {
			opacDiv.parentNode.removeChild(opacDiv);
			window.onresize = null;
		});
	}
};

/* GENERAL SETUP FUNCTIONS */
$(document).ready(function () {
	//Init tinyMCE.
	tinyMCE.init({
		extended_valid_elements: 'style,a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name]',
		relative_urls : false,
		remove_script_host : false,
		mode : "specific_textareas",
		editor_selector : "mce-editor",
		theme : "advanced",
		plugins : "spool,safari,pagebreak,style,layer,iespell,media,paste",
		theme_advanced_buttons1 : "spool,|,code,|,copy,paste,pastetext,pasteword,|,bold,italic,underline,|,charmap,|,justifyleft,justifycenter,justifyright, justifyfull,|,bullist,numlist,|,image,|,link,unlink",
		theme_advanced_buttons2 : "formatselect,|,removeformat,cleanup",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		paste_retain_style_properties: 'none'
	});

	//Init top navigation
	$('#topmenu-users').mouseover(function() {
		Spool.showTab('tab-users');
	});

	$('#topmenu-users').mouseout(function() {
		Spool.hideTab('tab-users');
	});

	$('#topmenu-sites').mouseover(function() {
		Spool.showTab('tab-sites');
	});

	$('#topmenu-sites').mouseout(function() {
		Spool.hideTab('tab-sites');
	});

	//Init side navigation
	$('.section-navigation a.section-link').click(function(obj) {
		var parts = obj.target.id.split("-");
		Spool.toggleSelection(parts[1]);
		return false;
	});

	//Init AJAX search box.
	//$('#searchbox #q').autocomplete({
	//	source: 'http://spooldev.boxkite.local/spool/search',
	//});

	//Init drag-and-drop tables
	$('.tablednd').tableDnD({
		onDrop: function(table, row) {
			rows = table.tBodies[0].rows;

			var txtNewRows = "";
			for (var i=0; i<rows.length; i++) {
				if(!isNaN(rows[i].id)) {
					txtNewRows += rows[i].id + "|";
				}
			}

			txtNewRows = txtNewRows.substring(1, txtNewRows.length);
			txtNewRows = txtNewRows.substring(0, txtNewRows.length-1);

			$.ajax({
				url: '/spool/ajax/saveOrder',
				type: 'post',
				data: {
					table: table.id,
					newRows: txtNewRows
				},
				success: function(data) {
				},
			});
		}
	});
});

