/*
To use this library, include both jsapi and this js on your HTML page:

     <script type="text/javascript" src="http://www.google.com/jsapi"></script>
     <script type="text/javascript" src="/en_US/Site_Utilities/js/geoTDSP.js"></script>
*/	

	/*  this is a private function.  Builds a dictionary of city/tdsp entries */
	function citiesArray() {
		/* all cities in this array should be in lower case */
		var cities = {} ;
		var cnp = ["houston","spring","katy","sugar land","pasadena","humble","cypress","pearland","baytown","richmond","missouri city","tomball","galveston","kingwood","magnolia","stafford","rosenberg","la porte","lake jackson","webster","deer park","crosby","channelview","friendswood","santa fe","freeport"] ;
		var onc = ["dallas","fort worth","arlington","plano","irving","tyler","grand prairie","odessa","waco","killeen","mesquite","midland","round rock","wichita falls","carrollton","richardson","mckinney","temple","euless","lufkin","north richland hills","bedford","mansfield","allen","pflugerville","grapevine"] ;
		var cpl = ["corpus christi","laredo","mcallen","harlingen","mission","victoria","edinburg","del rio","pharr","weslaco","eagle pass","rockport","kingsville","san benito","alice","bay city","rio grande city","brownsville","uvalde","portland","beeville","donna","aransas pass","mercedes","alamo","port lavaca"] ;
		var tnp = ["lewisville","league city","texas city","alvin","friendswood","angleton","dickinson","la marque","fort stockton","gatesville","pecos","brazoria","west columbia","sweeny","princeton","kermit","clifton","hamilton","pilot point","nocona","glen rose","whitney"] ;
		var wtu = ["abilene","san angelo","vernon","alpine","clyde","childress","ballinger","cisco","stamford","haskell","quanah","sonora","junction","anson","winters","merkel","big lake","hamlin"] ;
		var shryl = ["mcallen","mission"]
		for (i in cnp)
			cities[cnp[i]] = 'cnp' ;
		for (i in onc)
			cities[onc[i]] = 'onc' ;
		for (i in cpl)
			cities[cpl[i]] = 'cpl' ;
		for (i in tnp)
			cities[tnp[i]] = 'tnp' ;
		for (i in wtu)
			cities[wtu[i]] = 'wtu' ;
		for (i in shryl)
			cities[shryl[i]] = 'shryl' ;
		return cities ;
	}
	
	/*  this is a private function; given a city, returns a TDSP name.  A present, this returns a TDSP if the browser
	    is in Texas, otherwise just returns the state code, or 'none' if none can be determined.
	*/
	function lookupTDSP(state,city) {
		retval = 'none' ;
		if (state == 'TX') {
			var c = citiesArray() ;
			if (typeof(city) !== null) {
				retval = c[city.toLowerCase()] ;
			}
		}
		else {
			if (state != '') {
				retval = state
			}
		}
		return retval ;
	}
	/* use this function if you're developing your own switch statement to show content.
	   Returns the browser's TDSP: values can be cnp,onc,cpl,tnp,wtu, a state code, or none */
	   
	function geoTDSP() {
	/* google.loader is defined in http://www.google.com/jsapi */
		var q = null ; 
		try {
			q = getQueryVariable('tdsp') ; //for testing purposes only.  This function is defined in common.js
		}
		catch (except) {
		// do nothing
		}
  		if (q === null || q === '') {
	    	if (google.loader.ClientLocation)
				return lookupTDSP(google.loader.ClientLocation.address.region,google.loader.ClientLocation.address.city) ;
			else
				return 'none' ;
		} else {
			return q ;
		}
	}
	
	/*  convenience function; inserts appropriate image. images should all be in the same path */ 
	function insertGeoImage(path,map,defaultImage) {
		html = "<img id='geoimage' src='XPATH/XIMAGE'/>" ;
	    html = html.replace('XPATH',path) ;
	    var tdsp = geoTDSP() ;
	    if (map[tdsp] != undefined)
	    	html = html.replace('XIMAGE',map[tdsp]) ;
	    else
	    	html = html.replace('XIMAGE',defaultImage) ;
    	document.write(html) ;
	}
	
		function insertGeoImageLink(path,imagemap,defaultImage,linkmap,defaultLink) {
		var tdsp = geoTDSP() ;
		html = "<a id = 'geolink' href='XLINK'><img id='geoimage' src='XPATH/XIMAGE'/></a>" ;
	    html = html.replace('XPATH',path) ;
	    var tdsp = geoTDSP() ;
	    if (imagemap[tdsp] != undefined) {
	      html = html.replace('XIMAGE',imagemap[tdsp]) ;
	      html = html.replace('XLINK',linkmap[tdsp]) ; //we assume these maps are parallel.  A bit risky
	    }
	    else {
	      html = html.replace('XIMAGE',defaultImage) ;
	      html = html.replace('XLINK',defaultLink) ;
	    }
    	document.write(html) ;
	}
	
    function flashSnippet() {
      return '<!--[if !IE]> -->\
	<object type="application/x-shockwave-flash" data="XPATH/XFLASH" width="XWIDTH" height="XHEIGHT">\
    <!-- <![endif]-->\
    <!--[if IE]>\
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="flashbanner" width="XWIDTH" height="XHEIGHT"\
        codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0">\
        <param name="movie" value="XPATH/XFLASH" />\
    <!-->\
        <param name="loop" value="true" />\
        <param name="allowScriptAccess" value="always" />\
        <param name="menu" value="false" />\
        <param name="wmode" value="opaque">\
        XPARAM\
        <a href="XLINK"><img alt="Reliant Energy banner" src="XIMAGE"/></a>\
    </object>\
    <!-- <![endif]-->' ;
    }
/*
   convenience function.  'map' is an array of regions to flash: example: {'cnp':f,'onc':f,'none':f}
*/
	function insertGeoFlash(width,height,path,map,defaultFlash,defaultImage,defaultLink,extraparam) {

		html = flashSnippet() ;
    	html = html.replace('XIMAGE',defaultImage) ;
    	html = html.replace('XLINK',defaultLink) ;
    	html = html.replace('XWIDTH',width) ;
    	html = html.replace('XWIDTH',width) ; //replaces 2nd occurance. Could use regular expression match, but IE doesn't like it? :(
    	html = html.replace('XHEIGHT',height) ;
    	html = html.replace('XHEIGHT',height) ;
    	html = html.replace('XPATH',path) ;
    	html = html.replace('XPATH',path) ;
    	if (extraparam != null) {
    		html = html.replace('XPARAM',extraparam) ;
    	}

	    tdsp = geoTDSP() ;
	    flashstring = map[tdsp]
	    if (flashstring == undefined) {
	    	flashstring = defaultFlash ;
	    }
	    html = html.replace('XFLASH',flashstring) ;
	    html = html.replace('XFLASH',flashstring) ; //calling twice instead of a regex expression
    	document.write(html) ;
	}

	
	/* Convenience function.  Elements can be any block element, <div>, <p> etc.  One for each region.  Function will hide all elements,
	   then display just the correct one according to browser's TDSP
	   'map' is a dictionary of elements, like this: {'cnp':'elementid1', 'onc' = 'elementis2'; 'MD' = 'elementid3'}
	   where 'elementid' is an id to an element on the page
	*/
	function geoToggleElements(map,defaultId) {

	    for (var loc in map) {  //get all elements in the map
	      ele = document.getElementById(map[loc]) ;
	      try {
	        ele.style.display = 'none' ; //now hide these element's values
	      }
	      catch (err) {
	        //just move along to the next element
	      }    
	    }
		def = document.getElementById(defaultId) ;
		try {
			tdsp = geoTDSP() ;
			if (map[tdsp] != undefined) {
			    ele = document.getElementById(map[tdsp]) ;
				ele.style.display = 'block' ;
				def.style.display = 'none' ;
			}
		}
		catch (err) {
		  //nothing needs to be done
		}
	}


