$(window).load(function () {
	$.menuOff();
	$.adminLoad();
	$.menuHover();
	$('body').styleLoad();
	$.tableStyle(); 
	$.menuTree();
	$.menuList();
	$.fieldHelp(); 
	   });

//ADMIN
jQuery.adminLoad = function () { $('div.rowadmin').each(function() { $(this).css({width: ($(this).widthPlus('OUTER') - 18) + 'px'}); }); }

//MENU
jQuery.menuOff = function () { $('li.dead').children().css({'opacity':0.5});    }

jQuery.fn.styleLoad = function () { 
	$(this).fieldFile();	
	$(this).fieldMin();
	$(this).fieldLabel();     }	
	
jQuery.fn.fieldLabel = function () { 
	$(this).find('input.fieldlabel, textarea.fieldlabel').each(function() {
		var $label = $(this).val();
		$(this).focus(function() { if ($(this).val() == $label) { $(this).val(''); } });
		$(this).blur(function() { if ($(this).val() == '') { $(this).val($label); } });	    });     }

jQuery.menuHover = function () { 
	$('div.pagehead ul.menunav').each(function() { 
		var $li = $(this).children('li:has(ul)');
		$li.mouseenter(function() { $(this).children('ul').stop(true, true).slideDown('fast'); });
		$li.mouseleave(function() { $(this).children('ul').stop(true, true).slideUp('fast'); });
		var $last = $(this).children('li:last-child');
		if ($last.has('ul')) { 
			var $width = $last.children('ul').widthPlus('BOX');
			var $left = $('div.pagehead ul.menunav').position().left + $last.position().left;
			var $space = $('div.pagehead').widthPlus('INNER');
			if ($left + $width > $space) { 
				$offset = $width - ($space - $left) + 38; 
				$last.children('ul').css({marginLeft: '-' + $offset + 'px'});     }     }     });     }
			
jQuery.menuTree = function () {	
	$('ul.menutree input.button').live('click', function() {
		var $ul = $(this).parents('div.celltree').eq(0).next('ul');
		if ($(this).hasClass('buttonexpand')) { $ul.show('fast'); $(this).classSwitch('buttoncollapse', 'buttonexpand'); }												   
		else if ($(this).hasClass('buttoncollapse')) { $ul.hide('fast'); $(this).classSwitch('buttonexpand', 'buttoncollapse'); }     });     }
		
jQuery.menuList = function () {	
	$('dl.menulist > dt').live('click', function() {
		var $dd = $(this).next('dd');
		if ($dd.is(':hidden')) { $dd.slideDown('fast'); }
		else { $dd.slideUp('fast'); }     });     }	
			
//ELEMENT
jQuery.fn.fieldFile = function () {	
	$(this).find('input[type=file]').each(function() {
		$(this).wrap('<div class="cell cellfile"></div>');
		$(this).css({opacity: 0});
		if ($(this).is('.fieldimg')) { var $valid = 'img'; }
		else if ($(this).is('.fielddoc')) { var $valid = 'doc'; }
		$(this).parent('div.cellfile').append('<input class="field fieldtext fieldmid valid' + $valid + '" type="text" value="Browse . . ." /><input class="button buttonfile" type="button" value="find" />');
		$('body').delegate('input[type=file]', 'change', function() { 
			if ($(this).val()) { $(this).siblings('input[type=text]').val($(this).val()); }
			else { $(this).siblings('input[type=text]').val('Browse . . .'); }     });     });     }
			
jQuery.fieldHelp = function () {
	$('input.buttonhelp').live('click', function() {
		var $div = $(this).next('div.cellhelp');
		var $pos = $(this).offset();
		if ($(this).parents('div.win')) { var $scroll = $(window).scrollTop(); } else { var $scroll = 0; }
		$div.css({'left':$pos.left, 'top':$pos.top - $scroll });
		$('div.cellhelp').hide();
		$div.show('fast');     })
	$('div.cellhelp').live('mouseleave', function() { $('div.cellhelp').hide('fast'); })     }	
		
jQuery.fn.fieldMin = function () {
	$(this).find('select.fieldmin').each(function() {
		if ($(this).val()) { $selected = $(this).children('option:selected'); } else { $selected = $(this).children('option:first'); }
		$(this).wrap('<div class="cell cellselect" style="margin: ' + $(this).cssMargin() + ';"></div>');
		$(this).after('<span>' + $selected.text() + '</span>');
		$(this).css({'left':'0', 'margin':'0', 'opacity':0, 'position':'absolute', 'top':'0', 'z-index':'2'});   });    }
		
jQuery.tableStyle = function () { $('td').each(function() { if ($(this).text() == '&nbsp;' || $(this).text() == 'N/A') { $(this).classSwitch('none', ''); } }); }
