function $(id) { return document.getElementById(id) }

function addEvent(obj, type, fn){
  if (obj.addEventListener)
    obj.addEventListener(type, fn, false);
  else if (obj.attachEvent){
    obj["e"+type+fn] = fn;
    obj[type+fn] = function(){obj["e"+type+fn](window.event);}
    obj.attachEvent("on"+type, obj[type+fn]);
  }
}

/* class */
function addClass(el,c) { if(!containsClass(el, c)) el.className += " " + c; }
function removeClass(el, c) { el.className = el.className.replace(new RegExp(c,"g"),""); }
function containsClass(el, c) { return el.className.indexOf(c) != -1 }

/* el opacity */
function setOpacity(el, o)
{
	el.style.opacity = o/100;
	el.style.filter = "alpha(opacity="+o+")";
}

/* dynamic tabs */
var animationOn = false;
var defaultAnimation = 1;
function turnOnTabs()
{
	// tabs
	var tabsContent = new Array();
	var divs = $("tabs").parentNode.getElementsByTagName("div");
	for(var i = 0; i < divs.length; i++)
		if(containsClass(divs[i],"content"))
			tabsContent[tabsContent.length] = divs[i];
	
	var tabs = $("tabs").getElementsByTagName("a");
	for(var i = 0; i < tabs.length; i++)
		tabs[i].onclick = switchTab;
		
	var tab = location.hash;
	
	if(tab.length > 0)
	{
		defaultAnimation = 0;
		tabs[parseInt(tab.slice(4))-1].onclick();
		defaultAnimation = 1;
	}

	// normal links
	var links = $("tabs").parentNode.getElementsByTagName("a");
	for(var i = 0; i < links.length; i++)
		if(containsClass(links[i],"tab_jump"))
			links[i].onclick = switchTabByLink;
			
	function switchTabByLink()
	{
		tabs[this.hash.slice(4) - 1].onclick();
		return false;
	}
		
	function switchTab()
	{
		this.blur();
		
		if(containsClass(this.parentNode,"active")) return false;
		
		var posNew; var posOld;
		for(var i = 0; i < tabs.length; i++)
			if(tabs[i] == this) posNew = i;
			else if(containsClass(tabs[i].parentNode,"active")) posOld = i;
			
		animation(posNew,posOld,defaultAnimation);
		return false;
	}
	
	function animation(posNew,posOld,type)
	{
		if(animationOn) return;
		animationOn = true;
	
		if(type == 0)
			animationNormal(posNew,posOld);
		if(type == 1)
			animationAlpha(posNew,posOld);
			
		function animationNormal(posNew,posOld)
		{
			var oldContent = tabsContent[posOld];
			var oldTab = tabs[posOld].parentNode;
			var newContent = tabsContent[posNew];
			var newTab = tabs[posNew].parentNode;
			
			removeClass(oldContent,"active");
			removeClass(oldTab,"active");
			addClass(newContent,"active");
			addClass(newTab,"active");
			
			animationOn = false;
		}
		
			
		function animationAlpha(posNew,posOld)
		{
			var speed = 10;
			var opacityStep = 10;
			var oldContent = tabsContent[posOld];
			var oldTab = tabs[posOld].parentNode;
			var newContent = tabsContent[posNew];
			var newTab = tabs[posNew].parentNode;
			
			
			var opacity = 100;
			var step = setInterval(step1,speed);
		
			//alert(opacity)
		
			function step1()
			{
				opacity -= opacityStep;
				setOpacity(oldContent,opacity);
				if(opacity <= 0)
				{
					clearInterval(step);
					removeClass(oldContent,"active");
					setOpacity(newContent,opacity);
					addClass(newContent,"active");
					step = setInterval(step2,speed);
				}
			}
			
			function step2()
			{
				opacity += opacityStep;
				setOpacity(newContent,opacity);
				if(opacity >= 100)
				{
					clearInterval(step);
					step3();
				}
			}
			
			function step3()
			{
				addClass(newTab,"active");
				removeClass(oldTab,"active");
				animationOn = false;
			}
		}
	}
}

/* gallery */
function prepareGalleries()
{
	var imgTmp = new Array();

	var galleries = document.getElementsByTagName("ul");
	for( var i = 0; i < galleries.length; i++ )
		if(containsClass(galleries[i],"images"))
			prepareGallery(galleries[i]);
	
	function prepareGallery(gallery)
	{
		var images = gallery.getElementsByTagName("a");
		for( var i = 0; i < images.length; i++ )
		{
			images[i].onclick = popupImage;
			imgTmp[i] = new Image();
			imgTmp[i].src = images[i].href;
		}
	}
	
	function popupImage()
	{
		var img = new Image();
		img.src = this.href;
		
		var width = img.width;
		var height = img.height;
	
		var title = "Image";
	    var windowheight = height+40;
	    var windowwidth = width+20;
	    var left = (screen.width - windowwidth)/2;
	    var top = (screen.height - windowheight)/2 - 18;
	    var scrollbars = "no";
    
	    if (left<0) { scrollbars = "yes"; left = 32; windowwidth = screen.width - 2*left; }
	    if (top<0) { scrollbars = "yes"; top = 32; windowheight =  screen.height - 2*top - 18; top = top - 18; }

	    var win = window.open("","image_popup","toolbar=no,location=no,directories=no,"+
    	    "status=no,menubar=no,scrollbars=" + scrollbars + ",resizable=no,"+
        	"copyhistory=no, width="+windowwidth+",height="+windowheight+",left=" + left + ",top=" + top);
       	win.document.write("\<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"\>");
	    win.document.write("\<html\>");
	    win.document.write("\<head\>\<link rel=\"stylesheet\" type=\"text\/css\" href=\"\css/pll_gallery.css\"\/\>\<meta http-equiv=\"pragma\" content=\"no-cache\"\/\>\<title\>"+title+"\<\/title\>");
	    win.document.write("\<\/head\>");
	    win.document.write("\<body class=\"gallery\"\>\<a style=\"width:"+width+"px\" href=\"javascript:window.close();\" title=\"Close window\" \>\<img src=\""+this.href+"\" width=\""+width+"\" height=\""+height+"\" alt=\"\" \/\>\<\/a\>\<\/body\>\<\/html\>");
	    win.document.close()
	    win.focus();

		return false;
	}
}

window.onload = prepareGalleries;

/* to del */
function debug(object)
{
	var t = "";
	for(var i in object)
		t+=i+" -> "+object[i]+"<br>";
	document.write(t);
}

function popupThis(link)
{
	var w = window.open(link.href, 'video', 'menubar=no, toolbar=no, location=no, scrollbars=no, resizable=no, status=no, width=430, height=325, top='+((screen.height-325)/2)+', left='+(screen.width-430)/2  );
	if(w) w.focus();
	return false;
}
