// tools.js
// jquery version

var ie6 = ($.browser.msie && jQuery.browser.version == "6.0");
var ie7 = ($.browser.msie && jQuery.browser.version == "7.0");
var isPreview = false;
var previewString = "";


var common = ({
		init: function(){
         var previewRequest = $.get('/include/environment.jsf', {reqVar: 'previewString'}, function(ps){
            isPreview = window.location.href.indexOf(ps) == -1 ? false : true;
            previewString = ps;
            
   			common.nav();
   			common.buttons();
   			common.externals();
   			common.searches();
   			common.promos();
   			common.preview();
         });
		},
		nav: function(){

			$('#navigation li.nav')
				.css( {backgroundPosition: "0 0"} )
				.mouseover(function(){
					$(this).stop().animate(
						{backgroundPosition:"(-540px 0)"}, 
						{duration:500})
					})
				.mouseout(function(){
					$(this).stop().animate(
						{backgroundPosition:"(0 0)"}, 
						{duration:500})
					})


		},
		buttons: function(){
			
			$('.promo-button').bind('mouseenter',function(){
				$(this).children().css({
						backgroundPosition: '0 -20px'
				});			
			});
			
			$('.promo-button').bind('mouseleave',function(){
				$(this).children().css({
						backgroundPosition: '0 0'
				});			
			});
			
			
				
		},
		externals: function(){
			var exts = $("a.ext");
			exts.each(function(){
				$(this).attr("target","_blank");
			})
		},
		searches: function(){
			
			$("#locate-this").bind("focus",function(){
				if($(this).attr("value") == "Enter ZIP"){
					$(this).attr("value","")
				}
			}).bind("blur",function(){
				if($(this).attr("value") == ""){
					$(this).attr("value","Enter ZIP")
				}			
			});



			$("#search-box input[type='text']").bind("focus",function(){
				if($(this).attr("value") == "Enter Keyword"){
					$(this).attr("value","")
				}
			}).bind("blur",function(){
				if($(this).attr("value") == ""){
					$(this).attr("value","Enter Keyword")
				}			
			});

		},
		promos: function(){
			// set up promo tracking via GA
			var promos = $(".promolink");
			
			promos.each(function(){
				$(this).bind("click",function(){
					var GAcategory = "Promo";
					var GAaction = "click";
					var GAlabel = $(this).attr("href");
					pageTracker._trackEvent(GAcategory, GAaction, GAlabel);
					return true;
				});
				
			});
		},
       preview: function(){
           
           //var previewRequest = $.get('/include/environment.jsf', {reqVar: 'previewString'}, function(previewString){
			   //var isPreview = window.location.href.indexOf(previewString) == -1 ? false : true;
			   if(!isPreview){return;}
			   
			   var links = $("a, option.ap, area");
			   
			   var warning = "<div id=\"preview-warning\"><h2>WARNING! This site is a preview.</h2><span>This content has not yet been published.</span><a href=\"#\"><img src=\"/images/preview/close.gif\" /></a></div>";
			   $("#container").append(warning);
			   $("#container").css({
				  paddingTop: "90px"
			   });
			   var exclusions = ['.jpeg','.jpg','.png','.gif','.pdf','compare'];
			   
			   links.each(function(){
				  var isHref = true;
				  var cHref = $(this).attr("href");
				  if(cHref == null){
					 // if we don't have a href, assume a dropdown value
					 cHref = $(this).attr("value");
					 isHref = false;
				  }
				  if(cHref){
					  for(i=0;i<exclusions.length;i++){
						 if(cHref.indexOf(exclusions[i]) != -1){return};
					  }
					  
					  if($(this).attr("class").indexOf("ext") == -1 && cHref != "#"){
						 if(isHref){
							if(cHref.lastIndexOf("/") != (cHref.length-1)){
							   $(this).attr("href", cHref + "/" + previewString);
							}else{
							   $(this).attr("href", cHref + previewString);
							}
						 }else{
							if(cHref.lastIndexOf("/") != (cHref.length-1)){
							   $(this).attr("value", cHref + "/" + previewString);
							}else{
							   $(this).attr("value", cHref + previewString);
							}
						 }
					 }
				  }
			   });
			   
			   $("#preview-warning a").click(function(){
				  window.close();
			   });
		   //});
           
        }
});

$(document).ready(function(){
	common.init();
});