VRT = YAHOO.namespace("trp.vrt.common");

if (typeof VRT.page == "undefined")
{

//General convention is to use CSS class "nodisplay" to hide things

VRT.page = function(){

	return {

        //for dynamic properties - these are set at runtime
        properties:{
			iFrameCheckLoginCallback:null,
        	isInPreview: false,  //see  /includes/commonStylesJs.jsp
        	metricsReportedOn:false,
        	cookieCutterPrefix: "vrt",     	
			logon:false,
        	requestPhoneOverride:false,
        	allowPhoneOverride:false,
        	showUtilityLinks:true,
        	isUtilityPage:false,
        	showBreadcrumb:false,
        	showFooter:false,
        	allowIFrameScroll:true    	
        },
		
		popwin:function(href,title,properties) {
			//ie croaks on special chars in title - render linkspec is using channel name for title.	
			var safeTitle = VRT.page.browserSafeName(title);
			var popupWindow = window.open(href,safeTitle,properties);				
		},
		centerPopup : function(href, name, width, height, scroll, resize, menu) {
			var safeName = this.browserSafeName(name);
			var left = (screen.width) ? (screen.width-width)/2 : 0;
			var top = (screen.height) ? (screen.height-height)/2 : 0;
			if(scroll) {
				scroll=1;
			}
			else {
				scroll=0;
			}
			if(resize) {
				resize=1;
			}
			else {
				resize=0;
			}
			if(menu) {
				menu=1;
			}
			else {
				menu=0;
			}
			var settings = 
				"'height=" + height + 
				",width=" + width + 
				",left=" + left + 
				",top=" + top +
				",scrollbars=" + scroll + 
				",resizable=" + resize + 
				",menu=" + menu + "'";			
			var popup = window.open(href,safeName,settings);
			return popup;
		},
		centerPopunder : function(href, name, width, height, scroll, resize, menu) {
			var popup = this.centerPopup(href, name, width, height, scroll, resize, menu);
			popup.blur();
			window.focus();
			return popup;
		},
		browserSafeName : function(inputVal) {
			newString = "";
			for (i = 0; i < inputVal.length; i++) {
			 	ch = inputVal.substring(i, i+1);
			 	
				 if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z") ||
				 (ch >= "0" && ch <= "9") || ch == "_") {
				 newString += ch;
			 	}
			 }
			 if (newString == "") {
			 newString = "TRowePrice";
			 }
			 return newString;
		},
		/**
		This function will write an inline flash object in the current location.
		Use WMODE parameter to indicate whether item should be 'transparent', 'opaque', etc. If you do not select
		a valid WMODE the object will default to WMODE='window' and will have the highest z-index on the page (will sit above dropdown menus)
		*/
		writeFlashObject : function(PLAYERID, URL, WIDTH, HEIGHT, WMODE, ALTTEXT, SCALE, ALIGN, HSPACE, BGCOLOR) {
			if (!PLAYERID) PLAYERID = '';
			if (!URL) URL = '';
			if (!HEIGHT) HEIGHT = '';
			if (!WIDTH) WIDTH = '';
			if (!WMODE) WMODE = '';
			if (!ALTTEXT) ALTTEXT = '';
			if (!SCALE) SCALE = '';
			if (!ALIGN) ALIGN = '';
			if (!HSPACE) HSPACE = '';
			if (!BGCOLOR) BGCOLOR = '';
			
			var html = 
			'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' +
				' codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0" ' +
				' id="' + PLAYERID + '" ' + 
				' align="' + ALIGN + '" ' + 
				' hspace="' + HSPACE + '" ' + 
				' height="' + HEIGHT + '" ' +
				' width="' + WIDTH + '" ' + 
				' title="' + ALTTEXT + '"> ' + 
				' <param name="movie" value="' + URL + '"> ' +
				' <param name="quality" value="high"> ' +
				' <param name="wmode" value="' + WMODE + '"> ' + 
				' <param name="bgcolor" value="' + BGCOLOR + '"> ' +
				' <embed src="' + createJunctionURL(URL) + '" ' +
					' quality="high" ' +
					' name="' + PLAYERID + '" ' + 
					' scale="' + SCALE + '" ' + 
					' type="application/x-shockwave-flash" ' +
					' pluginspage="https://www.macromedia.com/go/getflashplayer" ' + 
					' height="' + HEIGHT + '" ' +
					' width="' + WIDTH + '" ' + 
					' title="' + ALTTEXT + '" ' + 
					' wmode="' + WMODE + '"> ' +
			'</object> ';
			document.write(html);
		},

		writeManagedFlashObject : function(PLAYERID, PLACEMENTPATH, WIDTH, HEIGHT, WMODE, ALTTEXT, SCALE, ALIGN, HSPACE, BGCOLOR) {
			if (!PLACEMENTPATH) PLACEMENTPATH = '';
		
			var URL = PLACEMENTPATH;
			if(VRT.page.properties.staticFilePathPrefix) {
				URL = VRT.page.properties.staticFilePathPrefix + PLACEMENTPATH;
			}
		
			writeFlashObject(PLAYERID, URL, WIDTH, HEIGHT, WMODE, ALTTEXT, SCALE, ALIGN, HSPACE, BGCOLOR);
		},
		
		writeIFrame : function(IFRAMEID, NAME, URL, HEIGHT, WIDTH, ONLOAD, SCROLLING, FRAMEBORDER, MARGINWIDTH, MARGINHEIGHT, CHECKLOGIN) {
			//Lock down VGN floating menu to prevent iframe scrolling issues
			vgnMenuVerticalPosition = 650;
		
			if (!IFRAMEID) IFRAMEID = '';
			if (!NAME) NAME = '';
			if (!URL) URL = '';
			if (!HEIGHT) HEIGHT = '';
			if (!WIDTH) WIDTH = '';
			if (!ONLOAD) ONLOAD = 'VRT.page.defaultIFrameOnload(\''+IFRAMEID+'\')';
			if (!SCROLLING) SCROLLING = 'no';
			if (!FRAMEBORDER) FRAMEBORDER = 0;
			if (!MARGINWIDTH) MARGINWIDTH = 0;
			if (!MARGINHEIGHT) MARGINHEIGHT = 0;
			if (!CHECKLOGIN) CHECKLOGIN = 'no';
		
/*
pass query string attributes of window location to application URL named in passTokenTo
base64 encode all attributes in a single attribute named tok
this allows apps using the web validation framework to get all attributes by adding
on a single allowed attribute to their valid uri list

*/			
			var passTokenTo = {
	  			"/toolsstaging/customerService/public/secure/":"TRUE",
	  			"/retailtools3/customerService/public/secure/":"TRUE",
	  			"/websearch/alaska/searchAlaska/":"TRUE"
  			};
  			
  			
			var pagelocation = window.location + "";
			
			if(VRT.cookie.getChip("searchAlaskaQuery") != undefined)  			
  			{
  				if(pagelocation.indexOf("?") > -1)
  					pagelocation = pagelocation + "&searchAlaskaQuery=" + VRT.cookie.getChip("searchAlaskaQuery");
  				else
  					pagelocation = pagelocation + "?searchAlaskaQuery=" + VRT.cookie.getChip("searchAlaskaQuery");
			}
  			var windowAttrs = VRT.parseUri.parse(pagelocation);

  			var appUrlAttrs = VRT.parseUri.parse(URL);
  			
			VRT.parseUri.removeMetricsAttrs(windowAttrs); 

  			var passToken = passTokenTo[appUrlAttrs.directory];

			if(passToken != undefined)
			{
				if(!jvl.validation.isEmpty(windowAttrs.query))
				{
					if(!jvl.validation.isEmpty(appUrlAttrs.query))
					{
						URL = URL + "&" + "tok=" + VRT.Base64.encode(windowAttrs.query);
					}
					else
					{
						URL = URL + "?" + "tok=" + VRT.Base64.encode(windowAttrs.query);
					}
				}
				
			}
		      
			var html = 
			'<iframe ' +
				' id="' + IFRAMEID + '" ' + 
				' name="' + NAME + '" ' + 
				' src="' + createJunctionURL(URL) + '" ' +
				' height="' + HEIGHT + '" ' + 
				' width="' + WIDTH + '" ' + 
				' onload="' + ONLOAD + '" ' + 
				' scrolling="' + SCROLLING + '" ' +
				' frameborder="' + FRAMEBORDER + '" ' + 
				' marginwidth="' + MARGINWIDTH + '" ' + 
				' marginheight="' + MARGINHEIGHT + '"></iframe> ';	
		
			document.write(html);
			var fr = document.getElementById(IFRAMEID);
			fr.setAttribute("orig_height", HEIGHT);
			fr.setAttribute("checklogin", CHECKLOGIN);
		},

		// default is resetting the height and the scroll.
		// one example is the mai login screen, which is not instrumented with the autoresize js.
		// the simple solution is to reset the login iframe to the preset iframe height. 
		defaultIFrameOnload : function(iframeid) {	
			var fr = null;
			if (iframeid!=null && iframeid.length > 0) {
				fr = document.getElementById(iframeid);
			}
			if (fr!=null) {
				fr.getAttribute("orig_height")!=null? fr.height = fr.getAttribute("orig_height"):null;
				if(fr.getAttribute("checklogin")=="yes") {				
					if(VRT.page.properties.iFrameCheckLoginCallback != null) {
						VRT.page.properties.iFrameCheckLoginCallback.call(this,fr);
					} 
					else {
						VRT.logger.log("Cannot check iframe login - callback not registered");					
					}		
				}
			}
			if(VRT.page.properties.allowIFrameScroll) {
				window.scrollTo(0,0);
			}
		},
		
		writeBrightcovePlayer : function(PLAYERID, NAME, SOURCEID, HEIGHT, WIDTH, ONLOAD, SCROLLING, FRAMEBORDER, MARGINWIDTH, MARGINHEIGHT) {
			if (!SOURCEID) SOURCEID = '';
		
			var URL = SOURCEID;
			if(VRT.page.properties.brightcovePlayerUriPrefix) {
				URL = VRT.page.properties.brightcovePlayerUriPrefix + SOURCEID;
			}
		
			writeIFrame(PLAYERID, NAME, URL, HEIGHT, WIDTH, ONLOAD, SCROLLING, FRAMEBORDER, MARGINWIDTH, MARGINHEIGHT);
		},
		
		getJunctionName : function() {
			var u = window.location.href;
			var regex = /^http[s]?:\/\/www3[\w]*.troweprice.com\/([\w]+)/;
			var j = "";
		
			var result = u.match(regex);
			if (result!=null && result.length==2)
			{
				j = result[1];
			}
			return (j.length > 0)? "/"+j : "";
		},
		
		createJunctionURL : function(url) {
			var junction = getJunctionName();
			// url == /relative_url
			if (url.indexOf("/") == 0)
			{
				//if the junction is already present just return the URL.
				if (junction != null && url.indexOf(junction) == 0) return url;
				url = junction + url;
			}
			return url;
		}		
		
	};

}();

//Legacy mappings
popwin = VRT.page.popwin;
vrtwin = VRT.page;
vrtPage = VRT.page;
writeBrightcovePlayer = VRT.page.writeBrightcovePlayer;
writeIFrame = VRT.page.writeIFrame;
writeManagedFlashObject = VRT.page.writeManagedFlashObject;
writeFlashObject = VRT.page.writeFlashObject;
getJunctionName = VRT.page.getJunctionName;
createJunctionURL = VRT.page.createJunctionURL;

}