
/* Start up function on doc load*/
$(document).ready(function(){ 
	
//	menu_drops();
	menu_current();
	alt_rows();
	first_last_div();
	final_col();
	init_magic_buttons();
	initCollapsiblePanels();
	initFixMacFirefoxBug();
	initFixIE7TableBorderBug();
	initHelpDlg();
	initGeneralDlg();
	initTooltips();
	initTicker();
	initAutogrowTextarea(null);
	initEmoticons();
});

//Resets back to the default after submitting etc
function resetAutogrowTextarea(context)
{

	$(".commentBox",context).each(function (i) 
				{ 
					$(this).val(this.default_text);
					$(this).css("height",$(this).css("line-height"));
				});	

	$(".controls",context).hide();
}

var emoticonsPopup;
function initEmoticons(context)
{
if(emoticonsPopup==null)
	emoticonsPopup = $(".emoticonsPopup")[0];

//If none on page not required for this page
if(emoticonsPopup==null)
	return;
	
//Show on hover
$(".showEmoticons img",context).hover( 
		function() 
			{ 
				$(this).next(".emoticonsPopup").show();
			});

//Hide when moving off
$(".showEmoticons",context).mouseleave( function() { $(".emoticonsPopup").hide()} );			

//Append popup to existing image that causes popup
$(".showEmoticons",context).append(emoticonsPopup);

//Insert into first found textarea when clicking (only for subscribed users - title will not be set for non-subs)
$(".emoticonsPopup img[title!='']",context).click( 
		function() 
			{ 
				var te=$(this).parents("form").find("textarea");
				$(te).val($(te).val()+$(this).attr("title")); //Symbols for emoticon is in its title
				$(".emoticonsPopup").hide();
				}
				)

}

function initAutogrowTextarea(context)
{
	$('textarea.autogrow',context).each( function(i) 
		{ 
			this.default_text = $(this).val();			 
		} );

	$('textarea.autogrow',context).autogrow({
		maxHeight: 2000,
		blur_callback:
					function(te)
					{
						textareaObj=te;
						//Need to delay this slightly so can insert emoticon text if user clicked this
						setTimeout("hideControls()", 500);
					},
		focus_callback:
					function(te)
					{
					if($(te).val()==te[0].default_text)
						$(te).val("");
					$(te).parent().next(".controls").show();
					$(".emoticonsPopup").hide();
					}
					
		//lineHeight: 14	//Needed for IE6, else boxes have no height, but can set line-height in CSS instead
		//minHeight: 28	//Appears to have no effect
	});			
}

//Called on timer when bluring textarea with emoticons
function hideControls() 
{
	if($(textareaObj).val()=="")
		{
		$(textareaObj).val(textareaObj[0].default_text);
		$(textareaObj).parent().next(".controls").hide();
	}
}

/** 
 * (g) header menu dropdowns applied to li with class .drop within ul with class .main-nav   
 */
function menu_drops() {	
	var drop_set = $("ul.mainMenu").find("li.drop");
	drop_set.hover(this.drop_over, this.drop_out);
	if (drop_set.get(0)) {
	$(drop_set.get(0)).find("ul").mouseover(this.drop_over).mouseout(this.drop_out);
	}
}
/** 
 * show dropdown 
 **/
function drop_over() {
//	$("ul",this).fadeIn("fast");		//This doesn't work in IE - the final opacity is lost
	$("ul",this).show();
}
/** end show dropdown */

/** 
 * hide dropdown
 */
function drop_out() {
//	$("ul",this).fadeOut("normal");	//This doesn't work in IE - the final opacity is lost
	$("ul",this).hide();

}
/** endhide  dropdown **/
/** end header menu dropdowns  **/

/* Alternatively colours rows of a table with the altRows class, or reversed with altRowsRev*/
function alt_rows(context)
{
	//Need 'find' in case there are 2 tables on the page
	$("table.altRows tbody",context).find("tr:not(:hidden):odd").addClass("alt");

	$("div.altRows",context).find("div:not(:hidden):odd").addClass("alt");

	$("table.altRowsRev tbody",context).find("tr:not(:hidden):even").addClass("alt");

	$("div.altRowsRev",context).find("div:not(:hidden):even").addClass("alt");

}

/* Redo the alt rows, if rows have been added - removes and then re-does */
function redo_alt_rows(context)
{
	$(".alt",context).removeClass("alt");
	alt_rows(context);
}

/*Applies a final column style that doesn't have a right border*/
function final_col()
{
	//Need 'find' in case there are 2 tables on the page
	$("table.tabData thead tr").find("th:last").addClass("finalCol");
}

/*Applies a final column style that doesn't have a right border*/
function first_last_div()
{
	//Need 'find' in case there are 2 tables on the page
	$(".hlPanel .listData").find("div.listDataItem:last").addClass("lastDiv");
	$(".hlPanel .listData").find("div.listDataItem:first").addClass("firstDiv");
}

/* Sets the active menu and submenu */
function menu_current()
{
	if(getMenuL1()!=null)
		$("#menu-"+getMenuL1()).addClass("current");

	if(getMenuL2()!=null)
		$("#menu-"+getMenuL2()).addClass("current");

}

//Initialise collapsible panels
//To make a .hlPanel collapsible add the .collapsible class to it
//To make the panel initially expanded add the .expanded class to it also
function activateCollapsiblePanels(scope)
{

	//Collapse all panels initially except those marked as expanded
	$(".collapsible:not(.expanded)",scope).addClass("collapsed");	

	//Expand on clicking
	$(".collapsible .hlPanelHeading",scope).click(
		function()
			{
				$(this).parent().toggleClass("collapsed");
				$(".collapsible",scope).not($(this).parent()).addClass("collapsed");
			}
			);

}

// Collapsible Panels
function initCollapsiblePanels(scope)
{
	$(".hlPanel.collapsible .hlPanelHeading",scope).append("<div class='collapse'/>").hover(collapsibleHover, collapsibleRemoveHover);
}

function collapsibleHover()
{
	$(".collapse",this).addClass("collapseHover");
}

function collapsibleRemoveHover()
{
	$(".collapse",this).removeClass("collapseHover");
}


/* Magic buttons */
// Find A tags with the magicBtn Class and build the button around them
function init_magic_buttons(context){

	 $(".magicBtn",context).each(function(i){
	 this.innerHTML = "<span class='mBtnLeft'/><span class='mBtnTxt'>"+this.innerHTML+"</span></span><span class='mBtnRight'></span>";
	 });
 
	 $(".mBtnLeft",context).hover(mBtnHover, mBtnRemoveHover);
	 $(".mBtnRight",context).hover(mBtnHover, mBtnRemoveHover);
	 
    $(".mBtnLeft",context).mousedown(mBtnDown);
    $(".mBtnRight",context).mousedown(mBtnDown);
    $(".mBtnLeft",context).mouseup(mBtnUp);
    $(".mBtnRight",context).mouseup(mBtnUp);
    $(".mBtnLeft",context).mouseout(mBtnUp);
    $(".mBtnRight",context).mouseout(mBtnUp);
 
}
 
 
function mBtnDown() {
    $("span.mBtnLeft", $(this).parent()).addClass("mBtnLeftDown");
    $("span.mBtnRight", $(this).parent()).addClass("mBtnRightDown");
    $("span.mBtnTxt", $(this).parent()).addClass("mBtnTxtDown");
}

function mBtnUp() {
    $("span.mBtnLeft", $(this).parent()).removeClass("mBtnLeftDown");
    $("span.mBtnRight", $(this).parent()).removeClass("mBtnRightDown");
    $("span.mBtnTxt", $(this).parent()).removeClass("mBtnTxtDown");
}
 
 // Define the hover function
function mBtnHover(){
	$('span.mBtnLeft', $(this).parent()).addClass("mBtnLeftHover");
	$('span.mBtnRight', $(this).parent()).addClass("mBtnRightHover");
	$('span.mBtnTxt', $(this).parent()).addClass("mBtnTxtHover");
};

// Define the remove hover function
function mBtnRemoveHover(){
	$('span.mBtnLeft', $(this).parent()).removeClass("mBtnLeftHover");
	$('span.mBtnRight', $(this).parent()).removeClass("mBtnRightHover");
	$('span.mBtnTxt', $(this).parent()).removeClass("mBtnTxtHover");
};


/** bubble tool tips functions dsiplays title atribute on any element with class help-btn **/
function initTooltips(context){
	var links,i,h;
	links=$(".tooltip",context);
	links.each(function(i){initDelayedTooltip(this);});

	links=$(".quickTooltip",context);
	links.each(function(i){initQuickTooltip(this);});

}
/**
 * strips title attribute from help links and attches as a var to de isplayed in bubble tool tip
 * and sets mouse over/out events to hide show tool tip
 * 
 * @param {Object} el = dom element
 */
function initQuickTooltip(el){
	var tooltip,t,b,s,l;
	t=el.getAttribute("title");
	if(t==null || t.length==0) {}else{ 
	el.removeAttribute("title");
	tooltip=t;
	el.tooltip=tooltip;
	el.onmouseover=showTooltip;
	el.onmouseout=hideTooltip;
	el.onmousemove=locateTooltip;
}
}

var timerID;

function initDelayedTooltip(el)
{
	var tooltip,t,b,s,l;
	t=el.getAttribute("title");
	if(t==null || t.length==0) {}
	else
	{ 
	el.removeAttribute("title");
	tooltip=t;
	el.tooltip=tooltip;
	el.onmouseover=startTooltipTimer;
	el.onmouseout=clearTooltipTimer;
	el.onmousemove=locateTooltipTimer;
	}
}

function locateTooltipTimer(e)
{
	//If visible move it, else restart the timer
	if($("#tooltipContainer:visible").length >0)
	{
		locateTooltip(e)
	}
	else
	{
		startTooltipTimer(e);
	}
}


function clearTooltipTimer(e)
{
	hideTooltip(e);
	clearTimeout(timerID);
}

function startTooltipTimer(e)
{
	$("#tooltipContainer").find("span.top").html(this.tooltip);
	locateTooltip(e);
	
	if(timerID!=null) clearTimeout(timerID);
	
	timerID =setTimeout(function(){
		$("#tooltipContainer").show();
	}
	,500);
}
/**
 * shows bubble tooltip and inserts title to display
 * 
 * @param {Object} e = dom element
 */
function showTooltip(e){
	$("#tooltipContainer").find("span.top").html(this.tooltip);
	$("#tooltipContainer").show();
	locateTooltip(e);
}
/**
 * hides buble tooltip
 * 
 * @param {Object} e = dom element
 */
function hideTooltip(e){
	//$("#btc").find("span.top").html("");
	$("#tooltipContainer").hide();
}
/**
 * Positions tooltip at mouse point
 * 
 * @param {Object} e = dom element (postitions tooltip at mouse piont)
 */
function locateTooltip(e){
	var posx=0,posy=0;
	if(e==null) e=window.event;
	if(e.pageX || e.pageY){
   		 posx=e.pageX; posy=e.pageY;
	} else if(e.clientX || e.clientY){
    		var doc_el = document.documentElement;
		var el_left = doc_el.scrollLeft;
		var el_top = doc_el.scrollTop;
		if(el_top){
			posx=e.clientX+el_left; posy=e.clientY+el_top; 
		} else{ 
			posx=e.clientX+document.body.scrollLeft; posy=e.clientY+document.body.scrollTop; 
		}	
   	 }
	var main_el = document.getElementById("tooltipContainer");
	main_el.style.top=(posy+5)+"px";
	main_el.style.left=(posx-122)+"px";
}
