//___________________________________________
function Webkitchen(){
	
	// define document content holder
	var userContentId_str		= 'userContent';
	var mailSubscribeInputId_str= 'emailSubscribe';
	var upperSearchInputId_str	= 'upperSearchInput';
	var lowerSearchInputId_str	= 'lowerSearchInput';
	var mailSubscribeFormId_str	= 'emailSubscribeForm';
	
	var mapThumbName_str		= 'google-map.gif';
	var videoThumbName_str		= 'video-image.gif';
	
	var videoContainerClass_str = 'googleVideo';
	var videoExternalLink_str	= 'View at Google Video';
	var mapExternalLink_str		= 'Get directions at Google';
	var googleMapWrapClass_str	= 'mapWrapper';

	var filesToIcon				= new Object();
	filesToIcon.xls				= true;
	filesToIcon.doc				= true;
	filesToIcon.ppt				= true;
	filesToIcon.pdf				= true;
	filesToIcon.zip				= true;
	
	
	//---------------------------------------
	function addLinkCSSClasses(){
		
		var content_htm			= document.getElementById(userContentId_str);
		if(content_htm){
			var links_arr			= content_htm.getElementsByTagName('a');
			for(var index_int=0; index_int<links_arr.length; index_int++){
				var link_htm		= links_arr[index_int];
				var rgxMatches_arr	= link_htm.href.match(/.([\w]{3}$)/i);
				if(rgxMatches_arr){
					var ext_str		= rgxMatches_arr[1];
					if(filesToIcon[ext_str]){
						link_htm.className = link_htm.className+' fileLink '+ext_str;
					}
				}
			}
		}
	};
	//---------------------------------------
	function makeSelectOnFocus(elementId_str){
		
		var element_htm			= document.getElementById(elementId_str);
		if(element_htm){
			element_htm.onfocus = function(){this.select();};
		}
	};
	//---------------------------------------
	function integrateEmbeds(){
		
		var map_rgx				= new RegExp(mapThumbName_str+'$','g');
		var video_rgx			= new RegExp(videoThumbName_str+'$','g');
		
		
		if (GBrowserIsCompatible()) {
			var images_arr	= document.images;
			for (var index_int=0; index_int<images_arr.length; index_int++){
				var image_img	= images_arr[index_int];
				var imgSrc_str	= image_img.src;
				
				// detect map thumbnails and switch for maps
				if(imgSrc_str.match(map_rgx)!=null){
					embedMap(image_img);
				}
				
				// detect video thumbnails  and switch for videos
				if(imgSrc_str.match(video_rgx)!=null){
					embedVideo(image_img);
				}
			}
		}
	};
	//---------------------------------------
	function embedVideo(image_img){
		
		// match video id
		var link_htm			= image_img.parentNode;
		var idMatch_arr			= link_htm.href.match(/docid=(-?\d*)/);
		if(idMatch_arr!=null){
			var videoId_str		= idMatch_arr[1];	
		
			// create a video container
			var container_htm		= document.createElement('div');
			container_htm.className = videoContainerClass_str;
			
			// create video HTML
			var videoEmbed_str 		= '<embed class="videoEmbed" style="width:400px; height:326px;" align="middle" type="application/x-shockwave-flash"';
			videoEmbed_str			+= 'src=http://video.google.com/googleplayer.swf?docId='+videoId_str;
			videoEmbed_str			+= ' allowScriptAccess="sameDomain" quality="best" bgcolor="#ffffff" scale="noScale" wmode="window" salign="TL"  FlashVars="playerMode=embedded"> </embed>';
			
			// create caption
			var caption_str			= '<div class="caption">- '+link_htm.title+'</div>';
			
			// insert into document
			container_htm.innerHTML	= videoEmbed_str+caption_str;
			link_htm.parentNode.appendChild(container_htm);
			link_htm.style.display = 'none';
		}
	};
	//---------------------------------------
	function embedMap(image_img){
		
		var latLong_rgx			= new RegExp('(ll=)?[^$]*ll=([^,]*),([^,&]*)');
		var mapClass_str		= 'googleMap';
		
		// parse lat/long from containing href
		var link_htm	= image_img.parentNode;
		var href_str	= link_htm.href;
		var mapTitle_str= link_htm.title;
		var latLong_arr	= href_str.match(latLong_rgx);
		if(latLong_arr!=null){
			var lat_flt		= latLong_arr[2];
			var long_flt	= latLong_arr[3];
			
			// remove map image and put text instead
			link_htm.innerHTML 	= mapExternalLink_str;
			link_htm.target		= '_blank';
			link_htm.className	= 'googleMapLink';
			
			// insert a GMap container and remove link and image
			var mapWrapper_htm		= document.createElement('div');
			mapWrapper_htm.className = googleMapWrapClass_str;
			var mapContainer_htm	= document.createElement('div');
			mapWrapper_htm.appendChild(mapContainer_htm);
			mapContainer_htm.className = mapClass_str;
			var parent_htm		= link_htm.parentNode;
			parent_htm.insertBefore(mapWrapper_htm,link_htm);
			mapWrapper_htm.appendChild(link_htm);
			
			// create a new Google Map and initialise to location
			var map = new GMap2(mapContainer_htm);
			var location_gll	= new GLatLng(lat_flt,long_flt);
			map.setCenter(location_gll,15);
			map.addControl(new GLargeMapControl());
			
			// add a marker
			var marker_mkr		= new GMarker(location_gll);
			map.addOverlay(marker_mkr);
			
			// add a label bubble (with a little delay)
			function addInfoWindow(){
				
				var title_htm	= document.createElement('h3');
				title_htm.innerHTML = mapTitle_str;
				marker_mkr.openInfoWindow(title_htm);
			};
			
			// add a listener to reinstate the infoWindow when the marker is clicked
			GEvent.addListener(marker_mkr, "mousedown", function(marker, point) {
			
				var infoWindow = map.getInfoWindow();
				if (!infoWindow || infoWindow.isHidden()) {
					addInfoWindow();
				}
			});
			window.setTimeout(addInfoWindow,2000);
		}
	};
	//_______________________________________
	this.processXHTML = function(){
		
		// process maps and video
		integrateEmbeds();
		
		// add onFocus events where required
		makeSelectOnFocus(mailSubscribeInputId_str);
		makeSelectOnFocus(upperSearchInputId_str);
		makeSelectOnFocus(lowerSearchInputId_str);
		
		// setup feedburner popup properties
		var mailForm_htm		= document.getElementById(mailSubscribeFormId_str)
		if(mailForm_htm){
		
			var feedburner				= new Object();
			feedburner.popupProps_str 	= 'scrollbars=yes,width=550,height=520';
//			mailForm_htm.onsubmit= function(){				
//				alert(mailForm_htm.target);
//				window.open('http://www.feedburner.com',mailForm_htm.target,feedburner.popupProps_str);
//			};
		}
		
		// add icons to relevant links
		addLinkCSSClasses();
		
		// post process images
		var textProcessor = new TextPostProcessor();
		textProcessor.execute(document.body);
	
		// define video locations and swap in
		var params_obj			= {
			'escapedPlacemarkerImage'	: 'moviemarker\.gif',
			'videoDir'					: window.videoSourceDirectory,
			'componentsDir'				: window.videoAssetDirectory,
			'autoRewind'				: 'true',
			'autoPlay'					: 'false',
			'defaultSkin'				: 'Halo_Skin_2',
			'playerName'				: 'FLVPlayer_Progressive'
		}
		var hqVideoModule			= new HQVideoModule(params_obj);
		hqVideoModule.swapIn();
	};
};	//_______________________________________
//___________________________________________

window.webkitchen	= new Webkitchen();