oPopupModule = {
	oPopupClasses:{sm:"popup-sm",med:"popup-med",lg:"popup-lg"},
	oPopupSizes:{sm:"height=400,width=500",med:"height=600,width=550",lg:"height=620,width=780"},
	oWindowAttrs:"status=1,tollbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1",
	oPopupOffset:{top:"25",left:"25"},

	init:function() {
		jQuery.each(this.oPopupClasses, function(key, value) {
			jQuery("a." + value).click( function(){
				var top = oPopupModule.oPopupOffset.top;
				var left = oPopupModule.oPopupOffset.left;
				oPopupModule.oPopupOffset.top = eval(top) + 25;
				oPopupModule.oPopupOffset.left = eval(left) + 25;
				var parameters = oPopupModule.oWindowAttrs + ",top=" + top + ",screenY=" + top + ",left=" + left + ",screenX=" + left + "," + oPopupModule.oPopupSizes[key];
				window.open(this.href, "popup", parameters).focus();
				return false;
			});
		});
	}
};

oGlossary = {
	oOpenLetter:null,
	
	init:function() {
		jQuery('div.letter:gt(0)').hide();
		this.oOpenLetter = jQuery('div.letter:eq(0)');
		jQuery('div.letter:eq(0)').addClass("setUI");
		var sInitialLetter = jQuery(this.oOpenLetter).attr("id");
		jQuery('ul#glossaryNav > li > a[@href^="#' + sInitialLetter + '"]').addClass("active");
		//bind toggle function to the click of the element
		jQuery('ul#glossaryNav > li > a').click( function() {
			jQuery('ul#glossaryNav > li > a.active').removeClass("active");
			var sURL = this.href;
			jQuery(this).addClass("active");
			var sLetter = sURL.split("#");
			if (oGlossary.oOpenLetter.attr("class") == jQuery('div#' + sLetter[1]).attr("class")) { return false; }
			oGlossary.oOpenLetter.slideUp("normal").removeClass("setUI");
			jQuery('div#' + sLetter[1]).slideDown("normal").addClass("setUI");
			oGlossary.oOpenLetter = jQuery('div#' + sLetter[1]);
			return false;
 		});
	}
};

oFormValidator = {
	oErrorMessage:'<div class="bubbleText"><p>\241Uy!<p>Olvidaste algo.</p><p>Vuelve a intentarlo.</p><a href="#" class="errorCloseBtn"></a></div>',
	oHasError:null,
	
	init:function() {
		jQuery.preloadImages('http://www.nationwide.com/locator/images/bgErrorBubbleLeft.gif','http://www.nationwide.com/locator/images/bgErrorBubbleRight.gif');
		jQuery('form').submit(function() {
			jQuery('.errorSubmit').remove();
			var oForm = jQuery(this).addClass('submit');
			var oFormSubmit = jQuery('form.submit > span > input.goBtn');
			jQuery('form.submit > .req').each(function() {
				if (typeof jQuery(this).attr('value') == 'undefined' && !oFormValidator.oHasError) {
					jQuery('<div></div>').addClass('errorSubmit')
					.append(oFormValidator.oErrorMessage)
					.insertAfter(jQuery(oFormSubmit));
					oFormValidator.oHasError = 1;
				}
			});
			if (oFormValidator.oHasError) {
				var oFormButton = jQuery('form.submit > span > input.goBtn');
				var leftOffSet = oFormValidator.findLeft(oFormButton.attr('class'));
				var rightOffSet = oFormValidator.findRight(oFormButton.attr('class'));
				var topOffSet = oFormValidator.findTop(oFormButton.attr('class'));
				var displayType = oFormValidator.findDisplay(oFormButton.attr('class'));
				if (jQuery.browser.msie) {
					leftOffSet = oFormValidator.findIELeft(oFormButton.attr('class')) != 0 ? oFormValidator.findIELeft(oFormButton.attr('class')) : leftOffSet;
					rightOffSet = oFormValidator.findIERight(oFormButton.attr('class')) != 0 ? oFormValidator.findIERight(oFormButton.attr('class')) : rightOffSet;
					topOffSet = oFormValidator.findIETop(oFormButton.attr('class')) != 0 ? oFormValidator.findIETop(oFormButton.attr('class')) : topOffSet;					
				}
				else if (jQuery.browser.safari) {
					leftOffSet = oFormValidator.findMacLeft(oFormButton.attr('class')) != 0 ? oFormValidator.findMacLeft(oFormButton.attr('class')) : leftOffSet;
					rightOffSet = oFormValidator.findMacRight(oFormButton.attr('class')) != 0 ? oFormValidator.findMacRight(oFormButton.attr('class')) : rightOffSet;
					topOffSet = oFormValidator.findMacTop(oFormButton.attr('class')) != 0 ? oFormValidator.findMacTop(oFormButton.attr('class')) : topOffSet;					
				}
				
				if (topOffSet) { jQuery('div.errorSubmit').css( "top",eval(topOffSet) ); }
				if (leftOffSet) { jQuery('div.errorSubmit').css( "left",eval(leftOffSet) ); }
				if (rightOffSet) { jQuery('div.errorSubmit').css( "right",eval(rightOffSet) ); }
				if (displayType == "Right") { jQuery('div.errorSubmit').addClass('bubbleRight'); }
				else { jQuery('div.errorSubmit').addClass('bubbleLeft'); }
				jQuery('div.errorSubmit').show("normal");
				jQuery('a.errorCloseBtn').click(function() {
					jQuery('div.errorSubmit').hide("normal");
					return false;
				});
				setTimeout(function(){jQuery('div.errorSubmit').hide("normal")},10000);
				jQuery(oForm).removeClass('submit');
				oFormValidator.oHasError = 0;
				return false;
			}
		});
	},
	
	findTop:function(sCN){ return (/top(-{0,1}\d+)/i.test(sCN))?RegExp.$1:0;},
	findRight:function(sCN){ return (/right(-{0,1}\d+)/i.test(sCN))?RegExp.$1:0;},
	findLeft:function(sCN){ return (/left(-{0,1}\d+)/i.test(sCN))?RegExp.$1:0;},
	findDisplay:function(sCN){ return (/display(\w+)/i.test(sCN))?RegExp.$1:"Left";},
	findIETop:function(sCN){ return (/ieTop(-{0,1}\d+)/i.test(sCN))?RegExp.$1:0;},
	findIERight:function(sCN){ return (/ieRight(-{0,1}\d+)/i.test(sCN))?RegExp.$1:0;},
	findIELeft:function(sCN){ return (/ieLeft(-{0,1}\d+)/i.test(sCN))?RegExp.$1:0;},
	findMacTop:function(sCN){ return (/macTop(-{0,1}\d+)/i.test(sCN))?RegExp.$1:0;},
	findMacRight:function(sCN){ return (/macRight(-{0,1}\d+)/i.test(sCN))?RegExp.$1:0;},
	findMacLeft:function(sCN){ return (/macLeft(-{0,1}\d+)/i.test(sCN))?RegExp.$1:0;}

};

oVideoPlayer = {
	oVideoPath:"/flash/",
	oMovieTitle:null,
	oProductXML:null,
	oVideo:null,
	
	init:function() {
		jQuery("img.videoImg").click(function() {
			jQuery("div.videoContent").html("&nbsp;");
			oVideoPlayer.oVideo = oVideoPlayer.findVideo(this.className);
			oVideoPlayer.oMovieTitle = oVideoPlayer.findVideoName(this.className);
			oVideoPlayer.oProductXML = oVideoPlayer.findProductXML(this.className);
			oVideoPlayer.launchPlayer();
			jQuery('.videoDetails').hide('normal', oVideoPlayer.replaceContent(jQuery(this).parent()));
		});
		jQuery("img.videoImg:eq(0)").trigger("click");
	},
	
	launchPlayer:function() {
		var so = new SWFObject("http://www.nationwide.com/flash/nwVideo.swf","nwVideo","320","274","8","#FFFFFF");
        so.addParam('allowScriptAccess', 'sameDomain');
        so.addParam('enableJavascript', 'true');
        so.addParam('wmode','transparent');
		so.addVariable("moviePath", oVideoPlayer.oVideoPath + oVideoPlayer.oVideo);
		so.addVariable("movieTitle", oVideoPlayer.oMovieTitle);
		so.addVariable("videoMenu",oVideoPlayer.oVideoPath + oVideoPlayer.oProductXML);
		so.addVariable('nwurl',location.href);
        so.write('videoPlayer');

	},
	
	replaceContent:function(videoChunk) {
		var oDetails = jQuery(videoChunk).children('.videoDetails').clone().prependTo("div.videoContent");
		jQuery(oDetails).show('fast');
	},

	findVideo:function(sCN){
		return (/flv([A-Za-z0-9-.]+)/i.test(sCN))?RegExp.$1:0;
	},

	findVideoName:function(sCN){
		return (/name(\w+)/i.test(sCN))?RegExp.$1:0;
	},

	findProductXML:function(sCN){
		return (/menu([A-Z0-9-.]+)/i.test(sCN))?RegExp.$1:0;
	}

};

oFAQShowHide = {
	init:function() {
	if (jQuery.browser.msie) {
		jQuery("p.question").mouseover( function() { jQuery(this).css("text-decoration","underline"); } );
		jQuery("p.question").mouseout( function() { jQuery(this).css("text-decoration","none"); } );
	}
		jQuery('h2.faqSection').addClass("faqSectionStyles");
		jQuery('p.question').addClass("faqQuestion");
		jQuery('div.answer').hide();
		jQuery('p.question').click(function() {
			if (jQuery(this).next('.shown')) {
				jQuery(this).next().removeClass('shown');
				jQuery(this).next().hide("normal");
			}
			jQuery(this).next().addClass('shown');
			jQuery(this).next().show("normal");
		});
		jQuery('h2.faqSection').click(function() {
			jQuery(this).next('.section').toggle("normal");
			jQuery(this).next('.section').children('.answer').hide("fast");
		});
	}
}

oFormHelp = {
	init:function() {
		jQuery.preloadImages('http://www.nationwide.com/locator/images/formHintButton.gif','http://www.nationwide.com/locator/images/formHintButtonSelected.gif');
		jQuery('div.formHelp > div').hide();
		jQuery('div.formHelp > img').click(function() {
			jQuery(this).next('div').toggle('normal');
			if (jQuery(this).attr("src") == "http://www.nationwide.com/locator/images/formHintButtonSelected.gif"){
			jQuery(this).attr("src","http://www.nationwide.com/locator/images/formHintButton.gif");
			}else{
			jQuery(this).attr("src","http://www.nationwide.com/locator/images/formHintButtonSelected.gif");
			}
			return false;
		});
	}
};

oIeBackgroundImgCache = {
	init:function(){
		try {
	  		document.execCommand("BackgroundImageCache", false, true);
			} catch(err) {}
	}
};

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

jQuery(document).ready(function(){
	oPopupModule.init();
	oGlossary.init();
	oFormValidator.init();
	oVideoPlayer.init();
	oFAQShowHide.init();
	oFormHelp.init();
	oIeBackgroundImgCache.init();
});