var sVideoBrowserUrl = "/news/video/thumbnails?videoChannel=";

function checkPlaylistMode() {
		var currentURL = document.location.href;
		if (currentURL.search("&pl=") != -1) {
			if((currentURL.split(/&pl=/)[1].substr(0,4)) == "true") {
			  return true;
			} else {return false;}
		} else {return false;}
}

function switchInfo(iTabId) {
	var aAvailableTabs = ["nowPlaying", "myPlaylist"];
	for (i=0; i<aAvailableTabs.length; i++) {
		if (i == iTabId) {
			sNewClass = "";
			sNewTabClass = "current";
		} else {
			sNewClass = "hidden";
			sNewTabClass = "";
		}
		document.getElementById(aAvailableTabs[i]).className = sNewClass;
		document.getElementById("videoInfo"+i).className = sNewTabClass;
	}
}

function browseMyChannels() {
		redrawSubchannels("");
        document.getElementById("ch"+browsingChannel).className = "videoChannel";
        browsingChannel = "MyChannels";
        document.getElementById("chMyChannels").className = "videoChannel active";
        if ((document.getElementById("myChannelsInfo")) && (document.getElementById("myChannelsDetail"))) {
                document.getElementById("myChannelsInfo").innerHTML = document.getElementById("myChannelsDetail").innerHTML;
        }
        searchId=getAllSearches(userVideoSettings);

        document.getElementById("videoBrowserFrame").src = "/news/video/mychannels?searchId="+searchId;
}

function browseChannel(iChannelId) {
	document.getElementById("ch"+browsingChannel).className = "videoChannel";
	browsingChannel = iChannelId;
	document.getElementById("ch"+iChannelId).className = "videoChannel active";
	document.getElementById("videoBrowserFrame").src = sVideoBrowserUrl + iChannelId;
}

function loadComingNext() {
	if (!((nextVideo.id == null) || (nextVideo == null)) && (document.getElementById("comingNext"))) {
		switch(nextVideo.source) {
			case "shuffle":
				sComingNextText = "Next in shuffle:";
				if (strLocalization == 'JP') {
					sComingNextText = '次へ';
				}
				break;
			case "playlist":
				sComingNextText = "Next in playlist:";
				if (strLocalization == 'JP') {
					sComingNextText = 'プレイリスト　次へ';
				}				
				break;
			default:
				sComingNextText = "Coming up next:";
				if (strLocalization == 'JP') {
					sComingNextText = '次へ';
				}				
		}
		var sHTML = '<div id="nxtvid'+nextVideo.id+'" style="margin:0px;padding:0px;"><div class="video"><div class="videoPlaylister"><a href="#" onmousedown="addToPlaylist(event, \''+nextVideo.id+'\');"><img src="/resources/images/iconAddPlaylist.gif" border="0" alt="Add to Playlist" title="Add to Playlist" /></a></div>';
		if(checkPlaylistMode()) {
			sHTML +='<div class="videoThumb"><a href="/news/video?videoId='+nextVideo.id+'&videoChannel='+nextVideo.channel+'"  onclick="vpl.startPlaylist(\'/news/video?videoId='+nextVideo.id+'&videoChannel='+nextVideo.channel+'\');return false;"><img id="nextVideo_'+nextVideo.id+ '" src="'+nextVideo.thumbnail+'" width="66" height="49" border="0" alt="0" onerror="javascript:setImageSrcNotFound(document, this.id);"/></a></div>';
		} else {
			sHTML +='<div class="videoThumb"><a href="/news/video?videoId='+nextVideo.id+'&videoChannel='+nextVideo.channel+'"><img id="nextVideo_'+nextVideo.id+ '" src="'+nextVideo.thumbnail+'" width="66" height="49" border="0" alt="0" onerror="javascript:setImageSrcNotFound(document, this.id);"/></a></div>';
		}
		if (checkPlaylistMode()) {
			sHTML += '<div class="videoText"><h6>' + sComingNextText + '</h6><a href="/news/video?videoId='+nextVideo.id+'&videoChannel='+nextVideo.channel+'"  onclick="vpl.startPlaylist(\'/news/video?videoId='+nextVideo.id+'&videoChannel='+nextVideo.channel+'\');return false;"><img src="http://static.reuters.com/resources/images/iconVideo.gif" border="0" alt="Video" class="inlineLinkIcon" />' + decodeURIComponent(nextVideo.title) + '</a>';
		} else {
			sHTML +='<div class="videoText"><h6>'+sComingNextText +'</h6><a href="/news/video?videoId='+nextVideo.id+'&videoChannel='+nextVideo.channel+'"><img src="http://static.reuters.com/resources/images/iconVideo.gif" border="0" alt="Video" class="inlineLinkIcon" />'+decodeURIComponent(nextVideo.title)+'</a>';
		}
		sHTML +='</div><div class="videoInfo">('+nextVideo.duration+") "+nextVideo.type+'</div></div></div>';
		document.getElementById("comingNext").innerHTML = sHTML;		
	}
}

function returnToLastVideo() {
	try {
		back();
	} catch(e) {}
}

function skipToNextVideo() {
	if (!((nextVideo.id == null) || (nextVideo == null))) {
		var newLocation = ReplaceQueryStringParam(document.location.href, "videoId", nextVideo.id);
		newLocation = ReplaceQueryStringParam(newLocation, "videoChannel", nextVideo.channel);
		if (newLocation.search("&pos=") != -1) {
			newLocation = ReplaceQueryStringParam(newLocation, "pos", 0);
		}
		document.location.href = newLocation.replace("&refresh=true", "");
	}	
}

function loadNextVideo() {
	if (!((nextVideo.id == null) || (nextVideo == null))) {
		var newLocation = ReplaceQueryStringParam(document.location.href, "videoId", nextVideo.id);
		newLocation = ReplaceQueryStringParam(newLocation, "videoChannel", nextVideo.channel);
		if (nextVideo.userChannel==undefined)
			;
		else {
			newLocation = ReplaceQueryStringParam(newLocation, "searchName", nextVideo.userChannel);
		}
		if (newLocation.search("&pos=") != -1) {
			newLocation = ReplaceQueryStringParam(newLocation, "pos", 0);
		}
		document.location.href = newLocation.split(/&refresh=true/)[0]+'&refresh=true';
	}
}

checkNextVideo = function() {
	if (getCookie("videoShuffle") != null) {
		getShuffleChannel();
	} else {
		loadComingNext();
	}
}

loadStylesheet = function (sUrl, sId) {
	if (!(document.getElementById(sId))) {
		if (document.createStylesheet) {
			document.createStylesheet(sUrl);
			return;
		}
		if (document.createElement && document.getElementsByTagName) {
			var head = document.getElementsByTagName("head")[0];
			var oCSS = document.createElement("link");
			oCSS.id = sId;
			oCSS.rel = "stylesheet";
			oCSS.href = sUrl;
			oCSS.type = "text/css";
			head.appendChild(oCSS);
		}
	}
}

loadAdSkin = function() {
	loadStylesheet("/resources/css/video-skinmode.css","videoAdSkin");
}

launchVideoPopup = function(startPosition) {
	if (currentVideo.id != null) {
		var sUrl = "/news/video/popup?videoId=" + currentVideo.id + "&videoChannel=" + currentVideo.channel + "&pos=" + startPosition; 
		commonPopup(sUrl, 650, 572, 4, "videopopup");
	}
}

function sendPauseVideo() {
	try { console.debug("Sending Pause Video"); } catch (e) {}
	var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
	if (InternetExplorer) {
		mc = window.videocontroller;
	} else {
		mc = window.document.videocontroller;
	}
	if(mc != null) {
		mc.GotoFrame(2);
	} 
}

function sendPlayVideo() {
	var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
	if (InternetExplorer) {
		mc = window.videocontroller;
	} else {
		mc = window.document.videocontroller;
	}
	if(mc != null) {
		mc.GotoFrame(3);
	} 
}




var fvCarousel = {         //fvCarousel is a global object to contain Featured Video Carousel objects, methods, variables
	howManyToDisplay: "",
	currentPosition: "",
	videos: [],
	updateCurPos : function (int) {
		this.currentPosition = parseInt(this.currentPosition+int, 10);
	},
	addVideo : function (strTitle,strThumb,strLink, strDuration, strType) {
			vidId = ""+strLink.split("videoId=")[1];
			vidId = vidId.split("&")[0];
			var o = {"title": strTitle, "thumbnail": strThumb,  "vidLink": strLink, "id": vidId, "duration": strDuration, "type": strType};
			this.videos.push(o);
	},
	getDisplay: function () {
		var howManyToDisplay  = this.howManyToDisplay;
		var currentPosition   = this.currentPosition;
		var videos            = this.videos;
		var returnArray       = [];
		var numVideos         = videos.length;
		var displayBoundary   = (currentPosition-1) + howManyToDisplay;
		if(currentPosition < numVideos && displayBoundary < numVideos && howManyToDisplay <= numVideos && currentPosition >= 0) {
			for(var i=currentPosition;i<=displayBoundary;i++) {
				returnArray.push({"thumbnail": videos[i].thumbnail, "title": videos[i].title, "vidLink": videos[i].vidLink, "id": videos[i].id, "duration": videos[i].duration, "type": videos[i].type});
			}
			return returnArray;
		} else {
				return [];
		}
	}	
};
function fvCarousel_btnClick(e) {
	switch(this.id.charAt(13)) {
		case "L" :
			if(document.getElementById(this.id).className === "buttonActive"){
				document.getElementById(this.id).style.cssText = "margin-left: -1px; padding: 1px 2px 0 0;";
			}
			break;    
		case "R" :
			if(document.getElementById(this.id).className === "buttonActive"){
				document.getElementById(this.id).style.cssText = "padding: 1px 0 0 1px;";
			}
			break;
		default:
		}
}

function fvCarousel_btnRelease(e) {
	if (!e) var e = window.event;
	document.getElementById(this.id).style.cssText = "";
}

function refresh_fvCarousel(arr, elem) {
	var vidHTML = fvCarousel_createContainer(arr[0], "videoFeature1");
	vidHTML = vidHTML + fvCarousel_createContainer(arr[1], "videoFeature2");
	elem.innerHTML = vidHTML;	
}

function setImageSrcNotFound(doc,elemId) {
	doc.getElementById(elemId).src="/resources/images/default_video_thumb.jpg";
}



function fvCarousel_createContainer(o, id) {
	var vidHTML = '<div id="fvid'+o.id+'">';
	   vidHTML += '<div id="'+id+'" class="videoFeature">';
	   vidHTML += '<div id="'+id+'pl" class="videoPlaylister"><a href="#" onmousedown="addToPlaylist(event,\''+o.id+'\');">';
	   vidHTML += '<img src="/resources/images/iconAddPlaylist.gif" border="0" alt="Add to Playlist" title="Add to Playlist" /></a></div>';
	   vidHTML += '<div id="'+id+'p" class="videoThumb"><a href="'+o.vidLink+'">';
	   vidHTML += '<img id="carusel_' + o.id + '" src="'+o.thumbnail+'" width="64" height="47" border="0" alt="0" onerror="javascript:setImageSrcNotFound(document, this.id);" /></a></div>';
	   vidHTML += '<div id="'+id+'t" class="videoText">';
	   vidHTML += '<img src="/resources/images/iconVideo.gif" border="0" alt="Video" class="inlineLinkIcon" /><a href="'+o.vidLink+'">'+decodeURIComponent(o.title)+'</a>';
	   vidHTML += '</div><div class="videoInfo">('+o.duration+') '+o.type+'</div>';
	   vidHTML += '</div>';
	   vidHTML += '</div>';
	return vidHTML;
}

function disable_fvCarouselBtn(buttonElem) {
	buttonElem.setAttribute("class", "buttonInactive");
	buttonElem.setAttribute("className", "buttonInactive"); //for IE
}

function enable_fvCarouselBtn(buttonElem) {
	buttonElem.setAttribute("class", "buttonActive");
	buttonElem.setAttribute("className", "buttonActive"); //for IE
}

function fvCarouselNextClick() {
	fvCarousel.updateCurPos(1);
	var arr = fvCarousel.getDisplay();
	if(arr.length !== 0) {
		refresh_fvCarousel(arr,document.getElementById("featVideoPlatform"));
		enable_fvCarouselBtn(document.getElementById("featuredVideoL"));
		if(fvCarousel.currentPosition+fvCarousel.howManyToDisplay === fvCarousel.videos.length) {
			disable_fvCarouselBtn(document.getElementById("featuredVideoR"));
		}
	} else {
		fvCarousel.updateCurPos(-1);
		disable_fvCarouselBtn(document.getElementById("featuredVideoR"));
	}
}

function fvCarouselPrevClick() {
	fvCarousel.updateCurPos(-1);
	var arr = fvCarousel.getDisplay();
	if(arr.length !== 0) {
		refresh_fvCarousel(arr,document.getElementById("featVideoPlatform"));
		enable_fvCarouselBtn(document.getElementById("featuredVideoR"));
		if(fvCarousel.currentPosition === 0) {
			disable_fvCarouselBtn(document.getElementById("featuredVideoL"));
		}
	} else {
		fvCarousel.updateCurPos(1);
		disable_fvCarouselBtn(document.getElementById("featuredVideoL"));
	}		
}

//video share
var RTR_VideoURL = location.href;
if(RTR_VideoURL.search("refresh=") != -1) {
	RTR_VideoURL = RTR_VideoURL.split("?refresh=")[0];
}
function videoShareLink(strEdtition, strId){
	if (typeof(RTR_VideoTitle) != 'undefined') {
		if (strEdtition == "US") {
			switch (strId) {
				case 'delicious':
					commonPopup('http://del.icio.us/post?v=4&noui&jump=close&url=' + encodeURIComponent(RTR_VideoURL + "&rpc=28") + '&title=' + encodeURIComponent(RTR_VideoTitle) + '&notes=' + encodeURIComponent(RTR_VideoBlurb) + '&tags=news', 700, 500, 1, 'deliciousPopup');
					break;
				case 'digg':
					commonPopup('http://digg.com/remote-submit?phase=2&url=' + encodeURIComponent(RTR_VideoURL + "&rpc=64") + '&title=' + encodeURIComponent(RTR_VideoTitle) + '&bodytext=' + encodeURIComponent(RTR_VideoBlurb), 540, 500, 1, 'diggPopup');
					break;
				case 'facebook':
					commonPopup('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(RTR_VideoURL + "&rpc=60") + '&t=' + encodeURIComponent(RTR_VideoTitle), 626, 436, 1, 'facebookPopup');
					break;
				case 'mixx':
					commonPopup('http://www.mixx.com/submit/video?title=' + encodeURIComponent(RTR_VideoTitle) + '&description=' + encodeURIComponent(RTR_VideoBlurb) + '&partner=REU&media_url=' + encodeURIComponent(RTR_VideoURL) + '&page_url=' + encodeURIComponent(RTR_VideoURL + "&rpc=62"), 1024, 640, 1, 'mixxPopup');
					break;
				case 'yahoo':
					commonPopup('http://buzz.yahoo.com/article/REUTERS/' + encodeURIComponent(RTR_VideoURL + "&rpc=46"), 980, 910, 1, 'yahooPopup');
					break;
			}
		}
		else if(strEdtition == "JP")  {
				switch (strId) {
				case 'hatena':
					commonPopup('http://b.hatena.ne.jp/append?'+RTR_VideoURL, 540, 500, 1, 'hatenaPopup');
					break;
				case 'livedoor':
					commonPopup('http://clip.livedoor.com/clip/add?link='+RTR_VideoURL+ "&title=" + encodeURIComponent(RTR_VideoTitle) + "&jump=myclip", 800, 500, 1, 'livedoorPopup');
					break;
				case 'yahoo':
					commonPopup('http://bookmarks.yahoo.co.jp/action/bookmark?t=' + encodeURIComponent(RTR_VideoTitle) + "&u=" + RTR_VideoURL, 800, 500, 1, 'yahoobmPopup');
					break;
				case 'newsIng':
					commonPopup('http://newsing.jp/nbutton?title=' + encodeURIComponent(RTR_VideoTitle) + "&url=" + RTR_VideoURL, 880, 500, 1, 'newsingPopup');
					break;
				case 'buzzurl':
					commonPopup('http://buzzurl.jp/entry/' + RTR_VideoURL, 800, 500, 1, 'buzzurlPopup');
					break;
				case 'choix':
					commonPopup('http://www.choix.jp/bloglink/' + RTR_VideoURL, 800, 500, 1, 'choixPopup');
					break;
				}				
		}
	}
}
