// JavaScript Document

function GetRevverUsername( user )
{
	if( user.indexOf( '@' ) > 0  )
		return user.substring( 0, user.indexOf( '@' ) );
	else
		return user;		
}

function FormatRevverDate( date )
{
	var year = date.substr(0,4);
	var month = date.substr(5,2);
	var day = date.substr(8,2);
	var newDate = month+"/"+day+"/"+year;
	
	return newDate;	
}

function CustomBracketVertical( oObj, api, query, returns, options, sWatchUrl, sUserUrl )
{					
	this.obj = oObj;
	
	this.callback = {
		 onSuccess: function (resultsandcount) {
			  oObj.innerHTML = "";
			  var resultCount = resultsandcount.size();
			  for ( var i = 0 ; i < resultCount ; i++)
			  {
					var video = resultsandcount[i];
					
					var videoLi = document.createElement('li');
					var videoDiv = document.createElement('div');
					videoDiv.innerHTML = "<table><tr><td><a href=\""+sWatchUrl+video.id+"\"><img src=\""+video.thumbnailUrl+"\" border=\"0\" width=\"100\" height=\"70\" /></a></td><td valign=\"top\"><strong><a href=\""+sWatchUrl+video.id+"\" class=\"video-title\">" + video.title + "</a></strong><br/><em>by <a href=\""+sUserUrl+GetRevverUsername(video.owner)+"\" class=\"owner-name\">" + GetRevverUsername(video.owner) + "</a></em><br/>" + video.views + " views</td></tr></table>";
					videoLi.appendChild(videoDiv);
					oObj.appendChild(videoLi);
			  }
		 }
	}
	
	this.Show = function Show()
	{
		api.open.video.find( query, returns, options, this.callback);
	}
}

function CustomFeatured( oObj, api, query, returns, options, sWatchUrl, sUserUrl )
{					
	this.obj = oObj;
	
	this.callback = {
		 onSuccess: function (resultsandcount) {
			  oObj.innerHTML = "";
			  var resultCount = resultsandcount.size();
			  for ( var i = 0 ; i < resultCount ; i++)
			  {
					var video = resultsandcount[i];
					
					var videoLi = document.createElement('li');
					
					videoLi.innerHTML = "<div onclick=\"javascript:location.href='"+sWatchUrl+video.id+"';\"><table cellspacing=\"0\" cellpadding=\"0\"><tr><td><img src=\""+video.thumbnailUrl+"\" border=\"0\" width=\"100\" height=\"70\" /></td><td valign=\"top\"  align=\"left\" style=\"padding: 2px;\"><span class=\"video-title\">" + video.title + "</span></strong><br/>Posted by <span  class=\"owner-name\">" + GetRevverUsername(video.owner) + "</span><br/><span class=\"date\">" + FormatRevverDate(video.publicationDate) + "</span><br/><span class=\"views\">" + video.views + " views</span></td></tr></table></div>";
					//videoLi.appendChild(videoDiv);
					oObj.appendChild(videoLi);
			  }
		 }
	}
	
	this.Show = function Show()
	{
		api.open.video.find( query, returns, options, this.callback);
	}
}


function BracketVertical3( oObj, api, query, returns, options, sWatchUrl, sUserUrl )
{				
	this.currPage = 1;
	this.totalResults = 0;
	this.limit = 3;
	this.pager = api;
	
	this.obj = oObj;
	
	this.callback = {
		 onSuccess: function (resultsandcount) {
			  oObj.innerHTML = "";
			  var resultCount = resultsandcount.size();
			  for ( var i = 0 ; i < resultCount ; i++) {
					var video = resultsandcount[i];
					var videoLi = document.createElement('li');
					var videoDiv = document.createElement('div');
										
					videoDiv.innerHTML = "<table><tr><td><a href=\""+sWatchUrl+video.id+"\"><img src=\""+video.thumbnailUrl+"\" border=\"0\" width=\"100\" height=\"70\" /></a></td><td valign=\"top\"><strong><a href=\""+sWatchUrl+video.id+"\" class=\"video-title\">" + video.title + "</a></strong><br/><em>by <a href=\""+sUserUrl+GetRevverUsername(video.owner)+"\" class=\"owner-name\">" + GetRevverUsername(video.owner) + "</a></em><br/>" + video.views + " views</td></tr></table>";
					videoLi.appendChild(videoDiv);
					oObj.appendChild(videoLi);
			  }
		 }
	}
	
	this.Show = function Show()
	{
		api.open.video.find( query, returns, options, this.callback);
	}
}

function Bracket5( oObj, api, sWatchUrl, sUserUrl )
{				
	this.currPage = 1;
	this.totalResults = 0;
	this.limit = 5;
	this.pager = api;
	
	this.obj = oObj;
	
	this.callback = {
		 onSuccess: function (resultsandcount) {
			  oObj.innerHTML = "";					
			  
			  totalResults = resultsandcount[0];
			  var results = resultsandcount[1];
			  var resultCount = results.size();
			  for ( var i = 0 ; i < resultCount ; i++)
			  {
					var video = results[i];
					var videoLi = document.createElement('li');
					var videoDiv = document.createElement('div');
					
					if (document.all) {
						videoLi.className= 'tv-video-list-item';
						videoDiv.className= 'tv-video-list-item';
					} else {
						videoLi.setAttribute('class', 'tv-video-list-item');
						videoDiv.setAttribute('class', 'tv-video-list-item');
					}
					
					videoDiv.innerHTML = "<a href=\""+sWatchUrl+video.id+"\" class=\"video-title\"><img name=\"\" src=\""+video.thumbnailUrl+"\" width=\"100\" height=\"70\" alt=\"\" border=\"0\" /></a> \
					<p><a href=\""+sWatchUrl+video.id+"\" class=\"video-title\"><b>"+video.title+"</b></a></p> \
					<p>by <a href=\""+sUserUrl+GetRevverUsername(video.owner)+"\" class=\"owner-name\">"+GetRevverUsername(video.owner)+"</a></p> \
					<p>"+video.views+" views</p>";
					
					videoLi.appendChild(videoDiv);
					oObj.appendChild(videoLi);
			  }
		 }
	}
	
	this.Show = function Show()
	{
		if( this.currPage < 1 ) 
		{
			this.currPage = 1;
		}
		
		api.page(this.limit, this.limit*(this.currPage-1), this.callback);
	}
	
	this.Previous = function Previous()
	{
		 if (this.currPage > 1) {
			 this.currPage -= 1;
			 this.Show();
		 }
	}
	
	this.Next = function Next()
	{
		if( this.limit*(this.currPage) < totalResults )
		{
			this.currPage += 1;
		}
		
		this.Show();
	}
	
}

function ProfileVideos( oObj, api, sWatchUrl, sUserUrl )
{				
	this.currPage = 1;
	this.totalResults = 0;
	this.limit = 8;
	this.pager = api;
	
	this.obj = oObj;
	
	this.callback = {
		 onSuccess: function (resultsandcount) {
			  oObj.innerHTML = "";
			  totalResults = resultsandcount[0];
			  var results = resultsandcount[1];
			  var resultCount = results.size();
			  for ( var i = 0 ; i < resultCount ; i++)
			  {
					var video = results[i];
					
					var videoLi = document.createElement('li');
					var videoDiv = document.createElement('div');
					var videoLink = document.createElement('a');
					var videoThumbDiv = document.createElement('div');
					var videoThumb = document.createElement('img');
					var videoText = document.createElement('span');
					if (document.all) {
						videoLi.className= 'tv-video-list-item';
						videoThumbDiv.className = 'pic';
					} else {
						videoLi.className= 'tv-video-list-item';
						videoThumbDiv.setAttribute('class', 'pic');
					}
					
					videoDiv.innerHTML = "<a href=\""+sWatchUrl+video.id+"\" class=\"video-title\"><img name=\"\" src=\""+video.thumbnailUrl+"\" width=\"120\" height=\"90\" alt=\"\" border=\"0\" /></a> \
					<p><a href=\""+sWatchUrl+video.id+"\" class=\"video-title\"><b>"+video.title+"</b></a></p> \
					<p>by <a href=\""+sUserUrl+GetRevverUsername(video.owner)+"\" class=\"owner-name\">"+GetRevverUsername(video.owner)+"</a></p> \
					<p>"+video.views+" views</p>";
										
					videoLi.appendChild(videoDiv);
					oObj.appendChild(videoLi);
			  }
		 }
	}
	
	this.Show = function Show()
	{
		if( this.currPage < 1 ) 
		{
			this.currPage = 1;
		}
		
		api.page(this.limit, this.limit*(this.currPage-1), this.callback);
	}
	
	this.Previous = function Previous()
	{
		 if (this.currPage > 1) {
			 this.currPage -= 1;
			 this.Show();
		 }
	}
	
	this.Next = function Next()
	{
		if( this.limit*(this.currPage) < totalResults )
		{
			this.currPage += 1;
		}
		
		this.Show();
	}
	
	this.GotoPage = function GotoPage( page )
	{
		if( this.limit*(page) <= totalResults )
		{
			this.currPage = page;
		}
		
		this.Show();
	}
	
}

function MyVideos( oObj, api, sWatchUrl )
{				
	this.currPage = 1;
	this.totalResults = 0;
	this.limit = 8;
	this.pager = api;
	
	this.obj = oObj;
	
	this.callback = {
		 onSuccess: function (resultsandcount) {
			  oObj.innerHTML = "";
			  totalResults = resultsandcount[0];
			  var results = resultsandcount[1];
			  var resultCount = results.size();
			  for ( var i = 0 ; i < resultCount ; i++)
			  {
					var video = results[i];
					
					var videoLi = document.createElement('li');
					var videoDiv = document.createElement('div');
					var videoLink = document.createElement('a');
					var videoThumbDiv = document.createElement('div');
					var videoThumb = document.createElement('img');
					var videoText = document.createElement('span');
					if (document.all) {
						videoLi.className= 'tv-video-list-item';
						videoThumbDiv.className = 'pic';
					} else {
						videoLi.className= 'tv-video-list-item';
						videoThumbDiv.setAttribute('class', 'pic');
					}
					
					videoDiv.innerHTML = "<a href=\""+sWatchUrl+video.id+"\" class=\"video-title\"><img name=\"\" src=\""+video.thumbnailUrl+"\" width=\"120\" height=\"90\" alt=\"\" border=\"0\" /></a> \
					<p><a href=\""+sWatchUrl+video.id+"\" class=\"video-title\"><b>"+video.title+"</b></a></p> \
					<p>"+video.views+" views</p>\
					<p><a href=\"/tv/editvideo/id/"+video.id+"\" class=\"link2\">edit</a> <a href=\"javascript:void(0)\" class=\"link2\" onclick=\"DeleteVideo("+video.id+")\>delete</a></p>\
					";
										
					videoLi.appendChild(videoDiv);
					oObj.appendChild(videoLi);
			  }
		 }
	}
	
	this.Show = function Show()
	{
		if( this.currPage < 1 ) 
		{
			this.currPage = 1;
		}
		
		api.page(this.limit, this.limit*(this.currPage-1), this.callback);
	}
	
	this.Previous = function Previous()
	{
		 if (this.currPage > 1) {
			 this.currPage -= 1;
			 this.Show();
		 }
	}
	
	this.Next = function Next()
	{
		if( this.limit*(this.currPage) < totalResults )
		{
			this.currPage += 1;
		}
		
		this.Show();
	}
	
	this.GotoPage = function GotoPage( page )
	{
		if( this.limit*(page) <= totalResults )
		{
			this.currPage = page;
		}
		
		this.Show();
	}
	
}

function MyFavorites( oObj, api, sWatchUrl, collectionId )
{				
	this.currPage = 1;
	this.totalResults = 0;
	this.limit = 8;
	this.pager = api;
	
	this.obj = oObj;
	
	this.callback = {
		 onSuccess: function (resultsandcount) {
			  oObj.innerHTML = "";
			  totalResults = resultsandcount[0];
			  var results = resultsandcount[1];
			  var resultCount = results.size();
			  for ( var i = 0 ; i < resultCount ; i++)
			  {
					var video = results[i];
					
					var videoLi = document.createElement('li');
					var videoDiv = document.createElement('div');
					var videoLink = document.createElement('a');
					var videoThumbDiv = document.createElement('div');
					var videoThumb = document.createElement('img');
					var videoText = document.createElement('span');
					if (document.all) {
						videoLi.className= 'tv-video-list-item';
						videoThumbDiv.className = 'pic';
					} else {
						videoLi.className= 'tv-video-list-item';
						videoThumbDiv.setAttribute('class', 'pic');
					}
					
					videoDiv.innerHTML = "<a href=\""+sWatchUrl+video.id+"\" class=\"video-title\"><img name=\"\" src=\""+video.thumbnailUrl+"\" width=\"120\" height=\"90\" alt=\"\" border=\"0\" /></a> \
					<p><a href=\""+sWatchUrl+video.id+"\" class=\"video-title\"><b>"+video.title+"</b></a></p> \
					<p>"+video.views+" views</p>\
					<p><a href=\"javascript:void(0)\" class=\"link2\" onclick=\"DeleteFromCollection("+collectionId+", "+video.id+")\"\>delete</a></p>\
					";
										
					videoLi.appendChild(videoDiv);
					oObj.appendChild(videoLi);
			  }
		 }
	}
	
	this.Show = function Show()
	{
		if( this.currPage < 1 ) 
		{
			this.currPage = 1;
		}
		
		api.page(this.limit, this.limit*(this.currPage-1), this.callback);
	}
	
	this.Previous = function Previous()
	{
		 if (this.currPage > 1) {
			 this.currPage -= 1;
			 this.Show();
		 }
	}
	
	this.Next = function Next()
	{
		if( this.limit*(this.currPage) < totalResults )
		{
			this.currPage += 1;
		}
		
		this.Show();
	}
	
	this.GotoPage = function GotoPage( page )
	{
		if( this.limit*(page) <= totalResults )
		{
			this.currPage = page;
		}
		
		this.Show();
	}
	
}

function VideosResults( oObj, api, sWatchUrl, sUserUrl )
{				
	this.currPage = 1;
	this.totalResults = 0;
	this.limit = 8;
	this.pager = api;
	
	this.obj = oObj;
	
	this.callback = {
		 onSuccess: function (resultsandcount) {
			  oObj.innerHTML = "";
			  totalResults = resultsandcount[0];
			  var results = resultsandcount[1];
			  var resultCount = results.size();
			  if( resultCount > 0 )
			  {
				  for ( var i = 0 ; i < resultCount ; i++)
				  {
						var video = results[i];
						
						var videoLi = document.createElement('li');
						var videoDiv = document.createElement('div');
						var videoLink = document.createElement('a');
						var videoThumbDiv = document.createElement('div');
						var videoThumb = document.createElement('img');
						var videoText = document.createElement('span');
						if (document.all) {
							videoLi.className= 'tv-video-list-item';
							videoThumbDiv.className = 'pic';
						} else {
							videoLi.className= 'tv-video-list-item';
							videoThumbDiv.setAttribute('class', 'pic');
						}
						
						videoDiv.innerHTML = "<a href=\""+sWatchUrl+video.id+"\" class=\"video-title\"><img name=\"\" src=\""+video.thumbnailUrl+"\" width=\"120\" height=\"90\" alt=\"\" border=\"0\" /></a> \
						<p><a href=\""+sWatchUrl+video.id+"\" class=\"video-title\"><b>"+video.title+"</b></a></p> \
						<p>by <a href=\""+sUserUrl+GetRevverUsername(video.owner)+"\" class=\"owner-name\">"+GetRevverUsername(video.owner)+"</a></p> \
						<p>"+video.views+" views</p>";
											
						videoLi.appendChild(videoDiv);
						oObj.appendChild(videoLi);
				  }
			  } else {
				  oObj.innerHTML = "No results";
				  $('nav-bar').style.visibility='hidden';
			  }
		 }
	}
	
	this.Show = function Show()
	{
		if( this.currPage < 1 ) 
		{
			this.currPage = 1;
		}
		
		api.page(this.limit, this.limit*(this.currPage-1), this.callback);
	}
	
	this.Previous = function Previous()
	{
		 if (this.currPage > 1) {
			 this.currPage -= 1;
			 this.Show();
		 }
	}
	
	this.Next = function Next()
	{
		if( this.limit*(this.currPage) < totalResults )
		{
			this.currPage += 1;
		}
		
		this.Show();
	}
	
	this.GotoPage = function GotoPage( page )
	{
		if( this.limit*(page) <= totalResults )
		{
			this.currPage = page;
		}
		
		this.Show();
	}
	
}

function ToggleDescription( btn, divId, expanded )
{	
	this.descriptionExpanded = expanded;
	
	this.Toggle = function Toggle()
	{
		if( !this.descriptionExpanded )
		{
			this.Expand();
			
		} else {
			this.Collapse();
		}
	}
	
	this.Expand = function Expand()
	{
		$(divId).style.height = "auto";
		$(divId).style.overflow = "visible";
		
		btn.innerHTML = "<b>Less &#9650;</b>";
		
		this.descriptionExpanded = true;
	}
	
	this.Collapse = function Collapse()
	{
		$(divId).style.height = "28px";	
		$(divId).style.overflow = "hidden";
		
		btn.innerHTML = "<b>More &#9660;</b>";
		
		this.descriptionExpanded = false;
	}
	
	if( this.descriptionExpanded )
		this.Expand();
	else
		this.Collapse();
}

function ToggleContent( divId, expanded )
{
	this.expanded = expanded;
	
	this.Toggle = function Toggle()
	{
		if( !this.expanded )
			this.Expand();
		else
			this.Collapse();
	}
	
	this.Expand = function Expand()
	{
		$(divId).style.display = "block";	
		this.expanded = true;
	}
	
	this.Collapse = function Collapse()
	{
		$(divId).style.display = "none";	
		this.expanded = false;
	}
		
	if( this.expanded )
		$(divId).style.display = "block";
	else
		$(divId).style.display = "none";
}
