var Tectavia;
if(Tectavia == undefined) {
  Tectavia = {};
}

Tectavia.util = function() {

	this.RoundedCheck = function RoundedCheck(){
		if(!document.getElementById || !document.createElement)
			return(false);
		var b=navigator.userAgent.toLowerCase();
		if(b.indexOf("msie 5")>0 && b.indexOf("opera")==-1)
			return(false);
			sCss = '.rtop,.rbottom{display:block} .rtop *,.rbottom *{display:block;height: 1px;overflow: hidden} .r1{margin: 0 5px} .r2{margin: 0 3px} .r3{margin: 0 2px} .r4{margin: 0 1px;height: 2px} .rs1{margin: 0 2px} .rs2{margin: 0 1px}';
			this.addCss(sCss);
		return(true);
	}

	this.Rounded = function Rounded(selector,bk,color,size){
		var i;
		var v=this.getElementsBySelector(selector);
		var l=v.length;
		for(i=0;i<l;i++){
			this.AddTop(v[i],bk,color,size);
			this.AddBottom(v[i],bk,color,size);
		}
	}

	this.RoundedTop = function RoundedTop(selector,bk,color,size){
		var i;
		var v=this.getElementsBySelector(selector);
		for(i=0;i<v.length;i++)
			this.AddTop(v[i],bk,color,size);
	}

	this.RoundedBottom = function RoundedBottom(selector,bk,color,size){
		var i;
		var v=this.getElementsBySelector(selector);
		for(i=0;i<v.length;i++)
			this.AddBottom(v[i],bk,color,size);
	}

	this.AddTop = function AddTop(el,bk,color,size){
		var i;
		var d=document.createElement("b");
		var cn="r";
		var lim=4;
		if(size && size=="small"){ cn="rs"; lim=2}
		d.className="rtop";
		d.style.backgroundColor=bk;
		for(i=1;i<=lim;i++){
			var x=document.createElement("b");
			x.className=cn + i;
			x.style.backgroundColor=color;
			d.appendChild(x);
			}
		el.insertBefore(d,el.firstChild);
	}

	this.AddBottom = function AddBottom(el,bk,color,size){
		var i;
		var d=document.createElement("b");
		var cn="r";
		var lim=4;
		if(size && size=="small"){ cn="rs"; lim=2}
		d.className="rbottom";
		d.style.backgroundColor=bk;
		for(i=lim;i>0;i--){
			var x=document.createElement("b");
			x.className=cn + i;
			x.style.backgroundColor=color;
			d.appendChild(x);
			}
		el.appendChild(d,el.firstChild);
	}

	this.getElementsBySelector = function getElementsBySelector(selector){
		var i;
		var s=[];
		var selid="";
		var selclass="";
		var tag=selector;
		var objlist=[];
		if(selector.indexOf(" ")>0){  //descendant selector like "tag#id tag"
			s=selector.split(" ");
			var fs=s[0].split("#");
			if(fs.length==1) return(objlist);
			return(document.getElementById(fs[1]).getElementsByTagName(s[1]));
			}
		if(selector.indexOf("#")>0){ //id selector like "tag#id"
			s=selector.split("#");
			tag=s[0];
			selid=s[1];
			}
		if(selid!=""){
			objlist.push(document.getElementById(selid));
			return(objlist);
			}
		if(selector.indexOf(".")>0){  //class selector like "tag.class"
			s=selector.split(".");
			tag=s[0];
			selclass=s[1];
			}
		var v=document.getElementsByTagName(tag);  // tag selector like "tag"
		if(selclass=="")
			return(v);
		for(i=0;i<v.length;i++){
			if(v[i].className==selclass){
				objlist.push(v[i]);
				}
			}
		return(objlist);
	}

	this.addCss = function(sCss) {
		var oStyle = document.createElement('style');
		var oHead = document.getElementsByTagName('head')[0];

		oStyle.type = 'text/css';    
		if(oStyle.styleSheet) {
			oStyle.styleSheet.cssText = sCss;
		} else {
			  var oNode = document.createTextNode(sCss);
			  oStyle.appendChild(oNode);
		}
		oHead.appendChild(oStyle);
	}

	this.hasClassName = function(element, className) {
		var elementClassName = element.className;

		return (elementClassName.length > 0 && (elementClassName == className ||
		  new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)));
	}

	this.addClassName = function(element, className) {
		if (!this.hasClassName(element, className))
		  element.className += (element.className ? ' ' : '') + className;
		return element;
	}

	this.removeClassName = function(element, className) {
		var newClass = this.clean(element.className.replace(new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' '));
		element.className = newClass;
		return element;
	}

	this.clean = function(string) {
		return string.replace(/^\s+/, '').replace(/\s+$/, '');
	};

	this.jumpto = function(config) {
		if (config && config.url)	{
			document.location.href= config.url;
		}
		return false;
	}

}
Tectavia.util = new Tectavia.util;

Tectavia.modal = function (config) {
  
  var ua = navigator.userAgent.toUpperCase();
  this.isIE =(ua.indexOf('MSIE') >= 0) ? true : false,
  this.isIE7=(ua.indexOf("MSIE 7.0") >=0),
  this.isIE8=(ua.indexOf("MSIE 8.0") >=0),
  this.isIEx=(this.isIE7 || this.isIE8 || this.isIE);
  
  var oModal=this;

  this.id=config.id;
  this.modalBgc = config.modalBgc;

  this.opts=null;
  this.rt={};
  this.iconPath="icons/";
 
 
  this.show=function(config) {
  
    if (document.getElementById('tectavia_contact_button')) {
	    Tectavia.util.addClassName(document.getElementById('tectavia_contact_button'), 'tectavia_contact_button_hidden');
    }
    Tectavia.util.addClassName(document.getElementsByTagName('html')[0], 'tectavia_contact_button_on');
	
	if(!document.getElementById(this.id)) {
      var e = document.createElement("div");
      e.id = "cnt$"+this.id;
      e.innerHTML = this.render(config);
      document.body.insertBefore(e, document.body.childNodes[0]);
		if(!Tectavia.util.RoundedCheck()) return;
		Tectavia.util.Rounded("div#"+this.id,"transparent",this.modalBgc);
    }

    if(!this.rt.win) {
      this.rt.win = document.getElementById(this.id);
      this.rt.frm = document.getElementById("frm$"+this.id);
      this.rt.ttl = document.getElementById("ttl$"+this.id);
    }
    
    if(config.overlay==true) this.showOverlay();
    
    this.setSize(config.width+"px", config.height+"px", config.center);
	this.zIndex = 10000+2;
    this.rt.win.style.zIndex = this.zIndex;
    this.rt.win.style.display="block";
	this.rt.ttl.innerHTML = config.title;
    
    var fn = function() {
          //oModal.rt.ttl.innerHTML = oModal.rt.frm.contentWindow.document.title;
          //oModal.rt.frm.contentWindow.openerWin = opt.openerWin ? opt.openerWin : window;
          //oModal.rt.frm.contentWindow.opener = opt.openerWin ? opt.openerWin : window;
          //oModal.rt.frm.contentWindow.options = opt.options?opt.options:{};
          //oModal.rt.frm.contentWindow.close=function() {
            //oModal.close();
          //};          
          //if (typeof(oModal.rt.frm.contentWindow.bodyOnLoad) != "undefined") oModal.rt.frm.contentWindow.bodyOnLoad();
        } ;
    
    if(this.rt.frm.attachEvent) this.rt.frm.attachEvent("onload", fn);
    if(this.rt.frm.addEventListener) this.rt.frm.addEventListener("load", fn, true);
    
    
    setTimeout(function() {oModal.rt.frm.src = config.url;}, 0);
    
  };
  
  this.close = function() {
    var d = document.getElementById("cnt$"+this.id);    
    if(d) {
      //if(this.rt.frm.contentWindow.bodyOnUnload) this.rt.frm.contentWindow.bodyOnUnload();
      d.parentNode.removeChild(d);
    }
	
	Tectavia.util.removeClassName(document.getElementsByTagName('html')[0], 'tectavia_contact_button_on');
	if (document.getElementById('tectavia_contact_button')) {
		Tectavia.util.removeClassName(document.getElementById('tectavia_contact_button'), 'tectavia_contact_button_hidden');
	} 
    this.hideOverlay();
  };
  
  this.hide=function() {
    if(!this.rt.win) {
      this.rt.win = document.getElementById(this.id);
    }
    this.rt.win.style.display="none";
  }; 
	this.showOverlay=function() {
    
    var ov;

    if(!document.getElementById("ovr$"+this.id)) {
      ov = document.createElement("div");
      ov.id = "ovr$"+this.id;
      ov.style.display="none";
      ov.style.position=((this.isIE) ? "absolute" : "fixed");
      ov.style.backgroundColor="#ffffff";
      ov.style.filter = "alpha(opacity=35)";
      ov.style.mozOpacity = "0.4";
      ov.style.opacity = "0.4";
      ov.style.Opacity = "0.4";
      document.body.insertBefore(ov, document.body.childNodes[0]);
    }
    
    var cl = this.clientSize();
      
    if(this.isIEx) {
        
      var db=document.body, de=document.documentElement, w, h;
      w=Math.min(
            Math.max(db.scrollWidth, de.scrollWidth),
            Math.max(db.offsetWidth, de.offsetWidth)
          );

      var mf=((de.scrollHeight<de.offsetHeight) || (db.scrollHeight<db.offsetHeight))?Math.min:Math.max;  
      h=mf(
            Math.max(db.scrollHeight, de.scrollHeight),
            Math.max(db.offsetHeight, de.offsetHeight)
          );

      cl.w = Math.max(cl.w, w);
      cl.h = Math.max(cl.h, h);
      
      ov.style.position="absolute";
    }
      
    ov.style.width = cl.w+"px";
    ov.style.height = cl.h+"px";
    ov.style.top="0px";
    ov.style.left="0px";
    ov.style.zIndex = 10000+1;
    ov.style.display="block";
    
  };
  
  this.hideOverlay=function() {
    var ov=document.getElementById("ovr$"+this.id);
    if(ov) ov.style.display="none";
  };
  
  this.setSize=function(w, h, center) {
    this.rt.win.style.width=w;
    this.rt.win.style.height=h;
    this.rt.frm.style.height=parseInt(h, 10)-30 + "px";
    if(center) {
      this.center();
    }
  };
  
  this.center=function() {
    
    var c=this.clientSize();
    var px=parseInt(this.rt.win.style.width, 10), py=parseInt(this.rt.win.style.height, 10);
    px=(isNaN(px)?0:(px>c.w?c.w:px));
    py=(isNaN(py)?0:(py>c.h?c.h:py));
    var p = {x:(c.w-px)/2, y:(c.h-py)/2};
    if(this.isIEx) {
      p.x=p.x+(document.body.scrollLeft||document.documentElement.scrollLeft);
      p.y=p.y+(document.body.scrollTop||document.documentElement.scrollTop);
    }
    this.setPosition(p.x, p.y);
  
  };
  
  this.setPosition=function(x, y) {
    
    this.rt.win.style.top=y+"px";
    this.rt.win.style.left=x+"px";    
    
  };
  
  this.render=function(config) {
    
    var s=[],j=0,ps=(this.isIEx)?"absolute":"fixed",setWidth = (this.isIEx)?"width:100%;":"";
    s[j++] = "<div class='Tectavia-modal-window' style='position:"+ps+";display:none;z-index:9999;background-color:#ffffff;filter:alpha(opacity=25);opacity:0.25;-moz-opacity:0.25;' id=\"dd$"+this.id+"\"></div>";
    s[j++] = "<div unselectable='on' id=\""+this.id+"\" style='position:"+ps+";z-index:100001;bacground-color:green'>";
    s[j++] = "  <div unselectable=\"on\" style=\"cursor:move;height:20px;background-color:"+this.modalBgc+";\" onmousedown=\"Tectavia.oModal._ddMouseDown(event, '"+this.id+"');\"><span  style=\"float:left;margin-top:7px;margin-left:11px;font-size:11px;font-weight:bold;font-family:tahoma,arial,verdana,sans-serif;color:"+config.titleColor+"\" id=\"ttl$"+this.id+"\"></span><img src=\"img/close.png\" onmousedown=\"event.cancelBubble=true;if(event.preventDefault) event.preventDefault();\" onclick=\"Tectavia.oModal.close();\" style='float:right;margin-top:5px;margin-right:5px;cursor:pointer' /></div>";
    s[j++] = "  <div id=\"divfrm$"+this.id+"\" style=\""+setWidth+"padding:5px;background-color:"+this.modalBgc+";\">";
	s[j++] = "    <iframe id=\"frm$"+this.id+"\" style=\"width:100%;\" frameborder='no'></iframe>";
	s[j++] = "  </div>";
    s[j++] = "</div>";
    return s.join("");
  };
  
  this.clientSize=function() {
    return {w:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth, 
            h:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight};
  };
  
  this._ddMouseDown=function(ev, elId) {
    
    var d=document;    
    
    d.onmousemove=function(e) {oModal._startDrag(e?e:ev);}
    d.onmouseup=function(e) {oModal._endDrag(e?e:ev);}
    d.onselectstart=function() { return false;}
    d.onmousedown=function() { return false;}
    d.ondragstart=function() { return false;}
    
    oModal.trgElm = document.getElementById(elId);
    
    oModal.gstElm = document.getElementById("dd$"+elId);
    oModal.gstElm.style.top=oModal.trgElm.style.top;
    oModal.gstElm.style.left=oModal.trgElm.style.left;
    oModal.gstElm.style.width=oModal.trgElm.style.width;
    oModal.gstElm.style.height=oModal.trgElm.style.height;
    oModal.gstElm.style.display="block";
    
    oModal.posDif = {x:ev.clientX-parseInt(oModal.trgElm.style.left, 10),
                       y:ev.clientY-parseInt(oModal.trgElm.style.top, 10)};    


  };
  
  this._startDrag = function(ev) {
    oModal.gstElm.style.left=(ev.clientX-oModal.posDif.x)+"px";
    oModal.gstElm.style.top=(ev.clientY-oModal.posDif.y)+"px";
  };
  
  this._endDrag = function(ev) {
    
    oModal.gstElm.style.display="none";
    
    oModal.trgElm.style.top=oModal.gstElm.style.top;
    oModal.trgElm.style.left=oModal.gstElm.style.left;
    
    document.onmousemove=null;
    document.onmouseup=null;
    document.onmousedown=function() { return true;};
    document.onselectstart=function() { return true;};
    document.onselectstart=function() { return true;};
    
  };
  
};

function openModalWindow(url, width, height, backgroundColor, titel, titelColor)  {
	var id = "ID"+(new Date()).getTime();
	Tectavia.oModal = new Tectavia.modal({id : id, modalBgc : backgroundColor});
	Tectavia.oModal.show({width: width, height:height, overlay:true, center:true,url:url, title : titel, titleColor : titelColor});
}
