﻿var g_ageRestricted = new Array();

$(document).ready(function(){
	$("#lstWebsites").addClass("Selected");
	$("#MyProfiles").show();
	if ($("#Video embed").attr("width")>420)
	{
		$("#Video object").attr("width",420); 
		$("#Video embed").attr("width",420);
	}
});

function ShowEmbed() { $("#EmbedCode").slideToggle("slow"); }
function ShowComments() { $("#AddCommentPanel").slideToggle("slow"); }

var pos=2;
var loading = false;
function PlaylistNext()
{
	if (!loading)
	{
		loading = true;
		pos = pos+2;
		
		$.get(metvServicesUrl + "channels/playlist.xml?position="+pos,{},
			function(data) {
				loading = false;
				if ($("/rsp/playlist/video",data).length == 0)
				{
					$("#btnNext").hide();
					pos = pos-2;
				}
				else
				{
					if ($("/rsp/playlist",data).attr("total") == pos+1)
					{
						$("#btnNext").hide();
					}
					$("#btnPrevious").show();
					if ($("/rsp/playlist/video",data).length < 2)
					{
						$("#btnNext").hide();
						$("#PlayingNext .Video").each(function(i) {
							$("#PlayingNext .Video:eq("+i+") span").html("&nbsp;&nbsp;&nbsp;");
							$("#PlayingNext .Video:eq("+i+") a").attr("href","javascript:void(0);");
							$("#PlayingNext .Video:eq("+i+") img:eq(0)").attr("src", metvMediaUrl + "images/Blank.gif");
							$("#PlayingNext .Video:eq("+i+") img:eq(1)").hide();
						});
					}
					$("/rsp/playlist/video",data).each(function(i) {
						$("#PlayingNext .Video:eq("+i+") a").attr("href","javascript:PlayVideo("+$("/rsp/playlist/video",data).eq(i).attr("id")+");");
						$("#PlayingNext .Video:eq("+i+") span:eq(0)").html($("/rsp/playlist/video",data).eq(i).attr("title").replace(/(.{1,35}).*/ig, '$1...'));
						$("#PlayingNext .Video:eq("+i+") img:eq(0)").attr("src",$("/rsp/playlist/video",data).eq(i).attr("thumbnail"));
						$("#PlayingNext .Video:eq("+i+") img:eq(1)").show();
						$("#PlayingNext .Video:eq("+i+") span:eq(1)").html("Host: <a href=\"\"></a>");
						$("#PlayingNext .Video:eq("+i+") span:eq(1) a").html($("/rsp/playlist/video/host",data).eq(i).attr("name"));
						$("#PlayingNext .Video:eq("+i+") span:eq(1) a").attr("href","http://"+$("/rsp/playlist/video/host",data).eq(i).attr("url"));
				        var id = $("/rsp/playlist/video",data).eq(i).attr("id");
				        window.g_ageRestricted[id] =  ($("/rsp/playlist/video", data).attr("ageRestricted") || "").toLowerCase() == "true" ? true : false;		
					});  
				}
			}
		);
	}
}

function PlaylistPrev()
{
	if (!loading)
	{
		loading = true;
		pos = pos - 2;
		$.get(metvServicesUrl + "channels/playlist.xml?position="+pos,{},
			function(data) {
				loading = false;
				$("#btnNext").show();
				if (pos == 0) 
				{
					$("#btnPrevious").hide();
					
					$("#PlayingNext .Video:eq(1) a").attr("href","javascript:PlayVideo("+$("/rsp/playlist/video",data).eq(0).attr("id")+");");
					$("#PlayingNext .Video:eq(1) span:eq(0)").html($("/rsp/playlist/video",data).eq(0).attr("title").replace(/(.{1,35}).*/ig, '$1...'));
					$("#PlayingNext .Video:eq(1) img:eq(0)").attr("src",$("/rsp/playlist/video",data).eq(0).attr("thumbnail"));
					$("#PlayingNext .Video:eq(1) img:eq(1)").show();
					$("#PlayingNext .Video:eq(1) span:eq(1)").html("Host: <a href=\"\"></a>");
					$("#PlayingNext .Video:eq(1) span:eq(1) a").html($("/rsp/playlist/video/host",data).eq(0).attr("name"));
					$("#PlayingNext .Video:eq(1) span:eq(1) a").attr("href","http://"+$("/rsp/playlist/video/host",data).eq(0).attr("url"));
					
					
					$("#PlayingNext .Video:eq(0) span").html("&nbsp;");
					$("#PlayingNext .Video:eq(0) a").attr("href","javascript:void(0);");
					$("#PlayingNext .Video:eq(0) img:eq(0)").attr("src", metvMediaUrl + "images/Blank.gif");
					$("#PlayingNext .Video:eq(0) img:eq(1)").hide();
					
				}
				else
				{
					$("/rsp/playlist/video",data).each(function(i) {
						$("#PlayingNext .Video:eq("+i+") a").attr("href","javascript:PlayVideo("+$("/rsp/playlist/video",data).eq(i).attr("id")+");");
						$("#PlayingNext .Video:eq("+i+") span:eq(0)").html($("/rsp/playlist/video",data).eq(i).attr("title").replace(/(.{1,35}).*/ig, '$1...'));
						$("#PlayingNext .Video:eq("+i+") img:eq(0)").attr("src",$("/rsp/playlist/video",data).eq(i).attr("thumbnail"));
						$("#PlayingNext .Video:eq("+i+") img:eq(1)").show();
						$("#PlayingNext .Video:eq("+i+") span:eq(1)").html("Host: <a href=\"\"></a>");
						$("#PlayingNext .Video:eq("+i+") span:eq(1) a").html($("/rsp/playlist/video/host",data).eq(i).attr("name"));
						$("#PlayingNext .Video:eq("+i+") span:eq(1) a").attr("href","http://"+$("/rsp/playlist/video/host",data).eq(i).attr("url"));
				        var id = $("/rsp/playlist/video",data).eq(i).attr("id");
				        window.g_ageRestricted[id] =  ($("/rsp/playlist/video", data).attr("ageRestricted") || "").toLowerCase() == "true" ? true : false;										
					});
				}
			}
		);
	}
}

function verifyAge(videoid)
{
    var ageVerified = true;
    if(window.g_ageRestricted && g_ageRestricted[videoid])
    {
        ageVerified = false;
        if(document.cookie.indexOf("verify_age") == -1)
        {
            if(confirm("This video may contain adult content. Are you 18 years or older?"))
            {
                //Not logged in
                if(document.cookie.indexOf("meTV_SD") == -1 || document.cookie.indexOf("meTV_SE") == -1 || document.cookie.indexOf("meTV_VC") == -1)
                {
                    document.cookie = document.cookie = "verify_age=1;path=/;domain=" + window.location.hostname.substring(window.location.hostname.indexOf("."));
                }
                else //Logged in
                {
                    var today = new Date();
                    today.setTime(today.getTime());
                    var expire_date = new Date(today.getTime() + 365*24*3600*1000);
                    document.cookie = "verify_age=1;expires=" + expire_date.toDateString() + ";path=/;domain=" + window.location.hostname.substring(window.location.hostname.indexOf("."));
                }
                ageVerified = true;
            }
            else        
            {
                ageVerified = false;
            }
        }
        else
        {
            ageVerified = true;
        }        
    }
    return ageVerified;
}

function PlayVideo(videoid)
{
    if(!verifyAge(videoid)) return;
	if (videoid == g_currentVideoId) return;
	g_currentVideoId = videoid;
	$.get(metvServicesUrl + "videos/view.xml?source=home&vid="+videoid,{}, playVideoCallback);
}

function playVideoCallback(data) 
{
	$("#VideoContainer").html(data);
	if ($("#Video embed").attr("width")>420) $("#Video embed").attr("width",420);
	var rating = $("/rsp/video",data).attr("ratingrounded");			
	var plRating = new RatingMD('plRating', 5, 'playlist',videoid);
	plRating.setRating(rating);
	plRating.postHandler = postVideoRating;
	g_currentVideoId = videoid;

}

var _videoid;


var lenmax = 1000;
function inputpress(inp, evt) 
{
	var dv = document.getElementById('dvChars');
	var len = inp.value.length;
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
	var isDel = charCode == 8 || charCode == 9 || charCode == 46 || (charCode > 36 && charCode < 41);
	if (len > lenmax - 1)	{
		if (!isDel && !evt.ctrlKey) {
			return false;
		}
	}
	if (!(evt.ctrlKey || charCode == 9 || charCode == 15 || (charCode > 36 && charCode < 41)))
	{
		if (!isDel)
			dv.innerHTML = lenmax - len-1;
		else if (len > 0)
			dv.innerHTML = lenmax - len+1;
		else
			dv.innerHTML = lenmax - len;
	}
	return true;
}

function inputup(inp) 
{
	var dv = document.getElementById('dvChars');
	var len = inp.value.length;
	if (len > lenmax) 
	{
		inp.value = inp.value.substring(0,lenmax);
		len = inp.value.length;
	}
	dv.innerHTML = lenmax-len;
}
