/*
 * javascript functions for m00!album's default skin
 *
 * by kakaopor (m00album!at!dev!dot!kakaopor!dot!hu, 2007)
 *
 * This is free stuff, use it! :)
 *
 */

// ----- "common" functions ----- //
function getobj(name)
{
	return document.getElementById(name);
}


// ----- tab handling ----- //

var lastpage = null;

function setpagevisiblity(id, visible)
{
	getobj('page_'+id).style.display = visible ? '' : 'none';
	getobj('tab_'+id).className = visible ? 'tab_active' : 'tab';
}

function switchpage(currentpage)
{
	if (lastpage != null)
		setpagevisiblity(lastpage, false);

	setpagevisiblity(currentpage, true);
	
	lastpage = currentpage;
}

function createTabs()
{
	var head = null;
	var page = null;

	var menu = getobj('js_menu');
	if (!menu) return false;
	menu.style.display = 'block';
	

	i = 1;
	while (((head = getobj('head_'+i)) != null) && ((page = getobj('page_'+i)) != null))
	{
		var tab = document.createElement('a');

		tab.className 	= 'tab_inactive';
		tab.innerHTML 	= head.innerHTML;
		tab.className	= 'tab';
		tab.id			= 'tab_'+i;
		tab.onmouseover	= function() { if (this.className == 'tab') this.className = 'tab_hover'; };
		tab.onmouseout	= function() { if (this.className == 'tab_hover') this.className = 'tab'; };
		tab.onclick 	= function() { switchpage(this.id.split("_")[1]); };
			
		getobj('head_'+i).style.display = 'none';
		
		if (i > 1)
			getobj('page_'+i).style.display = 'none';
		
		menu.appendChild(tab);

		i++;
	}
	
	return true;
}


// ----- e-mail address !at! and !dot! replacing ----- //

function fixMailAddresses()
{
	var links = document.getElementsByTagName("A");

	for (var i=0; i<links.length; i++)	
		if (links[i].className == 'mail')
			links[i].href = links[i].href.replace(/!at!/g, '@').replace(/!dot!/g, '.');
}


function disablePasswordField()
{
	if (!getobj('gallery_password_password'))
		return false;
		
	getobj('gallery_password_password').disabled = 'disabled';
	getobj('gallery_password_option_clear').onchange = function () { getobj('gallery_password_password').disabled = 'disabled'; return true; };
	getobj('gallery_password_option_leave').onchange = function () { getobj('gallery_password_password').disabled = 'disabled'; return true; };
	getobj('gallery_password_option_set').onchange = function () { getobj('gallery_password_password').disabled = ''; getobj('gallery_password_password').focus(); return true; };
}


// ----- initialization  ----- //

function init()
{
	if (createTabs())
		switchpage(1);
	fixMailAddresses();
	disablePasswordField();
}

