/*	Global Configuration
----------------------------------------------- */
function init() {
	document.body.className += "loading js";
}

$(function() {
	Setup.init();
	Navigation.init();
	Offset.init();
	
	Hosting.init();
	Voice.init();
	Broadcasting.init();
	Podcasting.init();
	Testimonials.init();
	AboutMe.init();
	ContactMe.init();
	Player.init();
	
	$(window).load(function(){
		$(document.body).removeClass('loading');
		$.doTimeout( 'load', 1000, function(){
			Offset.set();
			$('#Navigation a[href=#AboutMe]').click();
		});
		
	});
	
	$(window).resize(function() {
		$.doTimeout( 'resize', 200, function(){
			Offset.set();
			$('#Navigation a[href='+Navigation.Current+']').click();
		});
	});	
	
});

var Setup = {	
	Body: null,
	init: function() {
		var cc = this;
		cc.Body = $(document.body);
		cc.Body.addClass('jsReady');
	
		// external links
		$("a[rel='external']").each(function() {
			var link = $(this) 	;
			link.click(function() {
				return !window.open(link.attr('href'));
			});
		});	
		
		$('#Branding, #PodcastingContactMeLink').click(function(){
			$('#Navigation a[href=#ContactMe]').click();
			return false;
		});
		
		$('#Logo').click(function(){
			$('#Navigation a[href=#AboutMe]').click();
			return false;
		});
	}
};

var Offset = {
	Value: null,
	Container: null,
	init: function() {
		var cc = this;
		cc.Container = $('#Content');
		cc.set();
	},
	set: function() {
		var cc = this;
		cc.Value = (cc.Container.width()-960)/2;
	}
};

var Navigation = { 
	Triggers: null,
	Current: null,
	Container: null,
	init: function() {
		var cc = this;
		// initialize scrollable 
		cc.Triggers = $('#Navigation a');
		cc.Container = $('#Navigation');
		cc.events();
	},
	events: function() {
		var cc = this;
		// click
		cc.Triggers.click(function() {
			
			$("body").stop(true);
			Player.stop();
			
			var target = $(this).attr('href');	
			
			$('#Content').scrollTo(target, 1000, { 
				queue: false, 
				easing: 'easeInOutQuart', 
				offset: {left: -Offset.Value, top: 0},
				onAfter: function() { 
					
					if(cc.Current!=target) {
						Hosting.reset();
						Voice.reset();
						Broadcasting.reset();
						Podcasting.reset();
						Testimonials.reset();
						AboutMe.reset();
						ContactMe.reset();
					}
					
					if(target=='#Hosting') {
						Hosting.view();
					}
					else if(target=='#Voice') {
						Voice.view();
					}
					else if(target=='#Broadcasting') {
						Broadcasting.view();
					}
					else if(target=='#Podcasting') {
						Podcasting.view();
					}
					else if(target=='#Testimonials') {
						Testimonials.view();
					}
					else if(target=='#AboutMe') {
						AboutMe.view();
					}
					else if(target=='#ContactMe') {
						ContactMe.view();
					}
	
					cc.Current = target;
				}
			});
			cc.Triggers.removeClass('selected');
			$(this).addClass('selected')
			return false;
		});
		cc.Triggers.mouseenter(function(){
			var hover = $(this).parent().attr('class');
			cc.Container.attr('class', hover+' hover');
		});
		cc.Triggers.mouseleave(function(){
			cc.Container.attr('class', '');
		});
	}
};

var Hosting = {
	Info: null,
	init: function() {
		var cc = this;
		cc.Info = $('#Hosting div.info');
		cc.reset();
	},
	view: function() {
		var cc = this;
		cc.Info.show();
		cc.Info.animate({ 
	        left: "300px"
	    }, 600, 'easeInOutExpo');
	},
	reset: function() {
		var cc = this;
		cc.Info.hide();
		cc.Info.css('left', '-1200px');
	}
};

var Player = {
 	Api: null,
	init: function() {
		var cc = this;
		cc.Api = $("#Player");
		cc.Api.jPlayer();
	},
	play: function(file) {
		var cc = this;
		cc.Api.jPlayer("clearFile");
		cc.Api.jPlayer("setFile", file);
		cc.Api.jPlayer("play");
	},
	stop: function() {
		var cc = this;
		cc.Api.jPlayer("stop");
	}
};

var Voice = {
	Info: null,
	Triggers: null,
	init: function() {
		var cc = this;
		cc.Info = $('#Voice div.info');
		cc.Triggers = $('#Voice div.item a');
		
		var results =[];
		var elements = $("#Voice div.items").children('.item');
        $.map(elements, function(i, n){
			if(n%3 === 0 ){
				results.push(n);
			}
        });
        $.each(results , function(i,v){
			elements.slice(v, v+3).wrapAll('<div class="page"></div>');
        });

		$("#Voice .scrollable").scrollable();	
		cc.reset();
		cc.events();
	},
	view: function() {
		var cc = this;
		cc.Info.show();
		cc.Info.animate({ 
	        top: "223px"
	    }, 600, 'easeInOutExpo');
	},
	reset: function() {
		var cc = this;
		cc.Info.hide();
		cc.Info.css('top', '700px');
		cc.Triggers.removeClass('pause');
	},
	events: function() {
		var cc = this;
		cc.Triggers.click(function(){
			if($(this).hasClass('pause')) {
				cc.Triggers.removeClass('pause');
				Player.stop();
			}
			else {
				Player.play($(this).attr('href'));
				cc.Triggers.removeClass('pause');
				$(this).addClass('pause');
			}
			
			return false;
		});
	}
};

var Broadcasting = {
	Info: null,
	Star: null,
	Triggers: null,
	init: function() {
		var cc = this;
		cc.Info = $('#Broadcasting div.info');
		cc.Star = $('#Broadcasting div.star');
		cc.Triggers = $('#Broadcasting div.item a');
		$("#Broadcasting .scrollable").scrollable();	
		cc.reset();
		cc.events();
	},
	view: function() {
		var cc = this;
		cc.Info.show();
		cc.Info.animate({ 
	        left: "450px"
	    }, 600, 'easeInOutExpo', function(){
			cc.Star.show();
			cc.Star.animate({ 
		        top: "100px"
		    }, 600, 'easeInOutExpo');
		});
	},
	reset: function() {
		var cc = this;
		cc.Info.hide();
		cc.Star.hide();
		cc.Info.css('left', '2000px');
		cc.Star.css('top', '-500px');
	},
	events: function() {
		var cc = this;
		cc.Triggers.click(function(){
			if($(this).hasClass('pause')) {
				cc.Triggers.removeClass('pause');
				Player.stop();
			}
			else {
				Player.play($(this).attr('href'));
				cc.Triggers.removeClass('pause');
				$(this).addClass('pause');
			}

			return false;
		});
	}
};

var Podcasting = {
	Info: null,
	Drop: null,
	Trigger: null,
	init: function() {
		var cc = this;
		cc.Info = $('#Podcasting div.info');
		cc.Drop = $('#Podcasting div.drop');
		cc.Trigger = $('#PodcastTrigger');
		cc.reset();
		cc.events();
	},
	events: function() {
		var cc = this;
		cc.Trigger.click(function(){
			if($(this).hasClass('pause')) {
				cc.Trigger.removeClass('pause');
				Player.stop();
			}
			else {
				Player.play($(this).attr('href'));
				$(this).addClass('pause');
			}

			return false;
		});
	},
	view: function() {
		var cc = this;
		cc.Info.show();
		cc.Info.animate({ 
	        top: "110px"
	    }, 600, 'easeInOutExpo', function(){
			cc.Drop.show();
			cc.Drop.animate({ 
		        top: "100px"
		    }, 600, 'easeOutBounce');
		});
	},
	reset: function() {
		var cc = this;
		cc.Info.hide();
		cc.Drop.hide();
		cc.Info.css('top', '800px');
		cc.Drop.css('top', '-100px');
	}
};



var Testimonials = {
	Info: null,
	init: function() {
		var cc = this;
		cc.Info = $('#Testimonials div.info');
		$("#Testimonials div.info ul.tabs").tabs("#Testimonials div.info div.panes > div", {current: 'selected'});
		cc.reset();
	},
	view: function() {
		var cc = this;
		cc.Info.show();
		cc.Info.animate({ 
	        left: "90px"
	    }, 600, 'easeInOutExpo');
	},
	reset: function() {
		var cc = this;
		cc.Info.hide();
		cc.Info.css('left', '-2000px');
	}
};

var AboutMe = {
	Info: null,
	Drop: null,
	init: function() {
		var cc = this;
		cc.Info = $('#AboutMe div.info');
		cc.Drop = $('#AboutMe div.drop');
		cc.reset();
	},
	view: function() {
		var cc = this;
		cc.Info.show();
		cc.Info.animate({ 
	        left: "235px"
	    }, 600, 'easeInOutExpo', function(){
			cc.Drop.show();
			cc.Drop.animate({ 
		        top: "25px"
		    }, 600, 'easeOutBounce');
		});
		cc.Drop.find('a.link-2').click(function(){
			$('#Navigation a[href=#ContactMe]').click();
			return false;
		});
		cc.Drop.find('a.link-1').click(function(){
			if($(this).hasClass('pause')) {
				$(this).removeClass('pause');
				Player.stop();
			}
			else {
				Player.play($(this).attr('href'));
				$(this).addClass('pause');
			}
			return false;
		});
		
		
		
	},
	reset: function() {
		var cc = this;
		cc.Info.hide();
		cc.Drop.hide();
		cc.Info.css('left', '2000px');
		cc.Drop.css('top', '-100px');
	}
};

var ContactMe = {
	Info: null,
	Validator: null,
	Drop: null,
	init: function() {
		var cc = this;
		cc.Info = $('#ContactMe div.info');
		cc.Drop = $('#ContactMe div.drop');
	 	$('#ContactMeForm').validator({
			position: 'top left', 
			offset: [10, 170]
		});
		cc.Drop.click(function(){
			$('#Navigation a[href=#AboutMe]').click();
		});
		$("#ContactMeForm").validator().submit(function(e) {
			var form = $(this);

			// client-side validation passed
			if (!e.isDefaultPrevented()) {
				
				$.fancybox.showActivity();

				// submit the data to the server with AJAX
				$.post(form.attr("action"), $(form).serialize(), function(){
					$.fancybox('<p><strong>Thanks for your message, <br />I’ll get back to you asap.</strong><br /><br />“Lots of people act well, but few people talk well.<br />This shows that talking is the more difficult of the two.” <br />Oscar Wilde<br /><br />Until then,<br />Natalie B x</p>', {
						'autoDimensions'	: false,
						'width'         	: 350,
						'height'        	: 250,
						'transitionIn'		: 'none',
						'transitionOut'		: 'none'
						}
					);
				}, 'json');
				
				
			

				// prevent default form submission logic
				e.preventDefault();
			}
		});
		
		cc.Validator =  $('#ContactMeForm').data("validator");
		
				
		cc.reset();
	},
	view: function() {
		var cc = this;
		cc.Info.show();
		cc.Info.animate({ 
	        top: "45px"
	    }, 600, 'easeInOutExpo', function() {
			cc.Drop.show();
			cc.Drop.animate({ 
		        top: "25px"
		    }, 600, 'easeOutBounce');
		});
	},
	reset: function() {
		var cc = this;
		cc.Info.hide();
		cc.Drop.hide();
		cc.Info.css('top', '1000px');
		cc.Drop.css('top', '-100px');
		cc.Validator.reset();
	}
};
