/**
 * Store and namespace base functions used sitewide.
 */

//Create the Kumutu namespace KM
if (KM == null || typeof(KM) != "object") { var KM = new Object();}

/**
 * Set base URL for development environment.
 */
KM.setBaseUrl = function() {
	if (KM.baseUrl.indexOf('fmullenger') > -1) {
		KM.baseUrl = KM.baseUrl+'/kumutu';
	};
};

/**
 * Convenience base URL for ajax calls.
 */
KM.baseUrl = location.protocol+'//'+location.hostname;

/**
 * Handle ajax errors site wide.
 * 
 * Possible error codes include:
 * 400 : Bad Request, general use when request cannot be handled by the server
 * 403 : Forbidden, authorisation denied
 * 408 : Request Timeout, timeout on the server
 * 409 : Conflict, exception thrown or similar server side error
 * 500 : Internal Server Error, error server side
 */
KM.ajaxFailureHandler = function() {
	
	//Handling any ajax errors
	$("#ajax-error-dialog").ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError){

		 if (XMLHttpRequest.status == '409' || XMLHttpRequest.status == '403') {
		 
		   //Unbind ajaxStop
		   $(this).unbind('ajaxStop');
		 
		   //Use the ajax-error message dialog to display an error
		   $('#ajax-error-response', this).text(XMLHttpRequest.responseText);
		
		   $(this).dialog({
				bgiframe: true,
				modal: true,
				buttons: {
					Ok: function() {
						$(this).dialog('close');
					}
				}
			});
		   $(this).dialog('open');
		 }
	});
};

/**
 * Initiate the overlay for quickly signing in
 * Uses jquery UI dialog
 *
KM.initLogin = function() {
        $('#signin-button, .signin-dialog').click(function(){
        	//If dialog is not open and has not been opened, then open
        	if (!$('#signin').dialog('isOpen')) {
        		$('#signin').dialog('open');
        	    return false;
        	}   
        });

		$('#signin').dialog({
			modal: true,
			resizable: false,
			autoOpen: false,
			show: 'slide',
			title: 'Sign in to Kumutu',
			height: 500,
			width: 690,
		});
	
};
*/

//Highlights all notices for a brief amount of time
KM.highlightNotice = function() {
    setTimeout(function() { 
	    $('.notice').animate({backgroundColor:"#FFF7A0"},1000);
    }, 1000);
    setTimeout(function() { 
	    $('.notice').animate({backgroundColor:"#FFFBE5"},1000);
    }, 1500);
};


//Adjusts the sidebar minimum height to that of the main container to the left of it
KM.sidebarHeight = function() {
	var mainHeight = $('#main').height();
	var sidebarHeight = $('#sidebar').height();			
	if (sidebarHeight < mainHeight && sidebarHeight < 810) {
		$('DIV#sidebar').css("min-height", "810px");		
	}
	if (mainHeight < sidebarHeight && mainHeight < 810) {
		$('DIV#main').css("min-height", "810px");
	}


/*  DEPRECATED: Setting min-height if other column is taller
	var mainHeight = $('#main').height();
	var sidebarHeight = $('#sidebar').height();
	if(mainHeight > sidebarHeight) {
		$('DIV#sidebar').css("min-height", mainHeight + "px");
	} else if (sidebarHeight > mainHeight) {
		$('DIV#main').css("min-height", sidebarHeight + "px");
	}
*/	
	


};


/* Hides the overlay login box
KM.initCancelLogin = function(e) {
	$.each( ['#btnCancelLogin', '#linkCancelLogin'], function(i, n){
		$(n).click(function() {
			e.preventDefault();
		});
	});
};
*/

KM.loadingTabs = function() {
	$('#loading-tabs').hide();
};


/**
 * Set up the top drop Admin down menu
 */
KM.initMenu = function() {
        
 		$('ul.navlinks ul.subnav').parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)
	
		//$('ul.topnav li span').click(function() { //When trigger is clicked...
		$('ul.navlinks li').mouseover(function() { //instead open on hover
			
			//Following events are applied to the subnav itself (moving subnav up and down)
			$(this).find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on hover
	
			$(this).hover(function() {
			}, function(){
				$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
			});
	
			//Following events are applied to the trigger (Hover events for the trigger)
			}).parent().hover(function() {
				$(this).addClass("subhover"); //On hover over, add class "subhover"
			}, function(){	//On Hover Out
				$(this).removeClass("subhover"); //On hover out, remove class "subhover"
		});
};

/**
 * Set up the top drop down Nav Search box
 */
KM.initNavSearch = function() {
        
 		$('#navSearch span').click(function() {
			$('#navSearch-container').toggle('fast'); //Show Search container

			/*
			$('#navSearch-container').slideDown('fast').show(); //Show Search container
			
			$('#navSearch-container').hover(function() {
			}, function(){
				$('#navSearch-container').slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
			});
			*/
		});
};

$(document).ready(function(){
	KM.setBaseUrl();
	KM.ajaxFailureHandler();
	KM.loadingTabs();
//	KM.initLogin();
	KM.highlightNotice();
	KM.sidebarHeight();
	KM.initMenu();
//	KM.initCancelLogin();
	KM.initNavSearch();
});
