/**
* Website functions
*/

$(document).ready(function() 
{
 
	function isIE()
	{
    	if(navigator.userAgent.match(/MSIE \d\.\d+/))
        return true;
    	return false;
	}		
	
	// setup the drop downs
	jQuery('#navbar ul > li').hoverIntent(
		// mouseover so show the drop down
		function()
		{
			// make the nav item have the over state
			jQuery('> .sub-menu', this).toggle();
			jQuery('> .sub-menu-right', this).toggle();
			jQuery('> .sub-menu-righth', this).toggle();
			jQuery('a span', this).addClass('hovering');
		},
		// mouseout so hide the drop down
		function()
		{
			// remove the over state on the nav item
			jQuery('> .sub-menu', this).toggle();
			jQuery('> .sub-menu-right', this).toggle();
			jQuery('> .sub-menu-righth', this).toggle();
			jQuery('a span', this).removeClass('hovering');
		}
	);
	
	// IE fun
	if(isIE())
	{
		$('#navbar li li').hover(
			function()
			{
				$(this).addClass('hover_ie');
			},
			function()
			{
				$(this).removeClass('hover_ie');
			}
		);	
	}
	

	
	//from http://richa.avasthi.name/blogs/tepumpkin/2008/01/11/ie7-lessons-learned/
	function zIndexWorkaround()
	{
	    // If the browser is IE,
	    if(isIE())
	    {
	        /*
	        ** For each div with class menu (i.e.,
	        ** the thing we want to be on top),
	        */
	        $(".sub-menu").parents().each(function() {
	            var p = $(this);
	            var pos = p.css("position");
	
	            // If it's positioned,
	            if(pos == "relative" ||
	               pos == "absolute" ||
	               pos == "fixed")
	            {
	                /*
	                ** Add the "on-top" class name when the
	                ** mouse is hovering over it, and remove
	                ** it when the mouse leaves.
	                */
	                p.hover(function() {
	                        $(this).addClass("on-top");
	                    },
	                    function() {
	                        $(this).removeClass("on-top");
	                    });
	            }
	        });
			$(".sub-menu-right").parents().each(function() {
	            var p = $(this);
	            var pos = p.css("position");
	
	            // If it's positioned,
	            if(pos == "relative" ||
	               pos == "absolute" ||
	               pos == "fixed")
	            {
	                /*
	                ** Add the "on-top" class name when the
	                ** mouse is hovering over it, and remove
	                ** it when the mouse leaves.
	                */
	                p.hover(function() {
	                        $(this).addClass("on-top");
	                    },
	                    function() {
	                        $(this).removeClass("on-top");
	                    });
	            }
	        });
			$(".sub-menu-righth").parents().each(function() {
	            var p = $(this);
	            var pos = p.css("position");
	
	            // If it's positioned,
	            if(pos == "relative" ||
	               pos == "absolute" ||
	               pos == "fixed")
	            {
	                /*
	                ** Add the "on-top" class name when the
	                ** mouse is hovering over it, and remove
	                ** it when the mouse leaves.
	                */
	                p.hover(function() {
	                        $(this).addClass("on-top");
	                    },
	                    function() {
	                        $(this).removeClass("on-top");
	                    });
	            }
	        });
	    }
	}
	
	zIndexWorkaround();
	
	
})

function check_posting_area()
{
    var box = $('#message-box');
    if(!box.length) return false;
    var textarea = $('textarea#message');
    if(!textarea.length) return false;
    textarea.css('width', 'auto');
    // check textarea width
    var diff = 20,
        width = box.width(),
        cols = textarea.attr('cols');
    if((textarea.width() + diff) >= width)
    {
        // reduce width
        var min = 200;
        var count = 0;
        while(textarea.width() > (width - diff))
        {
            count ++;
            if(count > 20) // avoid infinite loop
            {
                return false;
            }
            cols -= ((textarea.width() - width) > 100) ? 10 : (((textarea.width() - width) > 30) ? 2 : 1);
            if(cols < 10 || textarea.width() < min) return;
            textarea.attr('cols', cols);
        }
        return true;
    }
    // increase width
    var count = 0;
    while(textarea.width() < (width - diff))
    {
        count ++;
        if(count > 20) // avoid infinite loop
        {
            return false;
        }
        cols += ((width - textarea.width()) > 100) ? 10 : (((width - textarea.width()) > 30) ? 2 : 1);
        textarea.attr('cols', cols);
    }
    return true;
}
$(window).bind('load resize', function()
{
    if(ie8) check_posting_area();
});


