(function() {

  // Utility methods
  //-----------------------------------------------------------------------------

  function isIE() {
    return navigator.userAgent.match(/MSIE/);
  }

  function addEventListener(elem, evnt, func) {
    if (elem.addEventListener)
      elem.addEventListener(evnt,func,false);
    else if (elem.attachEvent) {
      var r = elem.attachEvent("on"+evnt, func);
      return r;
    }
  } 

  function removeEventListener(elem, evnt, func, useCapture){
    if (elem.removeEventListener){
      elem.removeEventListener(evnt, func, useCapture);
      return true;
    } else if (elem.detachEvent){
      var r = elem.detachEvent("on"+evnt, func);
      return r;
    }
  }

  function setStyle(elem, style) {
    if (isIE()) {
      elem.style.setAttribute("cssText", style);
    }
    else {
      elem.setAttribute("style", style);
    }
  }

  // script vars
  //-----------------------------------------------------------------------------

  var __IC = {
    domain : "http://icomply.zpoley.net/",
    referer : document.referrer,
    fromIComply : false,
    scriptElement : null,
    scriptParams : {},
    visible : true,

    containerDiv : null,

    headerDiv : null,
    headerDivHeight : 49,

    popupDiv : null,
    popupDivHeight : 0,
    popupDivMaxHeight : 184,
    popupDivHeightDelta : 10,

    closeDiv : null,  

    boldFontStyle : "font: bold 17px Helvetica;",
    normalFontStyler : "font: normal 16px Helvetia;",
   
    tableDiv : null
  };

  var styleReset = "padding: 0px; margin: 0px; font-family: Helvetica; font-size: 14px;";

  // setup globals
  //-----------------------------------------------------------------------------

  if (window.__IC) {
    window.__IC.index++;
  }
  else {
    window.__IC = __IC;
    window.__IC.index = 0;
  }

  (function getCurrentScriptAndParams() {
    var scripts = document.getElementsByTagName("script");
    var thisScripts = [];
    for (var i = 0; (i < scripts.length); i++) {
      if (scripts[i].src.match("__IC=1")) {
        thisScripts.push(scripts[i]);
      } 
    }
    if (thisScripts.length) {
      __IC.scriptElement = thisScripts[window.__IC.index];
      var split_src = __IC.scriptElement.src.split("?");
      split_src.shift();
      var raw_params = split_src.join("?").split("&");
      for (var i = 0; (i < raw_params.length); i++) {
        var kv = raw_params[i].split("=");
        var key = kv.shift();
        var value = kv.join("="); 
        __IC.scriptParams[unescape(key)] = unescape(value); 
      }
    }
    (function setupDefaults() {
      var defaultContent = "For more information, please visit <a target='_blank' href='http://www.google.com'>this page</a>" + 
        " or <a target='_blank' href='http://bing.com'>view the official terms and conditions</a>.";
      var defaultStandardDisclosure = "Zach rules " + defaultContent;
      var defaultTweetDisclosure = "Yeah.... nothin.. " + defaultContent;
      __IC.standardDisclosure = __IC.scriptParams.standardDisclosure ? __IC.scriptParams.standardDisclosure : defaultStandardDisclosure;
      __IC.tweetDisclosure = __IC.scriptParams.tweetDisclosure ? __IC.scriptParams.tweetDisclosure : defaultTweetDisclosure;
      var defaultStandardDisclosureHeader = "Site Disclosure"; 
      var defaultTweetDisclosureHeader = "Tweet Disclosure";
      __IC.standardDisclosureHeader = __IC.scriptParams.standardDisclosureHeader ? __IC.scriptParams.standardDisclosureHeader : defaultStandardDisclosureHeader;
      __IC.tweetDisclosureHeader = __IC.scriptParams.tweetDisclosureHeader ? __IC.scriptParams.tweetDisclosureHeader : defaultTweetDisclosureHeader;
    })();
  })();

  // functionality
  //-----------------------------------------------------------------------------

  (function getOrigin() {
    __IC.visible = __IC.fromIComply = ((__IC.referer && __IC.referer.match(/icomp/)) || document.location.toString().match("__IC=1"));
  })();

  function onOpen() {
    function animateOpen() {
      if ((__IC.popupDivHeight + __IC.popupDivHeightDelta) < __IC.popupDivMaxHeight) {
        __IC.popupDivHeight += __IC.popupDivHeightDelta;
        setTimeout(function() { animateOpen(); }, 10);
      }
      else {
        __IC.popupDivHeight = __IC.popupDivMaxHeight;
        __IC.visible = true;
      }
      __IC.popupDiv.style.height = __IC.popupDivHeight + "px";
      __IC.containerDiv.style.height = (__IC.popupDivHeight + __IC.headerDivHeight) + "px";
    }
    setTimeout(function() { animateOpen(); }, 10);    

  }

  function onClose() {
    function animateClose() {
      if ((__IC.popupDivHeight - __IC.popupDivHeightDelta) > 0) {
        __IC.popupDivHeight -= __IC.popupDivHeightDelta;
        setTimeout(function() { animateClose(); }, 10);
      }
      else {
        __IC.popupDivHeight = 0;
        __IC.visible = false;
      }
      __IC.popupDiv.style.height = __IC.popupDivHeight + "px";
      __IC.containerDiv.style.height = (__IC.popupDivHeight + __IC.headerDivHeight) + "px";
    }
    setTimeout(function() { animateClose(); }, 10);    
  }

  function onClickHeader() {
    if (__IC.visible) {
      onClose();
    }
    else {
      onOpen();
    }
  }

  function setupWidget() {

    // main container
    __IC.containerDiv = document.createElement("div");
    setStyle(__IC.containerDiv, styleReset + "position: fixed; width: 303px; height: 49px;" + 
      "bottom: 0px; right: 10px; text-align: center; z-index: 99999;");

    // header
    __IC.headerDiv = document.createElement("div");
    setStyle(__IC.headerDiv, "margin: 0px auto 0px auto; height: 49px; width: 144px; " +
      "cursor: pointer; background: url(" + __IC.domain + "images/bubble.png) 0px 0px;");
    __IC.headerDiv.innerHTML = "<div style='" + __IC.boldFontStyle + "padding: 14px 12px 0px 8px;'>disclosures</div>";
    __IC.containerDiv.appendChild(__IC.headerDiv);

    addEventListener(__IC.headerDiv, "click", onClickHeader);

    // popup container
    __IC.popupDiv = document.createElement("div");
    setStyle(__IC.popupDiv, styleReset + "position:absolute; width: 303px; height: 0px;" +
      "background-image: url(" + __IC.domain + "images/pop-bg-cropped.png); overflow: hidden;");
    __IC.containerDiv.appendChild(__IC.popupDiv);

    // close button
    __IC.closeDiv = document.createElement("div");
    setStyle(__IC.closeDiv, styleReset + "cursor: pointer; padding: 2px 4px;");
    __IC.closeDiv.innerHTML = "<span style='" + __IC.boldFontStyle + "color: #424242;'>X</span>";

    addEventListener(__IC.closeDiv, "click", onClose);

    // info table container
    __IC.tableDiv = document.createElement("div");
    setStyle(__IC.tableDiv, "width: 290px; height: 164px; margin: 10px; padding: 0px;");
    var content = __IC.fromIComply ? __IC.tweetDisclosure : __IC.standardDisclosure;
    var header = __IC.fromIComply ? __IC.tweetDisclosureHeader : __IC.standardDisclosureHeader;
    __IC.tableDiv.innerHTML = "<table style='width: 290px;'>" +
      "<tr><td style='width: 36px;'><img src='" + __IC.domain + "images/notify.png'></img></td>" + 
      "<td style='text-align:left; padding: 6px;' valign=bottom><span style='" + __IC.boldFontStyle + "color: #424242;'>" + header + "</span></td>" + 
      "<td valign=top><div id='__ICCloseContainer' style=" + styleReset + "></div></td></tr>" +
      "<tr><td colspan=3 style='padding: 12px;'><div id='__ICDisclosureContent' style='text-align: left;'><span style='color: #424242; font-size: 16px;'>" + content + "</span></div></td></tr>" + 
      "</table>";
    __IC.popupDiv.appendChild(__IC.tableDiv);

    document.body.appendChild(__IC.containerDiv); 
    var closeContainer = document.getElementById("__ICCloseContainer");
    closeContainer.appendChild(__IC.closeDiv);
  
    function updateChildStyles(children) {
      for (var i = 0; (i < children.length); i++) {
        var grandChildren = children[i].childNodes;
        if (grandChildren.length > 0) {
          updateChildStyles(grandChildren);
        }
        if (children[i].style) {
          children[i].style.fontSize = "16px";
          children[i].style.textDecoration = "none";
        }
      }
    }

    var children = document.getElementById("__ICDisclosureContent").childNodes;
    updateChildStyles(children);


  }

  function onLoad() {
    setupWidget();
    if (__IC.visible) {
      onOpen();
    }
  }

  addEventListener(window, "load", onLoad);

})();

