﻿/// playerlayer.js: functions on top of media player

var positionOnLoad = -1;
var videoID = -1;
var mediaPlayerAdjY = 45;
var videoFile = "";

function visitVideoCount(vID)
{
    var paramArray = new Array();
    paramArray["VidoeID"] = vID;
    AJAXSubmit("visitvideo", paramArray, sync , false);
}

function sync(xmlDoc)
{
    switch(ListTab)
    {
        case "RecentList" :   getRecentVideos();
                              break;
        case "PopularList":   getPopularVideos();
                              break;
    }
}

///control of Video
		function FlashInteropCall(func, args) {
			var xml = "<invoke name='" + func + "' returntype='xml'>";
			xml += "<arguments>";
			if (args != null) {
				for (i=0; i<args.length; i++) {
					xml += "<" + args[i].type + ">" + args[i].value + "</" + args[i].type + ">";
				}
			}
			xml += "</arguments>";
			xml += "</invoke>";

			return document.getElementById("FlashPlayer").CallFunction(xml);

			// do something else for FireFox
		}
		
		//
		function VideoPlay()
		{
		    FlashInteropCall('VideoPlay', null);
		    ShowComments();
		}
		
		function VideoPause()
		{
		    FlashInteropCall('VideoPause', null);
		    SearchCommentsToShow();
		}
		
		function VideoStop()
		{
		    FlashInteropCall('VideoStop', null);
		    HideComments();
		}
		
		function VideoCurrentPosition()
		{
		    var time = FlashInteropCall('VideoCurrentPosition', null);
		    time = time.substring(8,time.length).replace("</number>","");
		    return time;
		}
		
		function VideoCurrentStatus()
		{
		    var status = FlashInteropCall('VideoGetStatus', null);
		    status = status.substring(8,status.length).replace("</string>","");
		    return status;
		}
		
		function VideoSetPosition(time)
		{   
		    args = new Array();
			args[0] = {
			    value: time,
				type: "number"
			};
		    FlashInteropCall("VideoSetPosition",args);
		}
		
		function VideoGetDuration()
		{
		    try
		    {
		        var VideoLength = FlashInteropCall("VideoGetDuration",null);
		        VideoLength = VideoLength.substring(8,VideoLength.length).replace("</number>","");
		        return VideoLength;
		    }
		    catch(e)
		    {
		        alert("click player to start!");
		    }
		}

function VideoLoad(url)
{
    ClearClientComments();
    args = new Array();
	args[0] = {
		value: url,
		type: "string"
	};
    FlashInteropCall("VideoLoad",args);
}		

var VideoClicked = false;

function loadVideoAndPlay(url,vID) 
{
    if(VideoCurrentStatus()=="disconnected")
    {
        alert("click player first!");
    }
    else if(VideoClicked)
    {
        alert("You'd Cannot Switch So Fast!");
    }
    else
    {
        VideoClicked = true;
        window.setTimeout("EnableSwitch()",3000);
        VideoLoad(url);
        CurrentVideoID = vID;
        HideComments();
        playIfLoaded(vID) ;
    }
    //document.MediaPlayer.controls.play();
}

function EnableSwitch()
{
    VideoClicked = false;
}

function playIfLoaded(vID) 
{
    if (VideoCurrentStatus() != "playing" && document.getElementById("barLeft1").style.display != "") 
    {
        if(VideoCurrentStatus() == "connectionError")
        {
            alert("Load Video Failed !!");
        }
        else
        {
            VideoPlay();
            window.setTimeout("playIfLoaded("+vID+")", 1000);
        }
    } 
    else 
    {
        visitVideoCount(vID);
        GetCommentOfVideo(vID);
        GetTagOfVideo(vID);
    }
}

// goes to the position in our currently playing video
function goToTimeAndPause(t) 
{
    loadVideoAndPause(videoID, t);
}

// loads the video, then pauses it at the frame we want as soon as its ready
function loadVideoAndPause(videoID, t) 
{
    positionOnLoad = t;
    loadVideo(videoID);
    if (positionOnLoad >= 0 && document.MediaPlayer.playState != 6 && document.MediaPlayer.playState != 0) 
    {
        // it was already loaded, force onVideoLoadSeekAndPause
        onVideoLoadSeekAndPause();
    }  
}

// --- position calculating functions ---
function xPercent(x) 
{
    return x / parseFloat(document.MediaPlayer.width);
}

function yPercent(y) 
{
    return y / (parseFloat(document.MediaPlayer.height) - mediaPlayerAdjY);
}

function xCoord(x) 
{
    return x * parseFloat(document.MediaPlayer.width);
}

function yCoord(y) 
{
    return y * (parseFloat(document.MediaPlayer.height) - mediaPlayerAdjY);
}


function playerPercentXToAbs(x) 
{
    return document.getElementById("mediaPosition").offsetLeft + xCoord(x);
}

function playerPercentYToAbs(y) 
{
    return document.getElementById("mediaPosition").offsetTop + yCoord(y);
}

