(function($){

	// Declare namespace if not already defined
	if(!$.TNP){
		$.TNP = new Object();
	}
	
    $.TNP.VideoGroup = function(vid_el, autoplay){
        
        base = this;
		
		base.autoplay = autoplay;
		
    	// Access to jQuery and DOM versions of vid_element
	   	base.$vid_el = $(vid_el);
        base.vid_el = vid_el;
	
        // Add a reverse reference to the DOM object
        base.$vid_el.data("TNP.VideoGroup", base);

        //base.$vidgroup = base.$vid_el.closest(".video-group");
        base.$thumblinks = base.$vid_el.find("li a");
        base.current_video = 0;
        base.video_count = base.$thumblinks.size();
        base.$caption = base.$vid_el.find(".vid_caption"); 
        base.player_obj = null;
		

        base.stateListener = function(obj)  {
            if(obj.newstate == "COMPLETED") {
                // this should be the correct object instance
                this.playNextVideo(this);
            }	else	{
				playclicked(obj);
			}
        };
        
        base.playOnClick = function(e)  {
            // baseObj is the correct object instance
            baseObj = e.data.baseObj;
            obj = e.currentTarget;
            if(baseObj.player_obj==null)  {
                alert("Please wait until the video player has fully loaded before clicking a thumbnail image.");
            }   else    {
                baseObj.playVideo(baseObj,obj);
            }
            return false;
        };
        
        base.playNextVideo = function(baseObj)    {
            if(baseObj.current_video < baseObj.video_count)   {
                baseObj.playVideo(baseObj, baseObj.$thumblinks.get(baseObj.current_video + 1));
            }
        };
        
        base.playVideo = function(baseObj,obj)  {
            $obj = $(obj);
            this_n = $obj.data('n');
            if(this_n != baseObj.current_video)    {
				current_image = baseObj.player_obj.getConfig()['image'];
				baseObj.$thumblinks.eq(baseObj.current_video).removeClass('now-playing');
				baseObj.$thumblinks.eq(this_n).addClass('now-playing');
				baseObj.current_video = this_n;
				filename = $obj.attr('href').toString();

				baseObj.player_obj.sendEvent("LOAD",{file: filename, image: filename.replace(/stream.mp4/,'stack.jpg')});
				baseObj.player_obj.sendEvent("PLAY",'true');
				baseObj.postPlay(baseObj,this_n)
            }
		}
		
		// This functions performs tasks to be done after a video plays. 
		// We will override it on the Untold Stories page
		
		base.postPlay = function(baseObj,this_n)	{
			baseObj.$caption.text(baseObj.$thumblinks.eq(this_n).find("img").attr("alt"));
			// set the add-to-scrapbook ID in the link
			baseObj.$vid_el.find('a.scrapbook-add').attr('href','#' + baseObj.$thumblinks.eq(this_n).attr('id').replace(/video_/,''));
			// save the thumb and caption as $.data on the trigger so we don't have to parse them out later
			baseObj.$vid_el.find('a.scrapbook-add').data('thumb', baseObj.$thumblinks.eq(this_n).find("img").attr('src').replace(/_player/,'-thumb.jpg'));
			baseObj.$vid_el.find('a.scrapbook-add').data('caption', baseObj.$thumblinks.eq(this_n).find("img").attr("alt") );
			
			// Set the video-pop icon link
			
			baseObj.$vid_el.find('a.video-pop').attr('href','/nationalparks/media_detail/' + baseObj.$thumblinks.eq(this_n).attr('id').replace(/video_/,''));
              
       };
       
       // Run this when the player is ready...
       base.setPlayer = function(baseObj, obj)   {
            baseObj.player_obj = $("#" + obj)[0];
             // Add listener to video player
            // Be sure to call the one associated with this video.
			if (base.autoplay)	{
		        baseObj.player_obj.addModelListener("STATE", "vg_objs['" + obj + "'].stateListener");
			}
       }
        
	   //  initialization
       base.init = function(){
      
            
            base.$thumblinks.each(function(n) {
	            $(this).data('n',n);
            });
           // Add click events to thumbnails
            
            base.$thumblinks.bind('click',{baseObj: base},base.playOnClick);
	    };
		
        base.init();
	}

    $.fn.tnp_VideoGroup = function(){
        return this.each(function(){
            (new $.TNP.VideoGroup(this));
        });
    }		

})(jQuery);

var vg_objs = {};
/*
function playerReady(obj) {
    myId = obj['id'];
//    alert(myId);
    // Send the correct object instance along with the call
	// But don't call anything for video video players not in groups
	if(vg_objs[myId]) {
		vg_objs[myId].setPlayer(vg_objs[myId], myId);
	}

};
*/
