
fbzBase = {
  /**
   * Proxy alerter
   * @author Arash Yalpani
   *
   * @param str Text for alert
   *
   * @example alert('An error occurred')
   */

  alert: function(str) {
    console.log('Feedeebuzz alert: ' + str);
  },

  /**
   * Returns FIRST matching element by tagname
   * @author Arash Yalpani
   *
   * @param tagName name of tags to filter
   * @return first matching dom node or false if none exists
   *
   * @example getByTagName('div') => domNode
   * @example getByTagName('notexistent') => false
   */

  getByTagName: function(tagName) {
    var els = document.getElementsByTagName(tagName);
    if (els.length > 0) {
      return els[0];
    }

    return false;
  },

  /**
   * Returns an array of matching DOM nodes
   * @author Arash Yalpani
   *
   * @param tagName name of tags to filter
   * @param attrName name of attribute, matching tags must contain
   * @param attrValue value of attribute, matching tags must contain
   * @param domNode optional: dom node to start filtering from
   * @return Array of matching dom nodes
   *
   * @example getElementsByTagNameAndAttr('div', 'class', 'highlight') => [domNode1, domNode2, ...]
   */
   getElementsByTagNameAndAttr: function(tagName, attrName, attrValue, domNode) {
    if (domNode) {
      els = domNode.getElementsByTagName(tagName);
    }
    else {
      els = document.getElementsByTagName(tagName);
    }

    if (els.length === 0) {
      return [];
    }

    var _els = [];
    for (var i = 0; i < els.length; i++) {
      if (attrName == 'class') {
        classNames = '';
        if (els[i].getAttribute('class')) {
          classNames = els[i].getAttribute('class');
        }
        else {
          if (els[i].getAttribute('className')) {
            classNames = els[i].getAttribute('className');
          }
        }

        var reg = new RegExp('(^| )'+ attrValue +'($| )');
        if (reg.test(classNames)) {
          _els.push(els[i]);
        }
      }
      else {
        if (els[i].getAttribute(attrName) == attrValue) {
          _els.push(els[i]);
        }
      }
    }

    return _els;
  },

  /**
   * Returns url param value
   * @author Arash Yalpani
   *
   * @param url The url to be queried
   * @param paramName The params name
   * @return paramName's value or false if param does not exist or is empty
   *
   * @example getUrlParam('http://localhost/?a=123', 'a') => 123
   * @example getUrlParam('http://localhost/?a=123', 'b') => false
   * @example getUrlParam('http://localhost/?a=',    'a') => false
   */

  getUrlParam: function(url, paramName) {
    var urlSplit = url.split('?');
    if (!urlSplit[1]) { // no query
      return false;
    }

    var urlQuery = urlSplit[1];
    var paramsSplit = urlSplit[1].split('&');
    for (var i = 0; i < paramsSplit.length; i++) {
      paramSplit = paramsSplit[i].split('=');
      if (paramSplit[0] == paramName) {
        return paramSplit[1] ? paramSplit[1] : false;
      }
    }

    return false;
  },
   
  postFetch: function(url, type, onerror) {
    if (type === 'script') {
      scriptOrStyle = document.createElement('script');
      scriptOrStyle.type = 'text/javascript';
      scriptOrStyle.src  = url;
    }
    else {
      scriptOrStyle = document.createElement('link');
      scriptOrStyle.type = 'text/css';
      scriptOrStyle.rel  = 'stylesheet';
      scriptOrStyle.href = url;
    }

    if (onerror) { scriptOrStyle.onerror = onerror; }

    var head = fbzBase.getByTagName('head');
    if (head) {
      head.appendChild(scriptOrStyle);
      return ;
    }

    fbzBase.alert('head tag is missing');
  }
}


FBS = {}

FBS.Control = {
  isOpened: false,
  init: function() {
    fbzBase.postFetch('http://feedeebuzz.de/widget/css/feedeebuzz.css?p=l&c=bl', 'css');
    
    // create Feedeebuzz Tab
    var tab         = document.createElement('a');
    tab.id          = 'fbzTab';
    tab.style.border = 0;
    tab.href        = 'javascript:;';
    tab.title       = 'Leave Feedback';
    tab.innerHTML   = 'Feedeebuzz';
    tab.onclick     = function() {
      tab.blur();
        if (!FBS.Control.isOpened)
            FBS.Control.open();
        else
            FBS.Control.close();
    };

    document.body.appendChild(tab);
  },
  
  position: function(dialog) {
    if (!dialog)
      return ;
      
    //get viewport's width and height
    vpWidth      = window.innerWidth  || document.body.clientWidth;
    vpHeight     = window.innerHeight || document.body.clientHeight;
    
    // get dialog height and width
    dialogHeight = 400;
    dialogWidth  = 500;
    
    //calculate position
    dialogTop  = ((vpHeight  / 2) - (dialogHeight  / 2)) / 2;
    dialogLeft = (vpWidth  / 2) - (dialogWidth  / 2);
    dialogTop  = dialogTop  < 0 ? 0 : dialogTop;
    dialogLeft = dialogLeft < 0 ? 0 : dialogLeft;

    //Position the Dialog
    dialog.style.position = 'fixed';
    dialog.style.top  = dialogTop  + "px";
    dialog.style.left = dialogLeft + "px";
  },
  
  open: function() {
    FBS.Control.isOpened = true;
    
    var cover = document.createElement('div');
    cover.id = 'fbzCover';
    cover.style.width = '100%';
    cover.style.height = '100%';
    cover.style.left = 0;
    cover.style.top = 0;
    cover.style.zIndex = 9999;
    cover.style.position = 'fixed';
    cover.style.background = 'transparent url(http://feedeebuzz.de/widget/img/cover.png) repeat scroll 0% 0%';

    var iframe = document.createElement('iframe');
    iframe.allowTransparency = 'true';
    iframe.style.width  = '500px';
    iframe.style.height = '400px';
    iframe.style.border = 0;
    iframe.frameBorder = 0;
    iframe.style.overflow = 'hidden';
    iframe.src = 'http://feedeebuzz.de/widget/form.php?to=mailbox%40hotelsunited.de&ref=' + window.location.href;

    var title = document.createElement('div');
    title.id = 'fbzDialogTitle';
    
    var html = '<table cellpadding="0" cellspacing="0" width="498"><tbody><tr>';
    if (window.location.href.indexOf('feedeebuzz') == -1)
      html+= '<td align="left"><a href="http://feedeebuzz.de/" target="_blank"><img src="http://feedeebuzz.de/widget/img/logo.png" alt="Feedeebuzz Feedback Formulare" border="0" /></a></td>';
      
    html+= '<td align="right"><a id="fbzClose" href="javascript:void(0);" onclick="FBS.Control.close()">Schlie&szlig;en X</a></td>';
    html+= '</tr></tbody></table>';
    
    title.innerHTML = html;
    
    var dialog = document.createElement('div');
    dialog.id = 'fbzDlg';
    dialog.style.position = 'absolute';
    
    dialog.style.zIndex = 10000;
    dialog.style.width  = '500px';
    dialog.style.height = '420px';
    dialog.allowTransparency = 'true';
    
    dialog.appendChild(title);
    dialog.appendChild(iframe);
    document.body.appendChild(cover);
    document.body.appendChild(dialog);
    
    FBS.Control.position(dialog);
  },
  
  close: function() {
    FBS.Control.isOpened = false;
    document.body.removeChild(document.getElementById('fbzDlg'));
    document.body.removeChild(document.getElementById('fbzCover'));
  }
}

FBS.Control.init();

window.onresize = function() {
  FBS.Control.position(document.getElementById('fbzDlg'));
}
