
/*******************************************************************************
Div height functions
*******************************************************************************/

function getScrollHeight()
{
	var top = 0;
	if (document.documentElement.scrollTop) //ie
		top = document.documentElement.scrollTop;
	else if (window.pageYOffset) //ns
		top = window.pageYOffset;

	return top;
}

function getWindowHeight()
{
	var height = getDivHeight('container');
	var h = 0;

	if (window.innerHeight) //ns
		h = window.innerHeight;
	else if (document.body.clientHeight) //ie
		h = document.body.clientHeight;

	if (h > height)
		height = h;

	return height;
}

function getWindowWidth()
{
	if (window.innerWidth) //ns
		return window.innerWidth;
	else if (document.body.clientWidth)
		return document.body.clientWidth;
	else
		return 0;
}

function getDivHeight(div)
{
	var d = $(div);
	var divHeight = 0;
	
	if(d.offsetHeight)
		divHeight = d.offsetHeight;
	else if(d.style.pixelHeight)
		divHeight = d.style.pixelHeight;

	return divHeight;
}

function fixContentHeight()
{
	/*
                       |	
	 ___              _v_
	|   |__________   ___ diff
	| l |          |   ^
	| e |  right   |   |
	| f |__________|
	| t |  footer  |
	|___|__________|
	
	*/
	
	var diff = 26;
	var left = getDivHeight('sidebar');
	var right = getDivHeight('content');
	
	if (left < (right + diff))
		left = right + diff;
	else if (left > (right + diff))
		right = left - diff;
	
	$('sidebar').style.height = left + "px";
	$('content').style.height = right + "px";
}

/*******************************************************************************
Quick links
*******************************************************************************/

function quickLinks(href)
{
	if (href != "#" && href != "")
		window.location = href;	
}

/******************************************************************************/
/* Used to highlight and select rows in a data table						  */
/******************************************************************************/

var selectedRow = -1;

function toggleHighlight(i)
{
	if (selectedRow == i)
		return;
	
	var row = document.getElementById('row' + i);
	
	if (row.className == 'selected')
		return;
	
	if (row.className == 'over')
		row.className = '';
	else
		row.className = 'over';
}

function toggleSelect(i)
{		
	//alert("i = " + i);
	var row = document.getElementById('row' + i);
		
	if (row.className == 'selected')
		row.className = '';
	else
		row.className = 'selected';
	
	// De-select the last selected row
	if (selectedRow > -1)
		document.getElementById('row' + selectedRow).className = '';
	selectedRow = i;
}

function checkSelectedRow(location)
{
	if (selectedRow == -1)
	{
		alert("Please select a row.\n\nTo select a row, click on it.");
		return;
	}
	
	window.location = location;
}

/*******************************************************************************
Redirect Urls
*******************************************************************************/

function setServerVar(name, value, messageContainer)
{
	if (!messageContainer)
		messageContainer = 'message';
	
	$(messageContainer).innerHTML = "<span class='loading_small'>Loading...</span>";
	var params = new Hash();
	params.set('name', name);
	params.set('value', value);
	
	new Ajax.Request('/page/set-var', {
		method: 'post',
		parameters: params,
		onSuccess: function(transport) {
			var resp = transport.responseJSON;
			if (resp.reload && resp.reload == true)
				window.location.reload();
			else if (resp.text.length > 0)
				$(messageContainer).innerHTML = resp.text;
		},
		onFailure: function() {
			$(messageContainer).innerHTML = "<p class='note error'>Error</p>";
		},
		sanitizeJSON: false
	});
}

function fixRelativeLinks()
{
	// Get the current url
	var base = location.pathname;
	if (base.substr(base.length-1, 1) == '/')
		base = base.substr(0, base.length-1);
	
	var links = document.getElementsByTagName('a');

	for (var i=0; i<links.length; i++)
	{
		var link = links[i];
		var href = link.getAttribute('href', 2);

		if (!href || href == null) 									continue;
		if (href == '/') 											continue;
		if (href.length > 1  && href.substr(0,1)  == '/')			continue;
		if (href.length > 4  && href.substr(0,4)  == 'http')		continue;
		if (href.length > 7  && href.substr(0,7)  == 'mailto:')		continue;
		if (href.length > 11 && href.substr(0,11) == 'javascript:')	continue;
		links[i].href = base + '/' + href;
	}                              
	
	// Images
	var images = document.getElementsByTagName('img');
	
	for (var i=0; i<images.length; i++)
	{
		var img = images[i];
		var href = img.getAttribute('src', 2);

		if (!href || href == null) continue;
		if (href.length > 1  && href.substr(0,1)  == '/')			continue;
		if (href.length > 4  && href.substr(0,4)  == 'http')		continue;
		images[i].src = base + '/' + href;
	}
}

/*******************************************************************************
Page load
*******************************************************************************/

function initTinymce()
{
	if (!tinyMCE)
		return;
	tinyMCE.init({   
		mode: "textareas",
		editor_selector: "mceEditor",
		width: "100%",
		height: "400px",
		theme: "advanced",
		theme_advanced_buttons1: "bold,italic,strikethrough,underline,formatselect,separator,bullist,numlist,liststyle,outdent,indent,separator,"+
			"justifyleft,justifycenter,justifyright,separator,link,unlink,image,g2image",
		theme_advanced_buttons2: "removeformat,charmap,separator,undo,redo,separator,tablecontrols,separator,fullscreen,code,forecolor,backcolor",
		theme_advanced_buttons3: "",
		language: "en",
		theme_advanced_toolbar_location: "top",
		theme_advanced_toolbar_align: "left",
		theme_advanced_path_location: "bottom",
		theme_advanced_resizing: true,
		browsers: "msie,gecko,opera,safari",
		dialog_type: "modal",
		theme_advanced_resize_horizontal: false,
		convert_urls: false,
		relative_urls: false,
		remove_script_host: false,
		force_p_newlines: true,
		force_br_newlines: false,
		convert_newlines_to_brs: false,
		remove_linebreaks: false,
		fix_list_elements: true,
		gecko_spellcheck: true,
		entities: "38,amp,60,lt,62,gt",
		content_css: "http://cba.ua.edu/themes/cba/css/tinymce.css?" + Math.floor(Math.random()*1000),
		valid_elements: "p/-div[*],-strong/-b[*],-em/-i[*],-font[*],-ul[*],-ol[*],-li[*],*[*]",
		save_callback: '',
		plugins: "inlinepopups,iespell,paste,table,fullscreen,filemanager,imagemanager,advimage,advlink,liststyle,g2image",
		inline_styles: true
	});
}

