/*
* Function to show or hide an element based on mouse event
* @usage showHideBasedOnInnerHtml ( element )
* @param ( element ) element that needs the function applied
* @version 1.5
*/
function showHideBasedOnInnerHtml ( id , element,  message1 , message2 ) {
	if ( $( id ).innerHTML == message1 ) {	
		$( id ).update( message2 );
		$( element ).show();
	}
	else {	
		$( id ).update(message1);
		$( element ).hide();
	}
}
/*
* Function to show or hide an element based on mouse event via a checkbox
* @usage showHideCheckbox ( element )
* @param ( element ) checkbox that needs the function applied
* @version 1.4
*/
function showHideCheckbox ( element , checkbox ) {
	// set the element variables
	var div = $(element), chk = $(checkbox);
	( chk.checked ) ? div.hide() : div.show();
}
/**
* Function that swaps two element's innerHTML's
* @param type
* @version 1.0
* @author Greg Shiers
*/
function swapCommentInnerHtml ( type ) {
	//set the innerHTML of each of the 2 elements to a variable
	var content1 = $("comments").innerHTML;
	var content2 = $("post_comment").innerHTML;
	// specific to the post / view comments section
	if ($('posting').innerHTML == "Post a comment") {
		$("comments").update( content2 );
		$("post_comment").update( content1 );
		$('posting').update("View comments");
		if ( $('blog_post_author') ) {
			$('blog_post_author').focus();
		}
	}
	else {
		$("comments").update( content2 );
		$("post_comment").update( content1 );
		$('posting').update("Post a comment");
	}
}
/**
* Below is a list of tinyMCE controls to manipulate the editor
**/
/**
* @descripton activates tinyMCE controls
* @param ID
* @version 1.2
* @author Greg Shiers
*/
function  activateTinyMce ( ID ) {
	if ( tinyMCE.getInstanceById( ID ) == null ) {	
		tinyMCE.execCommand( 'mceAddControl', false, ID );
	}
}
/**
* @descripton removes tinyMCE controls and disacrds them
* @param ID
* @version 1.2
* @author Greg Shiers
*/
function deactivateTinyMce ( ID ) {
	if ( tinyMCE.getInstanceById( ID ) != null ) {
		tinyMCE.execCommand( 'mceRemoveControl', false, ID )
	} 
}
/**
* @descripton forces focus on tinyMCE based on element
* @param ID
* @version 1.2
* @author Greg Shiers
*/
function forceTinyMceFocus ( ID ) {	
	if ( tinyMCE.getInstanceById( ID ) != null ) {	
		tinyMCE.execCommand('mceFocus',false, ID )
	}
}
/**
* @descripton checks if the editor element has been loaded yet
* @param element
* @version 1.2
* @author Greg Shiers
*/
function addToBookmarks ( name , url ) {
	if ( window.sidebar ) {
		window.sidebar.addPanel(name, url,"");
	}
	else if ( document.all ) {
		window.external.AddFavorite( url, name );
	}
	else{
		alert("Sorry your browser does not support this script");
	}
}

/*
* Function to grab one or more elements
* @usage $searchBlogs( element )
* @param search 
* @version 1.2
*/
function searchBlogs ( search ){
	// If there is no search phrase entered to the search box, then we tell the user to do so
	if (!search || search == "" ) {
		$('search_results').update('<ul class="leftbar_dots"><li class="pl10 red"><img src="/images/icons/mini_error.png" class="icor" />Enter a search term</li></ul>');
		return false;
	}
	// If there length of the string entered is less than for, we tell the user
	// as we are performing a full text search
	else if ($('keywords').value.length < 4 ) {
		$('search_results').update('<ul class="leftbar_dots"><li class="pl10 red"><img src="/images/icons/mini_error.png" class="icor" />4 Chars min</li></ul>');
	}
	// If it passes the above validation then perform an ajax call to the DB to search 
	// on that phase using the get method
	else {
		new Ajax.Request ('/blog.search.ajax.php?menu='+search, {
			method: 'get',
			onLoading : function () {
				$("search_results").update("<img src=\"/images/icons/progress.gif\" class=\"icor\" />Searching...<br /><br />");
			},
			onSuccess: function(transport) {
				$("search_results").update(transport.responseText);
			},
			onFailure: function () {
				$("search_results").update("The AJAX call failed");
			}
		});
	}
}

Event.observe(window, 'load', function () {
	document.onkeypress = function ( event  ) {	
		if (typeof event == "undefined") {
			event = window.event;
		}
		// Check if you press key 27 (esc) that we remove the dialog window
		if ( event.keyCode == 27 ) {
			if ( $('dialog') && $('tabs') ) {	
				if ($('blog_post_comment')) {	
					removeDialogWindow ('blog_post_comment');
				}
				else {	
					removeDialogWindow ();
				}
			}
			else {	
				return false;
			}
		}
	}
});
