﻿// JavaScript Document
var udv = 'undefined';
var IFrameObj; // our IFrame object
var bk_providers = new Array('favorities', 'google', 'baidu', 'qq', 'yahoo',
                             'shouker', 'delicious', 'misterwong', 'waakee');
var widget = {
  server: 'http://www.webshuqian.com/bkbtn/relay/',
  img_path: 'http://www.webshuqian.com/media/bkbtn/images/',
  div_id: 'bk_widget',
  provider_list: {'favorities': ['收藏夹', 'favorities.png'], 
                  'google': ['谷歌', 'goog.png'],
                  'baidu': ['百度', 'baidu.gif'],
                  'qq': ['QQ', 'qq.gif'],
                  'yahoo': ['雅虎', 'yahoo.gif'],
                  'shouker': ['收客', 'shouker.gif'],
                  'delicious': ['Del.icio.us', 'delicious.png'],
                  'misterwong': ['Mister-Wong', 'misterwong.png'],
                  'waakee': ['挖客', 'waakee.gif']
                 },
  inited: false,
  init: function () {
    // Add css style sheet
    var link_ele = document.createElement('link');
    link_ele.rel = 'stylesheet';
    link_ele.type = 'text/css';
    link_ele.href = 'http://www.webshuqian.com/media/bkbtn/css/widget.css';
    document.lastChild.firstChild.appendChild(link_ele);
    div_html = '<div id="' + this.div_id + '" style="display: none;">';
    div_html += '<table width="100%" style="border-collapse: collapse;">';
    div_html += '<tr><td style="font-size: 12px; height: 20px; ';
    div_html += 'background-color: #F5F5F5;">收藏 + 分享</td></tr></table>';
    div_html += '<table id="bk_provider_table" border="0">';
    var counter = 1;
    for(var i = 0; i < bk_providers.length; i++) {
      var bk_provider = bk_providers[i];
      if (bk_provider in this.provider_list) {
        if (counter%2) div_html += '<tr>';
        div_html += '<td width="50%" height="16px"><a href="/" onclick="return bk_request(\'';
        div_html += bk_provider + '\')"><img src="' + this.img_path;
        div_html += this.provider_list[bk_provider][1] + '" width="16" height="16" />&nbsp;';
        div_html += this.provider_list[bk_provider][0] + '</a></td>';
        counter++;
        if (counter%2) div_html += '</tr>';
      }
    }
    div_html += '<td width="50%" height="16px">';
    div_html += '<a href="/" onclick="return bk_request(\'webshuqian\');">';
    div_html += '&raquo; 更多选择</a></td></tr>';
    div_html += '</table>';
    div_html += '<table width="100%" style="border-collapse: collapse;">';
    div_html += '<tr><td style="font-size: 12px; height: 20px; text-align: right;">';
    div_html += '来自 <a href="http://www.webshuqian.com/">网络书签</a></td></tr></table>';
    div_ele = document.createElement('div');
    div_ele.innerHTML = div_html;
    document.body.insertBefore(div_ele, document.body.firstChild);
    this.bk_url = location.href;
    this.bk_pagetitle = document.title;
    this.inited = true;
  },
  show: function() {
    var bk_button = getbook('id_bk_button');
    var bk_widget = getbook('bk_widget');
    var parent = bk_button;
    var p_left = bk_button.offsetLeft;
    var p_top = bk_button.offsetTop + bk_button.offsetHeight - 150 - bk_button.offsetHeight;
    
    while (parent.offsetParent) {
      parent = parent.offsetParent;
      p_left += parent.offsetLeft;
      p_top += parent.offsetTop ;
    }//alert(bk_button.offsetTop +"|"+ bk_widget.style.height+"|"+p_top);
    bk_widget.style.left = p_left + 'px';
    bk_widget.style.top = p_top + 'px';
    bk_widget.style.display = 'block';
  },
  hide: function () {
    getbook('bk_widget').style.display = 'none';
  }
}
if (!widget.inited) widget.init();

function bk_request(provider) {
  var request = 'url=' + widget.bk_url + '&title=' + widget.bk_pagetitle;
  request += '&esctitle=' +  widget.bk_pagetitle;
  request += '&provider=' + provider + '&sid=' + Math.random();
  if( typeof user != udv)
    request += '&u=' + user

  if(provider === 'favorities') {
    if (IFrameObj) {
      document.body.removeChild(IFrameObj);
    }
    if (document.createElement) {
      // create the IFrame and assign a reference to the
      // object to our global variable IFrameObj.
      // this will only happen the first time 
      // callToServer() is called
      try {
        var tempIFrame=document.createElement('iframe');
        tempIFrame.setAttribute('id','RSIFrame');
        tempIFrame.style.border='0px';
        tempIFrame.style.width='0px';
        tempIFrame.style.height='0px';
        IFrameObj = document.body.appendChild(tempIFrame);
        
        if (document.frames) {
          // this is for IE5 Mac, because it will only
          // allow access to the document object
          // of the IFrame if we access it through
          // the document.frames array
          IFrameObj = document.frames['RSIFrame'];
        }
      } catch(exception) {
        // This is for IE5 PC, which does not allow dynamic creation
        // and manipulation of an iframe object. Instead, we'll fake
        // it up by creating our own objects.
        iframeHTML='<iframe id="RSIFrame" style="';
        iframeHTML+='border:0px;';
        iframeHTML+='width:0px;';
        iframeHTML+='height:0px;';
        iframeHTML+='"><\/iframe>';
        document.body.innerHTML+=iframeHTML;
        IFrameObj = new Object();
        IFrameObj.document = new Object();
        IFrameObj.document.location = new Object();
        IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
        IFrameObj.document.location.replace = function(location) {
          this.iframe.src = location;
        }
      }
    }
    if (IFrameObj.contentDocument) {
      // For NS6
      IFrameDoc = IFrameObj.contentDocument; 
    } else if (IFrameObj.contentWindow) {
      // For IE5.5 and IE6
      IFrameDoc = IFrameObj.contentWindow.document;
    } else if (IFrameObj.document) {
      // For IE5
      IFrameDoc = IFrameObj.document;
    }
    try {
      IFrameDoc.location.replace(widget.server + '?' + request);
    } catch(exception) {
    }
    try {
      if(document.all) {
        window.external.AddFavorite(widget.bk_url, widget.bk_pagetitle);
      } else {
        window.sidebar.addPanel(widget.bk_pagetitle, widget.bk_url, '');
      }
    } catch (exception) {
      alert('你用的啥浏览器??本软件还不支持');
    }
  } else {
    var relay_url = widget.server + '?' + request;
    config='scrollbars=yes,toolbar=yes,location=no,status=no,menubar=no,width=650,height=450,';
    config += 'resizable=yes';
    window.open(relay_url, 'Bookmark', config);
  }
  return false;
}


function showWidget() {
  widget.show();
}

function hideWidget() {
  widget.hide();
}


function getbook(id) {
  try {
    if (document.getElementById) {
      return document.getElementById(id);
    } else if (document.all) {
      return document.all[id];
    }
  } catch (e) {
  }
  return null;
}
