function $_(n) {
	if(!n) return false;
	if(!document.getElementById(n)) return false;
	return document.getElementById(n)
}

function onload_ready(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function init() {
	clouds();
	clouds_hover();
}


/*--- common ---*/
function selection_disable() {
	var sel;
	if(document.selection && document.selection.empty) {
		document.selection.empty();
	} else if(window.getSelection) {
		sel=window.getSelection();
		if(sel && sel.removeAllRanges) sel.removeAllRanges();
	}
}

function bubbling_no(e) {
	e = bubbling_ie(e);
	if (!e.cancelBubble) e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	return false;
}
function bubbling_ie( e ) {
	if (!e) var e = window.event;
	return e;
}

function screen_y(){
	if (document.documentElement && document.documentElement.scrollHeight) {	
		y = document.documentElement.scrollHeight;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ 
		y = document.body.scrollHeight;
	} else { 
		y = document.body.offsetHeight;
  	}
	return y;
}

function pos_left(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) curleft += obj.x;
	return curleft;
}
	
function pos_top(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
		    curtop += obj.offsetTop
		    obj = obj.offsetParent;
	    }
	}
	else if (obj.y) curtop += obj.y;
	return curtop;
}


function clouds() {
	if (!document.getElementsByTagName || !document.createTextNode) return;
	if ($_("tags")) {
		var dt = $_("tags").getElementsByTagName("dt");
		var dd = $_("tags").getElementsByTagName("dd");
		var dt_length = dt.length;

		function clouds_toggle(obj) {
			for (i = 0; i < dt_length; i++) {
				if (obj == dt[i])  {
					if(dt[i].className != "active") {
						dt[i].className = "active";
						slidedown(dd[i],i);
					} else {
						dt[i].className = "";
						slideup(dd[i],i);
					}
					
				} else {
					dt[i].className = "";
					slideup(dd[i],i);
				}
			}
		}
		
		for (i = 0; i < dt_length; i++) {
			dt[i].onclick = function() {
				clouds_toggle(this);
			}
			dt[i].ondblclick = function() {
				selection_disable();
			}
		}
	}
}

function clouds_hover() { 
	if (!document.getElementsByTagName || !document.createTextNode) return;
	if ($_("tags")) {
		var layer1 = document.createElement("div");
		var layer2 = document.createElement("div");
		var tags = document.getElementById("tags").getElementsByTagName("a");
		var tags_length = tags.length;
				
		layer1.setAttribute("id", "tag-quantity");
		document.body.appendChild(layer1);
		layer1.appendChild(layer2);
		
		for (i = 0; i < tags_length; i++) {
			tags[i].onmouseover = function() {
				layer2.innerHTML = this.rel;
				layer1.style.top = pos_top(this) - 35 + "px";
				layer1.style.left = pos_left(this) + ( this.offsetWidth ? this.offsetWidth : 0 ) - 56 + "px";
				layer1.style.display = this.rel ? "block" : "none";
			}
			tags[i].onmouseout = function() {
				layer1.style.display = "none";
			}
		}
	}
}

function overley_height() {
	var o = $_("overley");
	if(o && o.style.display == "block") {
		o.style.height = "100%";
		o.style.height = screen_y() + "px";
	}
}

function block_hide(el) {
	$_(el).style.display = "none";
}

function dropdown_toggle () {
	$_("drop_down").className = $_("drop_down").className == "" ? "active" : "";
}

function toggle_close() {
	for (i=0; i< arguments.length; i++) {
		if(arguments[i]) arguments[i].style.display = "none";
	}
}

function toggle_close2() {
	for (i=0; i< arguments.length; i++) {
		if($_(arguments[i])) $_(arguments[i]).style.display = "none";
	}
}

function toggle_show2() {
	for (i=0; i< arguments.length; i++) {
		if ($_(arguments[i])) $_(arguments[i]).style.display = "";
	}
}

function toggle_contacts(e, _this) {
	var o;
	var i = $_("contacts-info");
	var c = $_("contacts-close");
	var m = $_("contacts-map");

	if($_("overley")) {
		o = $_("overley");
	} else {
		o = document.createElement("div");
		o.setAttribute("id","overley");
		document.body.appendChild(o);
	}

	if (_this.className == "active") {
		toggle_close(o, i);
		_this.className = "";
	} else {
		if (i) i.style.display = "block";
		_this.className = "active";

		o.style.display = "block";
		o.onclick = function() {
			if ($_("zoom")) $_("zoom").className = "zoom zoom-p";
			if ($_("contacts-map")) $_("contacts-map").style.cursor = "pointer";
			if (m) zoom_default(m, "363", "158");
			toggle_close(o, i);
			_this.className = "";
		}; 
		c.onclick = function() {
			if ($_("zoom")) $_("zoom").className = "zoom zoom-p";
			if ($_("contacts-map")) $_("contacts-map").style.cursor = "pointer";
			if (m) zoom_default(m, "363", "158");
			toggle_close(o, i);
			_this.className = "";
		}; 
		overley_height();
	}
}

function toggle_cv(obj, el) {
	if(obj.className == "active") {
		$_(el).style.display = "none";
		obj.className = "";
	} else {
		$_(el).style.display = "block";
		obj.className = "active";
	}
}

var timeout = 0;
function zoom(obj, w, h) {
	if (timeout) return false;
	/*if (obj.offsetWidth == w) {
		zoom_process(obj.id, "363", "158", -1);
		$_("zoom").className = "zoom zoom-p";
	} else {
	*/
		zoom_process(obj.id, w, h, 1);
		$_("zoom").className = "zoom zoom-m";
		obj.style.cursor = "default";
	//}
}

function zoom_default(obj, w, h) {
	obj.style.width = w + "px";
	obj.style.height = h + "px";
}

function zoom_process(id, w, h, direction, step, steps) {
	var o = $_(id);
	var i = $_("contacts-info");
	if (!o) return false;
	if (!step) step = 1;
	if (!steps) steps = 20;
	
	if (step >= steps) {
		o.style.height = h + "px";
		o.style.width = w + "px";
		clearTimeout(timeout);
		timeout = 0;
	} else {
		if (direction == 1) {
			o.style.width = Math.round(o.offsetWidth + (w - o.offsetWidth) / (steps - step)) + "px";
			o.style.height = Math.round(o.offsetHeight + (h - o.offsetHeight) / (steps - step)) + "px";
		} else {
			o.style.width = Math.round(o.offsetWidth - (o.offsetWidth - w) / (steps - step)) + "px";
			o.style.height = Math.round(o.offsetHeight - (o.offsetHeight - h) / (steps - step)) + "px";
		}
		step++;
		timeout = setTimeout("zoom_process('"+id+"', "+w+", "+h+", "+direction+", "+step+", "+steps+")", 10);
	}
}


function visited_categories() {
	if(getCookie("vid" + arguments[0])) {
		var docs = getCookie("vid" + arguments[0]);
		var arguments_length = arguments.length;
		for (i = 0; i < arguments_length; i++) {
			if(in_cookie(arguments[i], docs)) {
				docs = docs + "," + arguments[i];
			}
		}		
    } else {
		var docs = Array.apply(null, arguments).join (",");
    }
    setCookie("vid" + arguments[0], docs, 0 , "/");
}

function in_cookie(v,a) {
	var a = a.split(",");
	var a_length = a.length;
	for (ai = 0; ai < a_length; ai++) {
		if (a[ai] == v) return false;
	}
	return true;
}
  
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else begin += 2;
    
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)	end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function mail(name, dom, a, display) {
	var m = "mailto:";
	document.write('<a href="'+m+name+'@'+dom+'.'+a+'">'+(display?display:name+'@'+dom+'.'+a)+'</a>');
}

onload_ready(init);

onresize = function() {
	overley_height();
}
