/* ------------------------------------------------------------
 * PROJECT        : 
 * FILENAME       : jqload.js
 * ------------------------------------------------------------
 * LAST UPDATED   : 14 May 2010
 * ------------------------------------------------------------
 * AUTHOR(S)      : Kevin Scholl (kevin@ksscholl.com)
 * ------------------------------------------------------------
 * NOTE(S)        : 
 * ------------------------------------------------------------ */

$(document).ready(function(){
 
	// adjust margin(s)
  $(""
		+ "div:last-child,"
		+ "fieldset:last-child,"
		+ "form:last-child,"
		+ "img:last-child,"
		+ "li:last-child,"
		+ "ol:last-child,"
		+ "table:last-child,"
		+ "ul:last-child,"
		+ "").css("margin-bottom","0");
	
	$("p:last-child").not("div.topic p:last-child").css("margin-bottom","0");
	
	$("div.withBorder").parent().find("div.withBorder:first").css("margin-top","10px");
	$("div.withBorder").parent().find("div.withBorder:last").css("margin-bottom","10px");
	
	// hide topic sections
	$("div.topic h4").click(function() {
		$(this).parent("div.topic").find("*:not(h4, p.intro)").slideToggle(function(){
			if ($(this).is(":visible")) {
				$(this).show();
				}			
			});
		});
		
	$("div.topic h2").click(function() {
		$(this).parent("div.topic").find("*:not(h2, p.intro)").slideToggle(function(){
			if ($(this).is(":visible")) {
				$(this).show();
				}			
			});
		});	
	
	// rounded corners
	var BG_CORNERS = "#F2F2F2"; // page background color
	$(".errorMsg, .systemMsg, .warningMsg")     .cornerz({background:"" + BG_CORNERS + "", radius:8});
	$("fieldset:not(.tabHeading, .tabHeadingFinance, .tools)")      .cornerz({background:"" + BG_CORNERS + "", radius:10});
	$("fieldset.tabHeading")                    .cornerz({background:"" + BG_CORNERS + "", radius:10, corners:"tr bl br"});
	$("fieldset.tabHeadingFinance")             .cornerz({background:"#E9E9DC" + BG_CORNERS + "", radius:10, corners:"tr bl br"});
	$("fieldset.homepage")                      .cornerz({background:"#FFF", radius:25, corners:"tl bl br"})
	                                            .cornerz({background:"#FFF", radius:25, corners:"tr"});
	$("fieldset.product1")                      .cornerz({background:"#EEE", radius:60, corners:"tl bl br"})
	                                            .cornerz({background:"#EEE", radius:60, corners:"tr"});
	$("fieldset.product2")                      .cornerz({background:"#FFF", radius:60, corners:"tl bl br"})
	                                            .cornerz({background:"#FFF", radius:60, corners:"tr"});
	$("fieldset.product3")                      .cornerz({background:"#FFF", radius:30, corners:"tl bl br"})
	                                            .cornerz({background:"#FFF", radius:30, corners:"tr"});
	$("fieldset.product4")                      .cornerz({background:"#FFF", radius:60, corners:"tl bl br"})
	                                            .cornerz({background:"#FFF", radius:60, corners:"tr"});
	$("fieldset.product5")                      .cornerz({background:"#FFF", radius:60, corners:"tl bl br"})
	                                            .cornerz({background:"#FFF", radius:60, corners:"tr"});
	$("fieldset.tools")                         .cornerz({background:"#FFF", radius:25, corners:"tl bl br"})
	                                            .cornerz({background:"#FFF", radius:25, corners:"tr"});
	$("div.introAd")                            .cornerz({background:"#FFF", radius:8});
	$("a.introAd")                              .cornerz({background:"#FFF", radius:6});
	$("div.callout")                            .cornerz({background:"#FFF", radius:10});

	// data grids and tables
	// alternate row coloring and row/column hover highlighting
	$("table.dataGrid tbody").each(function(){
		$("tr:nth-child(even)").addClass("alt");
		$("tr:last-child").addClass("lastRow");
		$("td").hover(
		  function() {$(this).addClass("over"); },
			function() {$(this).removeClass("over"); }
			);
		});
	
	
	$("table.dataGridGallery tbody").each(function(){
		$("tr:nth-child(even)").addClass("alt");
		$("tr:last-child").addClass("lastRow");
		$("td").hover(
		  function() {$(this).addClass("over"); },
			function() {$(this).removeClass("over"); }
			);
		});
		
	
	$("table.infoTable tbody").each(function(){
		$("tr:nth-child(even)").addClass("alt");
		$("tr:last-child").addClass("lastRow");
		$("tr").hover(
		  function() {$(this).addClass("over"); },
			function() {$(this).removeClass("over"); }
			);
		});	
		
	$("table.mColGrid tbody").each(function(){
	$("tr:last-child").addClass("lastRow");
	});

	// dropdown navigation
	$("ul.tabBar li").hover(
		function () {
			$(this).find(" > ul").delay(400).slideDown("fast");
			}, 
		function () {
  	  $(this).find(" > ul").clearQueue().fadeOut("fast");
			}
		);
	// add subnavigation indicator
	$("ul.tabBar li:has(ul)").find("> a")
	  .css("padding-right","20px")
	  .click(function () {
			return false;
			})
	  .append("<span class=\"hasChildren\">&nbsp;</span>");

  // account for hover tab status
	$("ul.tabBar li").has("ul").hover(
		function () {
			$(this).find("> a").not(".current").css("background","#5A8787 url(/images/bg_tab_hover.png) center bottom repeat-x");
			$(this).find("> a span.hasChildren").css("background-image","url(/images/icon_subnav_bullet_hover.png)");
			},
		function () {
			$(this).find("> a").not(".current").css("background","#436570");
			$(this).find("> a span.hasChildren").css("background-image","url(/images/icon_subnav_bullet.png)");
			});
	
	// display ordinal linked images on subnav
	$("ul.ordinal li").each(function(){
			$(this).prepend("<span><img src=\"/images/bullet_ord_" + ($(this).index() + 1) + ".png\" width=\"26\" height=\"26\" border=\"0\" /></span>")
			  .find("span")
				  .css("cursor","pointer")
					.click(function () {
					  //alert($(this).siblings("a").attr("href"));
					  $("#varLoad")
        		  .html("<div class=\"grid4\"><p style=\"margin: 0 10px;\">Loading...</p></div>")
							.load($(this).siblings("a").attr("href") + " #varLoad > *", function(response, status, xhr) {
							//alert(xhr.status);
							if (status == "error" || xhr.status == 0) {
								var msg = "Sorry, but there was an error: ";
								$("#varLoad").html("<div class=\"grid4\"><p style=\"margin: 0 10px; color: #C33\"><strong>" 
								  + msg 
									+ xhr.status + " " 
									+ xhr.statusText 
									+ "</strong></p></div>");
								}
							else {
								$("#varLoad").hide().fadeIn(1000);
								}
							});
						return false;
					  });
		});

  // substitute asterisks for sensitive data
	$(".hideText").each(function(){
		var i = "";
		for (j = 0; j < $(this).text().length; j++) { i = i + "*"; }
		$(this).text(i);
		});

	// form field focus
 	$("input:file, input:password, input:text, select, textarea").not("[readonly]").toggleActive();
 	$("input:text[value*='http'], #txtSearch").toggleActive({
	  toggleVal : true
		});

	// position radio buttons and checkboxes
	$("input:radio").addClass("radioBtn");
	$("input:checkbox").addClass("checkBox");

  // open external links in new browser window
	$("table.dataGridGallery").find("a[href^='http'], a[href^='https']").click(function() {	  
		popWindow($(this).attr("href"),"EXTERNAL",null,null,"no","yes");
		return false;
		});

  });
