/*
    Dimmer class
    Copyright (C) 2009  Lestat, Special for Powertracker.org

	License type: GPL v3
	
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>
       
			   -------------Code Parts--------
AttachEvent, MyAttachEvent, MyFireEvent - Copyrighted by Gavin Kistner, gavin@refinery.com. License: http://phrogz.net/JS/_ReuseLicense.txt

*/
function getScrollPos(){
if (window.pageYOffset){
return {y:window.pageYOffset, x:window.pageXOffset};
}
if(document.documentElement && document.documentElement.scrollTop){
return {y:document.documentElement.scrollTop,
x:document.documentElement.scrollLeft};
}
if(document.body){
return {y:document.body.scrollTop, x:document.body.scrollLeft};
}
return {x:0, y:0};
}
var curr_dimm="";
var dimmer = {
   dimm:"",
   dimm_c:"",
   dim_content:"",
   curr_dimm:"",
   handler:true,
  init:function(){     //  Param 1:    Close all dimmer objects when click on dimmer background
     if (arguments.length == 1){
      this.handler=arguments[0];
     }
     //========Create dimmer
   	 this.dimm=document.createElement("div");
	 this.dimm.id="dimm";
	 this.dimm.innerHTML="&nbsp;";
	 this.dimm.className=(is_ie)?"blockScreenIE":"blockScreen";
     this.dimm.style.height=document.body.scrollHeight + "px";  
	 document.body.insertBefore(this.dimm,null);
	 AttachEvent(this.dimm,'click',this.close_dimm_click,false);
	 //=========Create dimm container
	 this.dimm_c=document.createElement("div");	
	 this.dimm_c.id="dimm_c";
	 this.dimm_c.className="blockScreenWindow";
	 document.body.insertBefore(this.dimm_c,null);
	 this.dim_content=new Array();
  },
 //============System functions 
  dimm_page:function(){ 
     this.dimm.style.height=(document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight + 'px' 
	 this.dimm.style.display="block";
 },
  close_dimm:function(){
      var dimm=document.getElementById("dimm");
	  var dimm_c=document.getElementById("dimm_c");
	  dimm.style.display="none";
	  dimm_c.style.display="none";
	  dimmer.dim_content[curr_dimm].style.display="none";
 },
   close_dimm_click:function(){
    if(this.handler) this.close_dimm();
   },

   showWindow:function(itemname){
    curr_dimm=itemname;
    var dimc=this.dimm_c;
	var dimo=this.dim_content[itemname];
	var dimo_w=getStyle(dimo,"width").replace(/(px|auto)/,"");
	var dimo_h=getStyle(dimo,"height").replace(/(px|auto)/,"");

	if (dimo_h=="" || dimo_h==0) dimo_h=0; else dimc.style.height = dimo_h+"px";
	if (dimo_w=="" || dimo_w==0) dimo_w=0; else dimc.style.width  = dimo_w+"px";
    var cl =(document.body.clientWidth / 2) - (dimo_w / 2);
    var ct = (document.body.clientHeight / 2) - (dimo_h / 2);


     if (arguments.length == 2){
      if (arguments[1]=="mouse") {
         ct=(is_ie)?(mouseY+getScrollPos().y):mouseY;
       } else {
         ct=arguments[1]; 
       }
     }
     if (arguments.length == 3){
      if (arguments[2]=="mouse"){
       cl=mouseX;
      } else {
       cl=arguments[2];
      }
     }
    dimc.style.left=cl+"px";
    dimc.style.top=ct+"px";

   // dimc.style.width=dimo_h;
   // dimc.style.height=dimo_w;
      dimo.style.display="block";
	  dimc.style.display="block";
		 
},
   addItem:function (name,item){
      this.dim_content[name]=item;
   	  item.style.display="none";
	  this.dimm_c.appendChild(item);
   }
 //============================

}

dimmer.dialog={
   showInfo: function(caption,text,handler){
      dimmer.handler=handler;
      dimmer.dimm_page();
      document.getElementById("dlg_dimmer_cap").innerHTML=caption;
      document.getElementById("dlg_dimmer_msg").innerHTML=text;
     if (arguments.length == 4){
       dimmer.showWindow("dlg",arguments[3]); 
     }
     if (arguments.length == 5){
       dimmer.showWindow("dlg",arguments[4]);
     }
     if (arguments.length < 4){
      dimmer.showWindow("dlg");
     }
   }
}


dimmer.warn_dialog={
   showInfo: function(type,caption,obj,uid){
      dimmer.handler=true;
      dimmer.dimm_page();
      document.getElementById("dlg1_dimmer_cap").innerHTML=caption;
      document.getElementById("dlg1_dimmer_typ").innerHTML=type;
      document.getElementById("dlg1_dimmer_uid").innerHTML=uid;
       dimmer.showWindow("dlg1","mouse"); 
   }
}

dimmer.warn_info={
   show: function(caption,htmlText){
      dimmer.handler=true;
      dimmer.dimm_page();
      document.getElementById("dlg2_dimmer_cap").innerHTML=caption;
      document.getElementById("dlg2_dimmer_msg").innerHTML=htmlText;
       dimmer.showWindow("dlg2","mouse");
   }
}

dimmer.select={
   arr:"",
   show: function(list,callback){
     var obj=document.getElementById("dlg3_btn_list");
     arr=new Array();
     obj.innerHTML="";
     var arr=list.split(';;');
      dimmer.handler=true;
      dimmer.dimm_page();
      for (i=0;i<arr.length;i++){
       var el=document.createElement("div");
       var data=arr[i].split(',');
       el.className="dlg3button";
       el.innerHTML=data[0];
       el.href=data[1];
       AttachEvent(el,"click",callback,false);
       obj.appendChild(el);
      }
       dimmer.showWindow("dlg3","mouse");
   }
}
//==============Helper functions

function getStyle(el, style) {
   if(!document.getElementById) return;

     var value = el.style[style];

    if(!value)
        if(document.defaultView)
            value = document.defaultView.
                 getComputedStyle(el, "").getPropertyValue(style);

        else if(el.currentStyle)
            value = el.currentStyle[style];

     return value;
}


function getTarget(e){
 var targ;
   if(!e) var e=window.event;
    if (e.target) targ=e.target;
    else if (e.srcElement) targ=e.srcElement;
    if (targ.nodeType ==3) targ = targ.parentNode;
  return targ;
}

function setStyle(style, value) {
    obj.style[style] = value;
}

function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 

function MyAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

function navigate(url){
   document.location.assign(url);

}
