/*
 * General javascript functions
 */

function ajaxRequest(url, onSuccess, pars, method)
{
	var elmId = onSuccess;
	
	if (elmId && $(elmId).id)
		onSuccess = function(resp)
		{
			if (resp.responseText)
			{
				$(elmId).innerHTML = resp.responseText;
				$(elmId).innerHTML.evalScripts();
			}
		};
	
	if (!pars)
		pars = '';

	if (!method)
		method = 'POST';
	
	new Ajax.Request
	(
		url,
		{
			method: method,
			parameters: pars,
			onSuccess: onSuccess
		}
	);
}

function ajaxCall(subsection, action, parameters, respFunction, confirmText)
{
	
	var func = function(resp)
	{
		if(resp.responseText)
		{
			var error = resp.responseText.evalJSON();
			
			if(error && error.error)
			{
				setStatusMessage(error.message, "error");
			}
			else
			{
				eval(respFunction);
			}
		}
	}
		
	if (!confirmText || confirm(confirmText))
	{
		var get = "?";
		var post = "";
		
		if(subsection != null)
		{
			get += "subsection="+subsection;
		}
		if(action != null)
		{
			get += (get == "?" ? '' : "&") + "action="+action;
		}
		
		for(var name in parameters) 
		{
			post += (post == "" ? "" : "&") + name + "=" + parameters[name]; 
		}

		ajaxRequest(get, func, post, "post");
	}
}

function mail(code)
{
	var decoded = "";

	splitString = code.split(".");
	for (var i = 0; i < splitString.length; i++)
	{
		decoded += String.fromCharCode(splitString[i]-10);	
	}

	window.location.href = "mailto:" + decoded;
}

function readMore(anchor, elmId)
{
	if (!$(elmId).visible())
	{
		Effect.SlideDown(elmId);
		anchor.hide();
	}
}

/*
 * Swap images
 */

var currentSwapImageElm = false;
var swapImageTimer = false;

function moveToSwapImage(imageId)
{
	var interval = 12000;
	
	Effect.Queues.get('swapImages').each(function(e) { e.cancel() });
	clearTimeout(swapImageTimer);
	hideSwapImageElm(currentSwapImageElm);
	showSwapImageElm($('swap_image_'+imageId));
	
	swapImageTimer = setTimeout(function () { swapImageChanger(); }, interval);
}

function setSwapImageSelector(elm, state)
{
	var elmId = elm.identify();
	var imageId = elmId.substr(elmId.lastIndexOf('_')+1, elmId.length-1);
	
	if (state)
		$('swap_image_selector_'+imageId).addClassName('active');
	else
		$('swap_image_selector_'+imageId).removeClassName('active');
}

function showSwapImageElm(elm)
{
	var duration = 1.5;
	
	if (elm)
	{
		new Effect.Appear(elm, { duration: duration, queue: { position: 'end', scope: 'swapImages', limit: 2} });
		currentSwapImageElm = elm;
		setSwapImageSelector(elm, true);
	}
}

function hideSwapImageElm(elm)
{
	var duration = 1.5;

	if (elm)
	{
		new Effect.Fade(elm, { duration: duration, queue: {position: 'end', scope: 'swapImages', limit: 2} });
		setSwapImageSelector(elm, false);
	}
}

function getNextSwapImageElm()
{
	var elms = $$('div.swapImage');
	var next = false;
	var first = false;
	var result = false;
	var counter = 1;
	
	elms.each(function(elm, index)
	{
		if (!currentSwapImageElm || next)
		{
			result = elm;
			throw $break;
		}
		
		if (!first)
			first = elm;
		
		if (elm == currentSwapImageElm)
			next = true;
		
		if (next && elms.length == counter)
		{
			result = first;
			throw $break;
		}
		
		counter++;
	} );
	
	return result;
}

function swapImageChanger()
{
	var interval = 12000;
	
	hideSwapImageElm(currentSwapImageElm);
	showSwapImageElm(getNextSwapImageElm());
	
	swapImageTimer = setTimeout(function () { swapImageChanger(); }, interval);
}


var lastSectionId = 'section1';

function showSection(sectionId, contentId)
{
	// content
	if ($(contentId).visible())
	{
		$(contentId).hide();
		if (contentId == 'tekst3')
			$('tekst4').hide();
	}
	else
	{
		$(contentId).show();
		if (contentId == 'tekst3')
			$('tekst4').show();	
	}

	// section
	if (isActive(sectionId)) // hide
	{		
		if (isActive(sectionId))
			$('link_'+sectionId).removeClassName('active');
		
		$(sectionId).hide();
	}
	else // show		
	{
		if (lastSectionId)
			$(lastSectionId).hide();
		
		if (!isActive(sectionId))
			$('link_'+sectionId).addClassName('active');
		
		lastSectionId = sectionId;
	}

	showLastActiveSection();
}

function showLastActiveSection()
{
	var lastActiveId = 'section1';

	if (isActive(lastSectionId))
		lastActiveId = lastSectionId;
	else
	{
		$$('section').each(function(elm)
		{
			if (elm.id.startsWith('section'))
			{
				if (isActive(elm.id))
					lastActiveId = elm.id;
			}
		} );
	}
		
	$(lastActiveId).show();
	lastSectionId = lastActiveId;
}

function isActive(sectionId)
{
	if ($('link_'+sectionId))
		return $('link_'+sectionId).hasClassName('active');
	
	return true;
}

function hideContent()
{
	$$('div').each(function(elm)
	{
		if (elm.id.startsWith('tekst'))
		{
			elm.hide();
		}
	} );
}
