// ==UserScript==
// @name          User Script common stuff for everybody
// @author        Ciuly
// @namespace     http://www.ciuly.com
// @description   Sets common functions and variable for all my scripts
// @include       *
// ==/UserScript==

(function(wnd){
  var is_opera = navigator.userAgent.toLowerCase().indexOf('opera') != -1;
  var is_ie = navigator.userAgent.toLowerCase().indexOf('msie ') != -1;
  var is_ff = navigator.userAgent.toLowerCase().indexOf('gecko') != -1;

  wnd.ciuly_is_opera = is_opera;
  wnd.ciuly_is_ie = is_ie;
  wnd.ciuly_is_ff = is_ff;

  wnd.ciuly_debug = 0;

  wnd.ciuly_post_res = function(url, data, errormsg, ignoreFailure) {
	var request;
	if (wnd.is_ie){
	  if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	  } else if (window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	  }
        } else
          request = new XMLHttpRequest();
	var error = 0;
	while (!ignoreFailure && error!=-1){
		request.open("POST", url, false);
		request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		request.send(data);
	
		var error = request.responseText.indexOf(errormsg);
		if (error!=-1 || wnd.ciuly_debug==1){
			w = window.open('','');
			w. document.write(request.responseText);
			w.focus();
			if (error!=-1){
				if (confirm("Error posting. See new window. Try again?"))
					; 
				else
					return "";
			} else if (wnd.ciuly_debug==1) 
				alert("Click ok to continue.");
		}
	}
	return request.responseText;
  };

  wnd.ciuly_get_res = function(url){
    var req;
    if (wnd.is_ie){
      if (window.XMLHttpRequest) {
	req = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
	req = new ActiveXObject("Microsoft.XMLHTTP");
      }
    } else
      req = new XMLHttpRequest();
    req.open("GET", url, false);
    req.send(null);
    return req.responseText;
  }

  wnd.ciuly_execLater = function (func, naptime){
    wnd.ciuly_progress_log("<b id=ciuly_wait_progress >You must wait "+naptime+" seconds.</b>");
    setTimeout("ciuly_execLaterTrigger('"+func+"', "+(naptime-1)+")", 1000);
  }

  wnd.ciuly_execLaterTrigger = function(func, naptime){
    if (naptime>0){
      prg = document.getElementById("ciuly_wait_progress");
      prg.innerHTML = "Must wait "+naptime+" seconds.";
      setTimeout("ciuly_execLaterTrigger('"+func+"', "+(naptime-1)+")", 1000);
    } else {
      prg.id="";
      prg.innerHTML = "Waiting over.";
//        wnd.ciuly_progress_div.removeChild(prg);
      setTimeout(func, 0);
    }  
  }

  if (wnd.is_ie){// add getElementsByClassName to IE document
    document.getElementsByClassName = function(cl) {
      var retnode = new Array();
      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;
    };
  }

  // http://www.logiclabz.com/javascript/create-a-modal-window-div-element-with-javascript-on-html.aspx
  var ciuly_progress_maskpanel=function()
  {   
    this.divobj;
    this.show=function()
    {   
      if(!document.getElementById("xdivmasking"))   
      {   
          var divEle=document.createElement('div');   
          divEle.setAttribute("id","xdivmasking");   
          document.body.appendChild(divEle);   
          var divSty=document.getElementById("xdivmasking").style;   
          divSty.position="absolute";divSty.top="0px";divSty.left="0px";divSty.zIndex="46";divSty.opacity=".50";divSty.backgroundColor="#000";   
          divSty.filter="alpha(opacity=50)";   
  
          var divFram=document.createElement('iframe');   
          divFram.setAttribute("id","xmaskframe");   
          document.body.appendChild(divFram);   
          divSty=document.getElementById("xmaskframe").style;   
          divSty.position="absolute";divSty.top="0px";divSty.left="0px";divSty.zIndex="45";divSty.border="none";divSty.filter="alpha(opacity=0)";   
       }   
  
       this.divobj=document.getElementById("xdivmasking");   
       this.waitifrm=document.getElementById("xmaskframe");   
          
       var dsh=document.documentElement.scrollHeight;
       var dch=document.documentElement.clientHeight;
       var dsw=document.documentElement.scrollWidth;
       var dcw=document.documentElement.clientWidth;
       
       var bdh=(dsh>dch)?dsh:dch;
       var bdw=(dsw>dcw)?dsw:dcw;

       this.waitifrm.style.height=this.divobj.style.height=bdh+'px';
       this.waitifrm.style.width=this.divobj.style.width=bdw+'px';  
       this.waitifrm.style.display=this.divobj.style.display="block";
    };   
    this.hide=function()   
    {   
        this.waitifrm.style.display=this.divobj.style.display="none";   
    };   
  }
  
  wnd.ciuly_progress_visible = false;
  wnd.ciuly_progress_closing = false;

  wnd.ciuly_progress_show = function(){
    if (wnd.ciuly_progress_visible)
      return;
    wnd.ciuly_progress_visible = true;
    wnd.ciuly_progress_closing = false;
    if (!wnd.ciuly_progress_div){
      wnd.ciuly_progress_div = document.createElement('div');
      document.body.appendChild(wnd.ciuly_progress_div);
      wnd.ciuly_progress_div.id = "ciuly_div_script_progress";

      var a = new ciuly_progress_maskpanel();
      wnd.ciuly_progress_mask = a;
    }

    wnd.ciuly_progress_div.setAttribute("style", "z-index:101;position:absolute;left:100px;top:"+((wnd.ciuly_is_ie ? document.body.scrollTop : window.pageYOffset)+100)+"px;background-color:#D56100;color:#FFF;border:solid 2px #000;padding:10px;display:none;");
    wnd.ciuly_progress_div.innerHTML = wnd.ciuly_progress_div.innerHTML + "<h4>Script Progress</h4>";
    wnd.ciuly_progress_mask.show();
    document.getElementById("ciuly_div_script_progress").style.display="block";
  }

  wnd.ciuly_progress_log = function(message){
    if (!wnd.ciuly_progress_visible)
      wnd.ciuly_progress_show();
    wnd.ciuly_progress_div.innerHTML = wnd.ciuly_progress_div.innerHTML + message +"<br>";
  }

  wnd.ciuly_progress_hide = function(){
    wnd.ciuly_progress_closing = true;
    wnd.ciuly_progress_log("Closing this window in 3 seconds.");
    wnd.ciuly_execLater("ciuly_progress_hide_later()", 3);
  }

  wnd.ciuly_progress_hide_later = function(){
    if (!wnd.ciuly_progress_closing)
      return;
    document.getElementById("ciuly_div_script_progress").style.display="none";
    wnd.ciuly_progress_mask.hide();
    wnd.ciuly_progress_visible = false;
    wnd.ciuly_progress_closing = false;
  }

})(window.wrappedJSObject || window);