//#################################
//---------------------------------
//         Webdiscover.ru Share
//---------------------------------
//         Автор: Maxic
//---------------------------------
//#################################
if (!window.WD) window.WD = {};
if (!WD.Share) {
  WD.Share = {
    _popups: [],
	_post_url: [],
	
    button: function(post_url, but, index) {
	
	  // Index of window
	  if (index === undefined) {
        this._popups.push(false);
        index = this._popups.length - 1;
      }

	  // Define post_url
      if (!post_url || post_url == '') {
	     this._post_url[index] = WD.Share._loc;
	  } else { this._post_url[index] = post_url;}
	  
	  // Define buttin params
      if (!but) but = {but_id: 1};
      if (but === but.toString()) but = {but_id: 1, text: 'Поделиться'};
      if (!but.text) but.text = 'Поделиться';
	  
	  base_domain = 'http://webdiscover.ru/';
	  base_css = '<link rel="stylesheet" type="text/css" href="http://webdiscover.ru/engine/api/share/share.css" />';
	  
      if (but.id == 1) {
        return '<img src="'+ base_domain +'engine/api/img/button1.png" style="cursor:hand; cursor:pointer" onclick="WD.Share.click(' + index + ');" />';
      } else if (but.id == 2) {
	    return ''+ base_css +'<a href="http://webdiscover.ru/" id="wdbt1" onclick="WD.Share.click(' + index + '); return false;">'+ but.text +'</a>';
	  } else if (but.id == 3 || but.id == 4) {
	    return ''+ base_css +'<a href="http://webdiscover.ru/" id="wdbt2" onclick="WD.Share.click(' + index + '); return false;">'+ but.text +'</a>';
	  } else {
        return '<span style="position: relative" onclick="WD.Share.click(' + index + ');">' + but.text + '</span>';
      } 
    },
    click: function(index) {
      var popupName = '_blank';
      var width = 540;
      var height = 320;
      var left = (screen.width - width) / 2;
      var top = (screen.height - height) / 2.2;
	  var url = base_domain + 'share.php';
      var popupParams = 'scrollbars=0, resizable=1, menubar=0, left=' + left + ', top=' + top + ', width=' + width + ', height=' + height + ', toolbar=0, status=0';
      var popup = false;
      try {
        var doc_dom = '', loc_hos = '';
        try {
          doc_dom = document.domain;
          loc_hos = location.host;
        } catch (e) {
        }
        if (doc_dom != loc_hos) {
          var ua = navigator.userAgent.toLowerCase();
          if (!/opera/i.test(ua) && /msie/i.test(ua)) {
            throw 'wont work';
          }
        }
        popup = this._popups[index] = window.open('', popupName, popupParams);
        text = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' +
               '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">' +
               '<head><meta http-equiv="content-type" content="text/html; charset=windows-1251" /></head>' +
               '<body><form accept-charset="UTF-8" action="' + url + '" method="POST" id="share_form"><input type="hidden" name="url" value="'+this._post_url[index]+'" /></form><script type="text/javascript">document.getElementById("share_form").submit()</script></body></html>';
			   
        popup.document.write(text);
        popup.blur();
        popup.focus();
      } catch (e) { // ie with changed domain.
        try {
          if (popup) {
            popup.close();
          }
          popup = this._popups[index] = window.open(url, popupName, popupParams);
          popup.focus();
        } catch (e) {
        }
      }
    },
  }
  try {
    WD.Share._loc = location.toString();
  } catch(e) {
	WD.Share._loc = '';
  }
}
