﻿
var curPos = 1; //track the current position of the playlists
var maxPos = 1; //How many pages of clips do we have?
var cVideos = 4; //How many video Clips do we have?
var strLastVol = "0.5"; //Inital volumet setting

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false
// Temporary variables to hold mouse x-y pos.s
var tempX = 0;
var tempY = 100;

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Define global variables.
var plugin, toolTip, mainCanvas;

// Set global variables for the plug-in and main Canvas objects.
function onLoaded(sender, eventArgs)
{
    plugin = sender.getHost();
    mainCanvas = sender;
    document.onmousemove = getMouseXY;
    //Automatically load the first video
    mainCanvas.children.findName("VideoWindow").Source=get_mediainfo("0");
    mainCanvas.children.findName("PlaySymbol").visibility = "collapsed";
    //Set inital vol
    strLastVol = "0.5";
    slider_SetValue(mainCanvas.children.findName("VolumeControl"), 50);
}

function onMouseEnter(sender, args) {
    mainCanvas.children.findName("txtToolTip").Text = sender.tag;
    mainCanvas.children.findName("txtToolTip").visibility = "visible";
     
//    if (toolTip == null) {

//        var txtMsg = '';
//        // Define the XAML fragment for the tool tip.
//        var xamlFragment = '<Canvas Width="150" Height="30" Background="#FFFFE1">';
//           xamlFragment +=   '<Rectangle Width="150" Height="30" Stroke="#FFFFFFFF" Fill="#FF000000" />';
//           xamlFragment +=   '<TextBlock Text="' + sender.tag + '" Canvas.Left="10" Canvas.Top="5" Foreground="#c8b68d" />';
//           xamlFragment += '</Canvas>';

//        // Create the XAML fragment for the tool tip.
//        toolTip = plugin.content.createFromXaml(xamlFragment);

//// Set-up to use getMouseXY function onMouseMove
//        document.onmousemove = getMouseXY;


//        // Position the tool tip at a relative x/y coordinate value.
//        var cursorPosition = args.getPosition(null);        
//        //getMouseXY();
//        toolTip["Canvas.Left"] = tempX;
//        toolTip["Canvas.Top"] = tempY;
//    }

//    // Add the tool tip to the Canvas object.
//    mainCanvas.children.add(toolTip);
//    sender.opacity = 1;
}

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  
  return true
}

function onMouseLeave(sender, args)
{
//    // Determine whether the tool tip is created.
//    if (toolTip != null)
//    {
//        // Remove the tool tip from the Canvas object.
//        mainCanvas.children.remove(toolTip);
//        toolTip = null;
//    }
//    sender.opacity = .5;
    
    mainCanvas.children.findName("txtToolTip").Text = "";
    mainCanvas.children.findName("txtToolTip").visibility = "collapsed";

}

function onMouseLeftButtonDown(sender, args)
{
    var strname = sender.name;
    strname = strname.replace("play", "");
    mainCanvas.children.findName("txtHeader").text = "Now Playing: " + sender.tag;
    //Start the first video
    mainCanvas.children.findName("VideoWindow").Source=get_mediainfo(strname);
    //Hide the play button and show the pause button
}

function onVideoMouseLeftDown(sender, args)
{
    //Determine media state
    switch(sender.CurrentState){
        case "Playing":
            sender.Pause();
            break;
        case "Paused":
            sender.Play();
            break;
        case "Stopped":
            sender.Play();
            break;
    } 
}

function get_mediainfo(mediainfoIndex) {
    var strresult ="";
   switch (mediainfoIndex) {        

        case "0":
            strresult =  "videos/tgbilldance.wmv";   
            break;                                                             

        case "1":
            strresult =  "videos/tgfull.wmv";
            break;                                                                

        case "2":
            strresult =  "videos/tgtradeshow.wmv";
            break;                   
            
        case "3":
            strresult =  "videos/tgbullseye.wmv";
            break;                                                               
        default:
        break;
     }
     return strresult;
}

function SBonMouseEnter(sender, args)
{
    var strname = sender.tag + "_MouseEnter";
    plugin.content.findName(strname).Begin();
}

function SBonMouseLeave(sender, args)
{
    var strname = sender.tag + "_MouseLeave";
    plugin.content.findName(strname).Begin();
}


function onStopButtonDown(sender, args)
{
     plugin.content.findName("VideoWindow").Stop(); 
       //Show play button and hide the pause button
       showPlayButton(); 
}

function onPreviousButtonDown(sender, args)
{
    //Check if video is currently playing
   if (plugin.content.findName("VideoWindow").CurrentState == "Playing")
   {
       plugin.content.findName("VideoWindow").Stop();
       plugin.content.findName("VideoWindow").Play();
   }
   else{
       plugin.content.findName("VideoWindow").Stop(); 
       //Show play button and hide the pause button
       showPlayButton(); 
   }
}

function onPlayPauseButtonDown(sender, args)
{
    //Check if video is currently playing
   if (plugin.content.findName("VideoWindow").CurrentState == "Playing")
   {
       //Pause the video
       plugin.content.findName("VideoWindow").Pause();
       //Show play button and hide the pause button
       showPlayButton();
       
   }
   else{
       plugin.content.findName("VideoWindow").Play();   
       showPauseButton();
   }
}

function onMuteButtonDown(sender, args)
{
    if(plugin.content.findName("VideoWindow").volume == 0){
        plugin.content.findName("VideoWindow").volume = strLastVol;
        plugin.content.findName("MuteOffSymbol").visibility = "visible";
    }
    else{
       strLastVol = plugin.content.findName("VideoWindow").volume;
       plugin.content.findName("VideoWindow").volume = 0;
       plugin.content.findName("MuteOffSymbol").visibility = "collapsed";
    }
}


function onTimeLineDown(sender, args)
{
    if(intCurrentLength > 0)
    {
        var pos = plugin.content.findName("VideoWindow").position;
        pos.Seconds = (intCurrentLength * (args.getPosition(null).x - 196)/239)
        plugin.content.findName("VideoWindow").position = pos;
    }
    //plugin.content.findName("playingRect").width = 

}

function showPlayButton()
{
    //Show play button and hide the pause button
    mainCanvas.children.findName("PlaySymbol").visibility = "visible";
    mainCanvas.children.findName("PauseSymbol").visibility = "collapsed";
}

function showPauseButton()
{
   //Show play button and hide the pause button
   mainCanvas.children.findName("PlaySymbol").visibility = "collapsed";
   mainCanvas.children.findName("PauseSymbol").visibility = "visible";
}


var mouseDownPosition = 0;
var mouseDownValue = -1;
var thumbCenter = 0;

function slider_Loaded(sender, args) {
}

function slider_MouseLeftButtonDown(sender, args) {
    var coordinate = args.getPosition(null).x;
    var slider = sender.findName("VolumeControl");
    coordinate -= slider["Canvas.Left"];
    slider_SetValue(slider, coordinate - thumbCenter);  
}

function slider_thumb_MouseLeftButtonDown(sender, args) {
    var slider = sender.findName("VolumeControl");
    sender.captureMouse();
    mouseDownValue = slider_GetValue(slider);
    mouseDownPosition = args.getPosition(null).x;
}

function slider_thumb_MouseLeftButtonUp(sender, args) {
    var slider = sender.findName("VolumeControl");
    slider.releaseMouseCapture();
    mouseDownValue = -1;
}

function slider_thumb_MouseMove(sender, args) {
    var slider = sender.findName("VolumeControl");
    if (mouseDownValue != -1) {
        var newValue = mouseDownValue + (args.getPosition(null).x - mouseDownPosition);    
        slider_SetValue(slider, newValue);
    }   
}

function slider_GetValue(sender) {
    var thumb = sender.findName("slider_thumb");
    return thumb["Canvas.Left"]; 
}

function slider_SetValue(sender, newValue) {
    
    if (newValue > sender.width ) {
        newValue = sender.width;
        mouseDownValue = -1;
    }
    if (newValue < - thumbCenter) {
        newValue = - thumbCenter;
        mouseDownValue = -1;
    }
    var thumb = sender.findName("slider_thumb");
    
    thumb["Canvas.Left"] = newValue;
    if(newValue > 0)
    {
        plugin.content.findName("txtVolume").Text = newValue + "%";
    }
    else{
        plugin.content.findName("txtVolume").Text = "0%";    
    }
    plugin.content.findName("VideoWindow").volume = (newValue / 100);
    //Reset mute image to off
    plugin.content.findName("MuteOffSymbol").visibility = "visible";
}


var mouseDownPosition2 = 0;
var mouseDownValue2 = -1;
var thumbCenter2 = 5;

function slider_Loaded2(sender, args) {
}

function slider_MouseLeftButtonDown2(sender, args) {
    
    var coordinate = args.getPosition(null).x;
    var slider = sender.findName("VolumeControl");
    coordinate -= slider["Canvas.Left"];
    slider_SetValue2(slider, coordinate - thumbCenter2);  
}

function slider_thumb_MouseLeftButtonDown2(sender, args) {
    var slider = sender.findName("VolumeControl");
    sender.captureMouse();
    mouseDownValue2 = slider_GetValue(slider);
    mouseDownPosition2 = args.getPosition(null).x;
}

function slider_thumb_MouseLeftButtonUp2(sender, args) {
    var slider = sender.findName("TimelineControl");
    slider.releaseMouseCapture();
    mouseDownValue2 = -1;
}

function slider_thumb_MouseMove2(sender, args) {
    var slider = sender.findName("TimelineControl");
    if (mouseDownValue != -1) {
        var newValue = mouseDownValue2 + (args.getPosition(null).x - mouseDownPosition2);    
        slider_SetValue2(slider, newValue);
    }   
}

function slider_GetValue2(sender) {
    var thumb = sender.findName("slider_thumb");
    return thumb["Canvas.Left"]; 
}

function slider_SetValue2(sender, newValue) {
    
    if (newValue > sender.width ) {
        newValue = sender.width;
        mouseDownValue2 = -1;
    }
    if (newValue < - thumbCenter) {
        newValue = - thumbCenter;
        mouseDownValue2 = -1;
    }
    var thumb = sender.findName("slider_thumb");
    
    thumb["Canvas.Left"] = newValue;
    //plugin.content.findName("VideoWindow").volume = (newValue / 100);
}


var isMediaOpened = false;
var intCurrentLength = 0;
function mediaDownloadChanged(sender, args)
{
    var width = Math.floor(sender.downloadProgress * 300);
    sender.findName("downloadRect").Width = width;
    //intCurrentLength = (width-196)/239;
}

function timerComplete(sender, args)
{
    var player = sender.findName("VideoWindow");
    
    if(isMediaOpened)
    {
        var percent = player.position.seconds / player.naturalDuration.seconds;
        var width = Math.floor(percent * 300);
        sender.findName("playingRect").Width = width;
    }
    sender.begin();
}

function mediaOpened(sender, args)
{
    isMediaOpened = true;
    intCurrentLength = sender.findName("VideoWindow").naturalDuration.seconds;
}

function mediaEnded(sender, args)
{
    var player = sender.findName("VideoWindow");
    player.stop();
    player.play();
}