/*
 * Youtube Chromeless Video Plugin
 * http://www.viget.com/
 *
 * Copyright (c) 2010 Trevor Davis
 * Dual licensed under the MIT and GPL licenses.
 * Uses the same license as jQuery, see:
 * http://jquery.org/license
 *
 * @version 0.2
 */
 
(function($) {
  $.fn.ytchromeless = function(options){
    
			$videodiv = $( '#top_tv_screen' );
			$videodiv.fadeOut('fast');
			$('.loadingvideo').show();
			$videodiv.html('');
    //Initial configuration
    var config = {
      videoWidth  : '298',
      videoHeight : '180',
      videoIdBase : 'ytplayer',
      quality 	  : '1',
      params : { 
		    allowScriptAccess: 'always',
		    wmode: 'transparent',
		  }
    };
        
    return this.each(function(i) {
      
      
      // initial var setup
      
        var options    = $.extend(config, options),
      
            // set jQuery objects
            $link      = $(this),
            // set variables          
            url        = $link.attr('id'),
            videoId    = $link.attr('rel') || options.videoIdBase + i,
            ytVideoId  = url.substr(31),
            videotitle      = $link.attr("title"),

            // new DOM elements
            $video     = $videodiv.html( '<div class="video-player'+ videoId +'"></div>' ).find('div'),
            $controls  = $('<div class="video-controls"></div>' ).appendTo( $video ),
            $toReplace = $('<div class="video"></div>').prependTo( $video ).attr('id', videoId),
            $titleVideo= $('<div id="title">'+videotitle+'</div>').appendTo( $video ),
            $bar,
            $indicator,
            $loaded,
            $mute,
            $play,
            $seek,

            // set up the special player object
            player;
           
        // bind public methods upfront 
        $video.bind({

          // playing, pausing, muting, 
          'togglePlay' : function(){ $video.togglePlay(); },
          'play'       : function(){ $video.play(); },
          'pause'      : function(){ $video.pause(); },
          'toggleMute' : function(){ $video.toggleMute(); },
          'mute'       : function(){ $video.mute(); },
          'unMute'     : function(){ $video.unMute(); },
          'seek'       : function(){ $video.seek(); },
          'quality'	   : function(){ $video.quality();},

          // initializing and revising the player
          'update'     : function(){ $video.update(); },
          'cue'        : function(){ player.cueVideoById( ytVideoId ); }

        });


      // control methods
        // set quality of Video
		$video.quality = function(){
			if($quality.attr('id') == '1'){
				player.setPlaybackQuality('medium');
				$quality.attr('id','2').text('View HD');
			} 
			else {
				player.setPlaybackQuality('hd720');
				$quality.attr('id', '1').text('Normal');
			}
		}
        // function fired when the play/pause button is hit
        $video.togglePlay = function() {
          if( $play.hasClass('playing') ) {
            $video.trigger('pause');
          } else {
            $video.trigger('play');
          }
          return false;
        };
      
        // play the video
        $video.play = function() {
          player.playVideo();
          $play.removeClass('paused').addClass('playing').attr('title','Pause');        
        };  
      
        // pause
        $video.pause = function() {
          player.pauseVideo();
          $play.removeClass('playing').addClass('paused').attr('title','Play');
        };
        
        // function fired when the mute/unmute button is hit
        $video.toggleMute = function() {
          if( $mute.hasClass('muted') ) {
            $video.trigger('unMute');
          } else {
            $video.trigger('mute');
          }
          return false;
        };
      
        // mute the video
        $video.mute = function() {
          player.mute();
          $mute.addClass('muted').attr('title','Un-Mute');        
        };   
      
        // unmute
        $video.unMute = function() {
          player.unMute();
          $mute.removeClass('muted').attr('title','Mute');
        };
        
        //Seek to a position in the video
    		$video.seek = function(seekPosition) {
          var seekToPosition = Math.round(player.getDuration() * seekPosition);
          player.seekTo(seekToPosition, false);
        };
        
        
        
      // player init and update methods
      
        //Update the video status
    		$video.update = function() {
    		  
    		  if( player && player.getDuration ) {

            if( player.getPlayerState() === 1 ) {
              $video.play();
            } else if ( player.getPlayerState() === 0 ) {
              $video.pause();
            }

            if( player.getVideoBytesLoaded() > -1) {
              var loadedAmount = ( player.getVideoBytesLoaded() / player.getVideoBytesTotal())  * 100;
              $loaded.css( 'width', loadedAmount + '%' );
            }
            
            if( player.getCurrentTime() > 0 ) {
              var videoPosition = ( player.getCurrentTime() / player.getDuration() ) * 100;
              $indicator.css( 'left', videoPosition + '%' );
            }

    		  }

    		};
    		
  		
  			// the youtube movie calls this method when it loads
  			// DO NOT CHANGE THIS METHOD'S NAME
    		onYouTubePlayerReady = function( videoId ) {

    		  var $videoRef = $( document.getElementById( videoId ) ).parent();

    		  setInterval(function(){
    		    $videoRef.trigger('update');
    		  }, 250);
    		  
          $videoRef.trigger('cue');
          $videoRef.trigger('togglePlay');
        };
    	
  		
  		
      // init methods
      
        // the embed!
    		$video.init = function() {
  		  
    		  swfobject.embedSWF(
            'http://www.youtube.com/apiplayer?&enablejsapi=1&playerapiid=' + videoId,
            videoId, 
            options.videoWidth, 
            options.videoHeight, 
            '8', 
            null, 
            null, 
            options.params, 
            { id: videoId },
            function(){
              player = document.getElementById( videoId );
            }
          );
          
          $video.addControls();
    		};

        // add controls
    		$video.addControls = function() {

    		  //Play and pause button
    		  $play = $('<a/>', {
    		            href: '#',
            		    'class': 'play-pause',
            		    text: 'Play/Pause',
            		    title: 'Play',
            		    click: function() {
            		      $video.trigger('togglePlay');
            		      return false;
            		    }
            		  }).appendTo( $controls );
  		    		    	
    		  //Play and pause button
    		  $mute = $('<a/>', {
    		            href: '#',
            		    'class': 'volume',
            		    text: 'Volume',
            		    title: 'Mute',
            		    click: function() {
            		      $video.trigger('toggleMute');
            		      return false;
            		    }
            		  }).appendTo( $controls );
            
  		    //Play and pause button
          $seek = $('<div/>', {
            		    'class': 'status',
            		    click: function(e) {
                      var skipTo      = e.pageX - $seek.offset().left,
                          statusWidth = $seek.width();
                      $video.seek( skipTo / statusWidth );
            		    }
            		  }).appendTo( $controls );

          $bar       = $('<div class="bar"></div>').appendTo($seek);
          $loaded    = $('<div class="loaded"></div>').appendTo($bar);
          $indicator = $('<span class="indicator"></span>').appendTo($bar);
        };	
        $video.init();
			$videodiv.fadeIn('slow');
			$('.loadingvideo').hide();
    });
    return false;
  };
  })(jQuery);
