jQuery(document).ready(function($){



$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	    
	    
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 
		
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			




	function readyFormElements() {
	
		$(".swap").hover(
			function() {
				$(this).css({ "background-position" : "0px -20px" , "cursor" : "pointer" });
			},
			function() {
				$(this).css({ "background-position" : "0px 0px" });
			}
		);

		$(".swap").click(
			function() {
				$(this).blur().css({ "background-position" : "100% 0px" });
			}
		);

		numOfPresets = $("input.preset").size();
		
		for ( var i = 1 ; i <= numOfPresets; i++) {
			$("input.preset:eq("+ (i - 1) +")").attr( "title" , $("input.preset:eq("+ (i - 1) +")").val() )
		}
	
	    
		$("input.preset").focus(
			function() {
				thisTitle = $(this).attr("title")
				thisValue = this.value;
				if ( this.value == thisTitle) {
						this.value = "";
						this.style.fontStyle = "normal";
						this.style.color = "#000"
				} else {
					return false
				}		
			}
		);

		$("input.preset").blur(
			function() {
				thisTitle = this.title;
				thisValue = this.value;
				if ( thisValue == "") {
					$(this).val(thisTitle);
					this.style.fontStyle = "italic";
					this.style.color = "#666";
				}

			}
		);


	}

	readyFormElements();

	function bannerRotator() {
	
		var numOfBanners = $("div.first-read a").size();

        //alert("number of banners= " + numOfBanners);

		//set nav
		$("div.first-read a:eq("+( numOfBanners - 1 )+")").after('\r<ol>\r\t<li class="thumb t01 on" title="1"><a href="javascript:void(0)" title="1"><img src="images/first-read/thumbnail01.gif" width="64" height="49" alt="" /></a></li>\r</ol>');
		// generate additional tabs
		for( var i = 1; i < numOfBanners; i++ ) {
			$("div.first-read ol li:eq("+ ( i - 1 ) +")").after('<li class="thumb t0'+( i + 1 )+'" title="'+( i + 1 )+'"><a href="javascript:void(0)" title="'+ ( i + 1 )+'"><img src="images/first-read/thumbnail0'+ ( i + 1 ) +'.gif" width="64" height="49" alt="" /></a></li>')
			//$("div.first-read a:eq(" + ( i - 1 ) + ")").addClass("banner bn0"+ ( i ) +"").attr("title" , ( i ) );
		}
		// add classes to banners
		for( var i = 0; i <= numOfBanners; i++ ) {
			$("div.first-read a:eq(" + ( i - 1 ) + ")").addClass("banner bn0"+ ( i ) +"").attr("title" , ( i ) );
		}		
	

		$("div.first-read ol li").hover(
			function() {
				$(this).addClass("hovered")
			},
			function() {
				$(this).removeClass("hovered")
			}
		);


		//$(".bn01").fadeIn(); //commented out by AJL 11/7/2009
		$(".bn01").show();
		
		var i = 0

		function rotationfade() {

			if( i < ( numOfBanners - 1 ) ) {

	     		$(".bn0"+ (i + 1) +"").fadeOut("slow",
					function() {
		 	 			//$(".bn0"+ (i + 2) +"").fadeIn(); //commented out by AJL 11/7/2009
		 	 			$(".bn0"+ (i + 2) +"").show();
						$("div.first-read ol li").removeClass("on");
						$(".t0"+ (i + 2) +"").addClass("on");
		 				i = i + 1;
	        		}
	     		);
	     
			} else if ( i = ( numOfBanners - 1 ) ) {
	    		$(".bn0"+ (i + 1) +"").fadeOut("slow",
					function() {
		 				//$(".bn01").fadeIn(); //commented out by AJL 11/7/2009
		 				$(".bn01").show();
						$("div.first-read ol li").removeClass("on");
						$(".t01").addClass("on");
					}
	     		);
	    		i = 0
			}

		}

		var startRotation = setInterval( rotationfade , 4500);

		$("div.first-read ol li").click(
			function() {
				clearTimeout(startRotation);
				//alert(i);var
				$("div.first-read ol li").removeClass("on");
				$(this).addClass("on");
				thisLink = $(this).attr("title");
				$("div.first-read a.banner").fadeOut(100);
				//$(".bn0"+ thisLink +"").fadeIn(1000); //commented out by AJL 11/7/2009
				$(".bn0"+ thisLink +"").show();
			}
		);	
			
 
	}

	bannerRotator();
	
	function highlight_nav() {

		headerValue = $("h2.PageTitle").html();

		var numOfNavItems1 = $("ul.util_nav li").size();
		var numOfNavItems2 = $("ul.main_nav li").size();

		for ( var i = 1; i <= numOfNavItems1; i++ ) {
			
			var linkTitle = $("ul.util_nav li:eq("+( i - 1 )+") a").attr("title");

			if ( headerValue == linkTitle  ) {
				$("ul.util_nav li:eq("+( i - 1 )+") a").attr("class", "on")
			}
		}
		
		for ( var i = 1; i <= numOfNavItems2; i++ ) {
			
			var linkTitle = $("ul.main_nav li:eq("+( i - 1 )+") a").attr("title");
			
			if ( headerValue == linkTitle  ) {
				$("ul.main_nav li:eq("+( i - 1 )+") a").attr("class", "on")
			}
		}

	}

	highlight_nav();

});
