// JavaScript Document
var interval = null;
var oBracket = null;
var sDirection = null;


/*
Set the hidden channels
Must be syncronized with config/config_site.php
*/
var skipChannels = [
	'Bikini@WatchA3',
]

function IsChannelHidden( sChannel )
{
	var to = skipChannels.length;
	
	for( var i = 0 ; i < to ; i++ )
	{
		if( skipChannels[i] == sChannel )
		{
			return true;
		}
	}
	
	return false;
}

function Video( oObj, api, videoId, returns, sWatchUrl, sUserUrl )
{				
	this.currPage = 1;
	this.totalResults = 0;
	this.limit = 4;
	this.video = api;
	
	this.obj = oObj;
	
	this.callback = {
		 onSuccess: function (result) {
			  oObj.innerHTML = "";
			  
				var video = result;
				if( video && (typeof video.id != 'undefined') )
				{
					if( !IsChannelHidden(video.owner) )
					{
						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) {
							 videoDiv.className= 'bracket_video';
							 videoThumbDiv.className = 'pic';
						} else {
							videoDiv.setAttribute('class', 'bracket_video');
							videoThumbDiv.setAttribute('class', 'pic');
						}
						videoLink.setAttribute('href',sWatchUrl+video.id);
						videoThumb.setAttribute('src', video.thumbnailUrl);
						videoThumb.setAttribute('border', 0);
						//videoThumb.setAttribute('width', 150);
						//videoThumb.setAttribute('height', 100);
						videoText.innerHTML = "<strong><a href=\""+sWatchUrl+video.id+"\" class=\"video_title\">" + video.title + "</a></strong><br/><em>by <a href=\""+sUserUrl+video.owner+"\" class=\"owner_name\">" + video.owner + "</a></em><br/>" + video.views + " views";
						videoLink.appendChild(videoThumb);
						videoThumbDiv.appendChild(videoLink);
						videoDiv.appendChild(videoThumbDiv);
						videoDiv.appendChild(videoText);
						oObj.appendChild(videoDiv);
					}
				}
		 }
	}
	
	this.Show = function Show()
	{
		api.open.video.get( videoId, returns, this.callback);					
	}
	
}

function BracketVertical( oObj, api, query, returns, options, sWatchUrl, sUserUrl )
{				
	this.currPage = 1;
	this.totalResults = 0;
	this.limit = 4;
	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];
					
					if( !IsChannelHidden(video.owner) )
					{
						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\" /></a></td><td valign=\"top\"><strong><a href=\""+sWatchUrl+video.id+"\" class=\"video_title\">" + video.title + "</a></strong><br/><em>by <a href=\""+sUserUrl+video.owner+"\" class=\"owner_name\">" + 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 Bracket3Small( oObj, api, sWatchUrl, sUserUrl )
{				
	this.currPage = 1;
	this.totalResults = 0;
	this.limit = 3;
	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];
					
					if( !IsChannelHidden(video.owner) )
					{
						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) {
							videoDiv.className= 'bracket_video_small';
							videoThumbDiv.className = 'pic';
						} else {
							videoDiv.setAttribute('class', 'bracket_video_small');
							videoThumbDiv.setAttribute('class', 'pic');
						}
						videoLink.setAttribute('href',sWatchUrl+video.id);
						videoThumb.setAttribute('src', video.thumbnailUrl);
						videoThumb.setAttribute('border', 0);
						videoThumb.setAttribute('width', 120);
						videoThumb.setAttribute('height', 90);
						videoText.innerHTML = "<strong><a href=\""+sWatchUrl+video.id+"\" class=\"video_title\">" + video.title + "</a></strong><br/><em>by <a href=\""+sUserUrl+video.owner+"\" class=\"owner_name\">" + video.owner + "</a></em><br/>" + video.views + " views";
						videoLink.appendChild(videoThumb);
						videoThumbDiv.appendChild(videoLink);
						videoDiv.appendChild(videoThumbDiv);
						videoDiv.appendChild(videoText);
						oObj.appendChild(videoDiv);
					}
			  }
		 }
	}
	
	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 CustomVideoList( oObj, api, sWatchUrl, sUserUrl, itemsPerPage )
{				
	this.currPage = 1;
	this.totalResults = 0;
	this.limit = itemsPerPage;
	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];
					
					if( !IsChannelHidden(video.owner) )
					{
						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) {
							videoDiv.className= 'bracket_video_small';
							videoThumbDiv.className = 'pic';
						} else {
							videoDiv.setAttribute('class', 'bracket_video_small');
							videoThumbDiv.setAttribute('class', 'pic');
						}
						videoDiv.setAttribute( 'style', 'padding-bottom:5px;' )
						videoLink.setAttribute('href',sWatchUrl+video.id);
						videoThumb.setAttribute('src', video.thumbnailUrl);
						videoThumb.setAttribute('border', 0);
						videoThumb.setAttribute('width', 120);
						videoThumb.setAttribute('height', 90);
						videoText.innerHTML = "<strong><a href=\""+sWatchUrl+video.id+"\" class=\"video_title\">" + video.title + "</a></strong><br/><em>by <a href=\""+sUserUrl+video.owner+"\" class=\"owner_name\">" + video.owner + "</a></em><br/>" + video.views + " views";
						videoLink.appendChild(videoThumb);
						videoThumbDiv.appendChild(videoLink);
						videoDiv.appendChild(videoThumbDiv);
						videoDiv.appendChild(videoText);
											
						oObj.appendChild(videoDiv);
					}
			  }
		 }
	}
	
	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 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];
					
					if( !IsChannelHidden(video.owner) )
					{
						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) {
							videoDiv.className= 'bracket_video_small';
							videoThumbDiv.className = 'pic';
						} else {
							videoDiv.setAttribute('class', 'bracket_video_small');
							videoThumbDiv.setAttribute('class', 'pic');
						}
						videoDiv.setAttribute( 'style', 'padding-bottom:5px;' )
						videoLink.setAttribute('href',sWatchUrl+video.id);
						videoThumb.setAttribute('src', video.thumbnailUrl);
						videoThumb.setAttribute('border', 0);
						videoThumb.setAttribute('width', 120);
						videoThumb.setAttribute('height', 90);
						videoText.innerHTML = "<strong><a href=\""+sWatchUrl+video.id+"\" class=\"video_title\">" + video.title + "</a></strong><br/><em>by <a href=\""+sUserUrl+video.owner+"\" class=\"owner_name\">" + video.owner + "</a></em><br/>" + video.views + " views";
						videoLink.appendChild(videoThumb);
						videoThumbDiv.appendChild(videoLink);
						videoDiv.appendChild(videoThumbDiv);
						videoDiv.appendChild(videoText);
											
						oObj.appendChild(videoDiv);
					}
			  }
		 }
	}
	
	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 Bracket4( oObj, api, sWatchUrl, sUserUrl )
{				
	this.currPage = 1;
	this.totalResults = 0;
	this.limit = 4;
	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];
						
				  	if( !IsChannelHidden(video.owner) )
					{
						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) {
							videoDiv.className= 'bracket_video';
							videoThumbDiv.className = 'pic';
						} else {
							videoDiv.setAttribute('class', 'bracket_video');
							videoThumbDiv.setAttribute('class', 'pic');
						}
						videoLink.setAttribute('href',sWatchUrl+video.id);
						videoThumb.setAttribute('src', video.thumbnailUrl);
						videoThumb.setAttribute('border', 0);
						//videoThumb.setAttribute('width', 150);
						//videoThumb.setAttribute('height', 100);
						videoText.innerHTML = "<strong><a href=\""+sWatchUrl+video.id+"\" class=\"video_title\">" + video.title + "</a></strong><br/><em>by <a href=\""+sUserUrl+video.owner+"\" class=\"owner_name\">" + video.owner + "</a></em><br/>" + video.views + " views";
						videoLink.appendChild(videoThumb);
						videoThumbDiv.appendChild(videoLink);
						videoDiv.appendChild(videoThumbDiv);
						videoDiv.appendChild(videoText);
						oObj.appendChild(videoDiv);
					}
			  }
		 }
	}
	
	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 Bracket( oObj )
{
	this.oList = oObj;
	
	this.moveDirection = null;
	
	this.MoveList = function MoveList()
	{
		var iOldPos = 0;
		if( this.moveDirection == 'right' || this.moveDirection == 'left' )
			iOldPos = parseInt( this.oList.style.left );
		else
			iOldPos = parseInt( this.oList.style.top );
		
		var iNewPos = iOldPos;
		if( this.moveDirection == 'right' )
		{
			if( iOldPos > -( parseInt(this.oList.style.width) - 129 * 5)  )
				iNewPos -= 12;
		}else if( this.moveDirection == 'left' ){
			if( iOldPos < 0 )
				iNewPos += 12;
		}else if( this.moveDirection == 'down' ){
			if( iOldPos > -( parseInt(this.oList.style.height) - 129 * 5) )
				iNewPos -= 12;
		}else if( this.moveDirection == 'up' ){
			if( iOldPos < 0 )
				iNewPos += 12;
		}
		
		if( this.moveDirection == 'right' || this.moveDirection == 'left' )
			this.oList.style.left =  iNewPos+'px';
		else
			this.oList.style.top =  iNewPos+'px';		
	}
}

function MoveList()
{
	oBracket.moveDirection = sDirection;
	oBracket.MoveList();
}

function StartMoveListTo( oBracketToMove, sDirectionToMove )
{
	EndMove();
	oBracket = oBracketToMove;
	sDirection = sDirectionToMove;
	interval = setInterval( 'MoveList()', 10 );
}
	
function EndMove()
{
	clearInterval(interval);
}