$(document).ready(function(){

	$('#menu-topmenu li').click(function(){
		location.href = $(this).find('a').attr('href');
	});
	
	$('.content a > img').parent().addClass('picLink');
	
	$('a.fancyThis').fancybox();
	
	/* add fancybox to images linking to images */
	/* ---------------------------------------- */
	$('.content a > img').each(function(){
		var	href = $(this).parent().attr('href');
		var dotpos = href.lastIndexOf('.');
		var ending  = href.substring(dotpos + 1);
		if(ending == 'jpg' || ending == 'jpeg' || ending == 'png' || ending == 'gif'){
			$(this).parent().fancybox();
		}
	});
	
	/* Reasons effect */
	/* -------------- */
	var reasonHeight = $('#reasonText').css('display', 'block').height();
	$('#reasonText').css('height', 0);
	$('#reasons').hover(function(){
		$('#reasonText').stop(true, false).animate({'height': reasonHeight}, 300);
	}, function(){
		if(parseInt($('#reasonText').css('height')) >= 400)
			$('#reasonText').stop(true, false).animate({'height': 0}, 300);	
		else
			$('#reasonText').stop(true, false).delay(300).animate({'height': 0}, 300);	
	});
	
	$('#reasons').click(function(){
		$('#reasonText').stop(true, false).delay(300).animate({'height': 0}, 300);	
		location.href = $(this).find('a').attr('href');
		return false;
	});
	
	/* Gallery */
	/* ------- */
	var gallery = $('#gallery > img');
	if(gallery.length > 1){
		var galleryIndex = 0;
		var nextIndex = 0;
		var delay = 5000;
		locked = false;
				
		var Switch = function(){
			if(arguments.length > 0){
				nextIndex = parseInt(arguments[0]);
				$(gallery[galleryIndex]).clearQueue('fx').animate({'opacity': 0}, 800);
				$(gallery[nextIndex]).clearQueue('fx').animate({'opacity': 1},800, function(){
					locked = false;
				});
				galleryIndex = nextIndex;
			}
			else{
				nextIndex = (nextIndex + 1) >= gallery.length ? 0 : nextIndex + 1;
				
				$(gallery[galleryIndex]).delay(delay).animate({'opacity': 0}, 800);
				$(gallery[nextIndex]).delay(delay).animate({'opacity': 1}, 800, function(){
					galleryIndex = nextIndex;
					Switch()
				});
			}
		}
		
		Switch();
	
		$('#gallery .arrow').click(function(){
			if(locked == false){
				locked = true;
				if($(this).hasClass('left'))
					var newIndex = (galleryIndex - 1) < 0 ? gallery.length - 1 : galleryIndex - 1;
				else
					var newIndex = (galleryIndex + 1) >= gallery.length ? 0 : galleryIndex + 1;
					
				Switch(newIndex);
			}
		});
		
	}
});
