$(document).ready(function(){

	//Adjust panel height
	$.fn.adjustPanel = function(){ 
		$(this).find("ul, .kv-subpanel").css({ 'height' : 'auto'}); //Reset kv-subpanel and ul height
		
		var windowHeight = $(window).height(); //Get the height of the browser viewport
		var panelsub = $(this).find(".kv-subpanel").height(); //Get the height of kv-subpanel	
		var panelAdjust = windowHeight - 100; //Viewport height - 100px (Sets max height of kv-subpanel)
		var ulAdjust =  panelAdjust - 25; //Calculate ul size after adjusting sub-panel (27px is the height of the base panel)
		
		if ( panelsub >= panelAdjust ) {	 //If kv-subpanel is taller than max height...
			$(this).find(".kv-subpanel").css({ 'height' : panelAdjust }); //Adjust kv-subpanel to max height
			$(this).find("ul").css({ 'height' : ulAdjust}); //Adjust kv-subpanel ul to new size
		}
		else if ( panelsub < panelAdjust ) { //If kv-subpanel is smaller than max height...
			$(this).find("ul").css({ 'height' : 'auto'}); //Set kv-subpanel ul to auto (default size)
		}
	};
	
	//Execute function on load
	$("#kv-chatpanel").adjustPanel(); //Run the adjustPanel function on #kv-chatpanel
	$("#alertpanel").adjustPanel(); //Run the adjustPanel function on #alertpanel
	$("#kv-homepanel").adjustPanel(); //Run the adjustPanel function on #alertpanel
	
	//Each time the viewport is adjusted/resized, execute the function
	$(window).resize(function () { 
		$("#kv-chatpanel").adjustPanel();
		$("#alertpanel").adjustPanel();
		$("#kv-homepanel").adjustPanel();
	});
	
	//Click event on Chat Panel + Alert Panel	
	$("#kv-chatpanel a:first, #alertpanel a:first, #kv-homepanel a:first").click(function() { //If clicked on the first link of #kv-chatpanel and #alertpanel...
		if($(this).next(".kv-subpanel").is(':visible')){ //If kv-subpanel is already active...
			$(this).next(".kv-subpanel").hide(); //Hide active kv-subpanel
			$("#kv-footpanel li a").removeClass('active'); //Remove active class on the kv-subpanel trigger
		}
		else { //if kv-subpanel is not active...
			$(".kv-subpanel").hide(); //Hide all kv-subpanels
			$(this).next(".kv-subpanel").toggle(); //Toggle the kv-subpanel to make active
			$("#kv-footpanel li a").removeClass('active'); //Remove active class on all kv-subpanel trigger
			$(this).toggleClass('active'); //Toggle the active class on the kv-subpanel trigger
		}
		return false; //Prevent browser jump to link anchor
	});
	
	//Click event outside of kv-subpanel
	$(document).click(function() { //Click anywhere and...
		$(".kv-subpanel").hide(); //hide kv-subpanel
		$("#kv-footpanel li a").removeClass('active'); //remove active class on kv-subpanel trigger
	});
	$('.kv-subpanel ul').click(function(e) { 
		e.stopPropagation(); //Prevents the kv-subpanel ul from closing on click
	});
	
	//Delete icons on Alert Panel
	$("#alertpanel li").hover(function() {
		$(this).find("a.delete").css({'visibility': 'visible'}); //Show delete icon on hover
	},function() {
		$(this).find("a.delete").css({'visibility': 'hidden'}); //Hide delete icon on hover out
	});





	
});