function fadeInImage(imageObject){
	var targetElement = $(imageObject.targetId);
	targetElement.innerHTML = '';
	var aElement = new Element('a');	
	if(imageObject.targetId.split("_")[1] == 'web' && $(imageObject.targetId).title!=''){
		aElement.href = $(imageObject.targetId).title;
		aElement.target = '_blank';
	} else {
		aElement.href = imageObject.largeImageSrc;
		aElement.rel ='lightbox['+imageObject.group+']';
	}
	aElement.title = targetElement.title;
	aElement.appendChild(imageObject);
	$(targetElement.parentNode).setStyle({backgroundImage:'none'});
	targetElement.appendChild(aElement);	
	targetElement.observe('mouseover', function(e){ Effect.Appear(e.target.parentNode.parentNode, {duration:0.1});});
	targetElement.observe('mouseout', function(e){ Effect.Appear(e.target.parentNode.parentNode, {duration:0.2, to:0.5});});
	targetElement.setStyle({display:'block'});
	Effect.Appear(targetElement,{duration:0.3, to:0.5});
}
function loadThumbImage(img, groupId){
	var thumbImage = new Image();
	thumbImage.targetId = img;
	var splitImg = img.split("_");
	var thumbImageSrc = Array('', 'img', groupId, splitImg[2], splitImg[3], 't.jpg').join("/");
	var largeImageSrc = Array('', 'img', groupId, splitImg[2], splitImg[3], 'l.jpg').join("/");
	thumbImage.largeImageSrc = largeImageSrc;
	thumbImage.group = groupId;
	thumbImage.onload = function(e){fadeInImage(this);}.bind(thumbImage);
	thumbImage.src = thumbImageSrc;
}
function switchSub(e){
	var clickedItem = e.target;
	var splitId = clickedItem.id.split("_");
	var subId = splitId[0];
	openSub(subId);
}
function openSub(subId){
	if(formVisible){
		Effect.BlindUp($('contactform'), {duration:baseTransitionTime});
		formVisible = false;
	}
	if(openedSub){
		closeSub(openedSub);
	}
	if(openedSub == subId){
			openedSub = null;
	} else {
		new Effect.BlindDown(subId, {duration:baseTransitionTime});
		
		openedSub = subId;
		
	}
}
function closeSub(subId){
	new Effect.BlindUp(subId, {duration:baseTransitionTime});
}
function bgOffsetTweenHandle(p){
	this.setStyle({backgroundPosition:p+"px"+" 0px"});
}
function switchElementLanguage(el, newLanguage){
	var elWidth = el.getStyle('width').replace("px","");
	var transitionDuration = baseTransitionTime;
	switch(newLanguage){
		case "pl":
			new Effect.Tween(el, 0, -elWidth, {duration:transitionDuration}, bgOffsetTweenHandle.bind(el));
			break;
		case "en":
			new Effect.Tween(el, -elWidth,0, {duration:transitionDuration}, bgOffsetTweenHandle.bind(el));
			break;
	}
}
function switchContactForm(){
	if(formVisible){
		new Effect.BlindUp($('contactform'), {duration:baseTransitionTime});
		formVisible = false;
	} else {
		if(openedSub){
			closeSub(openedSub);
			openedSub = null;
		}	
		Effect.BlindDown($('contactform'), {duration:baseTransitionTime});
		formVisible = true;
	}
}

function switchLanguage(){
	if(currentLanguage == 'en'){
		currentLanguage = 'pl';
	} else {
		currentLanguage = 'en';
	}
	var counter = 1;
	var formPreg = /.*multi_form.*/;
	multiLabels.each(function(el){	
		if(!el.id.match(formPreg) || formVisible==true){
			switchElementLanguage.delay(counter*baseTransitionTime/2, el, currentLanguage);
			counter++;
		} else {
			switchElementLanguage(el, currentLanguage);
		}
	});
}
var baseTransitionTime = 0.5;
var openedSub;
var pageSize;
var currentLanguage = 'en';
var formVisible = false;
var multiLabels = new Array();
var imgArray = new Array();
document.observe('dom:loaded', function(){
	$('form_switch').observe('click', switchContactForm);
	$('language_switch').observe('click', switchLanguage);
	$$('img[id*=_button]').each(function(el){
		el.observe('click', switchSub);
	});
	$$('img[src*=thumb]').each(function(el){
		el.observe('click', openLarge);
	});
	pageSize = getPageSize();
	$$('div[id*=multi_]').each(function(el){
		multiLabels.push(el);
	});
	var kS = '';
	for(var k in imgArray){
		
		if(imgArray[k].shift){
			var images = imgArray[k];
			while(images.length){
				var img = images.shift();
				loadThumbImage(img, k);
			}
		}
	}
});
