var videos = new Array();
var rounds = new Array();
var player = new Array();
var load = true;
var playerSel;
var roundSel;
var itemHTML;
var currentClip;
var pageURL = document.location.href;
var pageTitle = 'usopen.com Video Player';
var pageAbstr = 'Video Highlights from U.S. Open 2009';
var videoTitle;
var videoStars;
var params = window.location.search;
var pvAdTag = "http://ad.doubleclick.net/pfadx/usga.usopen/video;tile=1;sz=1x2000;dcmt=application/smil;ord=[server-side rnd/timestamp]?";
$(document).ready(function(){
writeInlinePlayer('inlinePlayer', '/images/misc/usg_ms_video_bg.jpg', latestMediaFile, '640', '360');
$.ajax({
url: '/en_US/xml/gen/videos.xml',
type: 'GET',
dataType: 'xml',
error: function(){
alert('Error loading data');
},
success: function(xmlResp){
setVideoObjects(xmlResp);
}
});
});
/***************************************************************************
function to remove duplicate items in an array
***************************************************************************/
Array.prototype.unique = function () {
var r = new Array();
o:for(var i = 0, n = this.length; i < n; i++)
{
for(var x = 0, y = r.length; x < y; x++)
{
if(r[x]==this[i])
{
continue o;
}
}
r[r.length] = this[i];
}
return r;
}
/***************************************************************************
function to get and sort options for the round dropdown
***************************************************************************/
function setRoundDropdown() {
var selectRoundDropdown = document.getElementById('selectRoundList');
for (i = 0; i < selectRoundDropdown.options.length; i++) {
selectRoundDropdown.remove(i);
}
rounds.length = 0;
playerSel = $("#selectPlayerList").val();
rounds.push('By Round');
for (x = 0; x < videos.length; x++) {
if (videos[x].category == category) {
if (videos[x].player == playerSel) {
rounds.push(videos[x].round);
} else if (load) {
rounds.push(videos[x].round);
}
}
}
rounds = rounds.unique();
for (x = 0; x < rounds.length; x++) {
selectRoundDropdown.options[x] = new Option(rounds[x], rounds[x]);
if (x == 0) {
selectRoundDropdown.options[x].selected = true;
roundSel = $("#selectRoundList").val();
}
}
}
/***************************************************************************
function to set and sort the player dropdown
***************************************************************************/
function setPlayerDropdown(){ 
var selectPlayerDropdown = document.getElementById('selectPlayerList');
var playerList = "";
var roundList = "";
player.push('Search by Player');
for(x=0; x<videos.length; x++){
if (videos[x].category == category) {
if(videos[x].player && videos[x].player != ''){
for (var j = 0; j < videos[x].player.length; j++) {
player.push(videos[x].player[j]);
}
}
}
}
player = player.unique();
for(x=0; x<player.length; x++){
selectPlayerDropdown.options[x] = new Option(player[x],player[x]);
if(x==0){selectPlayerDropdown.options[x].selected = true;}
}
setRoundDropdown();
}
/***************************************************************************
function to create video objects
***************************************************************************/
function video(){
this.player;
this.round;
this.picture;
this.abstract;
this.link;
this.description;
this.sort_date;
this.editor;
this.stars;
this.votes;
this.category;
}
/***************************************************************************
function to populate video objects
***************************************************************************/
function setVideoObjects(xmlDoc){ 
$(xmlDoc).find("video").each(function() {
videos[videos.length] = new video();
videos[videos.length-1].player = $(this).find('player').text().split('|');
videos[videos.length-1].round = $(this).find('round').text();
videos[videos.length-1].picture = $(this).find('picture').text();
videos[videos.length-1].abstract = $(this).find('abstract').text();
videos[videos.length-1].link = $(this).find('link').text();
videos[videos.length-1].description = $(this).find('description').text();
videos[videos.length-1].date = $(this).find('date').text();
videos[videos.length-1].stars = $(this).find('stars').text();
videos[videos.length-1].sort_date = $(this).find('sort_date').text();
videos[videos.length-1].editor = $(this).find('editor').text();
videos[videos.length-1].votes = $(this).find('votes').text();
videos[videos.length-1].category = $(this).find('category').text();
});
if(load){
setPlayerDropdown();
viewCategory();
setEditorPicks(videos);
setSuggestedVideos(videos);
load = false;
}
}
/***************************************************************************
function to set the editors section
***************************************************************************/
function setEditorPicks(videos) {
for(x=0; x<videos.length; x++){
if(videos[x].editor){
if ((videos[x].editor>0) && (videos[x].editor<5)) {
var build =
'<table border="0" cellpadding="0" cellspacing="0" height="224">';
if (videos[x].picture) {
build = build + '<tr><td valign="top"><a href="javascript:playVideo(\''+videos[x].link+'\','+x+');"><img class="editorPic" src="' + videos[x].picture + '" width="140" height="80"' +
'alt="' + videos[x].abstract + '" style="display:block; border:1px solid #676767;"/></a></td></tr>';
} else {
//build = build + '<tr><td height="80">&nbsp;</td></tr>'
}
build = build + '<tr><td><b>' + videos[x].abstract + '</b><br/>' + videos[x].date +
'</td></tr>' +
'<tr><td height="70">' + videos[x].description + '</td></tr>' +
'<tr><td>'
if ((videos[x].stars != "") && (videos[x].votes)){
for ( var i = 1; i <= 5; i++) {
if (i <= videos[x].stars){
build += '<img style="display:inline;" src="/images/misc/usg_ms_02000g1_on.gif" width="11" height="10" alt="'+videos[x].stars + ' of 5 Stars" border="0" />';
}
else {
build += '<img style="display:inline;" src="/images/misc/usg_ms_02000g1_off.gif" width="11" height="10" alt="'+videos[x].stars + ' of 5 Stars" border="0" />';
}
}
build += '<span class="italic"> ' + videos[x].votes + ' votes</span>';
//build += videos[x].stars + ' of 5 Stars. ' + videos[x].votes +
// ' total votes';
} else {
//build += 'no votes available';
}
build += '</td></tr></table>'
document.getElementById('editor'+videos[x].editor).innerHTML = build;
}
}
} // for
}
/***************************************************************************
function to set the suggested videos section
***************************************************************************/
function setSuggestedVideos(videos){
var suggest_count = 1;
for(x=0; x<videos.length; x++){
if(videos[x].category.toLowerCase() == category.toLowerCase()) {
// if(videos[x].editor){
// if ((videos[x].editor>0) && (videos[x].editor<4)) {
var build =
'<table border="0" cellpadding="0" cellspacing="0" height="235" align="center">';
if (videos[x].picture) {
// 	 build = build + '<tr><td valign="top"><a href="javascript:playVideo(\''+videos[x].link+'\','+x+');"><img class="suggestedPic" src="' + videos[x].picture + '" width="138" height="80"' +
// 	 'alt="' + videos[x].abstract + '"/></a></td></tr>';
build = build + '<tr><td valign="top"><a href="javascript:playVideo(\''+videos[x].link+'\','+x+');"><img class="suggestedPic" src="' + videos[x].picture + '" width="300" height="171"' +
'alt="' + videos[x].abstract + '"/></a></td></tr>';
$("#suggestPlayButton").attr("href","javascript:playVideo(\'"+videos[x].link+"'\,'"+x+"');");
} else {
//build = build + '<tr><td height="80">&nbsp;</td></tr>'
}
build = build + '<tr><td><b>' + videos[x].abstract + '</b><br/>' + videos[x].date +
'</td></tr>' +
'<tr><td height="70">' + videos[x].description + '</td></tr>' +
'<tr><td>'
if ((videos[x].stars != "") && (videos[x].votes)){
for ( var i = 1; i <= 5; i++) {
if (i <= videos[x].stars){
build += '<img style="display:inline;" src="/images/misc/usg_ms_02000g2_on.gif" width="11" height="10" alt="'+videos[x].stars + ' of 5 Stars" border="0" />';
}
else {
build += '<img style="display:inline;" src="/images/misc/usg_ms_02000g2_off.gif" width="11" height="10" alt="'+videos[x].stars + ' of 5 Stars" border="0" />';
}
}
build += '<span class="italic"> ' + videos[x].votes + ' votes</span>';
//build += videos[x].stars + ' of 5 Stars. ' + videos[x].votes +
// ' total votes';
} else {
//build += 'no votes available';
}
build += '</td></tr></table>'
document.getElementById('suggest'+suggest_count).innerHTML = build;
//alert('here');
$('#suggestPlayButtonDiv').css('visibility','visible');
suggest_count++;
if (suggest_count > 0) { break; }
// }
}
} // for
}
/***************************************************************************
function to get values from dropdowns
***************************************************************************/
function getSelectVals(){
playerSel = $("#selectPlayerList").val();
roundSel = $("#selectRoundList").val();
if(!load){
viewCategory();
}
}
/***************************************************************************
function to write the video list
***************************************************************************/
function writeVideoLine(x){
itemHTML+='<li>';
if(videos[x].picture != ""){
itemHTML+='<div class="picture inactivePic"><a href="javascript:playVideo(\''+videos[x].link+'\','+x+');"><img style="display:inline;padding:3px 1px 2px;" src="'+videos[x].picture+'" width="76" height="42" border="0" alt="'+videos[x].abstract+'"/></a></div>';
itemHTML+= '<div class="text inactive"><a href="javascript:playVideo(\''+videos[x].link+'\','+x+');">'+videos[x].abstract+'</a>' +
'<br/>' + videos[x].date + '<br/>';
} else {
itemHTML+= '<div class="text content2"><a href="javascript:playVideo(\''+videos[x].link+'\','+x+');">'+videos[x].abstract+'</a>' +
'<br/>' + videos[x].date + '<br/>';
}
for ( var i = 1; i <= 5; i++) {
if (videos[x].stars == '' || videos[x].stars == 0) {
itemHTML += '<img style="display:inline;" src="/images/misc/usg_ms_02000g2_off.gif" width="11" height="10" alt="'+videos[x].stars + ' of 5 Stars" border="0" />';
} else {
if (i <= videos[x].stars){
itemHTML += '<img style="display:inline;" src="/images/misc/usg_ms_02000g2_on.gif" width="11" height="10" alt="'+videos[x].stars + ' of 5 Stars" border="0" />';
}				
}
}
itemHTML+='</div>'; 
itemHTML+='</li>';
}
/***************************************************************************
This function gets called when the view by category tab is clicked or a sort button is clicked
***************************************************************************/
function viewCategory(){
document.getElementById('tabsVideosList').innerHTML = "";
listHTML="";
itemHTML="";
for(var x=0; x<videos.length; x++){
if(videos[x].category.toLowerCase() == category.toLowerCase()) {
for (var j=0; j < videos[x].player.length; j++){
if (load) {
writeVideoLine(x); 
}
else if(((videos[x].player[j] == playerSel) || (playerSel == 'Search by Player')) && 
((videos[x].round == roundSel) || (roundSel == 'By Round'))){
writeVideoLine(x);
}
}
}
}
if(itemHTML == ""){
listHTML = '<div id="videosList">There are no videos that match your current selections.</div>';
} else {
listHTML+='<div id="videosList"><ul>'+itemHTML+'</ul></div>';
}
document.getElementById('tabsVideosList').innerHTML = listHTML;
}
/***************************************************************************
This function gets called when a sort button is clicked
***************************************************************************/
function orderByStarRatings() {
videos = videos.sort(sortByStars);
viewCategory();
} // sortByStars
function sortByStars (a,b) {
var x = a.stars;
var y = b.stars;
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
} // sortByStars
/***************************************************************************
This function gets called when a sort button is clicked
***************************************************************************/
function orderByLatest() {
videos = videos.sort(sortByLatest);
viewCategory();
} // sortByStars
function sortByLatest (a,b) {
var x = a.sort_date;
var y = b.sort_date;
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
} // sortByStars
/***************************************************************************
This function gets called when a video is selected for viewing
***************************************************************************/
function playVideo(med,which){
// var curRatingHTML = '';
// var yourRatingHTML = '';
// var yourRating = 0;
// var videoName = videos[which].abstract;
// document.getElementById('currentRating').innerHTML = '';
// document.getElementById('yourRating').innerHTML = '';
setNewVideo('inlinePlayer', med);
hideDiv('replayButton');
hideDiv('suggestedClips');
hideDiv('suggestPlayButtonDiv');
showDiv('videoFunctions');
currentClip = which;
pageTitle = videos[which].abstract;
pageAbstr = videos[which].description;
pageURL = document.location.href + '?initVideo=' + videos[which].link;
//pageURL = document.location.href + '?videoStars='+videos[which].stars+'&videoTitle='+ videos[which].abstract +'&initVideo=' + videos[which].link;
setNowPlaying(pageTitle,videos[which].stars);
}
function setNowPlaying(title,stars) {
var curRatingHTML = '';
var yourRatingHTML = '';
var yourRating = 0;
document.getElementById('currentRating').innerHTML = '';
document.getElementById('yourRating').innerHTML = '';
document.getElementById('tabsVideosText').innerHTML = 
'<span style="color:#999999; text-decoration:none; font-size:12px;">' +
'NOW PLAYING >> </span><span style="color:#FFFFFF; text-decoration:none; '+
'font-weight:bold; font-size:12px;">' + title + '</span>';
if (stars == '') {stars=0;}
for ( var i = 1; i <= 5; i++) {
if (i <= stars){
curRatingHTML += '<img style="display:inline;" src="/images/misc/usg_ms_02000g3_on.gif" width="14" height="13" alt="'+stars + ' of 5 Stars" border="0" />';
}
else {
curRatingHTML += '<img style="display:inline;" src="/images/misc/usg_ms_02000g3_off.gif" width="14" height="13" alt="'+stars + ' of 5 Stars" border="0" />';
}
}
document.getElementById('currentRating').innerHTML = 'Current Video Rating: ' + curRatingHTML;
writeYourRating(title);
}
function writeYourRating(videoName) {
var yourRatingHTML = "";
var rating = getRating(videoName);
if (rating == 0) {
yourRatingHTML += 'Rate This Video >> ';
}
else {
yourRatingHTML += 'Your Rating >> ';
}
for ( var i = 1; i <= 5; i++) {
if (i <= rating){
if (rating == 0){
yourRatingHTML += '<a href="javascript:setRating('+i+');">' + '<img style="display:inline;" src="/images/misc/usg_ms_02000g4_on.gif" width="14" height="13" alt="' + rating + ' of 5 Stars" border="0" /></a>';
} else {
yourRatingHTML += '<img style="display:inline;" src="/images/misc/usg_ms_02000g4_on.gif" width="14" height="13" alt="' + rating + ' of 5 Stars" border="0" />';
}
}
else {
if (rating == 0){
yourRatingHTML += '<a href="javascript:setRating('+i+');">' + '<img style="display:inline;" src="/images/misc/usg_ms_02000g3_off.gif" width="14" height="13" alt="' + i + ' of 5 Stars" border="0" /></a>';
} else {
yourRatingHTML += '<img style="display:inline;" src="/images/misc/usg_ms_02000g3_off.gif" width="14" height="13" alt="' + i + ' of 5 Stars" border="0" />';
}
}
}
document.getElementById('yourRating').innerHTML = yourRatingHTML;
}
function setRating(newRating) {
var yourRatingHTML = 'Your Rating >> ';
var measureURL = '/en_US/video/FanStar_'+videos[currentClip].link + '_' + newRating + '.html';
setCookie(videos[currentClip].link, newRating);
measureClick(measureURL);
writeYourRating(videos[currentClip].link);
}
function getRating(videoName) {
if (null == getCookie(videos[currentClip].link)) {
return 0;
}
else {
return getCookie(videos[currentClip].link);
}
}
function setCookie(name, value) {
var curCookie = name + "=" + escape(value) + "; expires=Wed, 09-Apr-2016 11:11:11 GMT; path=" + curPath + ";";
document.cookie = curCookie;
}
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
var cook = unescape(dc.substring(begin + prefix.length, end));
return cook;
}
function handleClipComplete() {
showDiv('replayButton');
showDiv('suggestedClips');
showDiv('suggestPlayButtonDiv');	
}
$(document).ready(function(){
//jquery code for index rollovers
$("#videosList ul li").hover(
function() {
$(this).find('div.text').removeClass('inactive');$(this).find('div.text').addClass('active');
$(this).find('div.picture').removeClass('inactivePic');$(this).find('div.picture').addClass('activePic');},
function() {$(this).find('div.text').removeClass('active');$(this).find('div.text').addClass('inactive'); 
$(this).find('div.picture').removeClass('activePic');$(this).find('div.picture').addClass('inactivePic');}
);
$("#videosList ul li").hover(
function() {
$(this).find('div.text').removeClass('inactive');$(this).find('div.text').addClass('active');
$(this).find('div.picture').removeClass('inactivePic');$(this).find('div.picture').addClass('activePic');},
function() {$(this).find('div.text').removeClass('active');$(this).find('div.text').addClass('inactive'); 
$(this).find('div.picture').removeClass('activePic');$(this).find('div.picture').addClass('inactivePic');}
);
$(".suggestedPic").hover(
function() {$(this).addClass('pictureBorder');},
function() {$(this).removeClass('pictureBorder');}
);
if (document.location.href.indexOf('initVideo') > -1) {
var playClip = window.location.search.split("=")[1];
for(x=0; x<videos.length; x++){
if (videos[x].link == playClip) {
setNowPlaying(videos[x].abstract, videos[x].stars);
} //
} // #for each video
}
});