jQuery(document).ready(function(){
	
	jQuery('a[href^="http://"]')	.attr({ target: "_blank" });

	function smartColumns() {
		
		jQuery("ul.column").css({ 'width' : "100%"});
		
		var colWrap = jQuery("ul.column").width();
		var colNum = Math.floor(colWrap / 200);
		var colFixed = Math.floor(colWrap / colNum);
		
		
		jQuery("ul.column").css({ 'width' : colWrap});
		jQuery("ul.column li").css({ 'width' : colFixed});

		
		
	}	
	
	smartColumns();	

	jQuery(window).resize(function () {
		smartColumns();
		
	}); 
	
		
});

jQuery(document).ready(function(){

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

});