 var AJAX_Pulling = false; 
var playerReady = 0; 
var newPlayer = ""; 
var outputHTML = ""; 
var paginationHTML = "";  
var myPlaylist = new Array();
          
function initialLoad(basketID, tabBasketID) {
	//alert("initial");   	  
	loadBasket(basketID, 1); 
	
	
	InSkinLoad.init(InSkinParams);
	 
		if (GetCookie('planetrugby_video_playlist')) { 
			//load the playlist into memory
			myPlaylist.load(GetCookie('planetrugby_video_playlist'));
		} else {
			//paint the default text into the playlist
			myPlaylist.refresh();
		}
	 if(tabBasketID) {
		loadTabBasket(tabBasketID, 1, ""+tabBasketID+"");
	}	
}     
      
function loadAbstract(storyId) { 
	if (storyId) {
		var clipAbstract = $("#clipAbstract"+storyId).val();
		var clipTitle = $("#clipTitle"+storyId).val();
		var clipDate = $("#clipDate"+storyId).val();  
		
		if (clipTitle.match(/%20/)) clipTitle = unescape(clipTitle);
		if (clipDate.match(/%20/)) clipDate = unescape(clipDate);
		if (clipAbstract.match(/%20/)) clipAbstract = unescape(clipAbstract);
	 
		var clipHTML = ""; 
		  
		 
			if (clipTitle) clipHTML += "<h2>" + clipTitle.escapeHTML() + "</h2>";
			if (clipDate) clipHTML += "<p class='text-caption'>" + clipDate.escapeHTML() + "</p>";
			if (clipAbstract) clipHTML += "<p>" + clipAbstract.escapeHTML() + "</p>"
		 $("#video-detail").html(clipHTML);
	}
}   
  
function loadBasket(vidBasketID, thisPageNum) {
   
	var myPageNum = thisPageNum;
	var numVids = 8;
	 
	$("#page-list").empty(); 
	$("#video-list").empty(); 
	 
	$("#video-list").html('<div align="center" style="width:300px;color:#000000"><img src="/Images/skysports/site/loadingAnimation.gif" border="0" /><br/><br/>Finding Video Clips</div>');
	 
	paginationHTML ="";  
	outputHTML ="";  
	
	var loadBasketURL = "/video_clip_list_xml/0,25682,thisisnotanoid_thisisnotapagenum_thisisnotavidnum,00.xml";
	
	if (typeof vidBasketID == "undefined") {vidBasketID = 15715;}
	if (typeof myPageNum == "undefined") {myPageNum = 1;}
 
	loadBasketURL = loadBasketURL.replace("thisisnotanoid", vidBasketID);
	loadBasketURL = loadBasketURL.replace("thisisnotapagenum", myPageNum);
	loadBasketURL = loadBasketURL.replace("thisisnotavidnum", numVids);
	 
	if (AJAX_Pulling != true) {
		AJAX_Pulling = true;
		
		$.get(loadBasketURL, {}, function(xmlData) {
			var vLength = $(xmlData).find('clip').length;
			var basketName = $(xmlData).find('clipList').attr("basketName");
			 
			if (vLength > 0) {	
			   
			   
					outputHTML += "<ul>";
				 $('clip',xmlData).each(function(i) {
				  
					i++; //i starts on 0, we need it to start with 1
					var cStoryID = $(this).attr("storyID");
					var cDate = $(this).attr("clipDate");	
					var cTitle = $(this).attr("title");
					var cAbstract = $(this).attr("abstract"); 
					
					var cID = $(this).attr("id");
					var cVidPath = $(this).attr("vidPath");	
					var cThumb = $(this).attr("thumbPath");
					var cOrigin = $(this).attr("origin");
					  
					if ((i%4) == 0) {
						 var clipClass = "no-bdr"
					} else { 
						var clipClass = ""	
					}
				    
					var cLinktag = " href=\"javascript:;\" onclick=\"javascript:fetchAndLoad('"+ cID +"_"+ cStoryID +"'); return false;\" "
					var cPlaylistTag = " href=\"javascript:;\" onclick=\"javascript:myPlaylist.addClip('"+ cID +"', '"+ cStoryID +"'); return false;\" "
					
					
						outputHTML += "<li class=\"" + clipClass + "\">";
						outputHTML += "<a "+ cLinktag +" title=\""+ cTitle +"\"><img src=\""+ cThumb +"\" alt=\""+ cTitle +"\" /></a>";
						outputHTML += "<p>"+ cDate +"</p>";
						outputHTML += "<h3>"+ cTitle.escapeHTML() +"</h3>";

						outputHTML += "<ul class=\"pr-clip-btns\">";						
	        			outputHTML += "<li><a "+ cLinktag +" title=\"Watch Now\">Watch Now<img src=\"/Images/PlanetRugby/Icons/pr-clip-btns-watch.gif\"></a></li>";
	    				outputHTML += "<li class=\"playlist\"><a "+ cPlaylistTag +" title=\"Add to playlist\">Add to playlist<img src=\"/Images/PlanetRugby/Icons/pr-clip-btns-add.gif\"></a></li>";
	        			outputHTML += "</ul>";						
						outputHTML += "</li>";
					 //Hidden values used as temp store for loadAbstract func 		
					outputHTML += "<input type=\"hidden\" id=\"clipAbstract" + cStoryID + "\" value=\"" + cAbstract.escapeHTML() + "\" />";
					outputHTML += "<input type=\"hidden\" id=\"clipTitle" + cStoryID + "\" value=\"" + cTitle.escapeHTML() + "\" />";
					outputHTML += "<input type=\"hidden\" id=\"clipDate" + cStoryID + "\" value=\"" + cDate.escapeHTML() + "\" />";
					outputHTML += "<input type=\"hidden\" id=\"clipImage" + cStoryID + "\" value=\"" + cThumb + "\" />";
				});	 
				
				 
						outputHTML += "</ul>";	
				  } else {
				outputHTML = "<div style='color:#000000;font-size:16px;'>There are currently no video clips in '"+ basketName +"'</div>"
			}  
			
			//////// PAGINATION ////////////////
			var pLength = $(xmlData).find('page').length;
			var pCurrent = $(xmlData).find('pagination').attr("currentPage");
			var pTotal = $(xmlData).find('pagination').attr("totalPages");
			
			if (pLength > 0) {     
			
			
				if (thisPageNum != 1) {
				 	paginationHTML += "<li><a href=\"javascript:;\" onclick=\"javascript:loadBasket('"+ vidBasketID + "', '1'); return false;\" title=\"Go to First Page\">&laquo; First</a></li>";
				}
				if (pCurrent > 1) {
				 	paginationHTML += "<li><a href=\"javascript:;\" onclick=\"javascript:loadBasket('"+ vidBasketID + "', '" + (eval(pCurrent)-1) + "'); return false;\" title=\"Go to Previous Page\">Prev</a></li>";
				} 
			 $('page',xmlData).each(function(i) {
					var pNumber = $(this).attr("pageNumber");
					var pIsCurrent = $(this).attr("isCurrent");
					var pLink = " href=\"javascript:;\" onclick=\"javascript:loadBasket('"+ vidBasketID +"', '"+ pNumber +"'); return false;\" title=\"Page "+ pNumber +"\"";
					
					 if(pIsCurrent == 1) { 
						paginationHTML += "<li>"+ pNumber +"</li>";
					} else {
						paginationHTML += "<li><a "+ pLink +">"+ pNumber +"</a></li>";
					}
				});   

			
				if (eval(pCurrent)+1 <= pTotal) {
					paginationHTML += "<li class=\"\"><a href=\"javascript:;\" onclick=\"javascript:loadBasket('"+ vidBasketID + "', '" + (eval(pCurrent)+1) + "'); return false;\" title=\"Go to Next Page\" class=\"pag-next\" >Next</a></li>";
				}
				if (pCurrent != pTotal) {
					paginationHTML += "<li class=\" no-bdr\"><a href=\"javascript:;\" onclick=\"javascript:loadBasket('"+ vidBasketID + "', '" + pTotal + "'); return false;\" title=\"Go to Last Page\" >Last &raquo;</a></li>";
				}	
			 }    
			
				
				$("#video-list").html(outputHTML);
				$("#page-list").html(paginationHTML);
			 }, "xml");
		
		if( typeof videoPageLoadCallback == 'function' ) {
			videoPageLoadCallback();
		} 
		
		AJAX_Pulling = false;
	}  
}   

function loadTabBasket(vidBasketID, thisPageNum, tabName) {
	var myPageNum = thisPageNum;
	var numVids = 8; 
	
	$("#"+tabName).empty(); 
	 
	$("#"+tabName).html('<div align="center" class="finding-video-text" style="width:100%;margin-top:100px;color:#000000"><img src="/Images/skysports/site/loadingAnimation.gif" border="0" /><br/><br/>Finding Video Clips</div>');
	
	tabPaginationHTML ="";  
	tabOutputHTML ="";  
	
	var loadBasketURL = "/video_clip_list_xml/0,25682,thisisnotanoid_thisisnotapagenum_thisisnotavidnum,00.xml";

	if (typeof vidBasketID == "undefined") {vidBasketID = 15715;}
	if (typeof myPageNum == "undefined") {myPageNum = 1;}		

	loadBasketURL = loadBasketURL.replace("thisisnotanoid", vidBasketID);
	loadBasketURL = loadBasketURL.replace("thisisnotapagenum", myPageNum);
	loadBasketURL = loadBasketURL.replace("thisisnotavidnum", numVids);

	if (AJAX_Pulling != true) {
		AJAX_Pulling = true;
		 
		$.get(loadBasketURL, {}, function(xmlData) {
			var vLength = $(xmlData).find('clip').length;
			var basketName = $(xmlData).find('clipList').attr("basketName");

			if (vLength > 0) {	
			  
				tabOutputHTML += "<ul class='clips-wrapper'>";
				
				$('clip',xmlData).each(function(i) {
				 
					i++; //i starts on 0, we need it to start with 1
					var cStoryID = $(this).attr("storyID");
					var cDate = $(this).attr("clipDate");	
					var cTitle = $(this).attr("title");
					var cAbstract = $(this).attr("abstract");
					
					var cID = $(this).attr("id");
					var cVidPath = $(this).attr("vidPath");	
					var cThumb = $(this).attr("thumbPath");
		
					 var cOrigin = $(this).attr("origin");
					  
					if ((i%4) == 0) {
						 var clipClass = "no-bdr"
					} else { 
						var clipClass = ""	
					}
				  
					var cLinktag = " href=\"javascript:;\" onclick=\"javascript:fetchAndLoad('"+ cID +"_"+ cStoryID +"'); return false;\" "
					var cPlaylistTag = " href=\"javascript:;\" onclick=\"javascript:myPlaylist.addClip('"+ cID +"', '"+ cStoryID +"'); return false;\" "

					
						
						tabOutputHTML += "<li class=\"clip-box\">";
						tabOutputHTML += "<a "+ cLinktag +" title=\""+ cTitle +"\"><img src=\""+ cThumb +"\" alt=\""+ cTitle +"\" /></a>";
						tabOutputHTML += "<p class=\"text-caption\">"+ cDate +"</p>";
						tabOutputHTML += "<h3><a "+ cLinktag +" title=\""+ cTitle.escapeHTML() +"\">"+ cTitle.escapeHTML() +"</a></h3>";
						
						tabOutputHTML += "<ul class=\"pr-clip-btns\">";
	        			tabOutputHTML += "<li><a "+ cLinktag +" title=\"Watch Now\">Watch Now<img src=\"/Images/PlanetRugby/Icons/pr-clip-btns-watch.gif\"></a></li>";
	    				tabOutputHTML += "<li class=\"playlist\">";
						tabOutputHTML += "<a "+ cPlaylistTag +" title=\"Add to playlist\">";
						tabOutputHTML += "Add to playlist";
						tabOutputHTML += "<img src=\"/Images/PlanetRugby/Icons/pr-clip-btns-add.gif\">";
						tabOutputHTML += "</a></li>";
						tabOutputHTML += "</ul>";						
						
						tabOutputHTML += "</li>";

					 //Hidden values used as temp store for loadAbstract func 		
					tabOutputHTML += "<input type=\"hidden\" id=\"clipAbstract" + cStoryID + "\" value=\"" + cAbstract.escapeHTML() + "\" />";
					tabOutputHTML += "<input type=\"hidden\" id=\"clipTitle" + cStoryID + "\" value=\"" + cTitle.escapeHTML() + "\" />";
					tabOutputHTML += "<input type=\"hidden\" id=\"clipDate" + cStoryID + "\" value=\"" + cDate.escapeHTML() + "\" />";
					tabOutputHTML += "<input type=\"hidden\" id=\"clipImage" + cStoryID + "\" value=\"" + cThumb + "\" />";
				});	 
				
				tabOutputHTML += "</ul>";	
				 
			} else {
				tabOutputHTML = "<div class='no-video-text' style='color:#000000;font-size:16px;'>There are currently no video clips in '"+ basketName +"'</div>"
			}

			//////// PAGINATION ////////////////
			var pLength = $(xmlData).find('page').length;
			var pCurrent = $(xmlData).find('paginationTab').attr("currentPage");
			var pTotal = $(xmlData).find('paginationTab').attr("totalPages");
			
			if (pLength > 1) {     
 			    //Only want to see paging if there's more than one page
				
				if (thisPageNum != 1) {
				 	tabPaginationHTML += "<li><a href=\"javascript:;\" onclick=\"javascript:loadTabBasket('"+ vidBasketID + "', '1' , '"+ tabName + "'); return false;\" title=\"Go to First Page\">&laquo; First</a></li>";
				}
				
				if (pCurrent > 1) {
				 	tabPaginationHTML += "<li><a href=\"javascript:;\" onclick=\"javascript:loadTabBasket('"+ vidBasketID + "', '" + (eval(pCurrent)-1) + "' , '"+ tabName + "'); return false;\" title=\"Go to Previous Page\">Prev</a></li>";
				} 
				   
				$('page',xmlData).each(function(i) {
					var pNumber = $(this).attr("pageNumber");
					var pIsCurrent = $(this).attr("isCurrent");
					var pLink = " href=\"javascript:;\" onclick=\"javascript:loadTabBasket('"+ vidBasketID +"', '"+ pNumber +"', '"+ tabName +"'); return false;\" title=\"Page "+ pNumber +"\"";
						
					if(pIsCurrent == 1) {
						tabPaginationHTML += "<li>"+ pNumber +"</li>";
					} else {
						tabPaginationHTML += "<li><a "+ pLink +">"+ pNumber +"</a></li>";
					}
				}); 

				
					if (eval(pCurrent)+1 <= pTotal) {
						tabPaginationHTML += "<li class=\"\"><a href=\"javascript:;\" onclick=\"javascript:loadTabBasket('"+ vidBasketID + "', '" + (eval(pCurrent)+1) + "' , '"+ tabName + "'); return false;\" title=\"Go to Next Page\" class=\"pag-next\" >Next</a></li>";
					}
					if (pCurrent != pTotal) {
						tabPaginationHTML += "<li class=\" no-bdr\"><a href=\"javascript:;\" onclick=\"javascript:loadTabBasket('"+ vidBasketID + "', '" + pTotal + "' , '"+ tabName + "'); return false;\" title=\"Go to Last Page\" >Last &raquo;</a></li>";
					}	  
				 }// End Pagination    

			
			
				if(basketName.toLowerCase() == "highlights"){
					tabPaginationHTML += "<li class=\"tab-note\">*UK & ROI only.</li>";
				}
			 tabOutputHTML += "<ul class='paginationTab' id='page-list'>"+ tabPaginationHTML +"</ul>";
			$("#"+tabName).html(tabOutputHTML);
		}, "xml");
		
		AJAX_Pulling = false;
	}		

	  
}


String.prototype.escapeHTML = function () {                                       
return(                                                                 
    this.replace(/&/g,'&amp;').                                         
        replace(/>/g,'&gt;').                                           
        replace(/</g,'&lt;').                                           
        replace(/"/g,'&quot;').
		replace(/'/g,'&#39;')                                         
    );                                                                     
} 
function trim(str) {
	return str.replace(/^ss*/, '').replace(/ss*$/, '');
}



	var bcExp;
var modVP;
var modExp;
var modCon;
var modAd; 

function onTemplateLoaded(experienceID) {
	//alert("EVENT: The template has loaded");
	
	bcExp = brightcove.getExperience(experienceID);
	
	
	modExp = bcExp.getModule(APIModules.EXPERIENCE);
	modCon = bcExp.getModule(APIModules.CONTENT);
	modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
	modAd = bcExp.getModule(APIModules.ADVERTISING);
	
	
	modExp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
	modExp.addEventListener(BCExperienceEvent.CONTENT_LOAD, onInitialLoad);
	modVP.addEventListener(BCVideoEvent.VIDEO_LOAD, onVideoLoad); 
	//modCon.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad); 
	modVP.addEventListener(BCMediaEvent.BEGIN, onVideoBegin);
}

function onTemplateReady(evt) {
	
    //alert("EVENT: The Player has loaded correctly"); 	
	playerReady = 1;
}
  	//http://adserver.adtech.de/addyn/3.0/500/1857292/0/16/ADTECH;=2
	//http://adserver.adtech.de/?adrawdata/3.0/999.1/2176856/0/16/noperf=1;cc=2;header=yes;alias=myalias;cookie=yes;adct=204;key=key1+key2;grp=1234567;misc=
	
function onInitialLoad(evt) {	
	
	//alert("EVENT: BCExperienceEvent.CONTENT_LOAD");
   	
	//var currentVideo = modVP.getCurrentVideo();
	
	
   	//modCon.getVideoAsynch(currentVideo.id);	
	//modVP.loadVideo(currentVideo.id);
}
		
function onVideoLoad(evt) {	
	
	//alert("EVENT: BCVideoEvent.VIDEO_LOAD");
    //modVP.loadVideo(evt.video.id);
}

function onVideoBegin(evt) {
   // Event fired when video starts playback, only fired when video plays not before pre-roll
	
	
		//alert(evt.media.adKeys);
	}

var fetchedId = null; //this allows me to store the id i'm fetching for use in the onTitleLoad function
	
function fetchAndLoad(titleId) {
	
	//alert('Player Ready ' + playerReady + 'New Player ' + newPlayer + 'Title' + titleId + '')

	if (playerReady == 1) {
		
		var title_array=titleId.split("_");
		titleId = title_array[0];
		storyId = title_array[1];
		
		
		//modCon.getVideoAsynch(titleId);
		
		
		
		modVP.loadVideo(titleId);
		
		fetchedId = titleId; 	// stores the titleId in a global variable to be used in content load	
		loadAbstract(storyId); 	// pulls in the story infobelow the player
	} else {
		alert('Player loading, please wait');
	}
}

	/***************************************
	 Playlist funcs
**************************************/

Array.prototype.refresh=function() {

	var playlistClearHTML = "";
	var thePlaylistHTML = "";

	//re-paint the playlist on the page
	if ($("#vid-playlist")) {

		//re-add the clips in the current order
		if (this.length>0) {
		
			for (var p = 0;p< this.length;p++) {
			
				var pImg = this[p].getImg();
				var pAbs = this[p].getAbstr();
				var pDate = this[p].getPubdate();
				var pSid = this[p].getSid();
				var pTitle = this[p].getTitle();
				
				var pPlayLink = " href=\"javascript:;\" onclick=\"javascript:myPlaylist.playClip(" + p + "); return false;\" title=\"" + pTitle + "\"";
				var pRemoveLink = " href=\"javascript:;\" onclick=\"javascript:myPlaylist.removeClip(" + p + "); return false;\" title=\"Remove from playlist\"";
										
					
					thePlaylistHTML += "<div class=\"pr-playlist-clip no-bdr\">";
					if(pImg) {
					thePlaylistHTML += "<a "+ pPlayLink +"><img src=\"" + pImg + "\" alt=\"" + pTitle + "\" title=\"" + pTitle + "\" /></a>";
					}
					thePlaylistHTML += "<h3><a "+ pPlayLink +">"+ pTitle +"</a></h3>";
					
					thePlaylistHTML += "<ul class=\"pr-playlist-clip-btns\">";
					thePlaylistHTML += "<li><a "+ pPlayLink +"><img src=\"/Images/PlanetRugby/Icons/pr-clip-btns-watch.gif\" alt=\"Watch Now\" title=\"Watch Now\" /> Watch Now</a></li>";
					thePlaylistHTML += "<li><a "+ pRemoveLink +"><img src=\"/Images/PlanetRugby/Icons/pr-clip-btns-remove.gif\" alt=\"Remove from playlist\" title=\"Remove from playlist\" /> Remove</a></li>";
					thePlaylistHTML += "</ul>";
					thePlaylistHTML += "</div>";
					
					thePlaylistHTML += '<input type="hidden" id="clipAbstract' + pSid + '" value="' + pAbs.escapeHTML() + '" />';
					thePlaylistHTML += '<input type="hidden" id="clipDate' + pSid + '" value="' + pDate + '" />';
					thePlaylistHTML += '<input type="hidden" id="clipTitle' + pSid  + '" value="' + pTitle + '" />';
				 }
			playlistClearHTML = "<div class=\"clear-playlist\"><a href=\"javascript:;\" onclick=\"javascript:myPlaylist.clear(); return false;\"><img src=\"/Images/PlanetRugby/Icons/pr-clip-btns-clear.gif\" />Clear playlist</a></div>";

		} else {
			thePlaylistHTML = "<p>Click the plus sign to add clips to your playlist.</p>";
		}

			
			thePlaylistHTML = "<div class=\"pr-tabs-container-scroll\">" + playlistClearHTML + "" + thePlaylistHTML + "</div>";
		 $("#vid-playlist").empty();
		$("#vid-playlist").append(thePlaylistHTML);

	}
}

Array.prototype.save=function() {
		//save the playlist to a session cookie
		var thePlaylistCookieText = '';

		for (var p = 0;p< this.length;p++) {
			//get the clipObject and save its contents to a string to save in the cookie
			if (p < 10) {
			thePlaylistCookieText += "this.push(new Clip(unescape(\"" +
			escape(this[p].getTitle()) + "\"),unescape(\"" +
			escape(this[p].getAbstr()) + "\"),\"" +
			this[p].getPubdate() + "\",\"" +
			this[p].getCid() + "\",\"" +
			this[p].getSid() + "\",\"" +
			this[p].getImg() + "\"));|";
			}
		}
		SetCookie('planetrugby_video_playlist',thePlaylistCookieText);
}

Array.prototype.load=function(cookietext) {
	
	//load the playlist from the session cookie
	var myPlaylistTextAry = cookietext.split("|");
	if (myPlaylistTextAry.length>0) {
		for (var p = 0;p< myPlaylistTextAry.length;p++) {
			if (myPlaylistTextAry[p]) {
				eval(myPlaylistTextAry[p]);
			}
		}
		myPlaylist.refresh();
	}
}

Array.prototype.addClip=function(cId,sId,src) {
	
	
	var cAbstract = $("#clipAbstract"+sId).val();
	var cTitle = $("#clipTitle"+sId).val();
	var cDate = $("#clipDate"+sId).val();
	var cImg = $("#clipImage"+sId).val();   
	
	//Create a clip object on the fly and add it to the playlist array
	this.push(new Clip(cTitle, cAbstract, cDate, cId, sId, cImg));
	
	this.refresh();
	this.save();
	
	//if the list is now of size 1, there will be no onComplete event for the current video
	//so add one now
	
	if (this.length==1) {
		onMediaComplete = function() {myPlaylist.playClip(0)}
		modVP.addEventListener(BCVideoEvent.VIDEO_COMPLETE, onMediaComplete);
	}
	
}
Array.prototype.removeClip=function(i) {
	//removes a clip from the playlist array

	this.splice(i,1);
	this.refresh();	
	this.save();
}

Array.prototype.clear=function(i) {
	//removes all clips from the playlist array
	if (this.length>1) {
		modVP.removeEventListener(BCVideoEvent.VIDEO_COMPLETE, onMediaComplete);
	}
	this.splice(i,this.length);
	this.refresh();	
	this.save();
	
}
	
Array.prototype.playClip=function(i) {
	//plays a clip at index i from the playlist array
	//defaults to the 'next' clip
	
	if (this.length > 0) {

		if (!i) i=0;
		
		var clipTitle = this[i].getTitle();
		var clipId = this[i].getCid();
		var Sid = this[i].getSid();
		var clipPubdate = this[i].getPubdate();	
		var clipAbstract = this[i].getAbstr();
		
		modVP.loadVideo(clipId);
		
		fetchedId = clipId; //stores the titleId in a global variable so that i can use it in the onTitleLoad function
		
		this.removeClip(i);		
		
		if (this.length > i+1) {
			//as we have just removed this clip, the index stays the same
			var nextClip = i;
		} else if (this.length > 0) {
			//wrap round to the start of the list
			var nextClip = 0;
		} else {
			var nextClip = -1
		}	
		
		if (nextClip > -1) {
			modVP.removeEventListener(BCVideoEvent.VIDEO_COMPLETE, onMediaComplete); 
			
			onMediaComplete = function() {myPlaylist.playClip(nextClip)}
			modVP.addEventListener(BCVideoEvent.VIDEO_COMPLETE, onMediaComplete); 		
		} else {
			//no more clips in the playlist, remove the event listener
			modVP.removeEventListener(BCVideoEvent.VIDEO_COMPLETE, onMediaComplete); 
		}
	
		//do this after the call to act more as a companion...
		if (document.getElementById("liveBANFrame")) document.getElementById("liveBANFrame").src = bannerIFRAME;
	
		loadAbstract(Sid, '');
	}

}
Array.prototype.moveClipUp=function() {
	//moves a selected clip up/forward in the list. tbc
}
Array.prototype.moveClipDown=function() {
	//moves a selected clip down/backwards in the list. tbc
}

/***************************************
	 Clip funcs
**************************************/

function Clip(cTitle, cAbstract, cDate, cId, sId, cImg) {
	this.title = cTitle;	
	this.abstract = cAbstract;	
	this.pubdate = cDate;	
	this.cid = cId;
	this.sid = sId;
	this.img = cImg
	
	this.getTitle = function () { return this.title; }
	this.getAbstr = function () { return this.abstract; }
	this.getPubdate = function () { return this.pubdate; }
	this.getCid = function () { return this.cid; }
	this.getSid = function () { return this.sid; }
	this.getImg = function () { return this.img; }
}

function clearRelatedClips() {
	$("#nextup").html('');
}

<!--

//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}
//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    <expires> - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    <path> - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    <domain> - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    <secure> - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


// -->
