////////////////////////////////////////////////////////////////
// BGIFRAME CODE                                              //
// To fix IE6 bug where select boxes that appear over         //
// dropdowns                                                  //
////////////////////////////////////////////////////////////////
(function($){$.fn.bgIframe=$.fn.bgiframe=function(s){if($.browser.msie&&/MSIE 6.0/.test(navigator.userAgent)){s=$.extend({top:'auto',left:'auto',width:'auto',height:'auto',opacity:true,src:'javascript:false;'},s||{});var prop=function(n){return n&&n.constructor==Number?n+'px':n;},html='<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+'style="display:block;position:absolute;z-index:-1;'+(s.opacity!==false?'filter:Alpha(Opacity=\'0\');':'')+'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+'"/>';return this.each(function(){if($('> iframe.bgiframe',this).length==0)this.insertBefore(document.createElement(html),this.firstChild);});}return this;};})(jQuery);
////////////////////////////////////////////////////////////////
// BUTTONS / DROPDOWNS                                        //
////////////////////////////////////////////////////////////////
$(document).ready(function(){
    bindTopNav();
    bindButtonBar();
    bindSideNav();
    bindSubNav();
    bindTableActions();
    bindActionBar();
    bindInputPrompt();
    bindCollapseBars();
    
});

function bindTopNav() {
    $(".topnav .dropdown").hover(
        function(){
            $(this).addClass("over");
            if ($(this).find(".subnav").is(":hidden")) { $(this).find(".subnav").show(); } else { $(this).find(".subnav").hide(); }
        },
        function(){ $(this).removeClass("over"); $(this).find(".subnav").hide(); }   
    );
    $(".topnav").find(".link").click(function(){ $(".topnav").removeClass("over"); $(".topnav").find(".subnav").hide(); });
}

function bindButtonBar() {
    
    $(".buttonbar .dropdown").hover(
        function(){
            if ($(this).find(".subnav").is(":hidden")) { $(this).find(".subnav").show(); } else { $(this).find(".subnav").hide(); }
        },
        function(){ $(this).find(".subnav").hide(); }   
    );
    $(".buttonbar").find(".link").click(function(){ $(".buttonbar").find(".subnav").hide(); });
    
    $(".buttonbar .button").hover( function() { if ($(this).hasClass("nomouseover") != true) { $(this).addClass("over"); } }, function(){ $(this).removeClass("over");
    } );
}

function bindSideNav() {
    $(".sidenav .button").hover( function(){  if($(this).parent().hasClass("static") != true && $(this).hasClass("nomouseover") != true){$(this).addClass("over");}  },  function(){ $(this).removeClass("over"); } );
    $(".sidenav.collapse .button-text").click(
        function(){
            if($(this).parent().hasClass("open") != true ){
                $(this).parent().addClass("open");
                $(this).next(".subnav").slideDown("fast");
            } else {
                $(this).parent().removeClass("open");
                $(this).next(".subnav").slideUp("fast");
            }
        }
    );
    
    $(".sidenav.accordion .button-text").click(
        function(){
            if($(this).parent().hasClass("open") != true ){
                $(".sidenav.accordion .subnav").slideUp("fast");
                $(".sidenav.accordion .button").removeClass("open");  
                $(this).parent().addClass("open");
                $(this).next(".subnav").slideDown("fast");
            } else {
                $(".sidenav.accordion .subnav").slideUp("fast");
                $(".sidenav.accordion .button").removeClass("open");   
            }
        }
    );
}

function bindSubNav() {
    $(".subnav").bgiframe();
    $(".subnav .link").hover( function(){ if ($(this).hasClass("nomouseover") != true) { $(this).addClass("over"); } },  function(){ $(this).removeClass("over"); } );
}

function bindTableActions() {
    $(".datatable .dropdown").hover(
        function(){
            if ($(this).hasClass("nomouseover") != true) {
               $(this).addClass("over");
               $(this).find(".subnav").show();
            }
        },
        function(){ $(this).removeClass("over"); $(this).find(".subnav").hide(); }   
    );
    $(".datatable").find(".link").click(function(){ $(".datatable").find(".subnav").hide(); });
}

function bindActionBar() {
    $(".actionbar .dropdown").hover(
        function(){
            $(this).addClass("over");
            $(this).find(".subnav").show();
        },
        function(){ $(this).removeClass("over"); $(this).find(".subnav").hide(); }   
    );
    $(".actionbar").find(".link").click(function(){ $(".actionbar").find(".subnav").hide(); });
}

function bindInputPrompt() {
    if($(".form .prompt").attr("title") == $(".form .prompt").val()) {
        $(".form .prompt").addClass("dim");
    }
    
    $(".form .prompt").focus(
        function(){
            if($(this).attr("title") == $(this).val()) {
                $(this).val("");
                $(this).removeClass("dim");
            }
        }
    );
    
    $(".form .prompt").blur(
        function(){
            if($(this).val() == "") {
                $(this).val($(this).attr("title"));
                $(this).addClass("dim");
            }
        }
    );
    
}

function bindCollapseBars() {
    $(".collapsebar .bar").hover( function(){  if ($(this).hasClass("nomouseover") != true) { $(this).addClass("over"); }  },  function(){ $(this).removeClass("over"); } );
    $(".collapsebar .bar").click(
        function(){
            if($(this).hasClass("open") != true ){
                $(this).addClass("open");
                $(this).next(".content").slideDown("fast");
            } else {
                $(this).removeClass("open");
                $(this).next(".content").slideUp("fast");
            }
        }
    );
}

function selectAllCheckboxes(select) {  if($(select).is(":checked")){ $("input").attr("checked", true); } else { $("input").attr("checked", false); } }
function showMessage(el, msg){  $(el).addClass("message"); $(el).removeClass("success"); $(el).removeClass("error"); $(el).html(msg); $(el).show(); }
function clearMessage(el){ $(el).fadeOut(500); }
function showError(el, msg){ $(el).addClass("error"); $(el).removeClass("success"); $(el).removeClass("message"); $(el).html(msg); $(el).show(); }
function clearError(el){ $(el).fadeOut(500); }
function showSuccess(el, msg){ $(el).addClass("success"); $(el).removeClass("error"); $(el).removeClass("message"); $(el).html(msg); $(el).show(); }
function clearSuccess(el){ $(el).fadeOut(500); }

function showLoading(div){
    $(div).prepend("<div id='loading-container'><div id='loading-mask' class='loading-mask'></div><div class='loading-img' id='loading-img'><img src='../images/skin-loading.gif' height='34' width='104' /></div></div>");
    $(div).find("#loading-mask").height($(div).height());
    $(div).find("#loading-mask").width($(div).width());
    $(div).find("#loading-img").css("margin-top",($(div).height()/2)-16);
    $(div).find("#loading-img").css("margin-left",($(div).width()/2)-52);
}

function clearLoading(div) {
    $(div).find("#loading-container").remove();
}

