if (typeof traceregister != "Object")
  traceregister = {};
if (!traceregister.widget) {
  traceregister.widget = {
	  app_host:"https://aws.traceregister.com/Modules_production",
      setOnResize:false,
      oldOverflow:"",
      avail_options: {
	      widget_value:"",
	      widget_value_prefix:"",
	      popup_width:800,
	      popup_height:830
	  },
      sel_options: {},
    render: function(node,options) {
      if (node && (typeof node.innerHTML) != "undefined") {
        if (!options) options = {};
        //mixin options
        for (option in this.avail_options) {
          if (options[option])
            this.sel_options[option] = options[option];
          else
            this.sel_options[option] = this.avail_options[option];
        }
        //insert html
        node.innerHTML = "<form method='GET' action='"+this.app_host+"/publicviews/view' onsubmit='var t=this;setTimeout(function(){traceregister.widget.submit(t);},10);return false;'>"+
          "<div style='position:relative;color:#134A8B;width:225px;'>"+
            "<img src='https://cdn.traceregister.com/img/logo_swoosh.gif' border=0 style='position:absolute;left:2px;top:47px;' />"+
            "<img src='"+this.app_host+"/system/application/views/publicviews/images/mid_left_box.png' style='position:absolute;left:0px;top:110px;' />"+
            "<table cellspacing=0 cellpadding=0 bgcolor='white' width='225px' border=0>"+
              "<tr bgcolor='#164D8D'>"+
	            "<td style='padding:40px 0px 45px 25px;' colspan='2'><font color='orange' size='-3'>TRACE YOUR FOOD:</font><br/><font color='white' size='3'>Enter your<br/>Traceability Code</font></td>"+
              "</tr>"+
              "<tr>"+
                "<td style='padding:15px 0px 0px 10px;'><input type='text' name='_search' value='"+this.sel_options.widget_value+"' maxlength='255' style='width:140px;font-size:19px;border:1px dotted black;border-right:none;' /></td>"+
                "<td style='padding:15px 10px 0px 0px;' align='right'>"+
                  ((document.all)?
                  "<input type='submit' value='TRACE' style='padding:0px 1px 0px 1px;height:26px;background-color:#134A8B;color:white;border:1px dotted black;border-left:none;' />":
                  "<input type='submit' value='TRACE' style='padding:0px 5px 0px 5px;height:26px;background-color:#134A8B;color:white;border:1px dotted black;border-left:none;' />")+
                "</td>"+
              "</tr>"+
              "<tr>"+
                "<td colspan=2 style='font-size:10px;padding:0px 0px 30px 10px;' align='left'>Powered By <a style='text-decoration:none;color:#134A8B;' href='http://www.traceregister.com'>Trace Register</a></td>"+
              "</tr>"+
            "</table>"+
          "</div>"+
        "</form>";
      }
    },
    submit: function(fm) {
    	if (fm._search.value.length) {
          //add _options
          var _html = "";
          if (fm._search.value.toLowerCase().substr(0,this.sel_options.widget_value_prefix.length) != this.sel_options.widget_value_prefix.toLowerCase())
            _html += "<input type='hidden' name='search' value='"+this.sel_options.widget_value_prefix+fm._search.value+"' />";
          else 
            _html += "<input type='hidden' name='search' value='"+fm._search.value+"' />";
          for (option in this.sel_options) {
            if (this.sel_options[option].toString().length)
            _html += "<input type='hidden' name='"+option+"' value='"+this.sel_options[option]+"' />"
          }
          if (fm.lastChild.nodeName.toLowerCase() != "span")
            fm.appendChild(document.createElement('span'));
          fm.lastChild.innerHTML = _html;
          //open window
          this.showPopup('traceregister_widget_render',this.sel_options.popup_height,this.sel_options.popup_width);
          //submit to window
          fm.target = "traceregister_widget_render";
          fm.submit();
    	}
    },
    showPopup: function(frameName,frameHeight,frameWidth) {
      var body = (document.body)?[document.body]:document.getElementsByTagName("body");
      //add white opaque background
      var bg = document.createElement('div');
      bg.className = "___traceregister_widget";
      bg.onclick = this.hidePopup;
      with (bg.style) {
        position = "absolute";
        top = "0px";
        left = "0px";
        backgroundColor = "white";
        opacity = .75;
        filter = "alpha(opacity=75)";
        height = "100%";
        width = "100%";
        zIndex = 999999998;
      }
      body[0].appendChild(bg);
      //add popup
      var popup = document.createElement('div');
      popup.className = "___traceregister_widget";
      with (popup.style) {
        position = "absolute";
        top = "100px";
        left = "100px";
        height = (parseInt(frameHeight)+32)+"px";
        width = (parseInt(frameWidth)+16)+"px";
        zIndex = 999999999;
      }
      body[0].appendChild(popup);
      //add popup
      popup.innerHTML += "<iframe frameborder='0' allowtransparency='false' bgcolor='white' scrolling='no' name='"+frameName+"' id='"+frameName+"' src=''></iframe>";
      var iframe = popup.lastChild;
      with (iframe.style) {
        position = "absolute";
        bottom = "6px";
        right = "0px";
        height = frameHeight+"px";
        width = frameWidth+"px";
        border = "1px solid #99999b";
        backgroundColor='white';
      }
      popup.appendChild(iframe);
      iframe.close = this.hidePopup;
      //add closer
      var closer = document.createElement('div');
      with (closer.style) {
        position = "absolute";
        top = "7px";
        right = "0px";
      }
      popup.appendChild(closer);
      closer.innerHTML = "<a style='text-decoration:none;color:gray;' href='javascript:traceregister.widget.hidePopup()'><strong>close [x]</strong></a>";
      //add drop shadow
      for (var x=1; x < 7; x++) {
        var shadow = document.createElement('div');
        with (shadow.style) {
          position = "absolute";
          if (x < 4) {
            bottom = "6px";
            left = (14-(x*2))+"px";
            height = frameHeight+"px";
            width = "2px";
            opacity = (.2/x);
            filter = "alpha(opacity="+(20/x)+")";
          }
          else {
            bottom = (6-((x-3)*2))+"px";
            left = "14px";
            height = "2px";
            width = frameWidth+"px";
            opacity = (.2/(x-3));
            filter = "alpha(opacity="+(20/(x-3))+")";
          }
          overflow = "hidden";
          backgroundColor = "black";
        }
        popup.appendChild(shadow);
      }
      var shadow = document.createElement('div');
      with (shadow.style) {
        position = "absolute";
        bottom = "0px";
        left = "8px";
        height = "6px";
        width = "6px";
        overflow = "hidden";
        backgroundColor = "black";
        opacity = .08;
        filter = "alpha(opacity=08)";
      }
      popup.appendChild(shadow);
      //center popup
      this.centerPopup();
      //hookup resize
      if (!this.setOnResize && body[0].onresize) {
        var _old = body[0].onresize;
        body[0].onresize = function(){
          _old();
          traceregister.widget.centerPopup;
        }
      }
      else if (!this.setOnResize) {
        body[0].onresize = this.centerPopup;
      }
      if (!this.setOnResize && window.onresize) {
        var __old = window.onresize;
        window.onresize = function(){
          __old();
          traceregister.widget.centerPopup;
        }
      }
      else if (!this.setOnResize) {
        window.onresize = this.centerPopup;
      }
      this.setOnResize = true;
      //no overflow
      if (document.all) {
        this.oldOverflow = body[0].scroll;
        body[0].scroll = "no";
      }
      else {
        this.oldOverflow = body[0].style.overflow;
        body[0].style.overflow = "hidden";
      }
    },
    centerPopup: function() {
      var body = (document.body)?[document.body]:document.getElementsByTagName("body");
      var nodes = document.getElementsByClassName("___traceregister_widget");
      for (var x = 0; x < nodes.length; x++) {
        if (parseInt(nodes[x].style.left) != 0) { 
          with (nodes[x].style) {
            if (document.all) {
              top = ((((document.documentElement.clientHeight)?document.documentElement.clientHeight:body[0].clientHeight)/2)-(parseInt(nodes[x].style.height)/2)-12)+"px";
              left = ((((document.documentElement.clientWidth)?document.documentElement.clientWidth:body[0].clientWidth)/2)-(parseInt(nodes[x].style.width)/2)-6)+"px";
            }
            else {
              top = ((((window.innerHeight)?window.innerHeight:body[0].clientHeight)/2)-(parseInt(nodes[x].style.height)/2)-12)+"px";
              left = ((((window.innerWidth)?window.innerWidth:body[0].clientWidth)/2)-(parseInt(nodes[x].style.width)/2)-6)+"px";
            }
          }
          if (parseInt(nodes[x].style.top) < 0)
            nodes[x].style.top = "1px";
          if (parseInt(nodes[x].style.left) < 0)
            nodes[x].style.left = "1px";
        } 
        else {
          with (nodes[x].style) {
            if (document.all) {
              height = ((document.documentElement.clientHeight)?document.documentElement.clientHeight:body[0].clientHeight)+"px";
              width = ((document.documentElement.clientWidth)?document.documentElement.clientWidth:body[0].clientWidth)+"px";
            }
            else {
              height = ((window.innerHeight)?window.innerHeight:body[0].clientHeight)+"px";
              width = ((window.innerWidth)?window.innerWidth:body[0].clientWidth)+"px";
            }
          }
        }
      }
    },
    hidePopup: function() {
      var body = (document.body)?[document.body]:document.getElementsByTagName("body");
      var nodes = document.getElementsByClassName("___traceregister_widget");
      while (nodes.length) {
        for (var x = 0; x < nodes.length; x++) {
          if (nodes[x] && nodes[x].parentNode)
            nodes[x].parentNode.removeChild(nodes[x]);
        }
        nodes = document.getElementsByClassName("___traceregister_widget");
      }
      //restore overflow
      if (document.all)
        body[0].scroll = (this.oldOverflow.toString().length)?this.oldOverflow:"yes";
      else
        body[0].style.overflow = (this.oldOverflow.toString().length)?this.oldOverflow:"auto";
    }
  }
}
//browser compat
if (!document.getElementsByClassName) {
  document.getElementsByClassName = function(cl) {
    var retnode = [];
    var myclass = new RegExp('\\b'+cl+'\\b');
    var elem = this.getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) {
      var classes = elem[i].className;
      if (myclass.test(classes)) retnode.push(elem[i]);
    }
    return retnode;
  }; 
} 

