//Global

$(function() {
   
   //Table TR strip and hover
   $("table.main tr:nth-child(odd)").addClass('striped');

   //For IE7 performance. http://groups.google.com/group/jquery-en/browse_thread/thread/8a5e72d1ff7b2dd9
   $('table.main tr').each( function() { 
      var row = $(this); 
      row.hover(
         function() { 
            setTimeout( function() { row.addClass('highlighted'); }, 0 ); 
         }, 
         function() { 
            setTimeout( function() { row.removeClass('highlighted'); }, 0 ); 
         } 
      );
   }); 
                  
   //popup window hover;   
   $("div.mainpop div:nth-child(even)").addClass('stripped');

   $("div.mainpop div input").click(function(e, other){
      e.stopPropagation();    
      //e.preventDefault();
      //return;
      
      var s = $(this).parent().parent().find(":nth-child(1)"); //get the hidden checkbox value;
      var d = $("#"+s.val());                //convert to the destination object;
                            
      if(this.checked){
         $(this).parent().addClass("selected");
         updateTarget(d,this.value,true);
      }
      else {
         $(this).parent().removeClass("selected");
         updateTarget(d,this.value,false);
      }
      
   });
   
   $("div.mainpop div").click(function(){
      
      var t = $(this).find(":nth-child(1)"); //get the checkbox;
      var v = t.val();                       //get the checkbox value;
      var c = t.attr("checked");             //get the checkbox status;

      if(c){
         $(this).removeClass("selected");
         t.attr("checked", false);
      }
      else {
         $(this).addClass("selected");
         t.attr("checked", true);
      }
      c = !c;
      
      var s = $(this).parent().find(":nth-child(1)"); //get the hidden checkbox value;
      var d = $("#"+s.val());                //convert to the destination object;
      
      updateTarget(d, v, c);
   });
   
   function updateTarget(d, v, c){//Target, value and checked
      var v1 = d.val();
      
      v1 = v1.split(',');
      
      if( $.inArray(v,v1) != -1 && !c) { 
         v1.splice($.inArray(v,v1),1);
      };
      if( $.inArray(v,v1) == -1 &&  c) {
         v1.push(v);
      }      
      v1.sort();      
      v1 = v1.join(",");
      v1 = v1.replace(/^[\s,]/,'');
      d.val(v1);
   }

   
});


function msgBox(o, p, popup){ //popup: from the jGrowl popup window. otherwise, from inline
   if(o.find("input[id*=msgInput]").size()==0){
      var t = (new Date()).getTime(); //get a unique id by current second of time.
      if(!popup){
         $("<div id='msgDiv" + t + "'><input type='text' class='popinput' autocomplete='off' id='msgInput" + t + "' size='30' name='a'><input type='submit' class='popinput' id='msgSub" + t + "' value='Send'><input type='reset' id='msgReset" + t + "' class='popinput' value='Cancel'></div>").appendTo(o);
         $('#msgReset' + t).click(function(){
            $('#msgDiv' + t).remove();
            return false;
         });      
      }
      else{
         $("<div id='msgDiv" + t + "'><input type='text' class='popinput' autocomplete='off' id='msgInput" + t + "' size='30' name='a'><input type='submit' class='popinput' id='msgSub" + t + "' value='Send'>").appendTo(o);
      }
      $('#msgSub' + t).click(function(){
         var m =$.trim($('#msgInput' + t).val());
         if(!m) {
            alert('Please write something.');
            $('#msgInput' + t).focus();
         }
         else{
            $.extend(p,{msg_body: m});
            //arrayShow(p);
            if(popup) msgHandler(o, p);
            else {
               //alert($('#msgDiv' + t).());
               $('#msgDiv' + t).remove();
               $.get('msg_process.php', p, function(data){$.jGrowl(data, {sticky: false})});
            }
         }
         return false;
      });
      $('#msgInput' + t).focus();
   }
}

function msgHandler(o, p){ //o=current object that fires the event, p: the values
   $.get('msg_process.php', p, function(data){$.jGrowl(data, {sticky: false})});
   if(o)o.hide();
}
