//add event generic function
function addEvent(elm, evType, fn, useCapture)
	{
			if(elm.addEventListener)
			{
				elm.addEventListener(evType, fn, useCapture);
				return true;
			}
			else if (elm.attachEvent)
			{
				var r = elm.attachEvent('on' + evType, fn);
				return r;
			}
			else
			{
				elm['on' + evType] = fn;
			}
	}
//Pop up window scripts
var newWindow = null;

function popUpWin(url, type, strWidth, strHeight){
        closeWin();
        if (type == "fullScreen"){
                strWidth = screen.availWidth - 10;
                strHeight = screen.availHeight - 160;
        }
        var tools="";
        if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=20,left=0, status=1, statusbar=1"; 
        if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+ strHeight+",left=0,top=20, status=1, statusbar=1"; 
        if (type == "noscroll") tools = "toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+ strHeight+",left=0,top=20, status=1, statusbar=1"; 		
        newWindow = window.open(url, 'newWin', tools);
        newWindow.focus();
}

function closeWin(){
        if (newWindow != null){
                if(!newWindow.closed)
                        newWindow.close();
        }
}

// end of intermediaries - what we do page functions


//---------------------------------------------------------------------------

function doJQueryFunctions()
{

	/*
	while I hate browser sniffing, Safari 2 does 
	not have full jQuery support, so we need to 
	disable some effects
	*/
	var unsupported_safari = (((navigator.userAgent.indexOf("Version/3")==-1)&&(navigator.userAgent.indexOf("Version/4")==-1))&&($.browser.safari));
	
	/* footer show/hide stuff */
	function footerExpandCollapse()
	{
		if(!unsupported_safari){
		
		//hide 'Explore Nationwide' for all pages
		$("#footer .footer-inner, #footer-homepage.collapse .footer-inner").hide();
		//hide 'Explore our products' but *only* on the home page
		$("#footer .footer-inner").hide();
		
		$("#footer h4 a").click(function(e){
				$("#footer .footer-inner").slideToggle('slow');
				return false;
			});
		$("#footer-homepage h4 a").click(function(e){
				$("#footer-homepage .footer-inner").slideToggle('slow');
				return false;
			});
		}
	}
	footerExpandCollapse();

	function highlightLinks(){
		$("body").append("<div id=\"highlightlink\"></div>")
		$("#highlightlink").css({
			"border-bottom":"5px solid yellow",
			"position":"absolute"
		});
		$("a:has(b)").each(function(){
			$(this).mousedown(function(){
				$("#highlightlink").css({        
					"display":"none" 
				});
			}).focus(function(){
				$("#highlightlink").stop(true,false).animate({        
					opacity: 1      
				},0).css({
					"width":$(this).find("b").width(),
					"height":$(this).find("b").height(),
					"left":$(this).find("b").offset().left,
					"top":$(this).find("b").offset().top
				}).animate({        
					opacity: 0      
				}, 3000 );	
			});	
		})
	}
	highlightLinks()
	
	
	function setUpNewsTicker(){
		
		$('#newsMarquee').gradient({
			from: '9da7cb',
			to: '03468a',
			direction: 'horizontal'
		});
		
		var activeLink = 1;
		
		
		function runNewsItems(){
			//alert("activeLink:" + activeLink);	
			$("#newsMarquee a:not(eq(" + activeLink + "))")
			.hide();
			
			$("#newsMarquee a:eq(" + activeLink + ")")
			.css({
				"marginLeft":"700px"
			})			
			.animate({
				opacity : "1"	
			},0)
			.show()
			.animate({
				marginLeft : "1.3em",
				opacity : "1"
						
			},500);
			
							
			activeLink++		
			var aLength = $("#newsMarquee a").length
			if (activeLink == aLength){
				activeLink = 0
			}
			setTimeout(runNewsItems,4000)
		}
		
		var t;
		$("#newsMarquee a:not(:first)").hide();
		var t = setTimeout(runNewsItems,4000)
	
	}
	setUpNewsTicker();
	
	
	
	(function popUpButtons(){
		if(!window.opener){
			$("div.helpMainContent").append("<p><a href=\"/commercial/\">Continue to the Commercial homepage</a></p>");
			$("div.helpContent input").remove();
		}
	})();
	
	function photoLibraryPreview(){		
		if($("div#photolib").length>0){ //on a photo page
			$("body").append("<div id=\"photoPreview\"></div>");
		}
		$("div#photolib div.thumb a img").click(function(e){
												
												var left = $("#contentPrimary").position().left;
																					
											    $("div#photoPreview").hide();
												$("div#photoPreview").css("left", left);
												$("div#photoPreview").html("");
												var imgHref = $(this).parent("a").attr("href");
												$("div#photoPreview").html("<a href=\"#\" id=\"photoClose\"><p>Close</p></a><img src=\"" + imgHref + "\" />");
												
												var previewPos = e.pageY-135;											
														
												$("div#photoPreview").css("top",previewPos);//135);
												$("div#photoPreview").click(function(){$("div#photoPreview").hide();return false;});
												$("div#photoPreview").fadeIn("slow");
												return false;
										 	});
	}
	photoLibraryPreview();
	
}
addEvent(window, 'load', doJQueryFunctions, false);
