$(document).ready(function(){
    //TODO wrap all these in a conditional for IE6 only, other browsers can rely on :hover
    //Maybe wrap into one function to add class ieHover
    $("#menu li span").mouseover(function(){$(this).addClass("ieHover");}).mouseout(function(){$(this).removeClass("ieHover");});
/*    $(".featdivide ul").mouseover(function(){$(this).addClass("ieHover");}).mouseout(function(){$(this).removeClass("ieHover");});
    $(".featdivide2 ul").mouseover(function(){$(this).addClass("ieHover");}).mouseout(function(){$(this).removeClass("ieHover");});
    $(".featdivide3 ul").mouseover(function(){$(this).addClass("ieHover");}).mouseout(function(){$(this).removeClass("ieHover");}); */
    $(".online").mouseover(function(){$(this).addClass("ieHover");}).mouseout(function(){$(this).removeClass("ieHover");});
    // add rollover for aqua buttons for ie6
 /*   $(".aqua-button").mouseover(function(){$(this).addClass("ieHover");}).mouseout(function(){$(this).removeClass("ieHover");}); */

    //Empty and reset form input text values
    if($(".emptyInput input:text")) {
        $(".emptyInput input:text").each(
            function(index, item){
                $( this ).bind (
                    "focus",
                    function(){
                        if(item.value===item.defaultValue) { item.value=''; }
                    }
                );
                $( this ).bind (
                    "blur",
                    function(){
                        if(item.value==='') { item.value=item.defaultValue; }
                    }
                );   
            }
        );
    }
    //Empty and reset form input textarea values
    if($(".emptyInput textarea")) {
        $(".emptyInput textarea").each(
            function(index, item){
                $( this ).bind (
                    "focus",
                    function(){
                        if(item.value===item.defaultValue) { item.value=''; }
                    }
                );
                $( this ).bind (
                    "blur",
                    function(){
                        if(item.value==='') { item.value=item.defaultValue; }
                    }
                );   
            }
        );
    }

    //Make input boxes pre-selected
    if($(".selectInput input")) {
        $(".selectInput input").each(
            function(index, item){
                $( this ).bind (
                    "focus",
                    function(){
                        item.select();
                    }
                );
                $( this ).bind (
                    "click",
                    function(){
                        item.select();
                    }
                );
            }
        );
    }

 //Add toggle for open and closing TODO make it more robust - maybe use a parent class as well to target
 if($(".toggle")) {
     $(".toggle").each(
         function(index, item){
             $( this ).bind (
                 "click",
                 function(e){
                     e.preventDefault();
                     if($(item).parent().hasClass('jshide')) { 
                    	 $(item).parent().removeClass('jshide');
                	 } else { 
                         $(item).parent().addClass('jshide');                    
                     }
                 }
             );
         }
     );
 }
 
   //Edit options
   if($(".edit-links")) {
       $(".edit-links").each(
            function(index, item){
                $( this ).bind (
                    "click",
                    function(e){
                        e.preventDefault();
                        e.stopPropagation();
                        // check if item is to show/hide
                        if($(item).hasClass('edit-links')) {
                            var show = true;
                        } 
                        //reset all other popups
                        var items=$(".show-edit-links");
                        items.removeClass('show-edit-links');
                        items.addClass('edit-links');
                        //show popup if required
                        if(show === true) {
                            $(item).removeClass('edit-links');
                            $(item).addClass('show-edit-links');
                        }
                        //re-enable clicks on pop-ups
                        $(".show-edit-links a").each(
                                 function(index, item){
                                     $( this ).bind (
                                         "click",
                                         function(e){
                                             if(false === $(item).hasClass('aqua-link')) {
                                                 e.stopPropagation();
                            
                                             }
                                         }
                                     );
                                 }
                            );
                    }
                );
            }
       );
 }

 //we want to hide all popups if container is clicked
  $( 'body' ).bind (
      "click",
      function(e){
          var items=$(".show-edit-links");
          items.removeClass('show-edit-links');
          items.addClass('edit-links');
          
      }
  );
  
 //ENable/disable form elements FIX
 if($(".enable-form")) {
     $(".enable-form .enabler").each(
         function(index, item){
             $( this ).bind (
                 "click",
                 function(e){
                     //e.preventDefault();
                     var parent = $(item).parent();
                     var disabled = parent.find(".form-disabled input");
                     for(i=0;i<=disabled.length;i++) {
                         if($(disabled[i]).attr("disabled") === true) {
                             $(disabled[i]).removeAttr("disabled");
                             
                         } else {
                             $(disabled[i]).attr("disabled", "disabled");
                             
                         }
                     }      
                 }
             );
         }
     );
 }
 
 
 //Email actions - requires class on the TR element - this could also be styled if needed.
 if($("#email-select")) {
     $("#checkEmail").bind (
            "change",
            function(e) {
                var checkBoxes = $("#inbox input:checkbox");
                    action = this.value;
                switch (action)
                    {
                    case "none":
                        checkBoxes.each( function () {
                          this.checked=false;
                        });
                        break;
                    case "all" :
                        checkBoxes.each( function () {
                          this.checked=true;
                        });
                        break;
                    case "read" :
                        checkBoxes.each( function () {
                             if($(this).parents('TR').hasClass('read')) {
                                 this.checked=true;
                             } else {
                                 this.checked=false;
                             }
                        });
                        break;
                    case "unread" :
                        checkBoxes.each( function () {
                            if($(this).parents('TR').hasClass('unread')) {
                                this.checked=true;
                            } else {
                                this.checked=false;
                            }
                        });
                        break;
                }
            }
         );
 	}
});