$(function() {
	$('input[type=text], textarea')
		.focus(function() {
			if(this.value!=this.title){
				return;
			}
			this.value='';
			$(this).removeClass('inactive');
		})
		.blur(function() {
			if(this.value!='' && this.value != this.title) {
				return;
			}
			this.value=this.title;
			$(this).addClass('inactive');
		})
		.trigger('blur');
});

/*******************************
 * SLIDESHOW JS
 *******************************/

var 
	startTimer,
	startSlideInterval = 5000,
	startSlideSpeed = 800; /* time for fade-in/out */
		
$(function() {
	if($('#start').length > 0) {
		$('#start .slide-info-content').each(function() {
			if(this.innerHTML == '')
				$(this.parentNode).remove();
		});
	
		var infos = $('#start .slide-info:not(:last) .slide-info-content').hide(); // hide all but the last
		var currentInfo = $('#start .slide-info:last .slide-info-content');
		infos.insertBefore(currentInfo);
		var length = $('#start .slide').length;
		for(i = length-1; i >= 0; i--) {
			var elem = $('<div />');
			elem.addClass('slide-button');
			elem.attr('id','start-button-'+i);
			$('#slideshow').append(elem);
			elem.css('right',i*elem.outerWidth()+parseInt(elem.css('right')));
			if(i==length-1) {
				elem.addClass('active');
			}
		}
		$('#start .slide-button').click(function() {
			var elem = $(this);
			if(elem.hasClass('active')) {
				return;
			}
			var regexp = RegExp('[0-9]+','');
			var index = regexp.exec(elem.attr('id'));
			
			var buttonActive = $('#start .slide-button.active');
			var buttonNext = elem;
			var slideActive = $('#start .slide.active');
			var slideNext = $('#start .slide:eq('+index+')');
			
			var infoActive = $('#start .slide-info-content.active');
			var infoNext = $('#start .slide-info-content:eq('+index+')');
			
			startChangeSlide(slideNext, slideActive);
			startChangeSlide(infoNext, infoActive);
			startChangeButton(buttonNext, buttonActive);
		});
		
		$('#start .slide:last').addClass('active');
		$('#start .slide-info-content:last').addClass('active');
		
		$('#slideshow')
			.mouseout(function() { // Starts the timer again
				clearTimeout(startTimer);
				startTimer = setTimeout('startSlideNext()', startSlideInterval);
				
			})
			.mouseover(function() { // Stops the timer on hover
				clearTimeout(startTimer);
			})
			.trigger('mouseout');
	}
});

function startSlideNext() {
	var active = $('#start .slide.active');
	var next = active.prev('.slide');
	
	if(next.length==0) {
		next = $('#start .slide:last');
	}
	
	var activeInfo = $('#start .slide-info-content.active');
	var nextInfo = activeInfo.prev('.slide-info-content');
	if(nextInfo.length==0) {
		nextInfo = $('#start .slide-info-content:last');
	}
	
	var activeButton = $('#start .slide-button.active');
	
	var nextButton = activeButton.next('#start .slide-button');
	if(nextButton.length==0) {
		nextButton = $('#start .slide-button:first');
	}
	
	startChangeSlide(next, active);
	startChangeSlide(nextInfo, activeInfo);
	startChangeButton(nextButton,activeButton);
	startTimer = setTimeout('startSlideNext()', startSlideInterval);
}

function startChangeSlide(next, active) {
	next
		.hide()
		.addClass('active');
	
	active
		.removeClass('active')
		.addClass('last');
	
	next.fadeIn(startSlideSpeed);
	active.fadeOut(startSlideSpeed, function() {
		$(this).removeClass('last');
	});
}

function startChangeButton(next, active) {
	next.addClass('active');
	active.removeClass('active');
}

/*******************
* LIGHTBOX JS
********************/

var joinLightboxSpeed = 400; // speed of transition animation

$(function() {
	$('.lightbox-show')
		.click(function() {
			$('#lightbox').fadeIn(joinLightboxSpeed);
		});
	$('#lightbox-bg,')
		.click(function() {
			$('#lightbox').fadeOut(joinLightboxSpeed);
		});
	
	if($('#lightbox div[title=display], #lightbox .error').length > 0)
		$('#lightbox').show();
});
