// JavaScript Document

$(document).ready(init);

var reviewsXMLData,videoXMLData;
var videoOBJAnimating = false;

function init(){
	jQuery.get("/superbowl/xml/reviews.xml?"+Math.floor(Math.random()*1000000000),reviewsXMLLoaded,"xml");
	jQuery.get("/superbowl/xml/videos.xml?"+Math.floor(Math.random()*1000000000),videosXMLLoaded,"xml");
}

function reviewsXMLLoaded(xmlData){
	var reviews_expert = '';
	var reviews_consumer = '';
	var vehicle,titletxt,user,bodytxt,href;
	
	reviewsXMLData = xmlData;
	
	$(xmlData).find("review[type='consumer']").each(function(){
		vehicle = $(this).find('vehicle').text();
		titletxt = $(this).find('titletxt').text();
		user = $(this).find('user').text();
		bodytxt = $(this).find('bodytxt').text();
		href = $(this).find('href').text();
		reviews_consumer += "<div class=\"review\"><div class=\"car\">"+vehicle+"</div><div><span class=\"title\">"+titletxt+"</span><span class=\"user\">, "+user+"</span></div><div class=\"body\">\""+bodytxt+"\" <a href=\""+href+"\">read more &raquo;</a></div></div>";
	});
	
	$(xmlData).find("review[type='expert']").each(function(){
		vehicle = $(this).find('vehicle').text();
		author = $(this).find('author').text();
		group = $(this).find('group').text();
		bodytxt = $(this).find('bodytxt').text();
		href = $(this).find('href').text();
		reviews_expert += "<div class=\"review\"><div class=\"car\">"+vehicle+"</div><div><span>by</span>&nbsp;<span class=\"author\">"+author+"</span><span class=\"group\">, "+group+"</span></div><div class=\"body\">\""+bodytxt+"\" <a href=\""+href+"\">read more &raquo;</a></div></div>";
	});
	
	$('#reviews #consumer .reviews').html(reviews_consumer);
	$('#reviews #expert .reviews').html(reviews_expert);
}

function videosXMLLoaded(xmlData){
	var videos_tv = '';
	var videos_web = '';
	var id,th,labeltxt,bodytxt,time,flashVars;
	
	videoXMLData = xmlData;
	
	var count = 0;
	$(xmlData).find("video[type='tv']").each(function(){
		id = $(this).attr('id');
		th = $(this).attr('th');
		labeltxt = $(this).find('labeltxt').text();
		bodytxt = $(this).find('bodytxt').text();
		time = $(this).find('time').text();
		flashVars = $(this).find('flashVars').text();
		videos_tv += "<a class=\"block video\" title=\""+labeltxt+"\" onclick=\"return swapVideo('"+encodeURI(flashVars)+"');\"><img class=\"floatleft\" src=\""+th+"\" alt=\""+labeltxt+"\" border=\"0\" /><span class=\"label\">"+labeltxt+"</span><br /><span class=\"time\">"+time+"</span><div class=\"clear\"></div></a>";
		count++;
	});
	
	var count = 0;
	$(xmlData).find("video[type='web']").each(function(){
		id = $(this).attr('id');
		th = $(this).attr('th');
		labeltxt = $(this).find('labeltxt').text();
		bodytxt = $(this).find('bodytxt').text();
		time = $(this).find('time').text();
		flashVars = $(this).find('flashVars').text();
		videos_web += "<a class=\"block video\" title=\""+labeltxt+"\" onclick=\"return swapVideo('"+encodeURI(flashVars)+"');\"><img class=\"floatleft\" src=\""+th+"\" alt=\""+labeltxt+"\" border=\"0\" /><span class=\"label\">"+labeltxt+"</span><br /><span class=\"time\">"+time+"</span><div class=\"clear\"></div></a>";
		count++;
	});
	
	$('#video #videos #tv').html(videos_tv);
	$('#video #videos #web').html(videos_web);
	
	flashVars = $(xmlData).find("video[type='tv']").first().find('flashVars').text();
	$('#player').html(getFlashObject(flashVars));
}

function swapVideo(flashVars){
	if(videoOBJAnimating){
		return false;
	}else{
		videoOBJAnimating = !videoOBJAnimating;
	}
	
	$('#player object').fadeOut('fast',function(){
		$(this).remove();
		$('#player').html(getFlashObject(decodeURI(flashVars)));
		$('#player object').hide();
		$('#player object').fadeIn('fast',function(){
			videoOBJAnimating = !videoOBJAnimating;
		});
	});
}

function getFlashObject(flashVars){
	return'<object id="flashObj" width="640" height="359" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,47,0"><param name="movie" value="http://c.brightcove.com/services/viewer/federated_f9?isVid=1&isUI=1" /><param name="bgcolor" value="#FFFFFF" /><param name="flashVars" value="'+flashVars+'" /><param name="base" value="http://admin.brightcove.com" /><param name="seamlesstabbing" value="false" /><param name="allowFullScreen" value="true" /><param name="swLiveConnect" value="true" /><param name="allowScriptAccess" value="always" /><embed src="http://c.brightcove.com/services/viewer/federated_f9?isVid=1&isUI=1" bgcolor="#FFFFFF" flashVars="'+flashVars+'" base="http://admin.brightcove.com" name="flashObj" width="640" height="359" seamlesstabbing="false" type="application/x-shockwave-flash" allowFullScreen="true" allowScriptAccess="always" swLiveConnect="true" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>';
}

function toggleSEOCopy(){
	$('#seocopy').toggle();
	return false;
}
