﻿function ShowWebsites()
{
	$("#lstWebsites").addClass("Selected");
	$("#lstFriends").removeClass("Selected");
	$("#MyProfiles").show();
	$("#MyFriends").hide();
}

function ShowFriends()
{
	$("#lstFriends").addClass("Selected");
	$("#lstWebsites").removeClass("Selected");
	$("#MyProfiles").hide();
	$("#MyFriends").show();
}

function winopen(url, winname, width, height)
{
	if(winname == null) winname = "popwin";
	if(width == null) width = 400;
	if(height == null) height = 320;
	if(url != null && url.length > 0) {
		var popwin = window.open(url, winname, "width=" + width + ",height=" + height + ",status=no,titlebar=no,toolbar=no,resizeable=no");
		popwin.focus();
	}
}

function winclose(url) {
	if (window.opener)
	{
		if (url != null && url.length > 0) {
			window.opener.location.href = url;
		} else {
			window.opener.location.reload();
		}
		window.opener.focus();
	}
	window.close();
}

function getQueryString()
{
	var locationstring = location.search.substring(1,location.search.length);
	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;
	}
}

function getXmlHttp()
{
	if(!window.XMLHttpRequest)
	{
		var xmls = new Array("Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP");
		for(var a = 0; a < xmls.length; a++) {
			try {return new ActiveXObject(xmls[a]);}
			catch(e) {}
		}
	}
	else
	{
		try {return new XMLHttpRequest();}
		catch(e) {}
	}
	return null;
}

function FindUploadCookieValue(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;
}

function addToFavorites(type, id) {	
	$.get(metvServicesUrl + "favorites/add.xml", {id: id, type: type}, function(responseXML) {
		alert(($("rsp", responseXML).attr("stat") == "ok") ? "This " + ((type == "1") ? "channel" : "video") + " has been added to your favorites" : $("err", responseXML).attr("msg"));
	});
}

function bookmarkVideo(id, addAsFavorite, token) {
	var favorite = (addAsFavorite) ? "true" : "false";
	var bm = window.open(metvUrl + 'videos/bookmark.html?vid=' + id + '&favorite=' + favorite + (token & token != '' ? '&ftk=' + token : '') + (g_bookmarkVersion ? '&version='+encodeURIComponent(g_bookmarkVersion) : ''), '_blank', 'width=520,height=680,scrollbars=yes');
	bm.focus(); 
}

function s_bookmarkVideo(url, title, addAsFavorite, token)
{
	var favorite = (addAsFavorite) ? "favorite=1&" : "";
	var bm = window.open(metvUrl + 'videos/bookmark.html?'+favorite+'url='+encodeURIComponent(url)+'&title='+encodeURIComponent(title)+(token && token != '' ? '&ftk=' + token : '') + (g_bookmarkVersion ? '&version='+encodeURIComponent(g_bookmarkVersion) : ''),'metvbm', 'width=520,height=680,scrollbars=yes');
	bm.focus();
}

//This is a function for adding tags from a popular tags list upon clicking
function addTag(source, target)
{
    var target = document.getElementById(target);
    if(!target) return;
    if(target.value.indexOf(source.innerHTML) != -1) return;
    else target.value = target.value ? target.value + "," + source.innerHTML : source.innerHTML;
}