(function($){
  $.own3d = {
    liveurl : 'live.php',
    channelurl : 'channel.php'
  };

  $.fn.own3d = function(options) {
    return this.each(function(){
      var self = $(this);
      self.data('liveid', options.liveid);

      if (options && options.liveid) {
        $.ajax({
            type: "GET",
            async: true,
            data: {live_id:options.liveid},
            url: $.own3d.liveurl,
            dataType: "jsonp",
            success: function(data){
              var title = (options.title) ? options.title : 'OWN3D CHANNEL ['+options.liveid+']';
              var liveclass = (options.hd) ? 'own3d-live-hd' : 'own3d-live';

              self.empty();
              self.addClass('own3d');

              var link = $('<a/>')
                  .attr('href','http://www.own3d.tv/live/'+options.liveid)
                  .attr('target','_blank');


              if (data.isLive && data.isLive == 'true') {
                link.html('<tr class="livestream_d0"><td><span style="color: #000"><strong>LIVE:</strong> '+title+' [ </span><span style="color: #ff7c00"><strong>'+data.liveViewers+'</strong></span><span style="color: #000"> ]</span></td></tr>');

                if ((options.showthumbnail || options.embed) && options.channelid) {
                  $.ajax({
                      type: "GET",
                      async: true,
                      data: {channel_id : options.channelid, stream_guid : 'http://www.own3d.tv/live/'+options.liveid},
                      url: $.own3d.channelurl,
                      dataType: "jsonp",
                      success: function(data){
                        if (options.showthumbnail && data.thumbnail) {
                          var table = $('<table/>');

                          var status = $('<td/>').append($('<div/>').addClass(liveclass).append(link));
                          table.append($('<tr/>').append(status));

                          var thumbnail = $('<td/>').append($('<img/>').attr('src',data.thumbnail));
                          table.append($('<tr/>').append(thumbnail));

                          self.append(table);
                        } else if (options.embed && data.title) {

                            var e = '<iframe height="360" width="640" frameborder="0" src="http://www.own3d.tv/liveembed/'+self.data('liveid')+'"></iframe>';
                            // var e = '<iframe height="360" width="640" frameborder="0" src="http://www.own3d.tv/liveembed/'+options.liveid+'?autoPlay=true"></iframe><br /><iframe height="360" width="640" scrolling="no" frameborder="0" src="http://www.own3d.tv/chat/own3D.tv_'+options.liveid+'/"></iframe>';
                            // var e = '<object width="640" height="360"><param name="movie" value="http://www.own3d.tv/livestream/'+options.liveid+'" /><param name="allowscriptaccess" value="always" /><param name="allowfullscreen" value="true" /><param name="wmode" value="transparent" /><embed src="http://www.own3d.tv/livestream/'+options.liveid+'" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="640" height="360" wmode="transparent"></embed></object>';
                            var table = $('<table/>');

                            var status = $('<td/>').append($('<div/>').addClass(liveclass).append(link));
                            table.append($('<tr/>').append(status));

                            var embeddedplayer = $('<td/>').html(e);
                            table.append($('<tr/>').append(embeddedplayer));

                            self.append(table);
                        };
                      },
                      error: function(XMLHttpRequest, textStatus, errorThrown){
                        // console.debug(errorThrown);
                      }
                  });
                } else {
                  self.append(link);
                  self.addClass(liveclass);
                }
              } else {
                link.html('<tr class="livestream_d0"><td><span style="color: #282421">OFFLINE: '+title+'</span></td></tr>');
                self.removeClass(liveclass);
                self.append(link);
              }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                // console.debug(errorThrown);
            }
        });
      } else if (options && options.channelid) {
        var ajaxdata = {
          channel_id : options.channelid
        };
        if (options.showall) { ajaxdata.showall = 1; };

        $.ajax({
            type: "GET",
            async: true,
            data: ajaxdata,
            url: $.own3d.channelurl,
            dataType: "jsonp",
            success: function(data){
              self.empty();

              var embedded = true;
              for(var i=0;i<data.streams.length;i++) {
                var stream = data.streams[i];

                var plugindata = {
                  liveid : stream.liveid,
                  title : stream.title,
                  channelid : options.channelid,
                  // embed the first, thumbnail the rest
                  showthumbnail : (options.showthumbnail) || (!(options.showthumbnail) && options.embed && !embedded) ? true : false,
                  embed : (options.embed) ? true && embedded : false
                };

                // prevent opening multiple embedded players
                embedded = false;

                self.append($('<div/>').own3d(plugindata));
              }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
              // console.debug(errorThrown);
            }
        });
      } else if (options && options.game) {
        var ajaxdata = {
          game : options.game
        };

        $.ajax({
            type: "GET",
            async: true,
            data: ajaxdata,
            url: $.own3d.channelurl,
            dataType: "jsonp",
            success: function(data){
              self.empty();

              var embedded = true;
              for(var i=0;i<data.streams.length;i++) {
                var stream = data.streams[i];

                var plugindata = {
                  liveid : stream.liveid,
                  title : stream.title,
                  channelid : options.channelid,
                  // embed the first, thumbnail the rest
                  showthumbnail : (options.showthumbnail) || (!(options.showthumbnail) && options.embed && !embedded) ? true : false,
                  embed : (options.embed) ? true && embedded : false
                };

                // prevent opening multiple embedded players
                embedded = false;

                self.append($('<div/>').own3d(plugindata));
              }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
              // console.debug(errorThrown);
            }
        });
      }
    });
  }
})(jQuery)
