window.size = function() {
  var w = 0;
  var h = 0;

  //IE
  if (!window.innerWidth) {
    //strict mode
    if (!(document.documentElement.clientWidth == 0)) {
      w = document.documentElement.clientWidth;
      h = document.documentElement.clientHeight;
    } else {  //quirks mode
      w = document.body.clientWidth;
      h = document.body.clientHeight;
    }
  } else { //w3c
    w = window.innerWidth;
    h = window.innerHeight;
  }
  return {width:w,height:h};
};

window.center = function() {
  var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
  var _x = 0;
  var _y = 0;
  var offsetX = 0;
  var offsetY = 0;

  //IE
  if (!window.pageYOffset) {
    //strict mode
    if (!(document.documentElement.scrollTop == 0)) {
      offsetY = document.documentElement.scrollTop;
      offsetX = document.documentElement.scrollLeft;
    } else {     //quirks mode
      offsetY = document.body.scrollTop;
      offsetX = document.body.scrollLeft;
    }
  } else {   //w3c
    offsetX = window.pageXOffset;
    offsetY = window.pageYOffset;
  }

  _x = ((this.size().width - hWnd.width) / 2);// + offsetX;
  _y = ((this.size().height - hWnd.height) / 2);// + offsetY;

  return{x:_x,y:_y};
};

/**
 * Gets element from ie and ff dom
 */
function getElement(elementId) {
  return document.getElementById(elementId);
}

/**
 * Clear error and invoke method on flash
 */
function subscribeWithElertGadget(subscriptionName, subscriptionURL, subscriptionType) {
  hideElertButtonErrorPopup();
  getElement("elert-subscription-button-container").registerSubscription(subscriptionName, subscriptionURL, subscriptionType);
}

/**
 * Hides error popup.
 */
function hideElertButtonErrorPopup() {
  var div = getElement("error-elert-notify-container");
  div.style.display = "none";
}

/**
 * Hides error popup.
 */
function hideAndRedirectElertButtonErrorPopup() {
  //window.location = getElement('error-elert-notify-link').href;
  openInNewWindow(getElement('error-elert-notify-link').href);
}

 /*
  * Create the new window and open url.
  */
function openInNewWindow(url) {
 var newWindow = window.open(url, '_blank');
 newWindow.focus();
 return false;
}

/**
 * Shows error popup.
 */
function showElertButtonErrorPopup(error) {
  var div = getElement("error-elert-notify-container");
  div.style.display = "block";
  var point = window.center({width:div.offsetWidth, height: div.offsetHeight});
  div.style.top = point.y + "px";
  div.style.left = point.x + "px";
}

/**
 * Performs actual SWF embedding operation.
 */
function initElertButtonSWF() {
  //initialize flash SWF variables
  var flashvars = {};
  flashvars.showErrorCallbackMethodName = "showElertButtonErrorPopup";
  flashvars.hideErrorCallbackMethodName = "hideElertButtonErrorPopup";

  var params = {};
  params.menu = 'false';
  params.quality = 'high';
  params.allowScriptAccess = "always";

  var attributes = {};

  //embed SWF - initializeLayout() required to be called first!
  swfobject.embedSWF('http://www.elertgadget.com/airpub/subscription_button/ElertSubscriptionButton.swf', 'elert-subscription-button-container', '1', '1', '10.0.0', 'http://www.elertgadget.com/airpub/subscription_button/expressInstall.swf', flashvars, params, attributes);
}

/**
 * Retrieves CSS with styles for notification popup.
 * Prepares layout for SWF content and error notification popup.
 */
function initializeLayout() {
  var headElement = document.getElementsByTagName("head")[0];

  //append css link
  var script = document.createElement('link');
  script.type = 'text/css';
  script.rel = 'Stylesheet';
  script.href = 'http://www.elertgadget.com/airpub/subscription_button/elert_subscription_button.css'; 
  headElement.appendChild(script);

  //append js swf embedder
  var swfScript = document.createElement('script');
  swfScript.type = 'text/javascript';
  var randomParam = '?__random_seed=' + Math.random();
  swfScript.src = 'http://www.elertgadget.com/airpub/subscription_button/swfobject.js'+randomParam;
  var onScriptLoaded = initElertButtonSWF;
  if (navigator.appName.indexOf('Internet Explorer') != -1) {
    swfScript.onreadystatechange = function() {
      if (this.readyState == 'loaded') {
        initElertButtonSWF();
      }
    };
  } else {
    swfScript.onload = initElertButtonSWF;
  }
  headElement.appendChild(swfScript);
  
  //add subscribe button div - in this div SWF object will be embedded so it must be added before loading swf js dependencies
  var bodyElement = document.getElementsByTagName("body")[0];
  var subscribeButtonDiv = document.createElement('div');
  subscribeButtonDiv.id = 'elert-subscription-button-container';
  bodyElement.appendChild(subscribeButtonDiv);

  //add subscribe button error popup div
  var subscribeButtonErrorDiv = document.createElement('div');
  subscribeButtonErrorDiv.id = 'error-elert-notify-container';
  subscribeButtonErrorDiv.className = 'error-elert-notify-container';
  subscribeButtonErrorDiv.style.display = 'none';
  subscribeButtonErrorDiv.style.textAlign = 'center';

  //find out configuration id
  var configIdElem = document.getElementById('get_elerts_link');
  var configId;

  if(configIdElem) {
    configId = configIdElem.value;
  }
  
  var downloadLink = 'http://www.elertgadget.com/download.php';
  if(configId) {
    downloadLink = 'http://www.elertgadget.com/downloads/verify.php?id=' + configId;
  }

  var errorPopupContent =
    '<table>'
      + '<tbody>'
      + '<tr>'
      + '  <td class="tl"></td>'
      + '  <td class="b"></td>'
      + '  <td class="tr"></td>'
      + '</tr>'
      + '<tr>'
      + '  <td class="b"></td>'
      + '  <td class="body">'
      + '    <div class="error-elert-notify-content">'

      //actual content starts here
      + '<div class="error-elert-notify-left">'
      + '  <a class="error-elert-notify-link-logo" target="_blank" href="http://www.elertgadget.com">'
      + '     <img src="http://www.elertgadget.com/inc/images/logo.png" alt="eLert Gadget"/>'
      + '  </a>'
      + '</div> '
      + '<div class="error-elert-notify-center"> '
      /*
       + '  <a class="error-elert-notify-link error-elert-notify-close" title="close this notification" onclick="hideElertButtonErrorPopup();">'
       + '    &times;'
       + '  </a>  '
       */
      + ' If you have eLert Gadget installed please launch it... <br/>'
      + 'OR <a id="error-elert-notify-link" class="error-elert-notify-link" target="_blank" href="' + downloadLink + '">install</a> a gadget.'
      + ' <br/>'
      + '<button class="error-elert-notify-ok-button" type="button" onClick="hideAndRedirectElertButtonErrorPopup();">&nbsp;&nbsp;Install &nbsp;&nbsp;</button>&nbsp;&nbsp;'
      + '<button class="error-elert-notify-ok-button" type="button" onClick="hideElertButtonErrorPopup();">&nbsp;Close [X]&nbsp;</button>'
      + '</div>   '
      //actual content ends here
    
      + '    </div>  '
      + '  </td>   '
      + '  <td class="b"></td>  '
      + '</tr>          '
      + '<tr>           '
      + '  <td class="bl"></td>  '
      + '  <td class="b"></td>    '
      + '  <td class="br"></td>  '
      + '</tr>            '
      + '</tbody>    '
      + '</table> ';

  subscribeButtonErrorDiv.innerHTML = errorPopupContent;
  bodyElement.appendChild(subscribeButtonErrorDiv);
}

//initialize swf object nad notification popup in layout
initializeLayout();