function stopB(e)
{
	if (e) {e.stopPropagation(); return e.currentTarget;}
	else {window.event.cancelBubble=true; return window.event.srcElement;}
}

function stopDefault(e)
{
	if (e && e.preventDefault) e.preventDefault();
	else window.event.returnValue = false;
}

function getMouseOutTarget(e)
{
	if (!e) e = window.event;
	return e.relatedTarget || e.toElement;
}

function getChildren(n, tag, cl)
{
	var nodeList = new Array();
	var re;
	if(tag)
	{
		tag = "^"+tag+"$";
		re = new RegExp(tag, "i");
	}
	if(cl)
	{
		cl = "^"+cl+"$";
		reCl = new RegExp(cl, "i");
	}

	for (var i=0; i<n.childNodes.length; i++)
	{
		var node = n.childNodes[i];
		if (node.nodeType == 1 && (!tag || re.test(node.nodeName)) && (!cl || reCl.test(node.className))) nodeList.push(node);
	}
	return nodeList;
}

function getNextSibling(n, tag)
{
	var re; if(tag){ tag = "^"+tag+"$"; re = new RegExp(tag, "i");}
	var s = n.nextSibling;
	while (s)
	{
		if (s.nodeType == 1 && (!tag || re.test(s.nodeName))) 
		{
			return s;
		}
		s = s.nextSibling;
	}
	return null;
}

function getPrevSibling(n, tag)
{
	var re; if(tag){ tag = "^"+tag+"$"; re = new RegExp(tag, "i");}
	var s = n.previousSibling;
	while (s)
	{
		if (s.nodeType == 1 && (!tag || re.test(s.nodeName))) 
		{
			return s;
		}
		s = s.previousSibling;
	}
	return null;
}

function getStyle(n, name)
{
	if (n.style[name]) return n.style[name];
	if (n.currentStyle) return n.currentStyle[name] || "";
	var s= document.defaultView.getComputedStyle(n,null);
	return s.getPropertyValue(name) || s[name];

}

function setCSS(css)
{
//	this.savedCSS = {};
	for (var i in css)
	{
//		this.savedCSS[i] = getStyle(this,i);
		this.style[i] = css[i];
	}

}

function saveCSS()
{
	this.savedCSS = {};
	var a = saveCSS.arguments;
	for (var i=0; i<a.length; i++)
	{
		this.savedCSS[a[i]] = getStyle(this,a[i]);
	}
}

function restoreCSS()
{
	for (var i in this.savedCSS)
	{
		this.style[i] = this.savedCSS[i];
	}
//	this.savedCSS = {};
}

function findPos(o)
{
	var l = t = 0;
	if (o.offsetParent)
	{
		l = o.offsetLeft;
		t = o.offsetTop;
		while (o = o.offsetParent && getStyle(o,"position") != "relative")
		{
			l += o.offsetLeft;
			t += o.offsetTop;
		}
	}
	return [l,t];
}

function findDim(o){return [o.offsetWidth, o.offsetHeight]; }
function pageDim ()
{
//	var h = document.getElementsByTagName("html")[0];
//	return [h.scrollWidth, h.scrollHeight];
//	return [window.outerWidth  || document.body.offsetWidth, window.outerWidth || document.body.offsetHeight];
//	return [h.scrollWidth  || document.body.offsetWidth, h.scrollHeight || document.body.offsetHeight];
	return [document.width  || document.body.offsetWidth, document.height || document.body.offsetHeight];
}

function setOpacity(v)
{
	if (this.filters) this.style.filter = 'alpha(opacity='+v+')'; // IE
	else                    this.style.opacity = v / 100;                // W3C
	return false;
}

function animateImgBg(element)
{
//	this.style.left = this.tL+"px";
	if (element.moveDir ? element.offsetLeft<=element.tL : element.offsetLeft>=element.tL)
	{
		element.style.left = element.tL+"px";
//		element.style.width = element.offsetWidth-1 + "px";
//		if (element.offsetWidth<element.tW) element.style.width = element.tW+"px";
	}
	else
	{
		var l = element.offsetLeft + (element.moveDir ? -6 : 6);
		element.style.left = l + "px";
//		element.style.width = element.offsetWidth+1 + "px";
	}
	if (element.offsetLeft==element.tL && element.offseWidth==element.tW)
	{
		clearTimeout(element.timer);
	}
	else { element.timer = setTimeout(function(){animateImgBg(element);},5); }

	return false;
}

function swapImg(e)
{
	stopB(e);
	this.blur();
	var mySrc = this.getAttribute("src").replace(/_mini/, "");

	var target = getChildren(getPrevSibling(this.parentNode, "div"), "img")[0];
	target.src = mySrc;

	var bg = this.parentNode.bg;
	clearTimeout(bg.timer);
	bg.tL = parseInt(this.offsetLeft)-1;
	bg.tW = parseInt(this.offsetWidth)+2;
	if (bg.offsetLeft==bg.tL && bg.offsetWidth==bg.tW) return false;
	bg.moveDir = bg.offsetLeft>bg.tL;

	animateImgBg(bg);
	return false;
}

function restoreImage(el)
{
	var divPic = getChildren(el,"div", "sitePicture")[0];
	var img = divPic.getElementsByTagName("img")[0];
	var mysrc = img.getAttribute("src").replace(/\d\.jpg$/, "1.jpg");
	img.setAttribute("src",mysrc);

	var bg = getChildren(getChildren(el,"div","thumbs")[0], "div", "miniBg")[0];
	var thumb = getChildren(getChildren(el,"div","thumbs")[0], "img")[0];

	clearTimeout(bg.timer);
	bg.tL = parseInt(thumb.offsetLeft)-1;
	bg.tW = parseInt(thumb.offsetWidth)+2;
	if (bg.offsetLeft==bg.tL && bg.offsetWidth==bg.tW) return false;
	bg.moveDir = bg.offsetLeft>bg.tL;

	animateImgBg(bg);
	return false;
}
function restoreImgIE()
{
	window.event.cancelBubble=true;
	restoreImage (window.event.srcElement);
}

function restoreImgFF(e)
{
	e.stopPropagation();
	var to = e.relatedTarget;
	while(to)
	{
		if (to == this) return false;
		to = to.parentNode;
	}
	return restoreImage(this);
}

function setAbsolute(el)
{
	var pos = findPos(el);
	el.setCSS( { "position":"absolute", "left":pos[0]+"px", "top": pos[1]+"px" });
}

function doClientsPopup(e)
{
	var el = stopB(e);
	el.blur();
	el = el.parentNode;

	clearTimeout(el.timer);
	var pos = findPos(el);
	var dim = findDim(el);
	for (var i=el.parentNode.kids.length; i; i--)
	{
		setAbsolute(el.parentNode.kids[i-1]);
	}
	if(el.style["pixelHeight"]) el.style["height"] = el.style["pixelHeight"]; // for IE
	el.a.setCSS({"display":"none"});
	el.cnt.setCSS({"display":"block","paddingBottom":"10px", "height":"auto"});
	if (el.h3) el.h3.setCSS({"display":"block", "height":"auto"});
	el.op.setCSS({"left":pos[0]+7+"px","top":pos[1]+7+"px","width":dim[0]-10+"px","height":dim[1]+"px","visibility":"visible"});
	el.direction = 0;
	e ? el.onmouseout  = undoClientsPopupFF : el.onmouseleave = undoClientsPopup;
	animate(el);

	return false;
}

function undoClientsPopup(e)
{
	var el = stopB(e);
	e ? el.onmouseout = null : el.onmouseleave = null;
	clearTimeout(el.timer);
	el.direction = 1;
	animate(el);
	return false;
}

function doClientsPopupFF(e)
{
	var from = e.relatedTarget;
	while (from)
	{
		if (from == this)
		{
			return false;
		}
		from = from.parentNode;
	}
	return doClientsPopup(e);
}

function undoClientsPopupFF(e)
{
	var to = e.relatedTarget;
	while(to)
	{
		if (to == this) return false;
		to = to.parentNode;
	}
	return undoClientsPopup(e);

	to = e.relatedTarget;
	var t = this;
	while(t)
	{
		if (t == to)
		{
			return undoClientsPopup(e);
		}
		t = t.parentNode;
	}
	return false;
	
}

function animate(el)
{
	var h = parseInt(el.offsetHeight);
	var delay = 10;

	if(el.direction)
	{
		h -= el.step;
		el.bgColor+=2;
		el.op.opLevel-=10;
		if(h<=el.minHeight)
		{
			h = el.minHeight;
			el.bgColor = 255;
			el.op.opLevel = 0;
			el.op.style["visibility"] = "hidden";
			el.cnt.restoreCSS();
			if (el.h3) el.h3.restoreCSS();
			el.a.restoreCSS();
			for (var i=0; i<el.parentNode.kids.length; i++) {
			el.parentNode.kids[i].restoreCSS();
			}

		}
	}
	else
	{
		h += el.step;
		el.bgColor-=2;
		el.op.opLevel+=10;
		if(h>=el.maxHeight)
		{
			h = el.maxHeight;
			el.cnt.setCSS({"paddingBottom":"10px"});
		}
	}
	el.style["height"] = h+"px";
	el.style["backgroundColor"] = "rgb("+el.bgColor+","+el.bgColor+","+el.bgColor+")";
	el.op.style["height"] = h+"px";
	el.op.setOpacity(el.op.opLevel);
	if(h>el.minHeight && h<el.maxHeight) el.timer = setTimeout(function(){animate(el);},delay);
	else // animation done
	{
		clearTimeout(el.timer);
		el.direction = !el.direction;
	}
	return false;
}

function initHeader()
{
	var el = document.getElementById("process");
	var a   = getChildren(el, "a")[0];
	var img = getChildren(el, "img")[0];
	var h3 = getChildren(el, "h3")[0];
	var op = document.getElementById("opaque");

	el.setCSS  = setCSS;
	el.restoreCSS  = restoreCSS;
	el.saveCSS  = saveCSS;
	el.minHeight = parseInt(el.offsetHeight);
	el.direction = 0;
	el.bgColor = 255;

	h3.setCSS  = setCSS;
	h3.restoreCSS  = restoreCSS;
	h3.saveCSS  = saveCSS;

	img.setCSS  = setCSS;
	img.restoreCSS  = restoreCSS;
	img.saveCSS  = saveCSS;

	a.setCSS = setCSS;
	a.restoreCSS = restoreCSS;
	a.saveCSS = saveCSS;
	a.onclick = doClientsPopup;

	op.setCSS  = setCSS;
	op.setOpacity = setOpacity;
	op.opLevel = 0;
	op.setOpacity(0);
	
	el.saveCSS("left", "top", "position", "background","visibility", "height");
	img.saveCSS("display", "paddingBottom", "height");
	h3.saveCSS("display", "height");
	a.saveCSS("display");
	
	a.setCSS({"display":"none", "visibility":"visible"});
	img.setCSS({"display":"block","height":"auto"});
	h3.setCSS({"display":"block","height":"auto"});
	el.setCSS({"height":"auto"});
	el.maxHeight = parseInt(el.offsetHeight)+10;
	el.restoreCSS();
	img.restoreCSS();
	h3.restoreCSS();
	a.restoreCSS();
	el.step = parseInt((el.maxHeight-el.minHeight)/10);

	el.cnt = img;
	el.a  = a;
	el.op = op;
	el.h3 = h3;
	el.parentNode.kids = getChildren(el.parentNode, "div");
	el.parentNode.kids.length--; // drop last element

// ======== services ==========
	el = document.getElementById("services");
	el.setCSS  = setCSS;
	el.restoreCSS  = restoreCSS;
	el.saveCSS  = saveCSS;
	el.saveCSS("left", "top", "position");

// ======== clients ==========
	    el = document.getElementById("clients");
	var ul  = getChildren(el, "ul")[0];
	    a   = getChildren(el, "a")[0];

	el.setCSS  = setCSS;
	el.restoreCSS  = restoreCSS;
	el.saveCSS  = saveCSS;
	el.minHeight = parseInt(el.offsetHeight);
	el.direction = 0;
	el.bgColor = 255;

	ul.setCSS  = setCSS;
	ul.restoreCSS  = restoreCSS;
	ul.saveCSS  = saveCSS;

	a.setCSS = setCSS;
	a.restoreCSS = restoreCSS;
	a.saveCSS = saveCSS;
	a.onclick = doClientsPopup;


	el.saveCSS("left", "top", "position", "background","visibility", "height");
	ul.saveCSS("paddingBottom", "height");
	a.saveCSS("display");
	
	a.setCSS({"display":"none", "visibility":"visible"});
	el.setCSS({"visibility":"hidden", "height":"auto"});
	ul.setCSS({"height":"auto"});
	el.maxHeight = parseInt(el.offsetHeight)+10;
	el.restoreCSS();
	ul.restoreCSS();
	a.restoreCSS();
	el.step = parseInt((el.maxHeight-el.minHeight)/10);

	el.cnt = ul;
	el.a  = a;
	el.op = op;
}

function showLogin(e)
{
	stopB(e).blur();
	var el = document.getElementById("login");
	var d = pageDim();
	var opacity=0;
	setCSS.call(el,{"display":"block","width":d[0]+"px","height":d[1]+"px"});
	var f = getChildren(el,"form")[0];
	f.style["left"] = (d[0]-f.scrollWidth)/2;
	f.style["top"]  = (d[1]-f.scrollHeight)/2;

	var timeout = (function t()
	{
		setOpacity.call(el,opacity);
		if (opacity >50) clearTimeout(timeout);
		else {
		opacity+=10 ;
		return setTimeout(function(){t();}, 10);
		}
	})();
	window.onresize = function()
	{
		el.style["width"]  = 0;
		el.style["height"] = 0;
		var d = pageDim();
		el.style["width"]  = d[0]+"px";
		el.style["height"] = d[1]+"px";
		var f = getChildren(el,"form")[0];
		f.style["left"] = (d[0]-f.scrollWidth)/2;
		f.style["top"]  = (d[1]-f.scrollHeight)/2;

		return false;
	};

//	alert(f.clientWidth+", "+f.clientHeight+"\n"+ f.scrollWidth+", "+f.scrollHeight+"\n"+ f.offsetWidth+", "+f.offsetHeight);
	return false;
}

function preload()
{
	var a = preload.arguments;
	for (var i=0; i<a.length; i++)
	{
		if (a[i].indexOf("#")!=0)
		{
			var img = document.createElement("IMG");
			img.src = a[i];
			img.style["display"] = "none";
			document.body.appendChild(img);

		}
	}
	return false;
}

window.onload = function()
{
	var p = document.getElementById("portfolios");
	var list = getChildren(p, "div");

	for (var i=0; i<list.length; i++)
	{
		var t = getChildren(list[i], "div", "thumbs")[0];
		t.bg = getChildren(t, "div", "miniBg")[0];
		var imgList = getChildren(t, "img");
		if (imgList.length > 1)
		{
			for (var j=0; j<imgList.length; j++)
			{
				imgList[j].onmouseover = swapImg;
				if(document.images)
				{
					var altImgSrc = imgList[j].getAttribute("src").replace(/_mini/, "");
					preload(altImgSrc);
				}
			}
		}
		else
		{
			t.style["visibility"] = "hidden";
		}
//		window.event ? list[i].onmouseleave = restoreImgIE : list[i].onmouseout = restoreImgFF;
		window.event ? list[i].onmouseleave = restoreImgIE : list[i].addEventListener('mouseout', restoreImgFF, false);
	}
	initHeader();
//	document.getElementById("loginLink").onclick = showLogin;
}


