/*
 * 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);
}


// Show image onhover
function showWork(id)
{
	var target = $("portfolio"); 

	target.style.display = "block"; 
	target.innerHTML = '<img src="/images/' + id + '/small" class="portfolio">'; 
}

// Fullscreen background script
var browser = navigator.appName; 

if(browser != "Microsoft Internet Explorer")
{
	// JavaScript Document
	var bgimg;
	var init = function() {
	  if (arguments.callee.done) return;
	  arguments.callee.done = true;
	  bgimg = document.getElementById('bg').firstChild.nextSibling;
	  if (!bgimg) return;
	  bgimg.onload = resizeBg;
	  bgimg.src = '/images/1';
	  if (window.addEventListener) window.addEventListener('resize', resizeBg, false);
	  else if (window.attachEvent) window.attachEvent('onresize', resizeBg);
	  else window.onresize = resizeBg;
	}
	var resizeBg = function() {
	  var w = self.innerWidth || document.documentElement.clientWidth;
	  var h = self.innerHeight || document.documentElement.clientHeight;
	  if (bgimg.width !== w) {
	    bgimg.parentNode.style.left = '0';
	    bgimg.parentNode.style.top = '0';
	    bgimg.height = (w / bgimg.width) * bgimg.height;
	    bgimg.width = w;
	  }
	  if (bgimg.height < h) {
	    bgimg.width = (h / bgimg.height) * bgimg.width;
	    bgimg.height = h;
	    bgimg.parentNode.style.left = '-' + ((bgimg.width - w) / 2) + 'px';
	  }
	  else if (bgimg.height > h) {
	    bgimg.parentNode.style.top = '-' + ((bgimg.height - h) / 2) + 'px';
	  }
	}
	 
	if (document.addEventListener) document.addEventListener("DOMContentLoaded", init, false);
	 
	if (/KHTML|Webkit|iCab/i.test(navigator.userAgent)) {
		var khtmltimer = window.setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
	  		window.clearInterval(khtmltimer);
	  		init();
			}
		}, 1000);
	}

}
else 
{
	void(0); 
}

// DD_roundies
DD_roundies.addRule('div#wrapper1', '30px'); 
DD_roundies.addRule('div#wrapper2', '25px'); 
DD_roundies.addRule('nav', '0, 20px, 20px, 0'); 
DD_roundies.addRule('img.portfolio_rechts', '5px'); 
DD_roundies.addRule('img.portfolio_links', '5px'); 
DD_roundies.addRule('img.kunstwerk', '5px'); 

