//
//
//
//
//
//
//
// Common functions
//
//
//
//
//
//
//

//
//
// Debug framework. Make it `not work` correctly unless we're using FF with firebug.
//
//
if(typeof(console) == 'undefined') var console = {
	log   : function(){},
	debug : function(){},
	info  : function(){},
	warn  : function(){},
	error : function(){},
	assert: function(){},
	dir   : function(){},
	dirxml: function(){},
	trace : function(){},
	group : function(){},
	groupEnd: function(){},
	time  : function(){},
	timeEnd : function(){},
	profile : function(){},
	profileEnd : function(){},
	count   : function(){}
};







Element.addMethods({
	show_loader: function(elm){
		var overlay = new Element('div', {
			'id'   : elm.identify()+'-loader',
			'class': 'loader'
		});
		var size = elm.getDimensions();
		var pos  = elm.cumulativeOffset();

		overlay.setStyle({
			background: '#f0f0f0',
			textAlign : 'center',
			position  : 'absolute',
			width     : size.width+'px',
			height    : size.height+'px',
			top       : pos.top+'px',
			left      : pos.left+'px'
		});
		overlay.update(
			'<table width="100%" height="100%">' +
			  '<tr>' +
			    '<td style="vertical-align: middle; text-align: center;">' +
			      '<img src="/img/loading.gif" />' +
			    '</td>' +
			  '</tr>'+
			'</table>'
		);
		overlay.setOpacity(0.4);

		document.body.appendChild(overlay);
		return elm;
	},
	hide_loader: function(elm){
		if(elm.id)
		{
			var element =  $(elm.id + '-loader');
			element.parentNode.removeChild(element);
		}
		return elm;
	},

	show_overlay: function(elm){
		document.body.appendChild(
			new Element('div', {
				'id'   : elm.identify()+'-overlay',
				'class': 'overlay'
			}).setStyle({
				background: '#ccc',
				textAlign : 'center',
				position  : 'absolute'
			}).setOpacity(0.8).clonePosition(elm)
		);
		return elm;
	},
	hide_overlay: function(elm){
		if(elm.id)
		{
			var element =  $(elm.id + '-overlay');
			element.parentNode.removeChild(element);
		}
		return elm;
	}
});
















//
////
//
//////	 COMMON.JS
//
////
//
var order_cookie_prefix   = "fa_order_";
var perpage_cookie_prefix = "fa_perpage_";




function checkAll(docform, elementname, selectit, btn)
{
	var docelements = docform.elements;
	var button = $(btn);
	for (n=0;n< docelements.length;n++){
		if (elementname=='' || elementname && docelements.item(n).name == elementname)
		{
			if(selectit)
				docelements.item(n).checked = true;
			else
				docelements.item(n).checked = false;
		}
	}
}

function toggle_div(elm, id)
{
	var div = $(id);

	if(elm.value == "Reply")
	{
		show_div(id);
		elm.value = "Cancel";
		return;
	}

	if(elm.value == "Cancel")
	{
		hide_div(id);
		elm.value = "Reply";
		return;
	}
}

function atoggle(img,id)
{
	var div = $(id);
	var elm = $(img);
	var _BASE = base();

	// if the div is hidden, show it...
	if(div.style.display == 'block')
	{
		div.style.display = '';
		elm.src = _BASE + '/adminstyle/images/plus.gif';
	}
	else
	{
		div.style.display = 'block';
		elm.src = _BASE + '/adminstyle/images/minus.gif';
	}
}

function show_div(id)
{
	var div = $(id);
	div.style.display = 'block';
}

function hide_div(id)
{
	var div = $(id);
	div.style.display = '';
}

function setcolor(id,newcolor)
{
	id.style.backgroundColor = newcolor;
}

function budsList(user,uniqueid,mode)
{
	////  Modes
	//
	//  'watched_by'
	//  'watches'
	//  'mutual'

	// the size of the popup window
	var width = 500;
	var height = 400;

	// the x,y position of the popup window
	// NOTE: this formula will auto-center the popup on the screen
	var y = (screen.height - height) / 2;
	var x = (screen.width - width) / 2;

	var url = base() + "/budslist/?name=" + user + "&id=" + uniqueid + "&mode=" + mode;
	var options = "width=" + width + ",height=" + height + ",top=" + y + ",left=" + x + ",resizable,scrollbars=1";

	// open the chat window as a popup, instead of embedded in webpage
	window.open( url, "buddylist", options );
}

function showcount(textarea,textbox,maxcount)
{
	var JSTextArea = $(textarea);
	var JSTextBox = $(textbox);
	var curCount;
	var tmp;
	var finalcnt;

	curCount = JSTextArea.value.length;
	tmp = maxcount - curCount;

	if(tmp >= 0) finalcnt = tmp;
	else
	{
		//if they type in too much, crop it off automatically...
		JSTextArea.value = JSTextArea.value.slice(0, maxcount);
		alert('This text box only allows for ' + maxcount + ' characters');
	}

	JSTextBox.value = tmp;
}

function showConfirm(message,url)
{
	if(confirm(message))
	{
		document.location = url;
	}
}

function swap(elm, NewImage)
{
	elm.src = NewImage;
}

function addtag(promptText,tag)
{
	var e;
	var field;

	e = prompt(promptText);
	if(e != null && e != '')
	{
		field = $('JSTag');
		field.value = field.value + "*"+tag+"('"+e+"');";
	}
}

function riptag(tag)
{
	var field;
	var text;
	var t_arr;
	var t_arr_len;

	field = $('JSTag');
	t_arr = field.value.split("*");
	t_arr_len = t_arr.length-1;

	field.value = "";

	var i=0;
	while(i<t_arr_len) {
		if(t_arr[i] != "") field.value += "*";
		field.value += t_arr[i];
		i++;
	}
}

//
//// YAK
//


function updateCounter()
{
	var max_num;
	var rest;
	var temp_msg;
	max_num = 222;

	if (typeof(messageForm) == 'undefined')
	{
		var messageForm;
		messageForm = document.getElementById('JSMessage');
	}

	if (typeof(restCounter) == 'undefined')
	{
		var restCounter;
		restCounter = document.getElementById('chars_left');
	}


	if (messageForm.value != null)
	{
		temp_msg = messageForm.value;
		rest     = max_num - temp_msg.length;
		if (rest < 0 )
		{
			messageForm.value = temp_msg.substring(0, max_num);
			rest = 0;
		}
		restCounter.value = rest;
	}

	return (rest > 0)
}


function toggle(element)
{
	element = $(element);

	if (element.style.display == 'none')
		element.style.display = '';
	else
		element.style.display = 'none';
}





var form_submitted = false;
function submit_form()
{
	if(form_submitted == true)
	{
		return;
	}

	document.myform.mysubmit.value = 'Sending...';
	document.myform.mysubmit.disabled = true;
	form_submitted = true;
	document.myform.submit();
}




function _select_to_cookie(object, cookie_prefix)
{
	object = $(object);

	if (object.tagName.toLowerCase() != 'select')
	{
		return;
	}

	select_value = $F(object).toLowerCase();
	select_name  = object.name.toLowerCase();

	var cookie_name = cookie_prefix + select_name;

	setCookie (cookie_name, select_value, expirymonth);
}


function fa_set_order(object)
{
	_select_to_cookie(object, order_cookie_prefix);
}


function fa_set_perpage(object)
{
	_select_to_cookie(object, perpage_cookie_prefix);
}

 /* Utility functions */


function refresh()
{
	document.location = document.location;
}


////
//
//// FORM ELEMENTS
//
////


////  Removes all options from a given SELECT element, leaving only the first one selected
//
function select_clear(select_id)
{
	var select_element = $(select_id);

	if (select_element == null || select_element.tagName != 'SELECT')
	{
		return false;
	}


	select_element.disable();

	while (select_element.length > 1)
	{
		select_element.options[1] = null;
	}
	select_element.options[0].selected = true;

	select_element.enable();
}


//// Adds the OPTIONs to the supplied SELECT, taken from an array of objects
//
//   Objects in the array are supposed to have a "value" and a "name" property
//
function select_build(select_id, item_array)
{
	var select_element = $(select_id);

	if (select_element == null || select_element.tagName != 'SELECT')
	{
		return false;
	}

	var orig_size = select_element.length;
	var limit	 = item_array.size();
	for (var i=0; i<limit; i++)
	{
		select_element.options[orig_size+i] = new Option(item_array[i].name, item_array[i].value);
	}
}



////  Selects a specific option in a given select. Selection on option value.
//
function select_choose(select_id, value_to_select)
{
	var select_element = $(select_id);

	if (select_element == null || select_element.tagName != 'SELECT')
	{
		return false;
	}

	var optionsLength = select_element.options.length;
	var i;

	for (i=0; i<optionsLength; i++)
	{
		if (select_element.options[i].value == value_to_select)
		{
			select_element.options[i].selected = true;
		}
	}
}











//
////
//
////// TOOLTIP.JS
//
////
//





//
////
//
//////	 COOKIE.JS
//
////
//
var today       = new Date();
var expiryyear  = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);
var expirymonth = new Date(today.getTime() + 30 * 24 * 60 * 60 * 1000);
var expiryday   = new Date(today.getTime() + 24 * 60 * 60 * 1000);

function getCookie(name)
{
	var reg= new RegExp("; "+name+";|; "+name+"=([^;]*)");
	var matches=reg.exec('; '+document.cookie+';');
	if (matches) return ((matches[1])?unescape(matches[1]):'');
	return null;
}

function setCookie (name,value,expires,path,domain,secure)
{
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");

	return getCookie(name)!=null?true:false;
}

function deleteCookie (name,path,domain)
{
	if (getCookie(name)!=null)
	{
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

function cookieEnabled()
{
	testCookieName="_testCookie_";
	if (setCookie(testCookieName,1))
	{
		deleteCookie(testCookieName);
		return true;
	}
	else return false;
}




//
////
//
//////	 BBCODE.JS
//
////
//
function bbtag(text)
{
	var field = $('JSMessage');

	field.value += ' '+text;
}




//
////
//
//////  TEXTAREA control functions
//
////
//
/**
 * Retrieve currently selected text inside a specified textarea
 *
 * @author                      Original script: guys at http://www.mybboard.net/, this modification: yak, yuri.kushinov@gmail.com
 * @param  {DOMNode} textarea   Textarea to operate on
 * @return                      Currently selected text inside the given textarea
 * @type   String
 * @final
 */
function getSelectedText(textarea)
{
	textarea.focus();
	if(document.selection)
	{
		var selection = document.selection;
		var range = selection.createRange();

		if((selection.type == 'Text' || selection.type == 'None') && range != null)
			return range.text;
	}
	else if(textarea.selectionEnd)
	{
		var select_start = textarea.selectionStart;
		var select_end = textarea.selectionEnd;
		if(select_end <= 2)
			select_end = textarea.textLength;

		var start = textarea.value.substring(0, select_start);
		var middle = textarea.value.substring(select_start, select_end);

		return middle;
	}
	return null;
};






/**
 * Insert the given text before and after the selected text is a specified textarea,
 * OR
 * Insert both pre- and post-pended text at the caret position in the specified textarea.
 *
 * @author                       Original script: guys at http://www.mybboard.net/, this modification: yak, yuri.kushinov@gmail.com
 * @param {DOMNode} textarea     Textarea to operate on
 * @param {String}  open_tag     Prepend the selected text with the value of thos variable
 * @param {String}  close_tag    Append the value of this variable to the selected text. Can be FALSE in case of a self-closing tag.
 * @final
 */
function performInsert(textarea, open_tag, close_tag)
{
	if(!close_tag)
		var close_tag = "";

	textarea.focus();

	if(document.selection)
	{
		// IE
		var selection = document.selection;
		var range = selection.createRange();

		if((selection.type == "Text" || selection.type == "None") && range != null)
		{
			if(close_tag != "" && range.text.length > 0)
			{
				range.text = open_tag+range.text+close_tag;
			}
			else
			{
				range.text = open_tag+close_tag+range.text;
			}
			range.select();
		}
		else
		{
			textarea.value += open_tag+close_tag;
		}
	}
	else if(textarea.selectionEnd)
	{
		// Mozilla
		var select_start = textarea.selectionStart;
		var select_end = textarea.selectionEnd;
		var scroll_top = textarea.scrollTop;

		if(select_end <= 2)
		{
			select_end = textarea.textLength;
		}

		var start = textarea.value.substring(0, select_start);
		var middle = textarea.value.substring(select_start, select_end);
		var end = textarea.value.substring(select_end, textarea.textLength);

		if(select_end - select_start > 0 && close_tag != "")
		{
			var keep_selected = true;
			middle = open_tag+middle+close_tag;
		}
		else
		{
			var keep_selected = false;
			middle = open_tag+close_tag+middle;
		}

		textarea.value = start+middle+end;

		if(keep_selected == true)
		{
			textarea.selectionStart = select_start;
			textarea.selectionEnd = select_start + middle.length;
		}
		else
		{
			textarea.selectionStart = select_start + middle.length;
			textarea.selectionEnd = textarea.selectionStart;
		}
		textarea.scrollTop = scroll_top;
	}
	else
	{
		textarea.value += open_tag+close_tag;
	}
}








/**
 *
 *
 *
 *
 *
 * Gallery type pages.
 * Submissiontype icon mouseover and mouseout events that show the description popup
 *
 *
 *
 *
 */
// This is a hack :|
//
var _root_container_element = 'li';
var popup_width      = 400;
var popup_x_offset   = 10;
var popup_y_offset   = -2;
var width_autocorrect_offset = 2;
function submissiontype_icon_mouseover(description_fill_callback, evt)
{
	// IE has a top offset of -2 already
	//
	use_popup_y_offset = popup_y_offset;
	if(Prototype.Browser.IE)
		use_popup_y_offset += 2;

	//
	//
	// Create the popup container and elements
	//
	//
	var tmp = $('description_popup');
	if(!tmp)
	{
		tmp           = document.createElement('div');
		tmp.id        = 'description_popup';
		tmp.className = 'invisible';

		tmp2 = document.createElement('h5');
		tmp.appendChild(tmp2);

		tmp3 = document.createElement('span');
		tmp.appendChild(tmp3);

		document.getElementsByTagName('body')[0].appendChild(tmp);
	}


	var container = evt.findElement(_root_container_element);

	// IE shows the tile's ALT text even when whe hover the overlay icon on top of it.
	// So for IE, we'll remove the ALT text on hover.
	//
	if(Prototype.Browser.IE)
		container.getElementsByTagName('img')[0].alt = '';



	console.group('Showing description for gallery tile %o', container);

	var id = /id_(\d+)/.exec(container.id)[1];

	var desc = eval('descriptions.id_'+id);
	if(desc)
	{
		console.info('Description found, %o. Proceeding...', desc);
		console.info('Calling the description fill callback, %o', description_fill_callback);

		// Filter the description popup's content
		//
		var desc_popup   = $('description_popup');
		var data         = Object.clone(desc);
		data.title       = data.title.escapeHTML().gsub("\n", '');
		data.description = desc.description.escapeHTML().gsub(/(\r?\n){3,}/, "\n\n").gsub("\n", '<br />').

			gsub(/:(icon|link)([^:]+):/, function(match){return '<em>'+match[2]+'</em>'}).
			gsub(/:([^:]+)(icon|link):/, function(match){return '<em>'+match[1]+'</em>'}).

			gsub(/\[url=[^\]]+\](.+?)\[\/url\]/, function(match){return '<em>'+match[1]+'</em>'}).
			gsub(/\[url\](.+?)\[\/url\]/, function(match){return '<em>'+match[1]+'</em>'}).

			gsub(/\[b\](.+?)\[\/b\]/, function(match){return '<strong>'+match[1]+'</strong>'}).
			gsub(/\[i\](.+?)\[\/i\]/, function(match){return '<em>'+match[1]+'</em>'}).
			gsub(/\[s\](.+?)\[\/s\]/, function(match){return '<strike>'+match[1]+'</strike>'}).
			gsub(/\[u\](.+?)\[\/u\]/, function(match){return '<u>'+match[1]+'</u>'});


		//
		// Calling the callback to fill the description
		//
		description_fill_callback(desc_popup, data);



		console.log('Calculating popup position...');

		// Figure out the coordinates where to put the description popup
		//
		var viewport_x = document.viewport.getWidth();
		var viewport_y = document.viewport.getHeight();

		var img = container.select('a img')[0];
		var tmp = img.cumulativeOffset();
		var container_x = tmp.left;
		var container_y = tmp.top;

		var container_width  = img.getWidth();
		var container_height = img.getHeight();

		//
		//
		// Initial popup placement and height
		//
		//
		var spawn_x = container_x + container_width + popup_x_offset;
		var spawn_y = container_y + use_popup_y_offset;

		var use_popup_width = popup_width;



		//
		// Corrections on axis X
		//

		if(spawn_x + use_popup_width < viewport_x)
		{
			// Horizontal direction = right
			//
			console.info('Horizontal position: right of tile');
		}
		else
		{
			// Horizontal direction = left
			//

			// Initial left placement X
			//
			spawn_x = container_x - popup_x_offset - use_popup_width;

			var tmp            = document.viewport.getScrollOffsets()[0];
			var left_overflow  = spawn_x - tmp;
			var max_width      = tmp + document.viewport.getDimensions().width;
			var right_overflow = max_width - (container_x + container_width + popup_x_offset + use_popup_width);



			if(left_overflow < 0)
			{
				// Popup went off the left side of the screen
				// We should determine on which side to show it, with reduced width
				//
				console.warn('Not enough space to fit the poup on neither sides');
				console.log('Overflow on the left: %d', left_overflow);
				console.log('Overflow on the right: %d', right_overflow);


				if(left_overflow > right_overflow)
				{
					// More space on the left
					//
					use_popup_width -= Math.abs(left_overflow) + width_autocorrect_offset;
					spawn_x = container_x - popup_x_offset - use_popup_width;
					console.info('Reducing popup width by %d, to %dpx', Math.abs(left_overflow), use_popup_width);
					console.info('Horizontal position: left of tile');
				}
				else
				{
					// More space on the right
					//
					use_popup_width -= Math.abs(right_overflow) + width_autocorrect_offset*2;
					spawn_x = container_x + container_width + popup_x_offset;
					console.info('Reducing popup width by %dpx, to %dpx', Math.abs(right_overflow), use_popup_width);
					console.info('Horizontal position: right of tile');
				}
			}
			else
			{

				console.info('Horizontal position: left of tile');
			}
		}




		//
		// Corrections on axis Y
		// Compensate for the height gain from adding variable length text to the popup.
		//
		var scroll_offset_y = document.viewport.getScrollOffsets()[1];
		var window_max_y    = (window.innerHeight ? window.innerHeight : document.documentElement.clientHeight) + scroll_offset_y;
		var popup_height    = desc_popup.getHeight();
		var popup_max_y     = spawn_y + popup_height;

		if(spawn_y < scroll_offset_y)
		{
			console.warn('Compensating for the popup overflow at the top. Moving the popup down by %dpx', scroll_offset_y - spawn_y);
			spawn_y = scroll_offset_y + 5;
		}
		else if(popup_max_y > window_max_y)
		{
			console.warn('Compensating for the popup overflow at the bottom. Moving the popup up by %dpx', popup_max_y - window_max_y);
			spawn_y -= popup_max_y - window_max_y + 5;
		}


		// Move to the position and show
		//
		desc_popup.style.left = spawn_x + 'px';
		desc_popup.style.top  = spawn_y + 'px';
		console.info('Moving popup to absolute coordinates: %dx%d', spawn_x, spawn_y);

		desc_popup.style.width = use_popup_width+'px';

		desc_popup.removeClassName('invisible');
		console.info('Displaying popup.');
	}
	else
	{
		console.warn('Description not found, aborting');
	}
}

function submissiontype_icon_mouseout(evt)
{
	$('description_popup').addClassName('invisible');
	// Processing ends when popup is closed
	//
	console.info('MouseOut event received. Hiding popup');
	console.groupEnd();
}
