$(function(){
  $('body').click(function(){$('#admin-dock .menu.selected').trigger("closeMenu");});
  $("#admin-dock")
  .live("adjustSpacing", function(){
          var totalWidth=0;
          $(this).find(".menu").each(function(){totalWidth+=$(this).width();});
		  $(this).find(".menu").parents("td").css({"width":totalWidth});
          $(this).find(".spacer").css({"width":($(window).width()-totalWidth)/2});
          $(this).find(".menu").each(function(){$(this).find(".subpanel").css({"left":$(this).offset().left-6});});
          return false;
      })      
  .trigger("adjustSpacing")
  .find(".menu")
    .live("closeMenu",function(){ if($(this).is(".selected")){ $(this).removeClass("selected").find(".subpanel").animate({"top":0},function(){$(this).hide();});} })
    .live("openMenu",function(){
          if($(this).is(".selected")){ $(this).trigger("closeMenu"); }
          else{  $(this).addClass("selected").removeClass("mouseover").find(".subpanel").show().animate({"top":0-$(this).find(".subpanel").height()}); }
       }) 
    .click(function(event){  $(this).trigger("openMenu");$("#admin-dock .menu.selected").not(this).trigger("closeMenu");event.stopPropagation(); })
    .hover(function(){$(this).filter(":not(.selected)").addClass("mouseover");},function(){$(this).filter(":not(.selected)").removeClass("mouseover");})                            
    .each(function(){ $(this).find(".subpanel div ul").before('<div class="panel-header">'+$(this).find(".header").text()+'</div>')})
    .find(".subpanel")
      .children("div") // add exta divs for dynamic width background images 
        .each(function(){$(this).addClass("main").after('<div class="mainright"> </div>')})
      .end()
      .live("adjustPanel",function(){ // adjust panel dimensions
        var numeric_w=1;
        $(this).show()  // show() to help determin widths
        .find('div.mainright').css({'height':'auto'}).hide().end()  // do not include 
        .find(".numeric")
          .each(function(){if($(this).width()>numeric_w)numeric_w=$(this).width(); })
          .each(function(){$(this).css({"padding-left":numeric_w-$(this).width()}); })
        .end()
        .css({ 'height' : 'auto', 'width': 'auto'})
        .find('ul').css({ 'height' : 'auto', 'width': 'auto'}).end();
        var w=$(this).width();
        $(this).css({'width':w+45}).find(".ul").css({'width':w+5});
        var panelAdjust = $(window).height() - 100;         
        var h=$(this).height();
        if (  h > panelAdjust ) {	 
          h=panelAdjust;
          $(this).css({ 'height' : panelAdjust })
                 .find('ul').css({ 'height' : panelAdjust-40});                           
        } 
        $(this).find('div.mainright').css({'height':h}).show();
        $(this).hide();       
        return false;
      })
      .trigger("adjustPanel")
      .find("li")
        .hover(function(){$(this).addClass("selected"); },function(){$(this).removeClass("selected"); })
      .end()
    .end()
  .end() 
  .css({'visibility':'visible','display':'none'}).fadeIn();
  $(window).resize(function () {    
     $('#admin-dock .menu.selected').trigger("closeMenu"); 
     $("#admin-dock").trigger("adjustSpacing")
     .find(".subpanel").trigger("adjustPanel");     	 
  });

  
  
  
  $("[imageInput]")
  .find("input[type=button]").click(function(){ 
     var parent=$(this).parents("[imageInput]");
     imageDragger.setConstrain(parent.get(0));   
     var src=parent.find("img").attr('src');
     var prefix=src.substring(0,src.lastIndexOf('/')>=0?src.lastIndexOf('/'):src.length);
     var file=prefix.lastIndexOf('/')>=0?src.substring(prefix.lastIndexOf('/')+1,src.length):'';
     imagePicker.open(function(img){
       parent.find("input[type=hidden]").attr('value',img);
       parent.find("img").attr('src',img==""?"no_image.gif":PREFS.Site_Dir+"/"+img);
     },parent.attr('category'),parent.attr('message'),file);     
  }).end()
  .find("img").each(function(){
    $(this).parent().html('<table class="picture-frame"><tbody><tr><td class="tl"></td><td class="t"></td><td class="tr"></tr><tr><td class="l"></td><td class="m">'
      +$(this).parent().html()+
      '</td><td class="r"></td></tr><tr><td class="bl"></td><td class="b"></td><td class="br"></td></tr></tbody></table>');
  }).end();  
  

  
  
  
   $.fn.vAlign = function(container) {
     return this.each(function(i){
	     if(container == null) {
	       container = 'div';
	     }
	     $(this).html("<" + container + ">" + $(this).html() + "</" + container + ">");
	     var el = $(this).children(container + ":first");
         $(el).css({'margin-top':0,'margin-bottom':0});
	     var elh = $(el).height(); //new element height
	     var ph = $(this).height(); //parent height
	     if(elh > ph) { //if new element height is larger apply this to parent
	       $(this).height(elh);
	       ph = elh;
	     }
	     var nh = (ph - elh) / 2; //new margin to apply
	     $(el).css({'margin-top': nh});
     });
   };
  $(".admin_sub_menu .menuItem").vAlign();
  
  
  if($('#admin-dock').size()>0){
    $("input,select,textarea").live('process', function(){
	                           $(this).focus(function(){
		                           var o=$(this).offset().top+40-$('#admin-dock').offset().top;
								   if(o>0) $(window).scrollTop($(window).scrollTop()+o);
         		                 })
		                    })
    .focus(function(){
        var o=$(this).offset().top+40-$('#admin-dock').offset().top;
	    if(o>0) $(window).scrollTop($(window).scrollTop()+o);
    })  
    jqueryProcessList[jqueryProcessList.length]="input,select,textarea";
  }  
  
  
});        
