function titleMouseOver(img, who) {
	var actualSrc = img.src;
	img.src = titles[who+"Hover"];
	titles[who] = actualSrc;
}

function titleMouseOut(img, who) {
//	if (img.src.search("selected")) {
//		img.src = titles[who+"Selected"];
//	} else {
		img.src = titles[who];
//	}
}

function titleSelect(who) {
	if (who == 'none') {
		$("title-ivan").src = titles["ivanNormal"];
		$("title-magda").src = titles["magdaNormal"];
	} else {
		$("title-"+who).src = titles[who+"Selected"];
		titles[who] = titles[who+"Selected"];
	}
}

function highlightBox(ele, color) {
	$(ele).addClassName("background-" + color);
	$(ele).down().addClassName("background-" + color);
	//$(ele).childElements()[0].removeClassName("color-ivan");
	$(ele).down().childElements()[0].addClassName("color-white");
}

/*
 * force - obide kontrolu 'selected'
 */
function removeHighlightBox(ele, color, force) {
	if (! force) {
		if ($(ele).hasClassName('selected')) { return; }
	}
	$(ele).removeClassName("background-" + color);
	$(ele).down().removeClassName("background-" + color);
	//$(ele).childElements()[0].addClassName("color-ivan");
	$(ele).down().childElements()[0].removeClassName("color-white");
}

function switchTab(who, tabToSelect, selectedTab) {
	$('page-referencie-' + who + '-clanok-' + selectedTab).hide();
	$('page-referencie-' + who + '-clanok-' + tabToSelect).show();
	
	$('referencie-taby-' + who + '-tab-' + selectedTab).removeClassName('selected');
	removeHighlightBox('referencie-taby-' + who + '-tab-' + selectedTab, 'color-'+who);
	
	$('referencie-taby-' + who + '-tab-' + tabToSelect).addClassName('selected');
	highlightBox('referencie-taby-' + who + '-tab-' + tabToSelect, 'color-'+who);
}

function showTab(who, tabToSelect) {
	var selectedTab;
	if (tabToSelect == 1) selectedTab = 2;
	if (tabToSelect == 2) selectedTab = 1;
	if (who == 'magda') {
		actualLeftPage = 'page-referencie-magda';
		$('referencie-taby-magda').show();
		switchTab('magda', tabToSelect, selectedTab);
		$('page-referencie-'+who+'-chooser').hide();
	} else {
		actualRightPage = 'page-referencie-ivan';
		$('referencie-taby-ivan').show();
		switchTab('ivan', tabToSelect, selectedTab);
		$('page-referencie-'+who+'-chooser').hide();
	}
}

function showPage(page, options) {
//	//stranka je prave zobrazena
//	if (actualRightPage == page) {
//		return; 
//	}
	
	switch(page) {
	
	// RIGHT PART
	case 'page-referencie-ivan':
		//stranka je prave zobrazena
		if (actualRightPage == page) {
			return; 
		}
		
		//options je ID tabu, ktory ma byt zobrazeny v referenciach -> podla toho switchnem tab najskor
		if (options == '2') {
			switchTab('ivan', '2', '1');
		} else {
			switchTab('ivan', '1', '2');
		}
		
		$(actualRightPage).fade({
			duration: 0.2, 
			afterFinish: function(){
				$('page-referencie-ivan-chooser').hide();
				$('page-referencie-ivan').appear({duration:0.2});
				$('profile-referencie-taby-ivan').slideUp({duration: 0.1});
				if (! $("profile-info-content-ivan").visible()) $('profile-info-content-ivan').slideDown({duration: 0.1});
				$('profile-contact-ivan').slideUp({duration: 0.1});
			}
		});
		actualRightPage = "page-referencie-ivan";
		titleSelect('ivan');
		break;
		
	case 'page-referencie-ivan-chooser':
		//stranka je prave zobrazena
		if (actualRightPage == 'page-referencie-ivan') {
			return; 
		}
		
		$('referencie-taby-ivan').hide();
		
		$(actualRightPage).fade({
			duration: 0.2, 
			afterFinish: function(){ 
				$('page-referencie-ivan-clanok-1').hide();
				$('page-referencie-ivan-clanok-2').hide();
				$('page-referencie-ivan-chooser').show();
				//alert('hsit');
				removeHighlightBox('referencie-taby-ivan-tab-1', 'color-ivan', true);
				removeHighlightBox('referencie-taby-ivan-tab-2', 'color-ivan', true);
				$('page-referencie-ivan').appear({duration:0.2});
				if ($('profile-referencie-taby-ivan').visible()) $('profile-referencie-taby-ivan').slideUp({duration: 0.1});
				if ($('profile-contact-ivan').visible()) $('profile-contact-ivan').slideUp({duration: 0.1});
				if (! $("profile-info-content-ivan").visible()) $('profile-info-content-ivan').slideDown({duration: 0.1});
			}
		});
		
		actualRightPage = "page-referencie-ivan";
		titleSelect('ivan');
		break;		
		
	case 'mainpage-magda':
		//stranka je prave zobrazena
		if (actualRightPage == page) {
			return; 
		}
		
		$(actualRightPage).fade({
			duration: 0.2, 
			afterFinish: function(){ 
				$('mainpage-magda').appear({duration:0.2}); 
				if (! $('profile-referencie-taby-ivan').visible()) $('profile-referencie-taby-ivan').slideDown({duration: 0.1});
				if (! $('profile-contact-ivan').visible()) $('profile-contact-ivan').slideDown({duration: 0.1});
			}
		});
		actualRightPage = "mainpage-magda";
		//titleSelect('none');
		break;
		
	case 'page-kontakt-ivan':
		//stranka je prave zobrazena
		if (actualRightPage == page) {
			return; 
		}
		
		$(actualRightPage).fade({
			duration: 0.2, 
			afterFinish: function(){ 
				$('page-kontakt-ivan').appear({duration:0.2}); 
				if (! $("profile-info-content-ivan").visible()) $('profile-info-content-ivan').slideDown({duration: 0.1});
				if ($('profile-contact-ivan').visible()) $('profile-contact-ivan').slideUp({duration: 0.1});
			}
		});
		
		actualRightPage = "page-kontakt-ivan";
		titleSelect('ivan');
		break;
		
	case 'page-profil-ivan':
		//stranka je prave zobrazena
		if (actualRightPage == page) {
			return; 
		}
		
		$(actualRightPage).fade({
			duration: 0.2, 
			afterFinish: function(){ 
				$('page-profil-ivan').appear({duration:0.2}); 
				if (! $('profile-referencie-taby-ivan').visible()) $('profile-referencie-taby-ivan').slideDown({duration: 0.1});
				if ($('profile-info-content-ivan').visible()) $('profile-info-content-ivan').slideUp({duration: 0.1});
				if ($('profile-contact-ivan').visible()) $('profile-contact-ivan').slideUp({duration: 0.1});
			}
		});
		
		actualRightPage = "page-profil-ivan";
		titleSelect('ivan');
		
		break;
		
		
		
	// LEFT PART
	case 'page-referencie-magda':
		//stranka je prave zobrazena
		if (actualLeftPage == page) {
			return; 
		}
		
		//options je ID tabu, ktory ma byt zobrazeny v referenciach -> podla toho switchnem tab najskor
		if (options == '2') {
			switchTab('magda', '2', '1');
		} else {
			switchTab('magda', '1', '2');
		}
		
		$(actualLeftPage).fade({
			duration: 0.2, 
			afterFinish: function(){ 
				$('page-referencie-magda-chooser').hide();
				$('page-referencie-magda').appear({duration:0.2});
				if ($('profile-referencie-taby-magda').visible()) $('profile-referencie-taby-magda').slideUp({duration: 0.1});
				if ($('profile-contact-magda').visible()) $('profile-contact-magda').slideUp({duration: 0.1});
				if (! $("profile-info-content-magda").visible()) $('profile-info-content-magda').slideDown({duration: 0.1});
			}
		});
		
		actualLeftPage = "page-referencie-magda";
		titleSelect('magda');
		break;
		
	case 'page-referencie-magda-chooser':
		//stranka je prave zobrazena
		if (actualLeftPage == 'page-referencie-magda') {
			return; 
		}
		
		$('referencie-taby-magda').hide();
		
		$(actualLeftPage).fade({
			duration: 0.2, 
			afterFinish: function(){ 
				$('page-referencie-magda-clanok-1').hide();
				$('page-referencie-magda-clanok-2').hide();
				$('page-referencie-magda-chooser').show();
				//alert('hsit');
				removeHighlightBox('referencie-taby-magda-tab-1', 'color-magda', true);
				removeHighlightBox('referencie-taby-magda-tab-2', 'color-magda', true);
				$('page-referencie-magda').appear({duration:0.2});
				if ($('profile-referencie-taby-magda').visible()) $('profile-referencie-taby-magda').slideUp({duration: 0.1});
				if ($('profile-contact-magda').visible()) $('profile-contact-magda').slideUp({duration: 0.1});
				if (! $("profile-info-content-magda").visible()) $('profile-info-content-magda').slideDown({duration: 0.1});
			}
		});
		
		actualLeftPage = "page-referencie-magda";
		titleSelect('magda');
		break;
		
	case 'mainpage-ivan':
		//stranka je prave zobrazena
		if (actualLeftPage == page) {
			return; 
		}
		
		$(actualLeftPage).fade({
			duration: 0.2, 
			afterFinish: function(){ 
				$('mainpage-ivan').appear({duration:0.2});
				if (! $('profile-referencie-taby-magda').visible()) $('profile-referencie-taby-magda').slideDown({duration: 0.1});
				if (! $('profile-contact-magda').visible()) $('profile-contact-magda').slideDown({duration: 0.1});
			}
		});
		actualLeftPage = "mainpage-ivan";
		//titleSelect('none');
		break;
		
		
	case 'page-kontakt-magda':
		//stranka je prave zobrazena
		if (actualLeftPage == page) {
			return; 
		}
		
		$(actualLeftPage).fade({
			duration: 0.2, 
			afterFinish: function(){ 
				$('page-kontakt-magda').appear({duration:0.2}); 
				if (! $("profile-info-content-magda").visible()) $('profile-info-content-magda').slideDown({duration: 0.1});
				if ($('profile-contact-magda').visible()) $('profile-contact-magda').slideUp({duration: 0.1});
			}
		});
		
		actualLeftPage = "page-kontakt-magda";
		titleSelect('magda');
		break;
		
	case 'page-profil-magda':
		//stranka je prave zobrazena
		if (actualLeftPage == page) {
			return; 
		}
		
		$(actualLeftPage).fade({
			duration: 0.2, 
			afterFinish: function(){ 
				$('page-profil-magda').appear({duration:0.2}); 
				if (! $('profile-referencie-taby-magda').visible()) $('profile-referencie-taby-magda').slideDown({duration: 0.1});
				if ($('profile-info-content-magda').visible()) $('profile-info-content-magda').slideUp({duration: 0.1});
				if ($('profile-contact-magda').visible()) $('profile-contact-magda').slideUp({duration: 0.1});
			}
		});
		
		actualLeftPage = "page-profil-magda";
		titleSelect('magda');
		
		break;
		
		
	} //switch
	
	if ($("gallery")) $("gallery").hide();
	if ($("gallery-ivan")) $("gallery-ivan").hide();
	
}


function galleryNavigateRight(who) {
	
	if (actualPhotoSet == lastPhotoSet - 1) {
		$("navigate-right-img-"+who).fade({duration:0.4});
	}
	
	if (actualPhotoSet == 1) {
		$("navigate-left-img-"+who).appear();
	}
	
	if (actualPhotoSet >= lastPhotoSet) {
		return;
	}
	
	var nextPhotoSet = actualPhotoSet + 1;

	$("photoset-" + actualPhotoSet + "-" +who).morph(
		"opacity: 0; top: -20px",
		{ 
			duration: 0.2,
			afterFinish: function(){
				$("photoset-" + actualPhotoSet + "-" +who).hide();
				//$("photoset-" + actualPhotoSet).setStyle("left: 0px");
				$("photoset-" + actualPhotoSet + "-" +who).style.top = "0px";
				
				$("photoset-" + nextPhotoSet + "-" +who).style.top = "20px";
				$("photoset-" + nextPhotoSet + "-" +who).style.opacity = 0;
				$("photoset-" + nextPhotoSet + "-" +who).show();
				$("photoset-" + nextPhotoSet + "-" +who).morph("opacity: 1; top: 0px", {duration:0.2});
				actualPhotoSet += 1;
			}
		}
	);
	
//	$("photoset-" + actualPhotoSet).fade({
//		duration: 0.2,
//		afterFinish: function(){
//			$("photoset-" + nextPhotoSet).appear({ duration:0.2 });
//		}
//	});
	
	
//	actualPhotoSet += 1;
}

function galleryNavigateLeft(who) {
	if (actualPhotoSet == 2) {
		$("navigate-left-img-"+who).fade({duration: 0.4});
	}
	
	if (actualPhotoSet == lastPhotoSet) {
		$("navigate-right-img-"+who).appear();
	}
	if (actualPhotoSet <= 1) {
		return;
	}
	
	var prevPhotoSet = actualPhotoSet - 1;
	
	$("photoset-" + actualPhotoSet + "-" + who).morph(
		"opacity: 0; top: 20px",
		{ 
			duration: 0.2,
			afterFinish: function(){
				$("photoset-" + actualPhotoSet + "-" + who).hide();
				//$("photoset-" + actualPhotoSet).setStyle("left: 0px");
				$("photoset-" + actualPhotoSet + "-" + who).style.top = "0px";
				
				$("photoset-" + prevPhotoSet + "-" + who).style.top = "-20px";
				$("photoset-" + prevPhotoSet + "-" + who).style.opacity = 0;
				$("photoset-" + prevPhotoSet + "-" + who).show();
				$("photoset-" + prevPhotoSet + "-" + who).morph("opacity: 1; top: 0px", {duration:0.2});
				actualPhotoSet -= 1;
			}
		}
	);
	
	
//	actualPhotoSet -= 1;
}

function gallerySetPhoto(webpathMedium, webpathFull, who) {
	//alert(webpathMedium);
	
	$("gallery-photo-"+who).style.background = " url('" + webpathMedium + "') center center";
	$("gallery-photo-"+who).innerHTML = "<a href=\""+webpathFull+"\" rel=\"lightbox\"><img width=\"480\" height=\"360\" src=\""+blankGif+"\" alt=\"\" /></a>;"
}

/**
 * "Klikne" na prvy thumb v slideri
 * @return
 */
function gallerySetFirstPhoto(who) {
	if ($("photoset-1-"+who)) {
		var src = $("photoset-1-"+who).down().title;
		$("gallery-photo-"+who).style.background = " url('" + src + "') center center";
		var webpathFull = src.replace("-medium", "");
		$("gallery-photo-"+who).innerHTML = "<a href=\""+webpathFull+"\" rel=\"lightbox\"><img width=\"480\" height=\"360\" src=\""+blankGif+"\" alt=\"\" /></a>;"
	}
}

function checkGalleryPosition() {
	//$("gallery").style.top = 0;
	var limit = $("main").cumulativeOffset()[1] + $("main").getHeight();
	var galleryLimitTmp = $("gallery").cumulativeOffset()[1] + $("gallery").getHeight();
	
	var documentTop = document.viewport.getScrollOffsets()[1];
	if (documentTop > 550) {
		var diff = documentTop - 550;
		if (galleryLimitTmp > limit) {
			diff = diff - ( galleryLimitTmp - limit ) - 10;
		}
		$("gallery").setStyle({"top":diff+"px"});
	} else {
		$("gallery").setStyle({"top":"0px"});
	}
}

function toogleSubpage(ele) {
	if ($(ele).visible()) {
		$(ele).slideUp({
			duration:0.2,
			afterFinish: function(){
				checkGalleryPosition();
			}
		});
	} else {
		$(ele).slideDown({
			duration:0.2,
			afterFinish: function(){
				checkGalleryPosition();
			}
		});
	}
}
