/* some styles should only apply when javascript is enabled */
if(document.getElementById)
{
	var style =
		'#filter_submit { display:none; }' +
		'.box table tbody td.name h3 a { text-decoration:none; }';
		// '.box table tbody tr { cursor:pointer; }';
		//'.box .name h4 a.info { display:none; }';
			
	document.write('<style type="text/css" media="screen">' + style + '</style>');
}

window.onload = function()
{
	if(document.getElementById)
	{	
		// preload images
		var preloadImages =
		[
			new Image(), new Image(), new Image(), new Image(), new Image(),
			new Image(), new Image(), new Image(), new Image(), new Image()
		];
		preloadImages[0].src = 'img/external2.gif';
		preloadImages[1].src = 'img/info2.gif';
		preloadImages[2].src = 'img/button_bg2.gif';
		preloadImages[3].src = 'img/transparent_black.png';
		preloadImages[4].src = 'img/up2.gif';
		preloadImages[5].src = 'img/down2.gif';
		preloadImages[6].src = 'img/star_empty.gif';
		preloadImages[7].src = 'img/star_full.gif';
		preloadImages[8].src = 'img/star_big_empty.gif';
		preloadImages[9].src = 'img/star_big_full.gif';
	
		var i;
		
		/* Make filter select auto-submittable */
		var filter_form = _('filter_form');
		if(filter_form)
		{
			var selects = filter_form.getElementsByTagName('SELECT');
			for(i = 0; i < selects.length; i++)
			{
				selects[i].onchange = function()
				{
					if(!this.options[this.selectedIndex].disabled)
					{
						_('filter_form').submit();
					}
				}
			}
		}
		
		/* convert table rows into links */
		var main_table = _('main_table');
		if(main_table)
		{
			var rowsLength = main_table.rows.length;
			if(rowsLength > 1)
			{
				var linkCell = -1;
				for(i = 0; i < main_table.rows[1].cells.length; i++)
				{
					if(main_table.rows[1].cells[i].className == 'name')
					{
						linkCell = i;
						break;
					}
				}
				if(linkCell != -1)
				{
					var link;
					for(i = 1; i < rowsLength; i++)
					{
						var links = main_table.rows[i].cells[linkCell].getElementsByTagName('A');
						link = links[0];
						if(link)
						{
							//main_table.rows[i].title = link.title;
							link.title = '';
							main_table.rows[i].onmouseover = table.mouseOver;
							main_table.rows[i].onmouseout = table.mouseOut;
							main_table.rows[i].onclick = table.open;
							main_table.rows[i].style.cursor = 'pointer';
						}
					}
				}
			}
		}
		
		/* links */
		var links = document.getElementsByTagName('A');
		var linksLength = links.length;
		var curClass;
		for(i = 0; i < linksLength; i++)
		{
			curClass = links[i].className;
			if(curClass == 'external' || curClass.indexOf('external_link') != -1)
			{
				links[i].onclick = externalLink;
			}
		}
		
		/* Make the search input selectable from outside of the input field */
		var search_input_holder = _('search_input_holder');
		if(search_input_holder)
		{
			window.search_input = search_input_holder.getElementsByTagName('INPUT')[0];
			search_input_holder.style.cursor = 'text';
			
			search_input.onfocus = function()
			{
				this.parentNode.parentNode.parentNode.className = 'search focus';
			}
			
			search_input.onblur = function()
			{
				this.parentNode.parentNode.parentNode.className = 'search';
			}
			
			search_input_holder.onclick = function()
			{
				search_input.focus();
			}
		}
		
		/* hover state of the logout/login button */
		var logout_button = _('logout_button');
		var login_button = _('login_button');
		if(logout_button)
		{
			logout_button.onmouseover = function()
			{
				this.src = 'img/odjava2.gif';
			};
			
			logout_button.onmouseout = function()
			{
				this.src = 'img/odjava.gif';
			};
			
			logout_button.onclick = function()
			{
				this.onmouseout = function(){};
				this.onmouseover = function(){};
			};
		}
		else if(login_button)
		{
			login_button.onmouseover = function()
			{
				this.src = 'img/prijava2.gif';
			};
			
			login_button.onmouseout = function()
			{
				this.src = 'img/prijava.gif';
			};
			
			login_button.onclick = function()
			{
				this.onmouseout = function(){};
				this.onmouseover = function(){};
			};
		}
		
		// inputs
		var inputs = document.getElementsByTagName('INPUT')
		var inputsLength = inputs.length;
		for(i = 0; i < inputsLength; i++)
		{
			if(document.all) // IE only
			{
				if(inputs[i].type == 'text' && inputs[i].id != 'top_search')
				{
					inputs[i].onfocus = window.inputs.focus;
					inputs[i].onblur = window.inputs.blur;
				}
				else if(inputs[i].type == 'submit')
				{
					inputs[i].onmouseover = window.inputs.submit.mouseOut;
					inputs[i].onmouseout = window.inputs.submit.mouseOver;
				}
			}
			if(inputs[i].className == 'date' && calendar && calendar.item)
			{
				new calendar.item(inputs[i]);
			}
		}
	}
}

/**
 * Handle tables
 */
var table =
{
	curRow : null,
	curRowEl : null,
	showTextTO : null,
	textEl : null,
	textElVisible : false,
	textElContent : null,
	waitTextTO : 400,
	textOpacity : 0,
	
	mouseOver : function(e)
	{
		this.className = 'hover';
		if(table.curRow != this.id)
		{
			if(table.textElVisible == true)
			{
				table.hideText();
			}
			table.curRow = this.id;
			table.curRowEl = this;
		
			if(!table.showTextTO)
			{
				getMousePosition(e);
				table.showTextTO = window.setTimeout('table.showText("' + table.curRow + '")', table.waitTextTO);
			}
		}
	},
	
	mouseOut : function(e)
	{
		this.className = '';
	},
	
	mouseMove : function(e)
	{	
		if(table.curRow)
		{
			var target = getTarget(e);
			if((target.tagName == 'A' && (target.parentNode.tagName != 'H3' || target.className == 'internal')) || hasParent(target, table.curRowEl) == false)
			{
				table.curRow = null;
				if(table.textElVisible)
				{
					table.hideText();
				}
			}
			if(table.textElVisible == true && table.curRow)
			{
				getMousePosition(e);
				table.showText(table.curRow);
			}
			if(table.showTextTO)
			{
				window.clearTimeout(table.showTextTO);
				
				if(table.curRow)
				{
					getMousePosition(e);
					table.showTextTO = window.setTimeout('table.showText("' + table.curRow + '")', table.waitTextTO);
				}
				else
				{
					table.showTextTO = null;
				}
			}
		}
	},
	
	showText : function(rowId)
	{
		table.showTextTO = null;
		if(rowId != table.curRow || typeof(rollovers) == 'undefined')
		{
			return false;
		}
		
		if(!table.textEl)
		{
			table.textEl = document.createElement('DIV');
			table.textEl.id = 'extra_text';
			table.textEl.onmouseover = function()
			{
				table.hideText();
			}
			document.body.appendChild(table.textEl);
		}
		
		if(table.textElContent != rollovers[table.curRow])
		{
			table.textEl.innerHTML = rollovers[table.curRow];
			table.textElContent = rollovers[table.curRow];
		}
		table.textEl.style.left = mousePos[0] + 'px';
		table.textEl.style.top = mousePos[1] + 23 + 'px';
		if(!table.textElVisible)
		{
			//setOpacity(table.textEl, 0);
			//table.textOpacity = 0;
			table.textEl.style.display = 'block';
			//table.fadingInterval = window.setInterval(table.fadeIn, 30);
			table.textElVisible = true;
		}
	},
	
	/*fadingInterval : null,
	
	buggy drawing..
	
	fadeIn : function()
	{
		if(table.textElVisible && table.textOpacity < 100)
		{
			table.textOpacity += 20;
			setOpacity(table.textEl, table.textOpacity);
		}
		else
		{
			window.clearInterval(table.fadingInterval);
		}
	},*/
	
	hideText : function()
	{
		if(table.showTextTO)
		{
			window.clearTimeout(table.showTextTO);
			table.showTextTO = null;
		}
		table.textEl.style.display = 'none';
		table.textElVisible = false;
	},
	
	open : function(e)
	{
		var target = getTarget(e);
		if(!e)
		{
			var event = window.event;
		}
		else
		{
			var event = e;
		}
		
		if(target.tagName == 'A')
		{
			return true;
		}
		else if(target.parentNode.tagName == 'A')
		{
			return true;
		}
		else
		{
			var link = this.getElementsByTagName('H3')[0].childNodes[0];
			if(link)
			{
				if(link.className == 'external_link' || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey)
				{
					window.open(link.href, '_blank');
				}
				else
				{
					window.location.href = link.href;
				}
			}
		}
		
		return false;
	}
}

document.onmousemove = table.mouseMove;

/**
 * Get the mouse position. Thanks Peter-Paul Koch.
 * http://www.quirksmode.org/js/events_properties.html
 *
 * @param e Mouse event
 */
function getMousePosition(e)
{
	var posx = 0;
	var posy = 0;
	if(!e)
	{
		var e = window.event;
	}
	if(e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if(e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	mousePos[0] = posx;
	mousePos[1] = posy;
}
var mousePos = [];

/**
 * returns target element of the mouse event
 *
 * @param e Mouse event
 */
function getTarget(e)
{
	var target;
	if(!e)
	{
		var event = window.event;
	}
	else
	{
		var event = e;
	}
	
	if(event.target)
	{
		target = event.target;
	}
	else if(event.srcElement)
	{
		target = event.srcElement;
	}
	if(target.nodeType == 3) // defeat Safari bug
	{
		target = target.parentNode;
	}
	
	return target;
}

/**
 * checks if an element is another element's parent
 *
 * @param el		reference element
 * @param parent	element maybe a parent of el
 */
function hasParent(el, parent)
{
	var parentEl = el.parentNode;
	while(parentEl != document.body)
	{
		if(parentEl == parent)
		{
			return true;
			break;
		}
		else
		{
			parentEl = parentEl.parentNode;
		}
	}
	return false;
}

/* inputs */
var inputs =
{
	focus : function()
	{
		this.className = this.className + ' focus';
	},
	
	blur : function()
	{
		this.className = this.className.replace(' focus', '');
	},
	
	submit :
	{
		mouseOver : function()
		{
			this.className = this.className + ' hover';
		},
		
		mouseOut : function()
		{
			this.className = this.className.replace(' hover', '');
		}
	}
}

/**
 * Set element's opacity
 *
 * @param element The element
 * @param value Opacity value (0-100)
 */
/*function setOpacity(element, value)
{
	element.style.opacity = value/100;
	element.style.filter = 'alpha(opacity=' + value + ')';
}*/

/**
 * Open a link in new window
 * @param event Mouse click event
 */
function externalLink(event)
{
	var target;
	if(!event)
	{
		var event = window.event;
	}
	
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey)
	{
		return true;
	}
	else
	{
		var newWindow = window.open(this.href, '_blank');
		if (newWindow)
		{
			if (newWindow.focus)
			{
				newWindow.focus();
			}
			return false;
		}
		
		return true;
	}
}

/**
 * document.getElementById shortcut
 * @param id Element id
 */
function _(id)
{
	return document.getElementById(id);
}
