//Variables used for the survey
var cookieSurvId = "RECALLSNAPSHOT051310";
var surveyURL = "/toyota/html/snapshot/english/recallsnapshot.jsp";
var survWidth = 700;
var survHeight = 640;
var maxInt = 1;
var popForm = true;
var gtfItems = 0;

(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];
				var branch = "";
				if (window.location.toString().indexOf('espanol') > -1){
					branch = '/espanol';
				} else if (window.location.toString().indexOf('chinese') > -1){
					branch = '/asian/chinese';
				} else if (window.location.toString().indexOf('korean') > -1){
					branch = '/asian/korean';
				} else if (window.location.toString().indexOf('vietnamese') > -1){
					branch = '/asian/vietnamese';
				}
				return function(videoId, title, date, desc) {
					var xmlPath = branch +'/recall/v2/xml/' + videoId + '.xml';
					var clickThruPath = branch +'/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') ||  body.hasClassName('legacy'))){ // 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') ||  body.hasClassName('legacy'))) { // this is the thumb on Recall Landing page
						var videoId = lnk.readAttribute('dest');
						var title = siblings[0].innerHTML;
						var desc = ((body.hasClassName('legacy'))?siblings[1].innerHTML:"");
						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);
		}
		
		// populate the Get The Facts
		if ($('gtfLanding')) {
			var script = document.createElement('script');
			script.type = "text/javascript";
			script.src = "/recall/v2/js/gtf.json";
			document.getElementsByTagName('head')[0].appendChild(script);
			
			//move the GTF carousel to the left
			$$(".arrowLeft")[0].observe('click', function(evt) {
				Event.stop(evt);
				
				new Effect.Move('gtfLanding', { x: -252, y: 0, mode: 'absolute', afterFinish:function()
				{
					childToMove = $$("#gtfLanding li")[0];
					$$("#gtfLanding li")[0].remove();
					new Insertion.Bottom($("gtfLanding"), "<li>" + childToMove.innerHTML + "</li>");
					$("gtfLanding").setStyle({left: '0px'});
				} });					
			});
			
			//move the GTF carousel to the right
			$$(".arrowRight")[0].observe('click', function(evt) {
				Event.stop(evt);
	
				childToMove = $$("#gtfLanding li")[gtfItems - 1];
				$$("#gtfLanding li")[gtfItems - 1].remove();
				new Insertion.Top($("gtfLanding"), "<li>" + childToMove.innerHTML + "</li>");
				$("gtfLanding").setStyle({left: '-252px'});
				
				new Effect.Move('gtfLanding', { x: 0, y: 0, mode: 'absolute'});
			});
		}
	});

})();

function populateGetTheFacts(data) {
	var tpl = new Template([
		'<li>',
			'<h3>#{title}</h3>',
			'#{content}',
		'</li>'
	].join(''));
	gtfItems = data.items.length;

	$('gtfLanding').update(data.items.collect(function(item, i) {
		return tpl.evaluate(item);
	}).join('')).addClassName('loaded');	
}
	
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" target="_blank">'+(($$('body')[0].hasClassName('espanol'))?'Ver art&iacute;culo completo':'View Full Post')+'</a>',
		'</li>'
	].join(''));
	
	$('newsroomLanding').update(data.items.collect(function(item, i) {
		if ((data.items.length -1) == i) {
			item['posId'] = "last";
		}
		return tpl.evaluate(item);
	}).join('')).addClassName('loaded');
}

