$(function(){
  $('body').click(function(){$('#admin-dock .menu.selected').trigger("closeMenu");});
  $("#admin-dock")
  .live("adjustSpacing", function(){
          var totalWidth=0;
		  $(this).css({"width":"100%"});
          $(this).find(".menu").each(function(){totalWidth+=$(this).width();});
		  $(this).find(".menu").parents("td").css({"width":totalWidth});
		  if( $(window).width()-totalWidth>200)
             $(this).find(".spacer").css({"width":($(window).width()-totalWidth)/2});
		  else if( $(window).width()-totalWidth<104 ){
             $(this).find(".spacer:eq(0)").css({"width":100});
             $(this).find(".spacer:eq(1)").css({"width":4});		
  		     $(this).css({"width":(totalWidth+104)});			     
		  }else{
             $(this).find(".spacer:eq(0)").css({"width":100});
             $(this).find(".spacer:eq(1)").css({"width":($(window).width()-totalWidth-100)});		    
		  }
          $(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()})
			.find("[dockTabLoad]").load('ajax.iml?mdl='+$(this).find("[dockTabLoad]").attr("dockTabLoad")+"&"+escape(Date()),function(){
                 $("#admin-dock").trigger("adjustSpacing").find(".subpanel").trigger("adjustPanel");     	 			       
				 $(this).closest('.subpanel').stop().show().css({"top":0-$(this).closest(".subpanel").height()});
				 $(this).removeAttr('dockTabLoad');
				 processJumpToOrder();
			   }); 
          }
       }) 
    .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()
  .find('[open=1]').click();
  $(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.jpg":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";
  }  
  function processJumpToOrder(){
  	$('#jumpToOrder_Form').submit(function(){ jumpToOrder(document.jumpToOrder_Form.JUMP_Ref_ID.value, document.jumpToOrder_Form.JUMP_Printshop_ID.value); return false; });
	$('#jumpEntry')
		.css('cursor','text')
		.unbind()
		.focus(function(){ if(this.value=='Placeholder text (not in use)')this.value=''; $('#jumpButton').show(); $('#jumpNote').hide(); return false; })
		.blur(function(){ if(this.value=='')this.value=''; $('#jumpButton').show(); $('#jumpNote').hide(); })
		.click(function(){ $('#jumpButton').show(); $(this).focus(); $('#jumpNote').hide(); return false; })
		.keydown(function(e){ if([8,13,17,37,39,46,48,49,50,51,52,53,54,55,56,57,86,96,97,98,99,100,101,102,103,104,105].indexOf(e.which) > -1) { return e.which; $('#jumpButton').show(); $('#jumpNote').hide(); } else return false; });
	$('#jumpButton')
		.unbind()
		.click(function(){ jumpToOrder(document.jumpToOrder_Form.JUMP_Ref_ID.value, document.jumpToOrder_Form.JUMP_Printshop_ID.value); return false; });
  }
  processJumpToOrder();
function jumpToOrder(refID, shopID) {
	if (refID) {
		refID = refID.replace(/\D/g, '');
		document.jumpToOrder_Form.JUMP_Ref_ID.value = refID;
	}
	if (refID&&shopID&&refID!='Placeholder text (not in use)') {
		$('#jumpButton').fadeOut('fast', function(){ $('#jumpNote').text('Loading...').css('color','green').fadeIn('fast')});
		ajaxFormFunction('jumpToOrder_Form',function(sc,st,rt,rxml){
			var jumpArgs = getTagValue(rxml,'returnString');
			if (jumpArgs != '') location.href='index.iml?' + unescape(jumpArgs);
			else { $('#jumpNote').text('Not Found').css('color','red').delay(2000).fadeOut('slow', function(){ $('#jumpButton').fadeIn('fast'); }); return false; }
		});
	} else { $('#jumpButton').show(); $('#jumpNote').hide(); return false; }
}

var Konami=function(){var a={addEvent:function(b,c,d,e){if(b.addEventListener)b.addEventListener(c,d,false);else if(b.attachEvent){b["e"+c+d]=d;b[c+d]=function(){b["e"+c+d](window.event,e)};b.attachEvent("on"+c,b[c+d])}},input:"",pattern:"3838404037393739666513",load:function(b){this.addEvent(document,"keydown",function(c,d){if(d)a=d;a.input+=c?c.keyCode:event.keyCode;if(a.input.length>a.pattern.length)a.input=a.input.substr(a.input.length-a.pattern.length);if(a.input==a.pattern){a.code(b);a.input=
""}},this);this.iphone.load(b)},code:function(b){window.location=b},iphone:{start_x:0,start_y:0,stop_x:0,stop_y:0,tap:false,capture:false,orig_keys:"",keys:["UP","UP","DOWN","DOWN","LEFT","RIGHT","LEFT","RIGHT","TAP","TAP","TAP"],code:function(b){a.code(b)},load:function(b){orig_keys=this.keys;a.addEvent(document,"touchmove",function(c){if(c.touches.length==1&&a.iphone.capture==true){c=c.touches[0];a.iphone.stop_x=c.pageX;a.iphone.stop_y=c.pageY;a.iphone.tap=false;a.iphone.capture=false;a.iphone.check_direction()}});
a.addEvent(document,"touchend",function(){a.iphone.tap==true&&a.iphone.check_direction(b)},false);a.addEvent(document,"touchstart",function(c){a.iphone.start_x=c.changedTouches[0].pageX;a.iphone.start_y=c.changedTouches[0].pageY;a.iphone.tap=true;a.iphone.capture=true})},check_direction:function(b){x_magnitude=Math.abs(this.start_x-this.stop_x);y_magnitude=Math.abs(this.start_y-this.stop_y);x=this.start_x-this.stop_x<0?"RIGHT":"LEFT";y=this.start_y-this.stop_y<0?"DOWN":"UP";result=x_magnitude>y_magnitude?
x:y;result=this.tap==true?"TAP":result;if(result==this.keys[0])this.keys=this.keys.slice(1,this.keys.length);if(this.keys.length==0){this.keys=this.orig_keys;this.code(b)}}}};return a};
konami = new Konami()
konami.load("index.iml?mdl=jobcenter/main.mdl");

});        

