var o = jQuery.fn.video = function(settings) 
{
  /*
    Functionality for creating the video player in flash and updating the player.
  */
  settings = jQuery.extend(
  {
    src: "video.swf",
    width: "507",
    height: "314",
    version: "8.0.0",
    tracking: "video-tracking.php"
  }
  , settings);
  return this.each(
    function()
    {
      var el = this;
      el.videoCfg = {
        video: jQuery(".video", this),
        videoControl: jQuery(".footer", this),
        videoHeader: jQuery(".header", this),
        videoAuthor: jQuery("h3 a", this),
        videoDescription: jQuery("p.description", this),
        videoLink: jQuery(".info p.link", this)
        
      };
      /*
        update function gets called o initial load to create the player from the data in the html page.
        Also can be called from the items in the video listing.
        1. Update the information in the grey area of the player:
            title
            description
            video links
            trasncript links
            etc etc
        2. Make sure Flash in IE is registered for ExternalInterface for Flash
        3. Update the Flash player by calling playVideo (see video.as)
        4. Track the view in the backend
      */
      el.update = function(item)
      {
        var li = item.get(0);
        
        if(li.author != "") {
          el.videoCfg.videoAuthor.text(li.author);
        }
        
                el.videoCfg.videoAuthor.attr("href", li.profileHref);
        jQuery("h4", el.videoCfg.videoHeader).text(li.videoTitle);
        jQuery("h5", el.videoCfg.videoHeader).text(li.date);
        el.videoCfg.videoDescription.text(li.description);
        
        if(el.videoCfg.videoLink.length == 0) {
          jQuery("div.nav", el).append('<p class="link"></p>');
          el.videoCfg.videoLink = jQuery(".info p.link", this);
        }
        
        el.videoCfg.videoLink.empty();
                if(li.castLinks)
                {
            li.castLinks.each(
              function()
              {
                var j = jQuery(this);
                var str = '<a href="' + j.attr("href") + '"';
                var target = "";
                if(j.attr("rel") != "") {
                  str += ' rel="' + j.attr("rel") + '"';
                  target = "_blank";
                }
                str += '">' + j.text() + '</a>';
            
                var link = jQuery("a", el.videoCfg.videoLink.append(str));
                if(target != "") {
                  link.get(0).target = target;
                }
            
            
              }
            );
                }
        
        //el.videoCfg.videoLinks.eq(0).text("Visit " + possessive + " profile");
        //el.videoCfg.videoLinks.eq(0).attr("href", li.profileHref);
        //el.videoCfg.videoLinks.eq(1).text("Visit " + possessive + " external page");
        
        
        jQuery("a[rel=bookmark]", this).attr("href", li.bookmark);
        jQuery("a[rel=alternate]", this).attr("href", li.transcript);
        jQuery("a[rel=alternate-transcript]", this).attr("href", li.alternateTranscript);
          
        jQuery(".highquality a", this).attr("href", li.highquality);
        jQuery(".lowquality a", this).attr("href", li.lowquality);
        
        // 
        
        var comment = jQuery("a[rel=comment]", el.videoCfg.videoControl);
        if(li.comment != null) {
          
          if(comment.length > 0) {
            comment.attr("href", li.comment);
          } else {
            el.videoCfg.videoControl.prepend('<a href="' + li.comment + '" rel="comment" class="comment">Comment</a>');
          }
        } else {
          if(comment.length > 0) {
            comment.remove();
          }
        }
        
        
        
        // Make sure Flash in IE is registered for ExternalInterface for Flash
        if(jQuery.browser.msie && jQuery.browser.version < 8) {
          __flash__addCallback(document.getElementById("videoplayer"), "playVideo");
        }
        
        document.getElementById("videoplayer").playVideo(li.highquality, li.lowquality, li.transcript, li.bookmark);
        
        var href = settings.tracking;
        jQuery.post(
          href,
          {
            action: "track_video",
            video_id: li.bookmark
          },
          function(data)
          {
          
          }
        );
        
      };
      // markup which is added only when you have javascript to enable the overlays in the js
      el.videoCfg.controlMarkup = '<div class="nav"><ul><li><a href="#" rel="embed">Embed</a></li><li><a href="#" rel="share">Share</a></li><li class="last"><a href="#" rel="download">Download</a></li></ul></div>';
      
      
      // else if ((navigator.userAgent.match(/AppleWebKit/i) && navigator.userAgent.match(/Mobile/i)) {
      //var something = 1;
      //}
      
      
      /*
        Embed the Flash player using swf object using the initial data from the html page (highquality url, lowquality url, transcript url, bookmark url etc etc)
      */
      el.videoCfg.video
        .each(
          function()
          {
            
            var highquality = jQuery("li[class='highquality'] a", this).attr('href');
            var lowquality = jQuery("li[class='lowquality'] a", this).attr('href');
            var transcript = jQuery("a[rel='alternate']", this).attr('href');
            var bookmark = jQuery("a[rel='bookmark']", this).attr('href');
            var base = jQuery("base").attr("href");
            if(base != null) {
              if(highquality.indexOf("http://") != 0) {
                highquality = base + highquality;
              }
              if(lowquality.indexOf("http://") != 0) {
                lowquality = base + lowquality;
              }
              if(transcript.indexOf("http://") != 0) {
                transcript = base + transcript;
              }
              /*if(bookmark.indexOf("http://") != 0) {
                bookmark = base + bookmark;
              }*/
            }
            
            var flashvars = {
              "highquality": highquality,
              "lowquality": lowquality,
              "transcript": transcript,
              "bookmark": bookmark
            };
            var params = {
              "allowFullScreen": "true",
              "allowScriptAccess": "always"
            };
            swfobject.embedSWF(settings.src, el.videoCfg.video.attr("id"), settings.width, settings.height, settings.version, null, flashvars, params, {style: "margin-bottom: -5px;"});
          }
        );
      /*
        Add the overlay buttons into the video player footer using js so only js people get them.
        Add the functionality to the new buttons to call the Flash methods embed, share and download (see video.as), and make sure these methods are registered in IE  
      
      */
      el.videoCfg.videoControl
        .each(
          function()
          {
            var footer = jQuery(this);
            footer.append(el.videoCfg.controlMarkup);
            jQuery("a", footer)
              .bind(
                'click',
                function(event)
                {
                  if(event.target.rel == "comment") {
                    
                    return true;
                  }
                  if(jQuery.browser.msie && jQuery.browser.version < 8) {
                    __flash__addCallback(document.getElementById("videoplayer"), "embed");
                    __flash__addCallback(document.getElementById("videoplayer"), "share");
                    __flash__addCallback(document.getElementById("videoplayer"), "download");
                  }
                  event.preventDefault();
                  document.getElementById("videoplayer")[event.target.rel]();
                }
              );
            
          }
        );
      
    }
  );
};