﻿var Utilities = new function()
{
	this.IsNumeric = function(Input)
	{
		if(Input == null)
			return false;
		for(var a = 0, cnt = Input.Length; a < cnt; a++)
		{
			var i = Input.charCodeAt(a);
			if(i > 57 || i < 48)
				return false;
		}
		return true;
	};
	this.RandomNumber = function(MaxValue)
	{
		return Math.floor(Math.random() * MaxValue + 1);
	};
	this.AvatarRandomDate = function()
	{
		var month = this.RandomNumber(12),
			day = this.RandomNumber(28),
			year = 1970 + this.RandomNumber(10),
			hour = this.RandomNumber(12),
			min = this.RandomNumber(59);
		return month+"/"+day+"/"+year+" "+hour+":"+(min<10?"0":"")+min+":00";
	};
	this.ReformatDate = function(Date)
	{
		var split0 = Date.split(" ");
		var split1 = split0[0].split("/");
		var split2 = split0[1].split(":");
		
		var month = parseInt(split1[0]),
			day = parseInt(split1[1]),
			year = parseInt(split1[2]) - 1900,
			hour = parseInt(split2[0]),
			min = parseInt(split2[1]),
			sec = parseInt(split2[2]);
		
		return (year<10?"0":"") + year
			+ (month<10?"0":"") + month
			+ (day<10?"0":"") + day
			+ (hour<10?"0":"") + hour
			+ (min<10?"0":"") + min
			+ (sec<10?"0":"") + sec;
	};
	this.FindUploadCookieValue = function(UploadID)
	{
		var name = "UD" + UploadID.toString() + "=";
		if(document.cookie.indexOf(name) != -1)
			return unescape(document.cookie.substring(document.cookie.indexOf(name) + name.length));
		return null;
	};
	this.ReadCookie = function(name)
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++)
		{
			var c = ca[i];
			while(c.charAt(0)==' ')
				c = c.substring(1,c.length);
			if(c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length,c.length);
		}
		return null;
	};
	this.GetQueryString = function()
	{
		var locationstring = location.search.substring(1,location.search.length);
		//gets rid of the leading ?
		if(locationstring.length != 0)
		{
			var args = locationstring.split("&");
			var array = new Object();
			for (var i = 0; i < args.length; i++) 
			{
				var namevalue = args[i].split("=");
				var thename = unescape(namevalue[0]).toLowerCase();
				array[thename] = (namevalue.length == 2 ? unescape(namevalue[1]) : "");
			}
			return array;
		}
	};
	this.FormatString = function(Input)
	{
		for(var a = 1, cnt = arguments.length; a < cnt; a++)
			Input = Input.replace(new RegExp("\\{" + (a - 1) + "\\}", "gi"), arguments[a]);
		return Input;	
	};
};

var FeaturedVideos = new function()
{
	this.Find = function(page, containerID, guideID)
	{
		$.get(_ajaxBase + "featured.aspx" + (guideID > 0 ? "?guide_id=" + guideID : ""), {page:page},
			function(data){$('#' + containerID).html(data);}
			);
	};
};

var Playlist = new function()
{
	var _data, _lastTemplate, _lastRenderCallBack, _currentPlaylist;
	
	this.PlaylistManagerTemplate = {
		"head":'<ul class="videoList3 programPlaylist flc">',
		"foot":'</ul>',
		"item":'<li>'
			+ '<div class="number">{4}.</div>'
			+ '<div class="box3"><div class="box3TL"><div class="box3TR"><div class="box3BL"><div class="box3BR">'
			+ '<div class="box3Content flc">'
			+ '<a href="" class="clear"><img src="<% = Urls.FindRootCDN() %>images/button_remove.gif" class="fr" /></a>'
			+ '<a href="{0}"><img src="{1}" class="thumb1" /></a>'
			+ '<span class="title">{5}</span>'
			+ '</div>'
			+ '</div></div></div></div>'
			+ '<input type="hidden" class="vid" value="{2}" />'
			+ '</div>'
			+ '</li>'
			};
			
	this.ProfileTemplate = {
		"head":'<ul class="videoList3 flc">',
		"foot":'</ul>',
		"item":'<li>'
			+ '<div class="box3"><div class="box3TL"><div class="box3TR"><div class="box3BL"><div class="box3BR">'
			+ '<div class="box3Content">'
			+ '<a href="{0}"><img src="{1}" class="thumb1" /></a>'
			+ '<a onclick="addToPlaylistPopUp(event,{2});" title="Add to Playlist" class="add1"></a>'
			+ '<a href="{0}"><img src="{3}" /></a>'
			+ '<a href="{0}" class="title"><span class="number">{4}.</span> {5}</a>'
			+ '</div>'
			+ '</div></div></div></div></div>'
			+ '</li>'
			};
	this.PlaylistTrayTemplate = {
		"head":'',
		"foot":'',
		"item":'<div class="video">'
			+ '<a href="{0}" class="thumb"><img src="{1}" alt="video thumbnail" /></a>'
			+ '{4}.'
			+ '<h3 class="myPlaylistTitle"><a href="{0}">{5}</a></h3>'
			+ '</div>'
			};
	// {0} = video url
	// {1} = thumbnail (medium)
	// {2} = video id for favorites
	// {3} = rating image
	// {4} = index
	// {5} = title
	// {6} = description
	// {7} = view count
	// {8} = expert name
	// {9} = expert url
	// {10} = video series url
	// {11} = video series title
	
	this.renderData = function(template)
	{
		var output = '', items;
		if((items = Playlist._data.videos) != null)
		{
			output += template.head;
			for(var a = 0, cnt = items.length; a < cnt; a++)
				output += Utilities.FormatString(
					template.item,
					items[a].url,
					items[a].thumbnailurl,
					items[a].id,
					items[a].ratingurl,
					items[a].index,
					items[a].title,
					items[a].description,
					items[a].views
					);
			output += template.foot;
		}
		return output;
	}	
	this.renderPager = function()
	{
		var output = '';
		if(Playlist._data.pager && Playlist._data.pager.pageCount > 0)
		{
			var buffer = 4;
			var page = (Playlist._data.pager.currentPage > buffer ? Playlist._data.pager.currentPage - buffer : 1);
			var end = page + (buffer * 2) + 1;
			if(end > Playlist._data.pager.pageCount)
			{
				page -= (end - Playlist._data.pager.pageCount);
				end = Playlist._data.pager.pageCount;
			}
			if(page < 1)
				page = 1;

			output += Utilities.FormatString('<li><a href="" onclick="{0}; return false;">&lt;&lt; Back</a></li>', 'Playlist.Go(\'method=find-playlist-videos&page=' + Playlist._data.pager.previousPage + '\')');
			for(page; page <= end; page++)
			{
				if(this._data.pager.currentPage != page)
					output += Utilities.FormatString('<li><a href="" onclick="{0}; return false;">{1}</a></li>', 'Playlist.Go(\'method=find-playlist-videos&page=' + page + '\')', page);
				else
					output += Utilities.FormatString('<li><b> {0} </b></li>', page);
			}
			output += Utilities.FormatString('<li><a href="" onclick="{0}; return false;">Next &gt;&gt;</a></li>', 'Playlist.Go(\'method=find-playlist-videos&page=' + Playlist._data.pager.nextPage + '\')');
		}
		return output;
	}

	this.Go = function(Criteria)
	{
		Playlist.Query(Criteria + "&playlist_id=" + Playlist._data.playlist.PlaylistID, Playlist._lastRenderCallBack, Playlist._lastTemplate);
	}	
	
	this.Render = function(RenderCallBack, DataTemplate)
	{
		Playlist._lastTemplate = DataTemplate;
		Playlist._lastRenderCallBack = RenderCallBack;
		if(Playlist._data)
		{
			RenderCallBack(
				Playlist._data.firstIndex,
				Playlist._data.lastIndex,
				Playlist.renderData(DataTemplate),
				Playlist.renderPager(),
				Playlist._data.playlist);
		}
	}
	
	this.Query = function(Criteria, RenderCallBack, DataTemplate)
	{
		$.getJSON(_ajaxBase + "playlist.aspx?" + Criteria + "&r=" + Utilities.RandomNumber(10000), function(data)
		{
			Playlist._data = data;
			Playlist.Render(RenderCallBack, DataTemplate);
		});
	}
	
	this.QuickSave = function(playlistTitle, playlistID, videoID, CallBack)
	{
		$.post(_ajaxBase + 'playlist.aspx', {method:'find-playlists',save_playlist:1, video_id:videoID, playlist_id:playlistID, title:playlistTitle, add_to_favorite:1},
			function(data){
				CallBack(data);
			}
			);
	};

	this.Remove = function(playlistID, callBack)
	{
		if(playlistID < 1)
			return false;
			
		$.post(_ajaxBase + 'playlist.aspx', {method:'find-playlists',remove_playlist:1,playlist_id:playlistID},
			function(data){
				$("#videoContainer .videoList3").html("<li>This Playlist has been deleted.</li>");
				callBack(data);
			}
			);
	};
	
	this.FindTrayHTML = function(playlist, containerID)
	{
		$.get(_ajaxBase + "playlist.aspx", {method:'find-tray-html',playlist_id:playlist},
			function(data){$('#' + containerID).html(data);}
			);
	};
	
	this.FindVideos = function(playlistID, page, dataTemplate, callBack)
	{
		if(playlistID < 1 || (Playlist._data && Playlist._data != null && playlistID == Playlist._data.playlist.PlaylistID))
			return false;
		this.Query("method=find-playlist-videos&playlist_id=" + playlistID + "&page=" + page, callBack, dataTemplate)
	};
	
	this.FullSave = function(playlistTitle, playlistID, videoIDs, page, dataTemplate, callBack)
	{
		this.Query("method=find-playlist-videos&save_playlist=1&playlist_id=" + playlistID + "&title=" + playlistTitle + "&video_ids=" + videoIDs + "&page=" + page, callBack, dataTemplate)
	};
};

var Videos = new function()
{
	var _data, _lastTemplate, _lastRenderCallBack, _resultsContainer;
	
	this.PlaylistManagerTemplate = {
		"head":'<ul class="videoList3 programPlaylist flc">',
		"foot":'</ul>',
		"item":'<li>'
			+ '<div class="number">{4}.</div>'
			+ '<div class="box3"><div class="box3TL"><div class="box3TR"><div class="box3BL"><div class="box3BR">'
			+ '<div class="box3Content flc">'
			+ '<a href="" class="clear"><img src="/images/button_remove.gif" class="fr" /></a>'
			+ '<a href="{0}"><img src="{1}" class="thumb1" /></a>'
			+ '<span class="title">{5}</span>'
			+ '</div>'
			+ '</div></div></div></div>'
			+ '<input type="hidden" class="vid" value="{2}" />'
			+ '</div>'
			+ '</li>'
			};
			
	this.ProfileTemplate = {
		"head":'<ul class="videoList3 flc">',
		"foot":'</ul>',
		"item":'<li>'
			+ '<div class="box3"><div class="box3TL"><div class="box3TR"><div class="box3BL"><div class="box3BR">'
			+ '<div class="box3Content">'
			+ '<a href="{0}"><img src="{1}" class="thumb1" /></a>'
			+ '<a onclick="addToPlaylistPopUp(event,{2});" title="Add to Playlist" class="add1"></a>'
			+ '<a href="{0}"><img src="{3}" /></a>'
			+ '<a href="{0}" class="title"><span class="number">{4}.</span> {5}</a>'
			+ '</div>'
			+ '</div></div></div></div></div>'
			+ '</li>'
			};
	this.IconTemplate = {
			"head":'<ul class="videoList3 flc">',
			"foot":'</ul>',
			"item":'<li>'
				+ '<div class="box3"><div class="box3TL"><div class="box3TR"><div class="box3BL"><div class="box3BR">'
				+ '<div class="box3Content">'
				+ '<a href="{0}"><img src="{1}" class="thumb1" /></a>'
				+ '<a onclick="addToPlaylistPopUp(event,{2});" title="Add to Playlist" class="add1"></a>'
				+ '<a href="{0}"><img src="{3}" /></a>'
				+ '<a href="{0}" class="title"><span class="number">{4}.</span> {5}</a>'
				+ '</div>'
				+ '</div></div></div></div></div>'
				+ '</li>'
			};
	// {0} = video url
	// {1} = thumbnail (medium)
	// {2} = video id for favorites
	// {3} = rating image
	// {4} = index
	// {5} = title
	// {6} = description
	// {7} = view count
	// {8} = expert name
	// {9} = expert url
	// {10} = video series url
	// {11} = video series title
	
	this.renderData = function(template)
	{
		var output = '', items;
		if((items = Videos._data.videos) != null)
		{
			output += template.head;
			for(var a = 0, cnt = items.length; a < cnt; a++)
				output += Utilities.FormatString(
					template.item,
					items[a].url,
					items[a].thumbnailurl,
					items[a].id,
					items[a].ratingurl,
					items[a].index,
					items[a].title,
					items[a].description,
					items[a].views
					);
			output += template.foot;
		}
		return output;
	}	
	this.renderPager = function()
	{
		var output = '';
		if(this._data.pager && this._data.pager.pageCount > 0)
		{
			var buffer = 4;
			var page = (this._data.pager.currentPage > buffer ? this._data.pager.currentPage - buffer : 1);
			var end = page + (buffer * 2) + 1;
			if(end > this._data.pager.pageCount)
			{
				page -= (end - this._data.pager.pageCount);
				end = this._data.pager.pageCount;
			}
			if(page < 1)
				page = 1;

			output += Utilities.FormatString('<li><a href="" onclick="{0}; return false;">&lt;&lt; Back</a></li>', 'Videos.Go(\'' + (this._data.pager.previousPage > 1 ? 'page=' + this._data.pager.previousPage : '') + '\')');
			for(page; page <= end; page++)
			{
				if(this._data.pager.currentPage != page)
					output += Utilities.FormatString('<li><a href="" onclick="{0}; return false;">{1}</a></li>', 'Videos.Go(\'' + (page > 1 ? 'page=' + page : '') + '\')', page);
				else
					output += Utilities.FormatString('<li><b> {0} </b></li>', page);
			}
			output += Utilities.FormatString('<li><a href="" onclick="{0}; return false;">Next &gt;&gt;</a></li>', 'Videos.Go(\'' + (this._data.pager.nextPage > 1 ? 'page=' + this._data.pager.nextPage : '') + '\')');
		}
		return output;
	}

	this.Go = function(Criteria)
	{
		this.Query(Criteria + '&method=' + this._data.submitmethod, this._lastRenderCallBack, this._lastTemplate);
	}	
	
	this.Render = function(RenderCallBack, DataTemplate)
	{
		this._lastTemplate = DataTemplate;
		this._lastRenderCallBack = RenderCallBack;
		if(this._data)
		{
			RenderCallBack(
				this._data.count,
				this._data.firstIndex,
				this._data.lastIndex,
				this.renderData(DataTemplate),
				this.renderPager());
		}
	}
	
	this.Query = function(Criteria, RenderCallBack, DataTemplate)
	{
		if(_resultsContainer)
		{
			$(_resultsContainer).fadeTo("fast", 0.25);
		}
		
		$.getJSON(_ajaxBase + "videos.aspx?" + Criteria + "&r=" + Utilities.RandomNumber(10000), function(data)
		{
			Videos._data = data;
			Videos.Render(RenderCallBack, DataTemplate);
			if(_resultsContainer)
			{
				$(_resultsContainer).fadeTo("fast", 1);
			}
		});
	}
	
	this.SetResultsContainer = function(resultsContainer)
	{
		_resultsContainer = resultsContainer;;
	}	
	
	this.FindHighestRatedVideos = function(callBack, DataTemplate)
	{
		this.Query("method=find-highest-rated", callBack, DataTemplate);
	};
	
	this.FindMostViewedVideos = function(callBack, DataTemplate)
	{
		this.Query("method=find-most-viewed", callBack, DataTemplate);
	};
	
	this.FindNewestVideos = function(callBack, DataTemplate)
	{
		this.Query("method=find-newest", callBack, DataTemplate);
	};
};

function RefreshAllPlaylists(html)
{
	$(".playlistSelect").each(function(i){
		$(this).html(html);
	});
	
	$(".addToPlaylistPopUp").hide("slow");
}

var Favorites = new function()
{
	this.Add = function(id, type, link, callBack)
	{
		$.post(_ajaxBase + 'favorites.aspx', {add:1, id:id, type:type},
			function(data){ callBack($.trim(data),id,type, link); }
			);
	};
	this.Remove = function(id, type, link, callBack)
	{
		$.post(_ajaxBase + 'favorites.aspx', {remove:1, id:id, type:type},
			function(data){ callBack($.trim(data),id,type, link);}
			);
	};
};

function AddFavoriteCallback(message,id,type,link) 
{
	if(message == "true")
	{
		var element = $(link);
	
		if(type == 1 || type == 2)
			element.html("Remove Favorite");
		else if(type == 5)
			element.html("Unsubscribe Category");
		else
			element.html("Unsubscribe");
		
		element.removeAttr("onclick");
		element.bind("click", function (){
			Favorites.Remove(id,type,link,RemoveFavoriteCallback);
			return false;
			}
		);		
	}
}

function RemoveFavoriteCallback(message,id,type,link) 
{
	if(message == "true")
	{	
		if(type == 1 || type == 2)
		{
			$(link).html("Add to Favorites");		
		}
		else if(type == 5)
		{
			$(link).html("Subscribe To Category");
		}
		else
		{
			$(link).html("Subscribe");
		}
		
		$(link).removeAttr("onclick");
		$(link).bind("click", function (){
			Favorites.Add(id,type,link,AddFavoriteCallback);
			return false;
			}
		);
		
	}
}

function AddMultiFavoriteCallback(message,id,type,link) 
{
	if(message == "true")
	{
		if(type == 1 || type == 2)
		{
			$("." + link.id).each(function(i){
				$(this).html("Remove Favorite");
				$(this).removeAttr("onclick");
				$(this).bind("click", function (){
					Favorites.Remove(id,type,link,RemoveMultiFavoriteCallback);
					return false;
				});
			});		
			
		}	
		else if(type == 5)
		{
			$(link).html("Unsubscribe Category");
		}
		else if(type == 6)
		{
			$("." + link.id).each(function(i){
				$(this).html("Unsubscribe Expert");
				$(this).removeAttr("onclick");
				$(this).bind("click", function (){
					Favorites.Remove(id,type,link,RemoveMultiFavoriteCallback);
					return false;
				});
			});			
		}		
		
		
		
	}
}

function RemoveMultiFavoriteCallback(message,id,type,link) 
{
	if(message == "true")
	{	
		if(type == 1 || type == 2)
		{
			$("." + link.id).each(function(i){
				$(this).html("Add Favorite");
				$(this).removeAttr("onclick");
				$(this).bind("click", function (){
					Favorites.Remove(id,type,link,RemoveMultiFavoriteCallback);
					return false;
				});
			});		
		}
		else if(type == 5)
		{
			$(link).html("Subscribe To Category");
		}
		else if(type == 6)
		{
			$("." + link.id).each(function(i){
				$(this).html("Subscribe Expert");
				$(this).removeAttr("onclick");
				$(this).bind("click", function (){
					Favorites.Add(id,type,link,AddMultiFavoriteCallback);
					return false;
				});
			});		
		}
		
	}
}

var Ratings = new function()
{
	this.Add = function(id, type, rating, callBack)
	{
		$.post(_ajaxBase + 'rate.aspx', {id:id, type:type, positive:rating},
			function(data){ callBack(data.indexOf("true") > -1); }
			);
	};
};

var Pager = new function()
{
	this.Increment = function(Input)
	{
		if(Input.page < Input.count)
			Input.page++;
		return Input;
	};
	this.Decrement = function(Input)
	{
		if(Input.page > 1)
			Input.page--;
		return Input;
	};
};

var Now = new function()
{
	var _displayCount = 9,
		_displayInterval = 8000;
	var _data, _elementID, _index, _count, _timer;
	
	var _template = '<li class="video flc{4}">'
		+ '<a href="{0}"><img class="thumb1" src="{1}"/></a>'
		+ '<div class="title"><a href="{0}">{2}</a></div>'
		+ '<div class="data">'
		+ '<strong class="strong">Views:</strong> {3}'
		+ '<p></p>'
		+ '</div>'
		+ '</li>'
		;
	// {0} = video url
	// {1} = thumbnail
	// {2} = title
	// {3} = views
	// {4} = alt
			
	
	this.Find = function()
	{
		$.getJSON(_ajaxBase + "now.aspx", function(data)
		{
			Now._data = data;
			Now.Render();
		});
	}
	
	this.Render = function()
	{
		if(Now._data && Now._data.videos)
		{
			_count = Now._data.videos.length;
			_index = (_count - _displayCount);

			for(var a = 0; a < Now._displayCount && a < _count; a++)
				Now.Down();
			setTimeout("Now.AutoSize()", 800);
		}
	}
	
	this.Start = function(ElementID)
	{
		if(Now._displayCount == null || Now._displayCount == undefined)
			Now._displayCount = 9;
		Now._elementID = ElementID;
		Now.Find();
		
		_timer = setInterval("Now.Down()", _displayInterval);
	}
	
	this.AutoSize = function()
	{
		var element = $("#" + Now._elementID);
		var height = element.height();
		element.parent().animate({height:height}, 'normal', function(){});
	}
	
	this.Up = function()
	{
		if(Now._data && Now._data.videos)
		{
			clearInterval(_timer);
		
			var next = _index + Now._displayCount;
			if(next >= _count)
				next -= _count;
		
			var item = Now._data.videos[next];
			var html = Utilities.FormatString(_template, item.url, item.thumbnailurl, item.title, item.views, (next % 2 == 1 ? ' alt' : ''));
		
			var height = $("#" + Now._elementID).children("li:first").height();
			if($("#" + Now._elementID).children().length >= Now._displayCount)
			{
				$("#" + Now._elementID)
					.append(html)
					.animate({marginTop:-height}, 'normal', '', function(){$("#" + Now._elementID).css("margin-top","0px").children("li:first").remove(); Now.AutoSize();})
					;
			}
			else
			{
				$("#" + Now._elementID).append(html);
			}
			
			_index++;
			if(_index >= _count)
				_index = 0;
			
			if(_count > Now._displayCount)
				_timer = setInterval("Now.Up()", _displayInterval);
		}
	}
	
	this.Down = function()
	{
		if(Now._data && Now._data.videos)
		{
			clearInterval(_timer);
		
			var prev = _index - 1;
			if(prev < 0)
				prev = _count - 1;

			var item = Now._data.videos[prev];
			var html = !item ? "" : Utilities.FormatString(_template, item.url, item.thumbnailurl, item.title, item.views, (prev % 2 == 1 ? ' alt' : ''));
			
			var height = $("#" + Now._elementID).children("li:first").height();
			if($("#" + Now._elementID).children().length >= Now._displayCount)
			{
				$("#" + Now._elementID)
					.prepend(html)
					.css("margin-top","-" + height + "px")
					.animate({marginTop:0}, 'normal', '', function(){$("#" + Now._elementID).children("li:last").remove(); Now.AutoSize();})
					;
			}
			else
			{
				$("#" + Now._elementID).append(html);
			}

			_index--;
			if(_index < 0)
				_index = _count - 1;
			
			if(_count > Now._displayCount)
				_timer = setInterval("Now.Down()", _displayInterval);
		}
	}
};

var Wpawn = new function()
{
	var _displayCount = 5,
		_displayInterval = 8000;
	var _data, _elementID, _index, _count, _timer;
	
	var _template = '<div class="video {4}">'
		+ '<a href="{0}" class="thumb"><img src="{1}" alt="video" /></a>'
		+ '<div class="data">'
		+ '<strong class="strong">Views:</strong><br />{3}'
		+ '<p><a onclick="addToPlaylistPopUp(event,{5});" href="javascript:void(0);" class="addButtonSm">playlist</a></p>'
		+ '</div>'
		+ '<h3 class="rv3Title"><a href="{0}">{2}</a></h3>'
		+ '</div>'
		;
	// {0} = video url
	// {1} = thumbnail
	// {2} = title
	// {3} = views
	// {4} = alt
	// {5} = id
			
	
	this.Find = function()
	{
		$.getJSON(_ajaxBase + "now.aspx", function(data)
		{
			Wpawn._data = data;
			Wpawn.Render();
		});
	}
	
	this.Render = function()
	{
		if(Wpawn._data && Wpawn._data.videos)
		{
			_count = Wpawn._data.videos.length;
			_index = (_count - _displayCount);

			for(var a = 0; a < _displayCount && a < _count; a++)
				Wpawn.Right();
		}
	}
	
	this.Start = function(ElementID)
	{
		Wpawn._elementID = ElementID;
		Wpawn.Find();
		
		_timer = setInterval("Wpawn.Right()", _displayInterval);
	}
	
	this.Left = function()
	{
		if(Wpawn._data && Wpawn._data.videos && Wpawn._data.videos.length > 0)
		{
			clearInterval(_timer);
		
			var next = _index + _displayCount;
			if(next >= _count)
				next -= _count;
		
			var item = Wpawn._data.videos[next];
			var html = Utilities.FormatString(_template, item.url, item.thumbnailurl, item.title, item.views, (next % 2 == 1 ? ' alt' : ''), item.id);
		
			var width = $("#" + Wpawn._elementID).children("div:first").width();
			if($("#" + Wpawn._elementID).children().length >= _displayCount)
			{
				$("#" + Wpawn._elementID)
					.append(html)
					.animate({marginLeft:-width}, 'normal', '', function(){$("#" + Wpawn._elementID).css("margin-left","0px").children("div:first").remove();})
					;
			}
			else
			{
				$("#" + Wpawn._elementID).append(html);
			}
			
			_index++;
			if(_index >= _count)
				_index = 0;
			
			if(_count > _displayCount)
				_timer = setInterval("Wpawn.Left()", _displayInterval);
		}
	}
	
	this.Right = function()
	{
		if(Wpawn._data && Wpawn._data.videos && Wpawn._data.videos.length > 0)
		{
			clearInterval(_timer);
		
			var prev = _index - 1;
			if(prev < 0)
				prev = _count - 1;

			var item = Wpawn._data.videos[prev];
			var html = Utilities.FormatString(_template, item.url, item.thumbnailurl, item.title, item.views, (prev % 2 == 1 ? ' alt' : ''), item.id);
			
			var width = $("#" + Wpawn._elementID).children("div:first").width();
			if($("#" + Wpawn._elementID).children().length >= _displayCount)
			{
				$("#" + Wpawn._elementID)
					.prepend(html)
					.css("margin-left","-" + width + "px")
					.animate({marginLeft:0}, 'normal', '', function(){$("#" + Wpawn._elementID).children("div:last").remove();})
					;
			}
			else
			{
				$("#" + Wpawn._elementID).append(html);
			}

			_index--;
			if(_index < 0)
				_index = _count - 1;
			
			if(_count > _displayCount)
				_timer = setInterval("Wpawn.Right()", _displayInterval);
		}
	}
};

var PreviewBox = new function()
{
	var _timer;
	this.Data = {};
	this.LastUrl;
	
	this.ItemRegEx = [
		{pattern:/video\/([0-9]+?)_/gi, url:"?url_type=1&url_id="},
		{pattern:/expert\/([0-9]+?)\.htm/gi, url:"?user_id="},
		{pattern:/member\/([0-9]+?)\.htm/gi, url:"?user_id="},
		{pattern:/video-series\/([0-9]+?)_[^\.]*\.htm/gi, url:"?url_type=2&url_id="},
		{pattern:/([^/\.]+)-guide\.htm/gi, url:"?guide_id="}
		];

	this.InitLinks = function(ClassName)
	{
		var links = $("." + ClassName);
		links.unbind("mouseover", PreviewBox.HrefOnMouseOver);
		links.unbind("mouseout", PreviewBox.OnMouseOut);
		links.mouseover(PreviewBox.HrefOnMouseOver);
		links.mouseout(PreviewBox.OnMouseOut);
		
		var box = $("#previewBox");
		box.unbind("mouseover", PreviewBox.BoxOnMouseOver);
		box.unbind("mouseout", PreviewBox.OnMouseOut);
		box.mouseover(PreviewBox.BoxOnMouseOver);
		box.mouseout(PreviewBox.OnMouseOut);
	}

	this.Templates = {
		Video:'<div class="pubHeader"><a href="{0}" class="title">{1}</a></div>'
			+ '<div class="pubContent flc">'
			+ '<img src="{2}" class="thumb1" />'
			+ '<a href="#" onclick="addToPlaylistPopUp(event,{4});" title="Add to Playlist" class="add1 fl mr5"></a>'
			+ 'By <a href="{5}">{6}</a><br/>'
			+ 'Series: <a href="{7}">{8}</a>'
			+ '<p>{3}</p>'
			+ '</div>',
		VideoSeries:'',
		UserAccount:'<div class="pubHeader"><a href="{0}" class="title">{1}</a></div>'
			+ '<div class="pubContent flc">'
			+ '<img src="{2}" class="thumb1" />'
			+ '<p>{3}</p>'
			+ '</div>',
		Guide:''
		};

// Video
// 0 = url
// 1 = title
// 2 = thumbnail
// 3 = description
// 4 = id
// 5 = expert url
// 6 = expert name
// 7 = series url
// 8 = series title

// UserAccount
// 0 url
// 1 name
// 2 thumbnailurl
// 3 aboutme

	this.Parse = function(Url)
	{
		if(Url)
		{
			var tmpData = PreviewBox.Data[Url];
			if(tmpData)
			{
				PreviewBox.RenderData(tmpData);
			}
			else
			{
				var query = null;
				for(var a = 0, cnt = PreviewBox.ItemRegEx.length; a < cnt; a++)
				{
					var match = new RegExp(PreviewBox.ItemRegEx[a].pattern).exec(Url);
					if(match)
					{
						query = PreviewBox.ItemRegEx[a].url + match[1];
						break;
					}
				}
				if(query && query.length > 0)
				{
					PreviewBox.LastUrl = Url;
					$.getJSON(_ajaxBase + "item.aspx" + query + "&r=" + Utilities.RandomNumber(10000), function(data)
					{
						$("#previewBox").html("requesting...");
						PreviewBox.Data[PreviewBox.LastUrl] = data;
						PreviewBox.RenderData(data);
					});
				}
				else
				{
					PreviewBox.OnMouseOut();
				}
			}
		}
	}

	this.RenderData = function(Data)
	{
		if(Data)
		{
			var output = '';
			switch(Data.type)
			{
				case "Video":
					output += Utilities.FormatString(
						PreviewBox.Templates.Video,
						Data.url,
						Data.title,
						Data.thumbnailurl,
						Data.description,
						Data.id,
						Data.expert.url,
						Data.expert.name,
						Data.series.url,
						Data.series.title
						);
					break;
				case "UserAccount":
					output += Utilities.FormatString(
						PreviewBox.Templates.UserAccount,
						Data.url,
						Data.name,
						Data.thumbnailurl,
						Data.aboutme						
						);
					break;
				case "VideoSeries":
					output += Utilities.FormatString(
						PreviewBox.Templates.VideoSeries,
						Data.id
						);
					break;
			}
			
			$("#previewBox").html(output);
		}
	}
	
	this.HrefOnMouseOver = function(e)
	{
		var box = $("#previewBox");
		if(box.css("display") == 'none' || PreviewBox.LastUrl != this.href)
		{
			clearTimeout(PreviewBox._timer);
			
			box.html("loading...");
			PreviewBox.Parse(this.href);
		
			var newX = e.pageX + 20 +'px',
				newY = e.pageY + 10 +'px';
			box.css({left: newX, top: newY});
			box.fadeIn("fast");
		}
		else
		{
			clearTimeout(PreviewBox._timer);
		}
	}
	this.BoxOnMouseOver = function(e)
	{
		clearTimeout(PreviewBox._timer);
	}
	this.OnMouseOut = function()
	{
		PreviewBox._timer = setTimeout('$("#previewBox").hide()', 50);
	}
};



function switchSelected(Element)
{
	if($(Element).attr("class").indexOf("current") == -1)
	{
		$(Element).parent().children().removeClass("current");
		$(Element).addClass("current");
	}
	else
	{
		return false;
	}
}

function sendToFriend(container, id, type)
{
	var toEmail, fromEmail, emailNote;
	toEmail = $("#" + container.id + " #to_email").val();
	fromEmail = $("#" + container.id + " #from_email").val();
	emailNote = $("#" + container.id + " #email_note").val();
	
	$.post(_ajaxBase + 'sendtofriend.aspx', {id:id, type:type, to:toEmail , from:fromEmail, message:emailNote},
			function(data)
			{
				if(data.indexOf("true") > -1)
				{
					$("#" + container.id + " #email_confirmation").html("Email was successfully sent.");
					$("#" + container.id + " #email_confirmation").show();
					$(".popUpBox2").fadeOut(2000);
				}
			}
			);
}

function RequestNewPassword(email, container)
{	
	$.post(_ajaxBase + 'misc.aspx', { emailaddress: email, method: 'forgot-password'},
			function(data)
			{
				if(data.indexOf("true") > -1)
				{
					$(".forgot_password_confirmation").html("Email was successfully sent.");
					$(".forgot_password_confirmation").show();
					$(container).fadeOut(2000);
				}
				else
				{
					$(".forgot_password_confirmation").html("Failed to find an account associated to the email address.");
					$(".forgot_password_confirmation").show();
				}
			}
	);
}

function SetVideoScrollerCSS(id)
{
	var videowindow = document.getElementById(id); 
	var videolist = $('#' + videowindow.id + ' .videoList2'); 
	$(videowindow).css("height","250px");
	$(videowindow).css("overflow","hidden");
	$(videolist).css("width","4020px");
	$(videolist).css("position","absolute");
	$(videolist).css("left","0");
}

function SetVideoSeriesScrollerCSS(id)
{
	var videowindow = document.getElementById(id); 
	var videolist = $('#' + videowindow.id + ' .videos'); 
	$(videowindow).css("height","110px");
	$(videowindow).css("overflow","hidden");
	$(videolist).css("width","115000px");
	$(videolist).css("position","absolute");
	$(videolist).css("left","0");
	$(videowindow).parent().children(".animate_right").css("display", "inline");
	$(videowindow).parent().children(".animate_left").css("display", "inline");
}

