var curPos = 1; //track the current position of the playlists
var maxPos = 2; //How many pages of clips do we have?
var cVideos = movieTotal; //How many video Clips do we have?
var cCats = 0;
var currentCategory = "All Videos";
var pluginID;
var pluginID2;
var pluginID3;
var pluginID4;
var pluginID5;
var pluginID6;
var pluginID7;
var pluginID8;
var pluginID9;
var thumbAllXPos = 0;
var sentFrom;

var playerPlugin;


var movieWidthTemp = 139;
var movieTotalWidth = movieWidthTemp * cVideos;
  

//alert(movieTotal);
function get_mediainfo(mediainfoIndex) {
        pluginID.Text= xmlObj.childNodes[mediainfoIndex].attributes[2].nodeValue;
        
        
        return  { "mediaUrl": xmlObj.childNodes[mediainfoIndex].attributes[1].nodeValue,
                      "placeholderImage": xmlObj.childNodes[mediainfoIndex].attributes[3].nodeValue,
                      "chapters": [               
                                  ] };
        
}

function StartWithParent(parentId, appId) {
    new StartPlayer_0(parentId);
}

function StartPlayer_0(parentId) {
    this._hostname = EePlayer.Player._getUniqueName("xamlHost");
    Silverlight.createObjectEx( {   source: 'player.xaml', 
                                        parentElement: $get(parentId ||"divPlayer_0"), 
                                        id:this._hostname, 
                                        properties:{ width:'100%', height:'100%', version:'1.0', background:document.body.style.backgroundColor, isWindowless:'false' }, 
                                        events:{ onLoad:Function.createDelegate(this, this._handleLoad) } } );
    //This sets the video to play
    this._currentMediainfo = movieSelected - 1;      
}

    function createThumbsXamlCode()
    {
            var movieCollection = "";
            var movieXPos = 0;
            var movieWidth = 139;
            var categoryLinks = ""
            var linkYPos = 0;  
            var activeCat = ""
            cCats=0;

            categoryLinks = categoryLinks + '<TextBlock Width="120" x:Name="cat'+cCats+'" Height="26" Canvas.Top="'+ linkYPos +'" Text="All Videos" TextWrapping="Wrap" Cursor="Hand" Canvas.Left="460" Foreground="#FF9A9797" FontSize="12"/>';                                       
            linkYPos = linkYPos + 20;
            cCats++;
            for(var i=0; i<cVideos; i++){
                 
                var curCat = xmlObj.childNodes[i].attributes[4].nodeValue;                                   
                if (curCat != activeCat) 
                {
                    activeCat = curCat;
                    categoryLinks = categoryLinks + '<TextBlock Width="120" x:Name="cat'+cCats+'" Height="26" Canvas.Top="'+ linkYPos +'" Text="'+ activeCat + '" TextWrapping="Wrap" Cursor="Hand" Canvas.Left="460" Foreground="#FF9A9797" FontSize="12"/>';                           
                    linkYPos = linkYPos + 20;  
                    cCats++;                  
                }
                              
                //alert("reading cat: " + curCat);            
                if (curCat == currentCategory || currentCategory == "All Videos") 
                {
                    var currentMovie = xmlObj.childNodes[i].attributes[3].nodeValue;
                    movieCollection = movieCollection + '<Image x:Name="play'+i+'" Opacity="0.74" Width="120" Height="80" Source="'+currentMovie+'" Stretch="Fill" Cursor="Hand" Canvas.Left="'+movieXPos+'" />';               
                    var movieCap = xmlObj.childNodes[i].attributes[0].nodeValue;
                    movieCollection = movieCollection + '<TextBlock Width="120"  Height="26" Canvas.Top="83" Text="'+ movieCap + '" TextWrapping="Wrap" Canvas.Left="'+movieXPos+'" Foreground="#FF9A9797" FontSize="12"/>';                
                    movieXPos = movieXPos + movieWidth;   
                }               
                         
            }
            
            var xaml = '<Canvas ' +
                    'xmlns="http://schemas.microsoft.com/client/2007" ' +
                    'xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ' +
                    'x:Name="playlist1" ' +
                    'Canvas.Top="70" ' +
                    'Canvas.Left="78" Width="257.275" Height="152.96">' + 
                    categoryLinks +
                    '<Rectangle Width="525" Height="140" Fill="#181414" Stroke="#FF000000" Canvas.Left="-5" Canvas.Top="450" Opacity="1"/>' +
                    '<Canvas Width="157.275" Height="152.96" Canvas.Left="0" Canvas.Top="460" x:Name="Library">'+movieCollection+
                        	    
                    
                 
                 '</Canvas>'+
                 
                 '<Rectangle Width="75" Height="140" Fill="#181414" Stroke="#FF000000" Canvas.Left="-80" Canvas.Top="450" Opacity="1"/>' +
                 '<Rectangle Width="75" Height="140" Fill="#181414" Stroke="#FF000000" Canvas.Left="520" Canvas.Top="450" Opacity="1"/>' +
                 
                 '<Path x:Name="LeftArrow" Opacity="0.25" Width="38" Height="38" Stretch="Fill" Stroke="#FF000000" Canvas.Left="-40" Canvas.Top="482" Data="M37.5,0.5 L37.5,37.5 0.5,37.5 z" Fill="#FFFFFFFF" RenderTransformOrigin="0.5,0.5" Cursor="Hand">'+
		'<Path.RenderTransform>'+
			'<TransformGroup>'+
				'<ScaleTransform ScaleX=".6" ScaleY=".6"/>'+
				'<SkewTransform AngleX="0" AngleY="0"/>'+
				'<RotateTransform Angle="134.119"/>'+
				'<TranslateTransform X="0" Y="0"/>'+
			'</TransformGroup>'+
		'</Path.RenderTransform>'+
	'</Path>'+
	'<Path x:Name="RightArrow" Opacity="0.25" Width="38" Height="38" Stretch="Fill" Stroke="#FF000000" Canvas.Left="510" Canvas.Top="482" Data="M37.5,0.5 L37.5,37.5 0.5,37.5 z" Fill="#FFFFFFFF" RenderTransformOrigin="0.5,0.5" Cursor="Hand">'+
		'<Path.RenderTransform>'+
			'<TransformGroup>'+
				'<ScaleTransform ScaleX=".6" ScaleY=".6"/>'+
				'<SkewTransform AngleX="0" AngleY="0"/>'+
				'<RotateTransform Angle="314.365"/>'+
				'<TranslateTransform X="0" Y="0"/>'+
			'</TransformGroup>'+
		'</Path.RenderTransform>'+
	'</Path>'+
	
                 '</Canvas>'; 
                 
            // This sets the thumb movement width
            pluginID4.To = -((139+(cVideos*1.7)) * (cVideos));
            pluginID4.From = movieTotalWidth;
            //alert(movieTotalWidth);
            pluginID5.From = -((139+(cVideos*1.7)) * (cVideos));
            pluginID5.To = movieTotalWidth;
            
            
            return xaml; 
            movieWidthTemp = movieWidthTemp+movieWidthTemp;
    }


StartPlayer_0.prototype= {
    _handleLoad: function(plugIn) {
        this._player = $create(   ExtendedPlayer.Player, 
                                  { // properties
                                    autoPlay    : true, 
                                    volume      : 1.0,
                                    muted       : false
                                  }, 
                                  { // event handlers
                                    mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                                    mediaFailed: Function.createDelegate(this, this._onMediaFailed)
                                   // FullScreenChange: Function.createDelegate(this, this._onFullScreenChange)
                                  },
                                  null, $get(this._hostname)  ); 
		
		//wire up the rollover and click events for each of our play buttons		
		
		txtXAML=plugIn.Content.findName('movieTitle');
		pluginID = txtXAML;
		
		captionXAML=plugIn.Content.findName('caption');
		pluginID2 = captionXAML;
		
		thumXAML=plugIn.Content.findName('playlist1');
		pluginID3 = thumXAML;
		
		leftAnimAML=plugIn.Content.findName('leftAnimateTo');
		pluginID4 = leftAnimAML;
		
		rightAnimXAML=plugIn.Content.findName('rightAnimateTo');
		pluginID5 = rightAnimXAML;

		leftAnimAMLStop=plugIn.Content.findName('MoveLeft01');
		pluginID6 = leftAnimAMLStop;
		
		pluginID8 = plugIn.Content.findName("RightArrow");
        pluginID9 = plugIn.Content.findName("LeftArrow");
		
		    
		var root = plugIn.content.root; 		
		var xaml = createResultsXaml();
         var child = plugIn.content.createFromXaml(xaml);
         root.children.add(child);
         
         thumbAll=root.findName('Library');
		    pluginID7 = thumbAll;
		    
        playerPlugin = plugIn;
             
        function createResultsXaml()
        {
            var movieCollection = "";
            var movieXPos = 0;
            var movieWidth = 139;
            var categoryLinks = ""
            var linkYPos = 0;  
            var activeCat = ""
            cCats=0;

            categoryLinks = categoryLinks + '<TextBlock Width="120" x:Name="cat'+cCats+'" Height="26" Canvas.Top="'+ linkYPos +'" Text="All Videos" TextWrapping="Wrap" Cursor="Hand" Canvas.Left="460" Foreground="#FF9A9797" FontSize="12"/>';                                       
            linkYPos = linkYPos + 20;
            cCats++;
            
            for(var i=0; i<cVideos; i++){
                 
                var curCat = xmlObj.childNodes[i].attributes[4].nodeValue;                                   
                if (curCat != activeCat) 
                {
                    activeCat = curCat;
                    categoryLinks = categoryLinks + '<TextBlock Width="120" x:Name="cat'+cCats+'" Height="26" Canvas.Top="'+ linkYPos +'" Text="'+ activeCat + '" TextWrapping="Wrap" Cursor="Hand" Canvas.Left="460" Foreground="#FF9A9797" FontSize="12"/>';                           
                    linkYPos = linkYPos + 20;  
                    cCats++;                  
                }
                                          
                if (curCat == currentCategory || currentCategory == "All Videos") 
                {
                    var currentMovie = xmlObj.childNodes[i].attributes[3].nodeValue;
                    movieCollection = movieCollection + '<Image x:Name="play'+i+'" Opacity="0.74" Width="120" Height="80" Source="'+currentMovie+'" Stretch="Fill" Cursor="Hand" Canvas.Left="'+movieXPos+'" />';               
                    var movieCap = xmlObj.childNodes[i].attributes[0].nodeValue;
                    movieCollection = movieCollection + '<TextBlock x:Name="cap'+i+'" Width="120"  Height="26" Canvas.Top="83" Text="'+ movieCap + '" TextWrapping="Wrap" Canvas.Left="'+movieXPos+'" Foreground="#FF9A9797" FontSize="12"/>';                
                    movieXPos = movieXPos + movieWidth;   
                }               
                         
            }
            
            var xaml = '<Canvas ' +
                    'xmlns="http://schemas.microsoft.com/client/2007" ' +
                    'xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ' +
                    'x:Name="playlist1" ' +
                    'Canvas.Top="70" ' +
                    'Canvas.Left="78" Width="257.275" Height="152.96">' + 
                    categoryLinks +
                    '<Rectangle Width="525" Height="140" Fill="#181414" Stroke="#FF000000" Canvas.Left="-5" Canvas.Top="450" Opacity="1"/>' +
                    '<Canvas Width="157.275" Height="152.96" Canvas.Left="0" Canvas.Top="460" x:Name="Library">'+movieCollection+
                        	    
                    
                 
                 '</Canvas>'+
                 
                 '<Rectangle Width="75" Height="140" Fill="#181414" Stroke="#FF000000" Canvas.Left="-80" Canvas.Top="450" Opacity="1"/>' +
                 '<Rectangle Width="75" Height="140" Fill="#181414" Stroke="#FF000000" Canvas.Left="520" Canvas.Top="450" Opacity="1"/>' +
                 
                 '<Path x:Name="LeftArrow" Opacity="0.25" Width="38" Height="38" Stretch="Fill" Stroke="#FF000000" Canvas.Left="-40" Canvas.Top="482" Data="M37.5,0.5 L37.5,37.5 0.5,37.5 z" Fill="#FFFFFFFF" RenderTransformOrigin="0.5,0.5" Cursor="Hand">'+
		'<Path.RenderTransform>'+
			'<TransformGroup>'+
				'<ScaleTransform ScaleX=".6" ScaleY=".6"/>'+
				'<SkewTransform AngleX="0" AngleY="0"/>'+
				'<RotateTransform Angle="134.119"/>'+
				'<TranslateTransform X="0" Y="0"/>'+
			'</TransformGroup>'+
		'</Path.RenderTransform>'+
	'</Path>'+
	'<Path x:Name="RightArrow" Opacity="0.25" Width="38" Height="38" Stretch="Fill" Stroke="#FF000000" Canvas.Left="510" Canvas.Top="482" Data="M37.5,0.5 L37.5,37.5 0.5,37.5 z" Fill="#FFFFFFFF" RenderTransformOrigin="0.5,0.5" Cursor="Hand">'+
		'<Path.RenderTransform>'+
			'<TransformGroup>'+
				'<ScaleTransform ScaleX=".6" ScaleY=".6"/>'+
				'<SkewTransform AngleX="0" AngleY="0"/>'+
				'<RotateTransform Angle="314.365"/>'+
				'<TranslateTransform X="0" Y="0"/>'+
			'</TransformGroup>'+
		'</Path.RenderTransform>'+
	'</Path>'+
	
                 '</Canvas>'; 
                 
            // This sets the thumb movement width
            pluginID4.To = -((139+(cVideos*1.7)) * (cVideos));
            pluginID4.From = movieTotalWidth;
            //alert(movieTotalWidth);
            pluginID5.From = -((139+(cVideos*1.7)) * (cVideos));
            pluginID5.To = movieTotalWidth;
            
            
            return xaml; 
            movieWidthTemp = movieWidthTemp+movieWidthTemp;
        }
        
        
        function moreSnow() {
                 //alert('ss');
            } 
            
                                   
		  
		for (var i = 0; i < cVideos; i++)
		{
			//alert(xmlObj.childNodes[i].attributes[3].nodeValue);
			var element = plugIn.Content.findName('play' + i);
			element.Source = xmlObj.childNodes[i].attributes[3].nodeValue;
			element.addEventListener("MouseEnter", Function.createDelegate(this,this._rollOver));
			element.addEventListener("MouseLeave", Function.createDelegate(this,this._rollOut));
			element.addEventListener("MouseLeftButtonUp", Function.createDelegate(this,this._playX));
		}
		

		for (var i = 0; i < cCats; i++)
		{		    
			//alert(xmlObj.childNodes[i].attributes[3].nodeValue);
			var element = plugIn.Content.findName('cat' + i);
			//element.Source = xmlObj.childNodes[i].attributes[4].nodeValue;
			//element.addEventListener("MouseEnter", Function.createDelegate(this,this._rollOver));
			//element.addEventListener("MouseLeave", Function.createDelegate(this,this._rollOut));
			element.addEventListener("MouseLeftButtonUp", Function.createDelegate(this,this._setCatX));
		}
		
		plugIn.Content.findName('LeftArrow').addEventListener("MouseEnter", Function.createDelegate(this,this._slideLeft));
		plugIn.Content.findName('LeftArrow').addEventListener("MouseLeave", Function.createDelegate(this,this._slideStop));
		//plugIn.Content.findName('LeftArrow').addEventListener("MouseLeftButtonUp", Function.createDelegate(this,this._slideLeft));
		plugIn.Content.findName('RightArrow').addEventListener("MouseEnter", Function.createDelegate(this,this._slideRight));
		plugIn.Content.findName('RightArrow').addEventListener("MouseLeave", Function.createDelegate(this,this._slideStop));
		//plugIn.Content.findName('RightArrow').addEventListener("MouseLeftButtonUp", Function.createDelegate(this,this._slideRight));

        
        
            
        //this._playNextVideo();    
    },

	_rollOver: function(sender, eventArgs, thumbNumb) {
		sender.opacity=1;
		var rollOverIMage = sender.Name;
		//var rolloverNum = rollOverIMage.charAt(4);
		var rolloverNum = rollOverIMage.substring(4,rollOverIMage.length);
		var rolloverLetter1Num = rollOverIMage.charAt(0);
		var rolloverLetter2Num = rollOverIMage.charAt(0);
		var rolloverLetter3Num = rollOverIMage.charAt(0);
		if((rolloverLetter1Num == "p") && (rolloverLetter2Num == "p") && (rolloverLetter3Num == "p")){
		    pluginID2.Text= xmlObj.childNodes[rolloverNum].attributes[2].nodeValue;
		    sender.Width = 135;
		    sender.Height = 85;
		}
   },
   
    _rollOut: function(sender, eventArgs) {
		sender.opacity=0.74;
		var rollOverIMage = sender.Name;
		
		var rolloverLetter1Num = rollOverIMage.charAt(0);
		var rolloverLetter2Num = rollOverIMage.charAt(0);
		var rolloverLetter3Num = rollOverIMage.charAt(0);
		if((rolloverLetter1Num == "p") && (rolloverLetter2Num == "p") && (rolloverLetter3Num == "p")){
		    pluginID2.Text = "";
		    sender.Width = 120;
		    sender.Height = 80;
		}
   },
   
   _playX: function(sender, eventArgs) {
		var X = Number(sender.Name.substring(4));
		this._currentMediainfo = X;
		this._player.set_mediainfo( get_mediainfo( X));
		sender.opacity=1;
   },
   
   _setCatX: function(sender, eventArgs) {
		currentCategory = sender.Text;
	
        var root = playerPlugin.content.root; 
                                
		var element = root.findName('playlist1');
        if (element != null) 
        {
            root.Children.Remove(element);
        }
                               
         var xaml = createThumbsXamlCode();
         var child = playerPlugin.content.createFromXaml(xaml);
         root.children.add(child);	
         
         var firstvideo = ""
        for (var i = 0; i < cVideos; i++)
		{
			var element = playerPlugin.Content.findName('play' + i);
			if (element != null) {
			    if (firstvideo == "") firstvideo = element.Name;
			    element.Source = xmlObj.childNodes[i].attributes[3].nodeValue;
			    element.addEventListener("MouseEnter", Function.createDelegate(player,player._rollOver));
			    element.addEventListener("MouseLeave", Function.createDelegate(player,player._rollOut));
			    element.addEventListener("MouseLeftButtonUp", Function.createDelegate(player,player._playX));
			}
		}
		
		for (var i = 0; i < cCats; i++)
		{		    
			var element = playerPlugin.Content.findName('cat' + i);
			//element.Source = xmlObj.childNodes[i].attributes[4].nodeValue;
			//element.addEventListener("MouseEnter", Function.createDelegate(this,this._rollOver));
			//element.addEventListener("MouseLeave", Function.createDelegate(this,this._rollOut));
			element.addEventListener("MouseLeftButtonUp", Function.createDelegate(player,player._setCatX));
		}
		
		playerPlugin.Content.findName('LeftArrow').addEventListener("MouseEnter", Function.createDelegate(player,player._slideLeft));
		playerPlugin.Content.findName('LeftArrow').addEventListener("MouseLeave", Function.createDelegate(player,player._slideStop));
		playerPlugin.Content.findName('RightArrow').addEventListener("MouseEnter", Function.createDelegate(player,player._slideRight));
		playerPlugin.Content.findName('RightArrow').addEventListener("MouseLeave", Function.createDelegate(player,player._slideStop));
			
			
		var X = Number(firstvideo.substring(4));
		player._currentMediainfo = X;
		player._player.set_mediainfo( get_mediainfo( X));
		
	    //player._playNextVideo();
   },   
   
   _slideLeft: function(sender, eventArgs) {
        if(cVideos > 4){
		       if(curPos == 1 || curPos == 2  || curPos == 0){
               
                var Local_movieTotalWidth = movieWidthTemp * cVideos;
                pluginID4.From =thumbAllXPos;
                pluginID4.To = 0;
                sender.findName("MoveLeft01").Begin();  
                sender.findName("LeftArrow").opacity = 0.75; 
            }
            
            else{
                switch(curPos)
                {
                    case 1:
                        //sender.findName("LeftArrow").opacity = 0.85;
                        break;
                    default:
                    
                }
            }
        }
        
        pluginID8 = sender.findName("LeftArrow");
        sentFrom = "arrowL"
    }, 
   
   
   
   _slideStop: function(sender, eventArgs) {
        pluginID6.Pause();
        thumbAllXPos = pluginID7["Canvas.Left"];
        if(sentFrom == "arrowL"){
            pluginID8.opacity = 0.25;
        }
        if(sentFrom == "arrowR"){
            pluginID9.opacity = 0.25;
        }
        
   },  
   
   _slideRight: function(sender, eventArgs) {
		if(cVideos > 4){
		    thumbAllXPos = pluginID7["Canvas.Left"];
    		
		    if(curPos == 1 || curPos == 2  || curPos == 0){
                var Local_movieTotalWidth = movieWidthTemp * cVideos;
                pluginID4.From = thumbAllXPos;
               //alert(thumbAllXPos);
                pluginID4.To = -(Local_movieTotalWidth-450);
                //alert(Local_movieTotalWidth);
                sender.findName("MoveLeft01").Begin();
                clearInterval("timeoutID");
                sender.findName("RightArrow").opacity = 0.75;
                   
            }
            else{
		        switch(curPos)
                {               
                    case maxPos:
                        //sender.findName("RightArrow").opacity = 0.85;  
				        break;				
                    default:
                }
            }
        }
        pluginID9 = sender.findName("RightArrow");
        sentFrom = "arrowR"
   },
    
    _onMediaEnded: function(sender, eventArgs) {
        //window.setTimeout( Function.createDelegate(this, this._playNextVideo), 1000);
    },
    _onMediaFailed: function(sender, eventArgs) {
        //alert(String.format( Ee.UI.Xaml.Media.Res.mediaFailed, this._player.get_mediaUrl() ) );
    },
    _playNextVideo: function() {
        if (this._currentMediainfo<cVideos)
            this._player.set_mediainfo( get_mediainfo( this._currentMediainfo++ ));    
    }       
}

function rundStop(){
           alert('ran');
            thumbAllXPos = pluginID7["Canvas.Left"];
            //alert(thumbAllXPos  );

            // This sets the thumb movement width
            pluginID4.To = movieTotalWidth;
            pluginID4.From = thumbAllXPos;
            //alert(movieTotalWidth);
            pluginID5.From = thumbAllXPos;
            pluginID5.To = 0;
        }
