jQuery(document).ready(function($) {	
	// Back to top
	$('.panel-backtotop a').click(function() {
		$('html,body').animate({ scrollTop: 0 }, 1000);
		
		return false;
	});
	
	
	// Aweber newsletter
	$('#awebername').focus(function() {
		if ($(this).val() == 'Your name ') $(this).val('');
	}).blur(function() {
		if ($(this).val() == '') $(this).val('Your name ');
	});
	
	$('#aweberemail').focus(function() {
		if ($(this).val() == 'Your email ') $(this).val('');
	}).blur(function() {
		if ($(this).val() == '') $(this).val('Your email ');
	});
	
	$('.form-fitnessmodelbook').submit(function() {
		var errorMessage = '';
		
		if ($('#awebername').val() == 'Your name ') errorMessage += "Invalid name.\r\n";
		
		reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		
		if ($('#aweberemail').val() == 'Your email ' || !reg.test($('#aweberemail').val())) errorMessage += "Invalid email address.\r\n";
		
		if (errorMessage != '') { alert(errorMessage); return false; }
	});
	
	
	// Searchbar input
	$('#input-keyword').focus(function() {
		if ($(this).val() == 'enter search keyword') $(this).val('');
	}).blur(function() {
		if ($(this).val() == '') $(this).val('enter search keyword');
	});
	
	
	//Share this image
	$('.panel-share .image-hover').each(function() { // Preload
		var src = $(this).attr('src');
		var hoversrc = src;
		var img = new Image();
		
		hoversrc = hoversrc.substr(0, hoversrc.lastIndexOf('.'));
		img.src = hoversrc + '_off.gif';
	}).hover(function() {
		var src = $(this).attr('src');
		
		src = src.substr(0, src.lastIndexOf('.'));
		
		$(this).attr('src', src + '_off.gif');
	}, function() {
		var src = $(this).attr('src');
		
		src = src.substr(0, src.lastIndexOf('_'));
		
		$(this).attr('src', src + '.gif');
	});
	
	
	// Panel icon
	$('.panel-icon .image-hover').each(function() { // Preload
		var src = $(this).attr('src');
		var hoversrc = src;
		var img = new Image();
		
		hoversrc = hoversrc.substr(0, hoversrc.lastIndexOf('.'));
		img.src = hoversrc + '_off.png';
	}).hover(function() {
		var src = $(this).attr('src');
		
		src = src.substr(0, src.lastIndexOf('.'));
		
		$(this).attr('src', src + '_off.png');
	}, function() {
		var src = $(this).attr('src');
		
		src = src.substr(0, src.lastIndexOf('_'));
		
		$(this).attr('src', src + '.png');
	});
	
	
	// Showcase
	
	var showcaseTimer = null;
	
	$('.listing-headline a').click(function() {
		var index = $(this).parent().parent().find('li').index($(this).parent());
		
		var imgsrc = $('img', this).attr('src');
		
		clearInterval(showcaseTimer);
		
		if (imgsrc.substr(imgsrc.lastIndexOf('-')) != '-on.png') {
			
			$('.layer-showcaseitem').stop(true, true);
			
			$('.listing-headline img').attr('src', imgsrc);
		
			imgsrc = imgsrc.substr(0, imgsrc.lastIndexOf('-'));
			
			var newimgsrc = imgsrc + '-on.png';
			
			$('img', this).attr('src', newimgsrc);
			
			$('.layer-showcaseitem:visible').fadeOut('fast', function() {
				$('.layer-showcaseitem:eq('+index+')').fadeIn('fast');
			});
		}
		
		showcaseTimer = setInterval(function() {
			if ($('.layer-showcaseitem:visible').is(':last-child')) $('.listing-headline a:eq(0)').trigger('click');
			else $('.listing-headline img[src$=on.png]').parent().parent().next().find('a').trigger('click');
		}, 6000);
		
		return false;
	});
	
	showcaseTimer = setInterval(function() {
		if ($('.layer-showcaseitem:visible').is(':last-child')) $('.listing-headline a:eq(0)').trigger('click');
		else $('.listing-headline img[src$=on.png]').parent().parent().next().find('a').trigger('click');
	}, 6000);
	
});

jQuery(window).load(function() {
	jQuery('.layer-showcase').height(jQuery('.layer-showcaseitem:eq(0)').height());
});
