var _url=top.location.href.split('#')[1];_url=(_url!=''&& typeof _url!='undefined'?_url:'main');

String.prototype.fit_text=function(l){
 return this.substring(0,l)+(this.length>l?'&hellip;':'');
}

var scripts={
 start:function(){
  $('a').focus(function(){this.blur();});

  //var nav_on=$('ul#nav>li>a')[0];
  $('ul#nav>li>a').click(function(){
   $(nav_on).removeClass('on');
   $(this).addClass('on');
   hash=this.href.split('/')[this.href.split('/').length-1].split('.')[0];
   parent.location.replace(parent.location.href.split('#')[0]+'#'+hash);
   nav_on=this;
  });
	
  if($('body.main').length){
   $('#page_title',parent.frames['top'].document.body).html(_url!='main'?' &#187; '+_pages[_url+'.php']:'');
   $('#show_render_time',parent.frames['bottom'].document.body).html($('#render_time').html());
   $('#show_num_queries',parent.frames['bottom'].document.body).html($('#num_queries').html());
	 
	 if($('form#add').length){
    $('form#add fieldset').css('opacity',0.5).mouseover(function(){$(this).css('opacity',1);this.hovering=true})
     .mouseout(function(){this.hovering=false;if(this.can_change) $(this).css('opacity',0.5);});
    $('form#add fieldset')[0].can_change=true;
    $('form#add fieldset')[0].hovering=false;
    $('input,select','form#add').focus(function(){$('form#add fieldset')[0].can_change=false;if(!$('form#add fieldset')[0].hovering) $('form#add fieldset').css('opacity',1);})
     .blur(function(){$('form#add fieldset')[0].can_change=true;if(!$('form#add fieldset')[0].hovering) $('form#add fieldset').css('opacity',0.5);});
   }
	 $('input.button','tr').css('opacity',0.6);
	 
   var td_edit_cc=null;
   $('td.edit').click(function(){
    if(td_edit_cc==this) return false;

		if(this.id.match(/FL-(.*?)-(\d+)-(mass|x|length)/)){
     idata=this.id.split('-');
     this.defValue=$(this).html();
     $(this).html($('#data_select').html());
		 
     sobj=$('select',this)[0];
     for(s=0;s<sobj.options.length;s++)
      if(sobj[s].value==idata[2])
       sobj.selectedIndex=s;
     sobj.focus();

     $('select',this).blur(function(){
      if($('select',td_edit_cc).val()!=idata[2]){
       $('form#loading_data').append('<input type="hidden" name="'+td_edit_cc.id+'-save" value="'+$('select',td_edit_cc).val()+'" />');
       $(td_edit_cc).html(sobj[sobj.selectedIndex].text.fit_text(18)+' - Save &#187;');
       $('#'+td_edit_cc.id.split('-').splice(0,2).join('-')+'-save')[0].disabled=false;
       $('#'+td_edit_cc.id.split('-').splice(0,2).join('-')+'-tr').css('background','#ffc');
				
 		  }else
       $(td_edit_cc).html(td_edit_cc.defValue);
			 
			td_edit_cc=null;
     });
		}else if(this.id.split('-')[2]=='units'){
     this.defValue=$(this).html();
     $(this).html($('#unit_select').html());
     
     sobj=$('select',this)[0];
     for(s=0;s<sobj.options.length;s++)
      if(sobj[s].value==this.defValue)
       sobj.selectedIndex=s;
     sobj.focus();
				
     $('select',this).blur(function(){
      if($('select',td_edit_cc).val()!=td_edit_cc.defValue){
       $('form#data').append('<input type="hidden" name="'+td_edit_cc.id+'-save" value="'+$('select',td_edit_cc).val()+'" />');
       $(td_edit_cc).html($('select',td_edit_cc).val().replace(/\^(\d)/,"<sup>$1</sup>"));
       $('#'+td_edit_cc.id.split('-').splice(0,2).join('-')+'-save')[0].disabled=false;
       $('#'+td_edit_cc.id.split('-').splice(0,2).join('-')+'-tr').css('background','#ffc');
				
      }else
       $(td_edit_cc).html(td_edit_cc.defValue);
			 
			td_edit_cc=null;
     });
		}else{
     this.defValue=(typeof $(this).attr('title')!='undefined'?$(this).attr('title'):$(this).html());
     $(this).html('<input type="text" id="" name="" value="'+this.defValue+'" class="text" />');
     $('input',this).blur(function(){
      if($('input',td_edit_cc).val()!=td_edit_cc.defValue){
       $(this).parents().find('form').append('<input type="hidden" name="'+td_edit_cc.id+'-save" value="'+$('input',td_edit_cc).val()+'" />');
       $(td_edit_cc).html($('input',td_edit_cc).val().toString().fit_text(10));
       $('#'+td_edit_cc.id.split('-').splice(0,2).join('-')+'-save')[0].disabled=false;
       $('#'+td_edit_cc.id.split('-').splice(0,2).join('-')+'-tr').css('background','#ffc');
 		  }else
       $(td_edit_cc).html(td_edit_cc.defValue.fit_text(10));
 
      td_edit_cc=null;
     })[0].select();
    }
		td_edit_cc=this;
   });
	}
 }
};
$(document).ready(scripts.start);