﻿//**********************************************
// JavaScript common functions library         *
//**********************************************


// common functions
//**********************************************
//get object by id
function $(id)
{
    if (typeof id == 'string')
        return document.getElementById(id);
    else
        return id;
}
//get object by id
function $N(name)
{
    if (typeof name == 'string')
        return document.getElementsByTagName(name);
    else
        return null;
}
//get body object
function $B()
{
    if(document.documentElement)
        return document.documentElement;
    else if(document.body)
        return document.body;
    else return null;
}
//return false if id is not an object
function $Null(el)
{
    return (el == undefined || el==null || el=="");
}
//get offset Left position
function $X(name)
{
    var el=$(name);
	if(el)
	{
		var off=el.offsetLeft;
		var pObj=el.offsetParent;
		if(pObj) off+=$X(pObj);
		return off;
	}
	else return 0;
}
//get offset Top position
function $Y(name)
{
    var el=$(name);
	if(el)
	{
		var off=el.offsetTop;
		var pObj=el.offsetParent;
		if(pObj) off+=$Y(pObj);
		return off;
	}
	else return 0;
}
//get object's width
function $W(name)
{
    var el=$(name);
	if(el)
		return el.clientWidth;
	else return 0;
}
//get object's height
function $H(name)
{
    var el=$(name);
	if(el)
		return el.clientHeight;
	else return 0;
}
//get value from the arguments string (a=1&b=2)
function $Arg(name,args)
{
    var regex = new RegExp("[?&]*" + name + "=([^&#]*)");
    var results = regex.exec(args);
    if(results == null) return "";
    else return results[1];
}

//attaches events and functions
function $AddEvt(obj,evt,func)
{
	if (document.addEventListener)
		obj.addEventListener(evt,func,false);
	else if (document.attachEvent) 
		obj.attachEvent("on" + evt,func);
	else
		eval(obj+".on" + evt + "=" + func);
}
//set or get cookie
function $Cookie(n,v,d)
{
    if(n && v)
    {//set cookie
        var expires;
        if(d)
        {
            var date = new Date();
            date.setTime(date.getTime()+(d*24*60*60*1000));
            expires = "; expires="+date.toGMTString();
        }
        else expires = "";
        document.cookie = n+"="+v+expires+"; path=/";
    }
    else if(n)
    {//get cookie
        n = n + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++)
        {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(n) == 0)
                return c.substring(n.length,c.length);
        }
    }
    return null;
}
//**********************************************


//Effects funcs
//**********************************************
var fx = {
    version: '1.0.1',
    interval: 5,
    percent: 10,
    slideX: function(name,x,f)
    {
        this.slide(name,x,true,f);
    },
    slideY: function(name,y,f)
    {
        this.slide(name,y,false,f);
    },
    slide: function(name,n,isX,f)
    {
        var el=$(name);
        if(!$Null(el))
        {
            var curN=(isX)?$X(name):$Y(name);
            el.style.position="absolute";
            
            var stepN=Math.round(n*this.percent/100);
            if(Math.abs(stepN)<1) {stepN=(n>0)?1:-1;};
            if(isX)el.style.left=(curN+stepN) + "px";
            else el.style.top=(curN+stepN) + "px";
            n=n-stepN;
            
            if(Math.abs(n)>0)
                {setTimeout("fx.slide('" + name + "'," + n + "," + isX + ",\"" + f + "\")",this.interval);}
            else if(!$Null(f)) eval(f);
        }
    },
    
    dimm: function(name,s,e,f)
    {
        var el=$(name);
        if(!$Null(el))
        {
            var n=e-s;//n>0 means fade in
            var stepN=(n>0)?10:-20;
            var o=s+stepN;
            //normalize opacity 0<0<100
            if(o>100) o=100;if(o<0) o=0;
            if(o>100) o=100;if(o<0) o=0;
            this.alfa(name,o);
            
            if(n>0)el.style.display="block";//show if fade in
            
            //$("out").innerHTML+="name=" + name + ", op=" + o + ", step=" + stepN + ", s=" + s + ", e=" + e + "<br>";
            
            if((n>0 && o<e) || (n<0 && o>e))
            {
                setTimeout("fx.dimm(\"" + name + "\"," + o + "," + e + ",\"" + f + "\")",this.interval);
            }
            else 
            {
                if(n<0 && o<=0)el.style.display="none";
                if(!$Null(f))eval(f);
            }
        }
        else if(!$Null(f)) eval(f);
    },
    alfa: function(name,val)
    {
        var el=$(name);
        if(!$Null(el))
        {
            if(document.all)
		        el.style.filter='alpha(opacity=' + val + ')';
	        else
		        el.style.opacity=val/100;
        }
    },
    hide: function(name,f)
    {
        var el=$(name);
        if(!$Null(el))
        {
            var curN=$H(name);
            el.style.overflow="hidden";
            el.style.paddingTop="0px";
            el.style.paddingBottom="0px";
            
            var stepN=Math.round(curN*this.percent/100);
            if(stepN<1) {stepN=1;};
            if(curN-stepN>0)
                el.style.height=(curN-stepN) + "px";
            else
                el.style.height="0px";
                
            //$("out").innerHTML+="H=" + curN + ", step=" + stepN + "<br>";
            //alert("curN=" + curN + ", stepN=" + stepN)
            if(curN>0)
                {setTimeout("fx.hide(\"" + name + "\",\"" + f + "\")",this.interval);}
            else
            {
                el.style.display="none";
                if(!$Null(f)) eval(f);
            }
        }
    },
    show: function(name,f,n)
    {
        var el=$(name);
        if(!$Null(el))
        {
            if($Null(n))
            {//get original height
                var pos=el.style.position,vis=el.style.visibility;
                el.style.position="absolute";el.style.visibility="hidden";el.style.display="block";el.style.height="auto";
                n=$H(name);
                el.style.display="block";el.style.visibility=vis;el.style.position=pos;
                el.style.height="0";
            }
            var curN=$H(name);
            el.style.display="block";
            el.style.overflow="hidden";
            
            var stepN=Math.round(n*this.percent/100);
            if(stepN<1) {stepN=1;};
            el.style.height=(curN+stepN) + "px";
            n=n-stepN;
            //alert(curN+stepN)
            if(n>0)
                {setTimeout("fx.show(\"" + name + "\",\"" + f + "\"," + n + ")",this.interval);}
            else
                if(!$Null(f)) eval(f);
        }
    }
};
//**********************************************

//Callback object
//**********************************************
function cbo()
{
  this.req = this.GetHttpObject();
};
cbo.prototype.GetHttpObject = function()
{ 
    var req = false;
    if(window.XMLHttpRequest) //Mozilla
    {
        req = new XMLHttpRequest();
        if(req.overrideMimeType) req.overrideMimeType("text/xml");
    }
    else if(window.ActiveXObject)
        {
	        try{req = new ActiveXObject("Msxml2.XMLHTTP");}
	        catch(e01)
	        {
		        try{req = new ActiveXObject("Microsoft.XMLHTTP");}
		        catch(e02){};
	        }
        }
    return req;
};
cbo.prototype.DoCallBack = function(eventTarget, eventArgument)
{
  var theData = '';
  var theform = document.forms[0];
  var thePage = window.location.pathname + window.location.search;
  var eName = '';
 
  theData  = '__EVENTTARGET='  + escape(eventTarget.split("$").join(":")) + '&';
  theData += '__EVENTARGUMENT=' + eventArgument + '&';
  theData += '__VIEWSTATE=' + escape(theform.__VIEWSTATE.value).replace(new RegExp('\\+', 'g'), '%2b') + '&';
  theData += 'IsCallBack=true&';
  for( var i=0; i<theform.elements.length; i++ )
  {
    eName = theform.elements[i].name;
    if( eName && eName != '')
    {
      if( eName == '__EVENTTARGET' || eName == '__EVENTARGUMENT' || eName == '__VIEWSTATE')
      {
        // Do Nothing
      }
      else
      {
        //theData = theData + escape(eName.split("$").join(":")) + '=' + theform.elements[i].value;
        //if( i != theform.elements.length - 1 ) theData = theData + '&';
        theData = theData + this.GetFrmData(theform.name)
      }
    }
  }
  if( this.req )
  {
    if( this.req.readyState == 4 || this.req.readyState == 0 )
    {
      var oThis = this;
      this.req.open('POST', thePage, true);
      this.req.onreadystatechange = function(){ oThis.ReadyStateChange(); };
      this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      this.req.send(theData);
    }
  }
};
cbo.prototype.FetchUrl = function(url,frm)
{
  var data = "cache=" + new Date();
  var f = document.forms[frm];
  if(f) data=this.GetFrmData(frm);
 
  if(this.req)
  {
    if(this.req.readyState == 4 || this.req.readyState == 0)
    {
      var oThis = this;
      this.req.open('POST', url, true);
      this.req.onreadystatechange = function(){ oThis.ReadyStateChange(); };
      this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
      if(!document.all)//fix for firefox
        this.req.overrideMimeType('text/html');
      this.req.send(data);
    }
  }
};
cbo.prototype.GetFrmData = function(frmId) 
{
    var f = document.forms[frmId];
    var str = ""; 
    for(var i = 0;i < f.elements.length;i++) 
    {
       var el=f.elements[i];
       switch(el.type) 
       {
           case "password":
           case "hidden":
           case "textarea":
           case "text":
                str += escape(el.name.split("$").join(":")) + "=" + escape(el.value).replace(new RegExp('\\+', 'g'), '%2b'); 
                break; 
           case "radio":
                if(el.checked)
                    str += escape(el.name.split("$").join(":")) + "=" + escape(el.value); 
                break; 
           case "select-one": 
                if(el.selectedIndex>=0)
                    str += escape(el.name.split("$").join(":")) + "=" + escape(el.options[el.selectedIndex].value); 
                break; 
       }
       if( i < f.elements.length ) str+='&';
    }
    str += 'IsCallback=true';
    return str; 
};
cbo.prototype.AbortCallBack = function()
{
  if( this.req )this.req.abort();
};
cbo.prototype.ReadyStateChange = function()
{
  if(this.req)
  {
      if( this.req.readyState == 1 ){this.OnLoading();}
      else if( this.req.readyState == 2 ){this.OnLoaded();}
      else if( this.req.readyState == 3 ){this.OnInteractive();}
      else if( this.req.readyState == 4 )
      {
        if( this.req.status == 0 ) 
            this.OnAbort();
        else if( this.req.status == 200 && this.req.statusText == "OK" )
          this.OnComplete(this.req.responseText,this.req.responseXML);
        else
          this.OnError(this.req.status,this.req.statusText, this.req.responseText);   
      }
  }
};
cbo.prototype.OnLoading = function(){};
cbo.prototype.OnLoaded = function(){};
cbo.prototype.OnInteractive = function(){};
cbo.prototype.OnComplete = function(responseText, responseXml){};
cbo.prototype.OnAbort = function(){};
cbo.prototype.OnError = function(status, statusText){};
//**********************************************





//Float Object object
//**********************************************
var fly = {
    version: '1.0.0',
    startPos: -1,
    lastPos: -1,
    isBusy:false,
    objectid:'',
    Start: function(name)
    {
        this.objectid=name;
        $AddEvt(window,"load",fly.Monitor);
    },
    Monitor:function()
    {
        if(!this.isBusy && !$Null(this.objectid))
        {
            if(this.startPos<0)this.startPos=$Y(this.objectid);
            if(this.lastPos<0)this.lastPos=this.startPos;
            this.isBusy=true;
            fly.Move();
        }
        var t=setTimeout("fly.Monitor()",1000);
    },
    Move:function()
    {
        var top=document.documentElement.scrollTop;
        var moveBy=top-this.lastPos + 20;
        if((moveBy>0 && top>this.startPos) || (moveBy<0 && this.lastPos>this.startPos))
        {
            if(moveBy<0 && (this.lastPos + moveBy)<this.startPos)moveBy=this.startPos-this.lastPos;
            fx.slideY(this.objectid,moveBy,"fly.isBusy=false;fly.lastPos+=" + moveBy + ";");
        }
        else this.isBusy=false;
    }
};
               
               
//Popup object
//**********************************************
var win={
    version: '1.0',
    url:'',
    frm:'',
    w:0,h:0,
    bg:null,
    pop:null,
    data:null,
    waitMsg:'please wait while loading',
    waitImg:'<IMG SRC="Img/wait.gif" width=30 height=30 />',
    Show: function(url,w,h)
    {
        this.url=url;
        this.w=w;this.h=h;
        
        this.showBg();
    },
    Hide: function()
    {
        fx.dimm(win.pop.id,90,0,"fx.dimm(win.bg.id,50,0,null)");
    },
    showWin:function()
    {
        if(!this.pop)
        {
            this.pop=document.createElement("div");
            this.pop.style.cssText="position:absolute;overflow:auto;top:0px;display:none;";
            this.pop.id="myPopUp";
            this.pop.innerHTML='<div id="myPopUpHead"><a href="#close" onclick="win.Hide()">Close</a></div><div id="myPopUpBody"></div>';
            document.body.appendChild(this.pop);
            
            this.data=$("myPopUpBody");
        }
        this.showWaitMsg();
        
        this.pop.style.top=(($B().clientHeight-this.h-50)/2+$B().scrollTop) + "px";
        this.pop.style.left=($B().clientWidth-this.w)/2 + "px";
        this.pop.style.width=this.w + "px";
        this.data.style.height=this.h + "px";
        //this.pop.style.display="block";
        fx.dimm(this.pop.id,30,100,"");
        
        this.loadUrl();
    },
    showWaitMsg: function()
    {
        this.data.innerHTML="<div style='padding-top:50px;text-align:center;'>"
                + "<div>" + this.waitImg + "</div>"
                + "<div style='padding-top:5px;'>" + this.waitMsg + "</div>" 
                + "</div>"; 
    },
    showBg:function()
    {
        if(!this.bg)
        {
            this.bg=document.createElement("div");
            this.bg.style.cssText="position:absolute;top:0px;left:0px;";
            this.bg.id="myPopUpBg";
            document.body.appendChild(this.bg);
        }
        this.bg.style.width=document.body.offsetWidth + "px";
        this.bg.style.height=((document.body.offsetHeight>$B().clientHeight)?document.body.offsetHeight:$B().clientHeight) + "px";
        //this.bg.style.display="block";
        
        fx.dimm(this.bg.id,0,50,"win.showWin()");
    },
    loadUrl: function()
    {
        var req=new cbo();
        req.OnComplete = function(rT,rX){win.loadComplete(rT)};
        req.FetchUrl(this.url,this.frm);
    },
    loadComplete: function(rT,rX)
    {
        this.data.innerHTML=rT;
    }
};             


var schem={
    version: '1.0',
    Init:function()
    {
        var style=$Cookie("schem");
        this.Activate(style);
    },
    Activate:function(title)
    {
        for(var i=0;(a = document.getElementsByTagName("link")[i]); i++)
        {
            if(a.getAttribute("rel").indexOf("alt")>-1 && a.getAttribute("title"))
            {
                a.disabled = true;
                if(a.getAttribute("title") == title)
                {
                    a.disabled = false;
                    $Cookie("schem",title,365);
                }
            }
        }
        if(title=="none")$Cookie("schem","none",365);
    }
};


//Smooth page scroll object
//**********************************************
var ss = {
    steps: 25,
    Init: function() //set event handler to all local ancor links
    {
        // Get a list of all links in the page
        var links = $N('a');
    
        // Walk through the list
        for (var i=0;i<links.length;i++) 
        {
            var lnk = links[i];
            if ((lnk.href && lnk.href.indexOf('#') != -1) && ( (lnk.pathname == location.pathname) ||
	            ('/'+lnk.pathname == location.pathname) ) && (lnk.search == location.search)) 
	        {
                // If the link is internal to the page (begins in #)
                // then attach the smoothScroll function as an onclick
                // event handler
                $AddEvt(lnk,'click',ss.smoothScroll);
            }
        }
    },
    smoothScroll: function(e) 
    {
        // This is an event handler; get the clicked on element,
        // in a cross-browser fashion
        target=(window.event)?window.event.srcElement:e.target;
  
        // Make sure that the target is an element, not a text node
        // within an element
        if (target.nodeType == 3) 
        {
            target = target.parentNode;
        }
  
        // Paranoia; check this is an A tag
        if (target.nodeName.toLowerCase() != 'a') return;
  
        // Find the <a name> tag corresponding to this href
        // First strip off the hash (first character)
        var anchor = target.hash.substr(1);
        var dest = ss.findAncor(anchor);
  
        // If we didn't find a destination, give up and let the browser do
        // its thing
        if (!dest) return true;
  
        // Find the destination's position
        var destx = $X(dest); 
        var desty = $Y(dest);
  
        // Stop any current scrolling
        clearInterval(ss.interval);
  
        var cypos = ss.getCurrentYPos();
  
        var ss_stepsize = parseInt((desty-cypos)/ss.steps);
        ss.interval = setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
  
        // And stop the actual click happening
        ss.cancelBubble(e);
    },
    cancelBubble: function(e) 
    {
        if (window.event) 
        {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }
        if (e && e.preventDefault && e.stopPropagation) 
        {
            e.preventDefault();
            e.stopPropagation();
        }
    },
    findAncor: function(name) 
    {
        var links = $N('a');
        for (var i=0;i<links.length;i++) 
        {
            var lnk = links[i];
            if (lnk.name && lnk.name == name) 
                return lnk;
        }
        return null;
    },
    scrollWindow: function(scramount,dest,anchor)
    {
        var wascypos = ss.getCurrentYPos();
        var isAbove = (wascypos < dest);
        window.scrollTo(0,wascypos + scramount);
        var iscypos = ss.getCurrentYPos();
        var isAboveNow = (iscypos < dest);
        if ((isAbove != isAboveNow) || (wascypos == iscypos)) 
        {
            // if we've just scrolled past the destination, or
            // we haven't moved from the last scroll (i.e., we're at the
            // bottom of the page) then scroll exactly to the link
            window.scrollTo(0,dest);
            // cancel the repeating timer
            clearInterval(ss.interval);
            // and jump to the link directly so the URL's right
            location.hash = anchor;
        }
    },
    getCurrentYPos: function() 
    {
        if (document.body && document.body.scrollTop)
            return document.body.scrollTop;
        if (document.documentElement && document.documentElement.scrollTop)
            return document.documentElement.scrollTop;
        if (window.pageYOffset)
            return window.pageYOffset;
        return 0;
    }
};

$AddEvt(window,"load",ss.Init);















//**********************************************
// Web site script on page                     *
//**********************************************


// Master page
//**********************************************

schem.Init();

function Contact()
{
    win.Show('contactform.aspx',560,360);
}
function AntiSpam(to,dom)
{
    document.write("<a href='" + "mail" + "to:" + to + "@" + dom + "'>" + to + "@" + dom + "</a>");
}


// Projects slideshow
//**********************************************
var lastProject=0;
var timerProjectSlideshow=6; //seconds
function ShowProject()
{
    //var num=Math.floor(Math.random()*maxProjects) + 1;
    var maxProjects=document.getElementById("projHol").getElementsByTagName("span").length;
    var num=maxProjects-lastProject;
    
    //if(num!=lastProject)
    {
        fx.dimm("projHol",100,0,"SetNewProject(" + num + ")");
    }
    setTimeout("ShowProject()",timerProjectSlideshow*1000);
}
function SetNewProject(num)
{
    var maxProjects=document.getElementById("projHol").getElementsByTagName("span").length;
    //show new item
    var holNew=document.getElementById("proj" + num);
    if(holNew) 
    {
        holNew.style.display="block";
        
        lastProject++;
        if(lastProject>=maxProjects) lastProject=0;
        
        //hide old item
        var numOld=num+1;
        if(num==maxProjects) numOld=1;
        var holOld=document.getElementById("proj" + numOld);
        if(holOld) holOld.style.display="none";
        
        fx.dimm("projHol",0,100,"");
    }
}