var hello_box_height = 120;
var before_hello_open_height = 0;
var current_slide = 0;
var slideshow_paused = false;
var slideshow_speed = 3000;
var interval = "";

var app = {
	initialise: function() {
		app.log('initialised');
		//alert('initialised');
	},
	
	log: function(message) {
		if(typeof console != 'undefined') console.log(message);
	}

};

var is_animating = {
	panes: function() {
		if( $('#on_box:animated').length > 0 || $('#content_holder:animated').length > 0 || $('#callouts:animated').length > 0 ){
			return true;
		}else{
			return false;
		}
	},
	
	slides :function() {
		if( $('#slides .slide:animated').length > 0){
			return true;
		}else{
			return false;
		}
	}
};

var main_menu = {
	initialise: function() {
		$('#header .menu ul a').each(function(index){
			$(this).click(function() {
				document.title = 'Jean-Marc Denis - ' + $(this).text();
				panes.go_to(index);
				return false;
			});
		});
	},
	
	highlight: function(index) {
		$('#on_box').animate({left:(index*75)},333);
		$('#header .menu ul a').removeClass('on');
		$('#header .menu ul a:eq('+index+')').addClass('on');
	}
};

var hello_box = {
	initialise: function() {
		$('#header .logo, #hello_box .close').click(function(){
			$('#hello_box').toggleClass('showed');
			return false;
		});
	},

	toggle: function() {
		if(!is_animating.panes()){
			if( $('#hello_box').hasClass('open') ){
				hello_box.hide(false);
			}else{
				hello_box.show();
			}
		}
	},
	
	show: function() {
		if( !$('#hello_box').hasClass('open') ){
			$('#hello_box').addClass('open').slideDown(333);
			before_hello_open_height = $('#content_holder').height();
			$('#content_holder').animate( {height: (before_hello_open_height - hello_box_height), opacity: 0.5  }, 333 );		
			$('#callouts').animate( {opacity: 0.5}, 333 );
		}
	},
	
	hide: function(is_content_height_taken_care_of) {
		if( $('#hello_box').hasClass('open') ){
			$('#hello_box').removeClass('open').slideUp(333);
			if(is_content_height_taken_care_of){
				$('#callouts').animate( {opacity: 1}, 333 );
			}else{
				$('#content_holder').animate( {height: before_hello_open_height, opacity: 1  }, 333 );
				$('#callouts').animate( {opacity: 1}, 333 );
			}
		}
	}
};

var panes = {
	initialise: function() {
		panes.fix_heights();
	},
	
	go_to: function(index){
			main_menu.highlight(index);
			hello_box.hide(true);
		
			var left = index * 800;
			$('#strip, #callout_holder').animate(
				{ left: (left * -1) },
				333,
				function() {
					app.log('finished');
			});
		
			var target_height = $('#strip .pane:eq('+index+')').height();
			var min_target_height = 300;
			if(target_height < min_target_height){ target_height = min_target_height; }
		
			$('#content_holder').animate(
				{ height: target_height, opacity: 1 },
				333,
				function() {
					app.log('finished');
				}
			);
		
	},
	
	fix_heights: function() {
		$('#strip .pane').each(function() {
			$(this).height($(this).height());
		});
	}
};

var slideshow = {
	initialise: function() {
		$('#next_slide').click(function(){
			if(!is_animating.slides()  && !($('#hello_box').hasClass('open')) ){
				slideshow.next_slide();
			}
			hello_box.hide(false);
			return false;
		});
		$('#prev_slide').click(function(){
			if(!is_animating.slides()  && !($('#hello_box').hasClass('open')) ){
				slideshow.prev_slide();
			}
			hello_box.hide(false);
			return false;
		});
		
		$('.slide').click(function(){
			if(!is_animating.slides() && !($('#hello_box').hasClass('open')) ){
				slideshow.next_slide();
			}
			hello_box.hide(false);
			return false;
		});
		

		$(".pane:eq(0)").mouseenter(function(){
			slideshow.pause();
			$('#next_slide, #prev_slide').dequeue().fadeTo(333, 1);
	    }).mouseleave(function(){
			slideshow.play();
			$('#next_slide, #prev_slide').dequeue().fadeTo(333, 0);
	    });
		
	},
	
	next_slide: function() {
		$('#slides .slide:eq('+current_slide+')').fadeOut(333, function(){
			if(current_slide == ($('#slides .slide').length - 1)){
				current_slide = 0;
			}else{
				current_slide = current_slide + 1;
			}
			slideshow.fade_in_current();
		});
	},
	
	prev_slide: function() {
		$('#slides .slide:eq('+current_slide+')').fadeOut(333, function(){
			if(current_slide == 0){
				current_slide = ($('#slides .slide').length - 1);
			}else{
				current_slide = current_slide - 1;
			}
			slideshow.fade_in_current();
		});
	},
	
	fade_in_current: function() {
		$('#slides .slide:eq('+current_slide+')').fadeIn(333);
	},
	
	slide_interval: function() {
		if(!slideshow_paused){
			if(!$('#hello_box').hasClass('open')){
				slideshow.next_slide();
			}
		}else{
			clearInterval(interval);
		}
	},
	
	pause: function() {
		if(!slideshow_paused){
			slideshow_paused = true;
		}
	},
	
	play: function() {
		if(slideshow_paused){
			slideshow_paused = false;
			clearInterval(interval);
			interval = setInterval("slideshow.slide_interval();", slideshow_speed);
		}
	}
};

var looking = {
	initialise: function() {
		$('.slide').each(function(index){
			if(index != 0){
				$(this).hide();
			}
		});
		
		$('#looking').delay(2000).fadeOut(333, function(){
			$('#slideshow').fadeIn(333);
			clearInterval(interval);
			interval = setInterval("slideshow.slide_interval();", slideshow_speed);
		});
	}
};

var goodies = {
	initialise: function() {
		$('#goodies li').find('.overlay').dequeue().animate( {opacity:0}, 0);
		
		
		$('#goodies li').mouseenter(function(){
			if( !$('#hello_box').hasClass('open') ){
				$(this).find('.overlay').dequeue().animate( {opacity:1}, 250);
			}
		}).mouseleave(function(){
			if(!$('#hello_box').hasClass('open')){
				$(this).find('.overlay').dequeue().animate( {opacity:0}, 400);
			}
		});
	}
};

var contact_form = {
	initialise: function() {
		$('#submit_contact').click(function(){
			$(this).closest('form').submit();
			return false;
		});
		
		$('#contact_form').submit(function(){
			
			if( !(contact_form.validate_email( $('#contact_email').val() ))   ){
				$('#result').html('<span class="error_text" >Invalid email adress...</span>');
				$('#contact_email').focus().select();
			}else{
				contact_form.ajax_submit($(this));
			}			
			return false;
		});
		
	},
	
	ajax_submit: function(form) {
		
		$('#submit_contact').text('Sending...').addClass('sending');
		
		var form_data = form.serialize();
		
		$.ajax({
		    type: "POST",
		    url: form.attr('action'),
		    data: form_data,
		    success: function(data) {
				$('#submit_contact').text('Send').removeClass('sending');			
				$('#result').html(data);
		    }
		  });
	},
	
	validate_email: function(email_address) {
		var pattern = new RegExp(/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i);
		return pattern.test(email_address);
	}
};

$(document).ready(function(){
	app.initialise();
	main_menu.initialise();
	hello_box.initialise();
	panes.initialise();
	slideshow.initialise();
	goodies.initialise();
	contact_form.initialise();
	$('input[placeholder],textarea[placeholder]').placeholder();
});

$(window).load(function(){
	looking.initialise();
});

