//var shadowedBorder = RUZEE.ShadedBorder.create({ corner:0, shadow:8, border:0 });
//var centershadowedBorder = RUZEE.ShadedBorder.create({ corner:8, shadow:0, border:1 });
//var noteBorder = RUZEE.ShadedBorder.create({ corner:8, shadow:20, border:1 });
var subDefault = null;
var subLastLoaded;
var subTimer;
var subWriteTimer;
var subData;

$(function(){
	breadcrumb_break();
	button_styler();
	table_styler();
	wire_calendars();
	set_readonly();
	// If "dynamicSubMenu" is set to false (in Nav_DisplayHeaderContents) then don't enabled the menu:
	if(!(typeof(dynamicSubMenu)=='boolean' && dynamicSubMenu==false)){ wire_subnav(); }
	wire_rollovers();
	
	// Wire animations
	animate();
	
	// Wire tooltips
	wire_tooltips();
	
	// "Hot" rows:
	$(".coldrow").each(function() {
		$(this).hover(function(){
				$(this).addClass("hotrow");
			},function(){
				$(this).removeClass("hotrow");
		});
	});
	
	// Shadows:
	$(".note,.wideshadowed").each(function(){
		$(this).wrap("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"shatbl\" style=\"width: 100%\"><tr class=\"shm\"><td></td></tr></table>");
	});
	
	$(".shadowed").each(function(){
		$(this).wrap("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"shatbl\"><tr class=\"shm\"><td></td></tr></table>");
	});
	
	$(".shatbl tr.shm").each(function(){
		$(this).append("<td class=\"shr\"><img src=\""+tplBasePath+"media/shtr.gif\" /></td>");
		$(this).after("<tr><td class=\"shb\"><img src=\""+tplBasePath+"media/shbl.gif\" /></td><td><img src=\""+tplBasePath+"media/shbr.gif\" /></td></tr>");
	});
	
	if(($.browser.msie) && ($.browser.version<7)){
		//window.onload = function(){ RepositionFooter(); }// Fix footer positioning
		$('.am').css('text-align','center');		// Fix text-align:center for IE6 users
		$('img[@src$=.png], #panel').each(function(){if($(this).attr('src').indexOf('/media/')>0){$(this).pngfix();}});
	}
	else { RepositionFooter(); }
});

function RepositionFooter(){
	$('div.foot').css('position','absolute').css('bottom','0px');
}

function animate(type) {
	var blnCont = false;
	$(".animate").each(function() {
		blnCont = true;
		$(this).fadeTo("slow", 0.4, function() { $(this).fadeTo("slow", 1)});
	});
	
	if (blnCont)
		setTimeout('animate()', 2000);
	
}

function wire_tooltips() {
	if ($.Tooltip) {
		$('[@title]').Tooltip({
			track: true,
			delay: 0,
			showURL: false,
			showBody: " - "
		})
	}
}

function wire_rollovers(){
	$('#tbtns a img').each(function(){
		this.onmouseover = function(){ this.src = this.src.replace('.off.','.on.'); };
		this.onmouseout = function(){ this.src = this.src.replace('.on.','.off.'); };
	});
}
function wire_calendars(){
	if (typeof popUpCal != 'undefined') {
		popUpCal.speed='';
		popUpCal.closeAtTop = false;
	}
	$('.calin').each(function(i){
		$(this).attr("readonly","readonly");
		$(this).calendar();
	});
}
function set_readonly() {
	$("input:text").each(
		function() {
			if ($(this).attr("readonly") == true)
				$(this).addClass('readonly');
		}
	);
	$("select.readonly").each(function() {
	    if ($(this).find("option:selected").length > 0)
	        $(this).find("option:not(:selected)").remove()
	});
}
function reset_subnav(){
	if($('#otbllgo a.selected').html()!=subLastLoaded) {
		set_subnav('');
	}
}
function set_subnav(s) {
	if(s==''){ s = subData; }
	$('#oSubNav').fadeOut('fast',function(){ $(this).html(s).toggle(); });
}

function wire_subnav(){
	subLastLoaded = $('#otbllgo a.selected').html();
	$('#oSubNav').hover(
		function(){ clearTimeout(subTimer); },
		function(){ subTimer = setTimeout("reset_subnav()",500);	}
	);
	
	subData = $('#oSubNav').html(); //Save the page nav.
	$('#otbllgo a').hover(function(){
		clearTimeout(subTimer); // Clear any other tab timeouts.
	  clearTimeout(subWriteTimer);
		var self = this;
	  var sMenuHtml = '';
	  var bFound = false;
		$(oAolMenu.menu).each(function(){
			var _Nav = this;
			if(this.tab!=null){
				if(self.innerHTML.toLowerCase()==this.tab.toLowerCase()){
					bFound = true;
					var _Tab = this;
					$(this.links).each(function(i){
						if(this.link!=''){
							if(this.active==1){ sMenuHtml += '<a href="'+this.link+'" class="selected">'+this.name+'</a>'; }
							else { sMenuHtml += '<a href="'+this.link+'">'+this.name+'</a>'; }
							if(i<(_Tab.links.length-2)){ sMenuHtml += '&nbsp;&nbsp;|&nbsp;&nbsp;'; }
						}
					});
					sFunc = (self.className=='selected')? "reset_subnav();" : "set_subnav('"+sMenuHtml+"');";
					sFunc += "subLastLoaded='"+self.innerHTML+"';"
					subWriteTimer = setTimeout(sFunc,200);
				}
			}
		});
		if(bFound==false){ subWriteTimer = setTimeout("set_subnav(\'&nbsp;\');subLastLoaded='"+this.innerHTML+"';",200); }
	},function(){
		clearTimeout(subTimer);
		clearTimeout(subWriteTimer);
		subTimer = setTimeout("reset_subnav()",500);
	});
}

// Seperates the subnav links:
function breadcrumb_break() {
	a = $('.bct a');
	a.each(function(i){
		if(i!=(a.length-1)){ $(this).after('&nbsp;&nbsp;|&nbsp;&nbsp;'); }
	});
}

// Applies classes to input buttons:
function button_styler(){
	$('input:button,input:submit').each(function (o){
		if(this.className==''){
			switch(this.value) {
				case 'PREVIOUS': this.className='mbtnp'; break;
				case 'NEXT': this.className='mbtnn'; break;
				default: this.className = 'mbtn';
			}
		}
	});
}

function table_styler(){
	// Apply "last" class to last rows:
	o = $('table.filters tbody tr');
	o.each(function(i){
		if(this.className=='subhd') {
			if(i>0){ $(o[i-1]).addClass('last'); }
			if((i+1)<o.length){ $(o[i+1]).addClass('first'); }
		}
	});
	
	$('table tbody tr:last-child').addClass('last');
	$('.filters tbody td:first-child').addClass('begin');
	$('.filters tbody td:last-child').addClass('end');
	$('.filters .colhd th:last-child').addClass('end');
	
	// Apply alternate row class:
	$('table.altr tr:odd').each(function(){
		if($(this).attr("class")!='void'){ $(this).addClass('alt'); }
	});
	
	// Apply alternate list item class:
	$('ul.altl li:odd').addClass('alt');
}
