/* --------------- */
/* Startup Scripts */
/* --------------- */
function configScripts() {
	// Defined on a "per-page" basis
}

function configButtons()
{
	$(".btn-close").livequery( 'click', function(event) {
		// Hide the nearest parent or, if specified, the nearest id from the 'rel' attribute
		if ( $(this).attr("rel") )
			$(this).closest("#"+$(this).attr("rel")).fadeOut("medium");
		else
			$(this).parent().fadeOut("medium");
		
		// Allow the closing element to send an ajax request before dieing
		if ( $(this).attr("callback") )
			$.get( $(this).attr("callback") );
	});
	
	$(".btn-print").livequery( 'click', function(event) {
		// Print the entire window or the specific frame specified in the 'rel' attribute
		if ( $(this).attr("rel") ) {
			$("#"+$(this).attr("rel")).each( function() {
				this.contentWindow.focus();
				this.contentWindow.print();
			});
		} else {
			$(window).print();
		}
	});
}

/* --------- */
/* Comm Tabs */
/* --------- */
(function($) {
	$.commTabs = {
	  init : function() {
		if ( $.runOnce("commtabs") )
		{
			$("#comm-menu a[rel=tab]").livequery( 'click', function(e) {
				if ( $(this).closest("li").hasClass("active") )
					return e.preventDefault();
					
				$.commTabs.loadTab( $(this), e );
				
				$("#comm-menu li").removeClass("active");
				$(this).closest("li").addClass("active");
			});
		}
		
		this.initTabs();
	  }, // END .init()
	  
	  initTabs : function() {
		var currTab = $("#comm-menu li.active a[rel=tab]").attr("href");
		$("#comm-internal div").not("#comm-internal div"+currTab).hide();
	  }, // END .initTabs()
	  
	  loadTab : function( elmt, event ) {
		var tabid = elmt.attr("href");
		$("#comm-internal div:visible").fadeOut("medium", function() {
			$("#comm-internal div"+tabid).fadeIn("medium");
		});
		
		event.preventDefault();
	  } // END .loadTab( element)
	};
	
	$(document).ready(function(){ $.commTabs.init(); });
})(jQuery);


/* ----------- */
/* Comm iFrame */
/* ----------- */
jQuery(function($) {
	$.fn.commIFrame = function() {
		if (!this)
			return false;
			
		return this.each( function() {
			$("#commIFrame").attr("src", $(this).attr("href"));
			return true;
		});
	};
	
	// Initialize the iframe
	if ( $.runOnce("commIFrame") )
	{
		$("body").append( '<iframe id="commIFrame" width="0" height="0" src="javascript:\'\'"></iframe>' );
		$("#commIFrame").hide();
	}	
});
		
function resizeNav() {
  var navFill = document.getElementById('navfiller');
  var navContent = document.getElementById('navcontent');
  var mainContent = document.getElementById('maincontent');
  
  if ( !navFill || !navContent || !mainContent )
	return;
	
  var navHeight = navContent.clientHeight;
  var mainHeight = mainContent.clientHeight;
  var hdiff = mainHeight - navHeight;
  
  if ( navFill )
  {
	if ( hdiff > 0 )
	{
		hdiff = hdiff + 20;
		navFill.style.height = hdiff + "px";
	}
	else if ( hdiff < 0 && navFill.style.height != "" )
	{
		navFill.style.height = "";
		// Recursively call us to make sure we are properly set
		// TODO: Figure out our real difference by adding our set fill height to the offset
		//       If that is positive, set our new height to that difference (need REGEXP)
		resizeNav();
	}
  }
}

$(document).ready(function() {
	if ( $.runOnce("main") )
	{
		configScripts();
		configButtons();
		resizeNav();
	}
});


function openwin(page) {
		var win = window.open(page,"happy","location=no,scrollbars=no,menu=no,personalbar=no,resizable=no,width=800,height=600");
		win.focus();
}

// Main Page scripts
function ContactsUpdate(to)
{
	if (to.value != '') {
		window.location = 'contact.cgi?c=' + to.value;
	}
}

// Community Scripts
function GettingIn(location,x,y)
{
	var win = open("","newwin","location=no,scrollbars=no,menu=no,personalbar=no,resizable=yes,width="+x+",height="+y);
	win.document.writeln("<html><head><title>Where are my numbers?</title></head><body bgcolor=#D9DBE1 text=black leftmargin=0 topmargin=0><center>");
	win.document.writeln("<font size=+1>This is a sample statement that shows you where your numbers are located:<br><br><img src=" + location + "><br>");
	win.document.writeln("<input type=button value=\"Close Window\" onclick=window.close()></body></html>");
	win.focus();
}

// Newsletter scripts
function ShowNews()
{
	var r = document.forms[0].id;
	var news_id = 0;

	if (!r.length) {
		if (r.checked) {
			news_id = r.value;
		}
	} else {
		for(i=0;i<r.length;i++) {
			if (r[i].checked) {
				news_id = r[i].value;
			}
		}
	}

	if (news_id) {
		var win = window.open("newsletter.cgi?popup=1&id="+news_id,"news","location=no,scrollbars=yes,menu=no,personalbar=no,resizable=yes,width=700,height=600");
		win.moveTo(screen.Width/2-350,screen.Height/2-300);
		win.focus();
	}
}

function SmartStreet(func) {
	var win = window.open("smartstreet.cgi?"+func, "sswin", "location=no,scrollbars=yes,menu=no,statusbar=no,personalbar=no,resizable=no,width=550,height=550");
	win.moveTo(screen.Width/2-275,screen.Height/2-275);
	win.focus();
}