// vim: fileencoding=utf-8 tabstop=2 expandtab shiftwidth=2 softtabstop=2
var HHRumors = {
  UrlBase: '/rumors/',
  GroupKey: null,
  GetGroupKey: function() {
    if ( HHRumors.GroupKey!=null ) {
      return HHRumors.GroupKey;
    }
    var rkey = $(':input[name=groupk]').val();
    if ( rkey!=null && rkey.length>0 ) {
      HHRumors.GroupKey = rkey;
    }
    return HHRumors.GroupKey;
  },
  OpenLink: function(sender,rkey) {
    if ( sender==null || rkey==null || rkey.length==0 ) return false;
    var href = $(sender).attr('href');
    if ( href==null || href.length==0 ) return false;
    window.open(href,rkey.replace(/[\-]/, '', 'g'));
    $.post(
      HHRumors.UrlBase + 'rumoropen',
      {r:rkey}
    );
    return false;
  },
  PageSetup: function(gkey) {
    if ( gkey!=null && gkey.length>0 && window.location!=null ) {
      var t = window.location.hash;
      if ( t!=null && t.length>6 && t[0]=='#' && t[1]=='g' && t.indexOf('-')>=3 && t.substring(1,t.indexOf('-'))!=gkey ) {
         $.get(
          HHRumors.UrlBase + 'gsearch',
          {h:t.substring(1)},
          function ( data, textStatus, XMLHttpRequest ) {
            if ( data['gurl']!=null && data['gurl'].indexOf('/archive/rumors/')==0 ) {
              window.open(data['gurl'], '_self');
            }
          },
          'json'
        );
      }
    }
    HHRumors.GroupKey = gkey;
    $('img[class=social]').bind(
        'mouseover',
        function (evt) {
          var src = $(this).attr('src');
          if ( src.indexOf('tag.png')!=-1 ) return;
          var suffix = 'BN';
          var pos = src.lastIndexOf('.');
          if ( src.substring(pos-suffix.length, pos)==suffix ) {
            $(this).attr('src', src.substring(0,pos-suffix.length)+src.substring(pos));
          }
        }
    ).bind(
        'mouseout',
        function (evt) {
          var src = $(this).attr('src');
          if ( src.indexOf('tag.png')!=-1 ) return;
          var suffix = 'BN';
          var pos = src.lastIndexOf('.');
          if ( src.substring(pos-suffix.length, pos)!=suffix ) {
            $(this).attr('src', src.substring(0,pos)+suffix+src.substring(pos));
          }
        }
    );
    $('td.rumors > div.rumor > div.tag').each(function (pos, item) {
      var qitem = $(this);
      var rkey = qitem.data('rkey');
      if ( rkey==null || rkey.length==0 ) return;
      item.appendChild(document.createTextNode(' | \xA0'));
      var share = document.createElement('a');
      share.className = 'share';
      share.setAttribute('href', '#' + rkey);
      share.appendChild(document.createTextNode('share'));
      item.appendChild(share);
      item.appendChild(document.createTextNode(' \xA0'));
      HHRumors.ShareRumorSetup(item, rkey, 'facebook', 'Share on Facebook', 'http://cdn.hoopshype.com/img/fb_32x32BN.png', 'http://cdn.hoopshype.com/img/fb_32x32.png');
      HHRumors.ShareRumorSetup(item, rkey, 'twitter', 'Share on Twitter', 'http://cdn.hoopshype.com/img/tw_32x32BN.png', 'http://cdn.hoopshype.com/img/tw_32x32.png');
      HHRumors.ShareRumorSetup(item, rkey, 'forums', 'Comment on HoopsHype forums', 'http://cdn.hoopshype.com/img/hh_32x32BN.png', 'http://cdn.hoopshype.com/img/hh_32x32.png');
    });
    $('div[class=rumor]').bind(
        'mouseover',
        function (evt) {
          $(this).addClass('rumoractive');
        }
    ).bind(
        'mouseout',
        function (evt) {
          $(this).removeClass('rumoractive');
        }
    ).bind(
      'copy',
      function (evt) {
        console.log(evt);
        $(this).unbind('copy');
        $(this).trigger('copy');
        evt.stopImmediatePropagation();
        return false;
      }
    );
    
    $('img[class=hworld]').bind(
        'mouseover',
        function (evt) {
          var src = $(this).attr('src');
          var suffix = '-over';
          var pos = src.lastIndexOf('.');
          if ( src.substring(pos-suffix.length, pos)!=suffix ) {
            $(this).attr('src', src.substring(0,pos)+suffix+src.substring(pos));
          }

        }
    ).bind(
        'mouseout',
        function (evt) {
          var src = $(this).attr('src');
          var suffix = '-over';
          var pos = src.lastIndexOf('.');
          if ( src.substring(pos-suffix.length, pos)==suffix ) {
            $(this).attr('src', src.substring(0,pos-suffix.length)+src.substring(pos));
          }
        }
    );
  },
  ShareRumor: function(sender, rkey, target) {
    if ( target==null || target.length==0 ) return true;
    var url = '';
    if ( target=='forums' ) {
      url = 'http://forums.hoopshype.com/forums/';
    } else if ( target=='facebook' ) {
      url = 'http://facebook.com/sharer.php?u=' + encodeURIComponent('http://hoopshype.com/rumors.htm#' + rkey);
    } else if ( target=='twitter' ) {
      $('#' + rkey + ' >p.rumortext > a.quote:first').each(function(i,q) {
        var quote = $(q).text();
        quote = quote.substring(0,1).toUpperCase() + quote.substring(1);
        url = 'http://twitter.com/home?status=' + encodeURIComponent('HoopsHype Rumor: ' + quote + ' ... http://hoopshype.com/rumors.htm#' + rkey);
      });
    }
    if ( url!=null && url.length>0 ) {
      window.open(url, rkey + '-' + target);
    }
    return false;
  },
  ShareRumorSetup: function(target, rkey, service, title, img, img2) {
    var share = document.createElement('a');
    share.setAttribute('title', title);
    share.setAttribute('rel', 'nofollow');
    share.setAttribute('href', 'javascript:void(true);');
    $(share).bind('click', {rkey:rkey, target:service}, function(evt){return HHRumors.ShareRumor(this, evt.data['rkey'], evt.data['target']);});
    var simg = document.createElement('img');
    simg.setAttribute('height', 32);
    simg.setAttribute('width', 32);
    simg.setAttribute('alt', title);
    simg.setAttribute('src', img);
    simg.className = 'social';
    $(simg).bind('mouseover', {img:img2}, function (evt) {this.setAttribute('src', evt.data['img']);}).bind('mouseout', {img:img}, function (evt) {this.setAttribute('src', evt.data['img']);});
    share.appendChild(simg);
    target.appendChild(share);
  }
};

