(function() {
	Event.onDOMReady(function() {
		var playVideo = (function() {
			// cache references to the videoPlayer and title elements
			var videoPlayer = (navigator.appName.indexOf("Microsoft") != -1)?($('videoPlayerIE')):($('videoPlayer'));
			if (videoPlayer && $('flashContent')) {
				var siblings = $('flashContent').siblings();
				var currentTitle = siblings[0];
				var currentDesc = siblings[1];
				var currentDate = siblings[2];

				return function(videoId, title, date, desc) {
					var xmlPath = '/asian/vietnamese/recall/v2/xml/' + videoId + '.xml';
					var clickThruPath = '/asian/vietnamese/recall/videos/index.html#' + videoId;

					if (videoPlayer.openVideo) {
						currentTitle.update(title);
						//currentDate.update(date);						
						currentDesc.update(desc);						
						videoPlayer.openVideo(xmlPath,clickThruPath);
						
					} else {
						window.setTimeout(arguments.callee, 0, videoId, title, date, desc);
					}
				};
			}
		})();
		
		// check for an initial video
		if (window.location.hash.length >0) {
			var videoId = window.location.hash.split('#')[1];
			
			// find the corresponding video
			var videoEl = $$('.videos ul a').find(function(anchor) {
				return anchor.readAttribute('dest') == videoId;
			});
			var siblings = videoEl.siblings();
			var title = siblings[0].innerHTML;
			var desc = siblings[1].innerHTML;
			//var date = siblings[2].innerHTML;
			var date = null;
			 
			playVideo(videoId, title, date, desc);
		}
		
		// link up the videos
		$$('.videos ul a').each(function(lnk) {
				var siblings = lnk.siblings();
				var body = $$('body')[0];
				var tagging = TMSSite.i18n[TMSSite.lang].location + '_' + lnk.readAttribute('tag');
				lnk.observe('click', function(evt){
					console.log("Siblings:" + siblings.length);
				/*	if (siblings.length > 0 && body.hasClassName('landing')){ // only stop on Recall Landing page
						// Stop event
						Event.stop(evt);
					}*/
					// Omniture tagging
					var oEvent = {
						properties: {
							'46': tagging
						},
						linkTrackVars: 'prop46',
						linkTrackType: 'o',
						linkTrackName: tagging
					};
					console.log(tagging);
					// Sends event
					TMSSite.analytics.sendTrackingLink(oEvent);
				/*	if (siblings.length > 0 && body.hasClassName('landing')) { // this is the thumb on Recall Landing page
						var videoId = lnk.readAttribute('dest');
						var title = siblings[0].innerHTML;
						var desc = "";
						var date = "";
						// Plays selected video
						playVideo(videoId, title, date, desc);
					}*/
				});
		});
		
		// populate the newsroom
		if ($('newsroomLanding')) {
			// insert the newsroom jsonp script
			var script = document.createElement('script');
			script.type = "text/javascript";
			script.src = "http://expresslane.toyota.com/f/100003s2c87caqbjvev.json?jsonp=populateNewsroom";
			document.getElementsByTagName('head')[0].appendChild(script);
		}
	});


})();
	
function populateNewsroom(data) {
	// only show the first three for the landing page
	if ($$('body')[0].hasClassName('landing')) {
		data.items = data.items.slice(0, 2);
	}
	
	var tpl = new Template([
		'<li class="#{class}">',
			'<h3>#{title}</h3>',
			'<em class="datestamp">#{pubdate}</em>',
			'<a href="#{link}" class="view_full_lnk">View Full Post</a>',
		'</li>'
	].join(''));
	
	$('newsroomLanding').update(data.items.collect(function(item, i) {
		if ((data.items.length -1) == i) {
			item['class'] = "last";
		}
		return tpl.evaluate(item);
	}).join('')).addClassName('loaded');
}

