var currentChatBoxId = null;

function footerChatPanelClick()
{
    jQuery(this).removeClass("chat_alert").addClass("chat");
    
    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...
        // Remember what panel we're on'
        currentChatBoxId = jQuery(this).parent(":first").attr("id");
        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
//        jQuery(".subpanel").find("ul").scrollTop(jQuery(".subpanel").find("ul").height());

        // Adjust the panel
        footerBarAdjustPanel(jQuery(this).parent());
        var elem = jQuery(this).next(".subpanel").find(".chatscrollbox");
        elem.scrollTop(100000);
        jQuery(this).next(".subpanel").children(".chatscrollbox").children("li").next("li").children("input").focus();

    }
    return false;
}


function footerBarAdjustPanel()
{
    footerBarAdjustPanel(this);
//    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)
//    }
};

function footerBarAdjustPanel(chatElem)
{

    jQuery(chatElem).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(chatElem).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(chatElem).find(".subpanel").css({ 'height' : panelAdjust }); //Adjust subpanel to max height
        jQuery(chatElem).find("ul").css({ 'height' : ulAdjust}); //Adjust subpanel ul to new size
    }
    else if ( panelsub < panelAdjust )
    { //If subpanel is smaller than max height...
        jQuery(chatElem).find("ul").css({ 'height' : 'auto'}); //Set subpanel ul to auto (default size)
    }

};

