$(document).ready(function() {
	//initialise the videos on video page
	if ($('#vidlist').length > 0) {
		getVideoMRSS('init');
		setAjaxLinks();
		setVideoLinks();
	}
}); 

function getCatTitle(catTitle) {
	//set category name for tracking
	catTitle = catTitle.replace(/-/g, "_");
	catTitle = catTitle.replace(/\s/g, "_");
	catTitle = "sky_arts/" + catTitle.toLowerCase();
	return catTitle;
}

//get MRSS and set the itemObj from the video ID sent to this function (videoId)
function getVideoMRSS(videoId) {
	var mrssLink = $("head #videomrss").attr('href'),
		itemObj = {};		
		itemObj.videoid = '',
		itemObj.title = '',
		itemObj.mediaUrl = '',
		itemObj.description = '',
		itemObj.tvguide = '',
		itemObj.catTitle = '';	
	
	$.get(mrssLink, function(data){
		if (videoId != null) {
			$(data).find('rss channel item').each(function(){
				var $itemObj = $(this).children();
				//w3c browsers
				if ($itemObj[0].textContent != undefined) {
					if (videoId == 'init' || $itemObj[4].textContent == videoId) {
						itemObj.title = $itemObj[0].textContent;
						itemObj.link = $itemObj[1].textContent;
						itemObj.description = $itemObj[2].textContent;
						itemObj.pubdate = $itemObj[3].textContent;
						itemObj.videoid = $itemObj[4].textContent;
						itemObj.catTitle = $itemObj[5].textContent;
						itemObj.catTitle = getCatTitle(itemObj.catTitle);
						itemObj.thumbnailURL = $itemObj[6].attributes[0].textContent;
						itemObj.mediaUrl = $itemObj[7].attributes[1].textContent;
						itemObj.tvguide = getTVGuideURL(itemObj.videoid);
						setVideoMenu(itemObj);
						initPlayer(itemObj);
						return false;
					}
				//ie6
				} else {
					if (videoId == 'init' || $itemObj[4].text == videoId) {
						itemObj.title = $itemObj[0].text;
						itemObj.link = $itemObj[1].text;
						itemObj.description = $itemObj[2].text;
						itemObj.pubdate = $itemObj[3].text;
						itemObj.videoid = $itemObj[4].text;
						itemObj.catTitle = $itemObj[5].text;
						itemObj.catTitle = getCatTitle(itemObj.catTitle);
						itemObj.thumbnailURL = $itemObj[6].attributes[0].text;
						itemObj.mediaUrl = $itemObj[7].attributes[1].text;						
						itemObj.tvguide = getTVGuideURL(itemObj.videoid);
						setVideoMenu(itemObj);
						initPlayer(itemObj);
						return false;
					}
				}
			});
		} else {
			
		}
		
	}, 'xml');
};

//set tv guide url by traversing the HTML and checking appropriate videoId
function getTVGuideURL(videoid) {
	var tvguide = '';
	if (videoid != null) {
		$('div.vidItem').each(function(){
			var $vidItemObj = $(this).children();
			if ($(this)[0].id == videoid) {
				//w3c browsers
				if ($vidItemObj[3].textContent != undefined) {
					tvguide = $vidItemObj[3].textContent;
				//ie6
				} else {
					tvguide = $vidItemObj[3].text;
				}				
			}
		});
		return tvguide;
	}
}

//set the video menu (title, desc and tv guide link)
function setVideoMenu(itemObj) {
	//set video data
	$('#vpHeadings h4').html(itemObj.title);
	$('.vidinfo h4').html(itemObj.title);
	$('.vidinfo div#vidDesc').html(itemObj.description);
	if(itemObj.tvguide && itemObj.tvguide !=''){
		$('#tv-guide-link').html('<p><a href="' + itemObj.tvguide + '">See TV listings for this programme</a></p>');
	}
}

//set up flash Sky video player
function initPlayer(itemObj) {
	
	var configPath = "/inc/player/config.xml"; //default config path
	
	switch(window.location.hostname)
	{
		case "skyarts.local":
			configPath = "/inc/player/config_local.xml"		  
			break;
		case "stage.skyarts.sky.com":
			configPath = "/inc/player/config_stage.xml"
			break;
	}
	
	if ($('object#vidplayer').length > 0) {
		 var mRSSItem = buildNewRSSItem(itemObj);
		 playItem(mRSSItem);
	} else {
		var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
		if(is_chrome) {
			var width = "847";
		} else {
			var width = "849";
		}
		var height = "478",
			curPage = window.location;
			
		// Define the flashVars:    		
		var flashVars = {
			id: itemObj.videoid,
			title: itemObj.title,
			category: itemObj.catTitle,
			autoPlay: "false",
			autoPause: "true",
			configPath: configPath,
			pageUrl: curPage,
			mediaUrl: itemObj.mediaUrl
		};
		
		var params = {
			//needs to be relative to the player folder
			base:"/inc/player/", 
			allowfullscreen: "true",
			allowscriptaccess: "always"
		};
		
		var attributes = {
			name: "SkyMediaPlayer"
		};
		
		// Embed the player SWF onto the div named 'vidplayer' using the flashVars:
		try {
			swfobject.embedSWF("/inc/player/SkyMediaPlayer.swf", "vidplayer", width, height, "10.0.0", "/inc/player/expressInstall.swf", flashVars, params, attributes);
		} catch(e) {
						
		}
	}
}

//set ajax category and pagination links in video player page
function setAjaxLinks(){
	//set particular links to load within the page without browser  reload
	$('.vlcontainer .nav_categories a').click(function(){
	
		var link_url = $(this).attr('href'),
			mrss = '/video/mrss/C' + $(this).attr('name') + '/';

		$.get(link_url, function(data){		
			$(".vlcontainer").html($(".vlcontainer", data).html());
			$("head #videomrss").attr('href', mrss);
			setVideoLinks();
			setAjaxLinks();
		});
		return false;
	});
	
	//set particular links to load within the page without browser  reload
	$('.vlcontainer .plinks a').click(function(){
		var link_url = $(this).attr('href');
		
		$.get(link_url, function(data){
			$(".vlcontainer").html($(".vlcontainer", data).html());
			setVideoLinks();
			setAjaxLinks();
		});
		return false;
	});
	
}

//set video links in video player page
function setVideoLinks(){
	
	$('#vidlist div.vidItem').click(function(){ 
		var videoid = $(this).attr('id');
		getVideoMRSS(videoid);
		
		//auto scroll the page up to see the player
		if ( $('object#vidplayer').length > 0 ) {	
			$('html,body').animate({scrollTop: $('#vpHeadings').offset().top},'fast');
		}
		
		//set active state
		$('#vidlist div.vidItem a').removeClass('vidon');
		$('a', this).addClass('vidon');
	});	
}

// Function to retrieve the swf movie			
function retrieveSWF(swfName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[swfName];
	} else {
		return document[swfName];
	}
}

//load new video without initialising the whole flash player
function playItem(mRSSItem)	{
	retrieveSWF("SkyMediaPlayer").playItem(mRSSItem);
}

function buildNewRSSItem(itemObj){
	itemObj.description = "<![CDATA[" + itemObj.description + "]]>";
	var newMRSSItem = '<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss" xmlns:atom="http://www.w3.org/2005/Atom"><channel>\
		<title>Sky Arts</title>\
		<link>http://www.skyarts.co.uk/</link>\
		<description>' + itemObj.title + '</description>\
		<item>\
			<link>' + itemObj.link + '</link>\
			<title>' + itemObj.title + '</title>\
			<description>' + itemObj.description + '</description>\
			<pubDate>' + itemObj.pubdate + '</pubDate>\
			<guid>' + itemObj.videoid + '</guid>\
			<media:category>' + itemObj.catTitle + '</media:category>\
			<media:thumbnail url="' + itemObj.thumbnailURL + '" width="109" height="60" />\
			<media:content type="video/x-flv" url="' + itemObj.mediaUrl + '" medium="video" duration="0137" />\
			<media:description type="html">' + itemObj.description + '</media:description>\
		</item>\
	</channel>\
	</rss>';
	return newMRSSItem;
}










