var base = (function() {
	
	var $this = this;
	
	this.init = function() {
		
		$("#menu-item-172 a, #menu-item-194 a").attr({href: "/site/contact-us", "box-width": 500, "box-height": 560}).addClass("lightbox iframe");
		
		$this.page.init();
		$this.plugins.init();
		
		$.get(config.site_url + '/beta-confirmation/',function(data) {
			$("#beta_confirmation").html(data);
		});
		
		$.get(config.site_url + '/newsletter-confirmation/',function(data) {
			$("#newsletter_confirmation").html(data);
		});
	}
	
	this.plugins = {
		
		init: function() {
			$this.plugins.popups();
			$this.plugins.lightbox("#content-info");
		},
		
		lightbox: function(elem) {
			if (elem == undefined) {
				elem = "body";
			}
			
			$(elem).find("a.lightbox").each(function() {
				
				var box_width = parseInt($(this).attr("box-width"));
				var box_height = parseInt($(this).attr("box-height"));
				
				if ($(this).hasClass("ajax")) {
					$(this).fancybox({
						transitionIn	:	'elastic',
						transitionOut	:	'elastic',
						speedIn			:	600, 
						speedOut		:	200,
						width			: 	box_width ? box_width : 750,
						height			: 	box_height ? box_height : 500,
						autoDimensions	: false,
						ajax : {
							type	: "POST"
						}
					});
				} else if ($(this).hasClass("iframe")) {
					$(this).fancybox({
						transitionIn	:	'elastic',
						transitionOut	:	'elastic',
						speedIn			:	600, 
						speedOut		:	200,
						width			: 	box_width ? box_width : 750,
						height			: 	box_height ? box_height : 500,
						autoDimensions	: false,
						type			: 'iframe'
					});
				} else {
					$(this).fancybox({
						transitionIn	:	'elastic',
						transitionOut	:	'elastic',
						speedIn			:	600, 
						speedOut		:	200,
						titleShow		: 	false
					});
				}
			});
		},
		
		popups: function() {
			
			$('a.pop').live('click',function(event) {
				
				var width  = $(this).attr("pop-width") ? $(this).attr("pop-width") : 575,
					height = $(this).attr("pop-height") ? $(this).attr("pop-height") : 400,
					left   = Math.ceil((window.screenX || window.screenLeft || 0) + ((window.screen.width  - width)  / 2)),
					top    = Math.ceil((window.screenY || window.screenTop || 0) + ((window.screen.height  - height)  / 2)),
					url    = this.href,
					opts   = 'status=1' +
							 ',width='  +  width  +
							 ',height=' + height +
							 ',top='    + top    +
							 ',left='   + left;
							 
				window.open(url, url, opts);
			
				return false;
			});
			
		}
		
	}
	
	this.forms = {
		
		beta: false,
		
		init: function() {
			
			/* Form input overlabels */
			
			jQuery.fn.overlabel.defaults = {
		 
				label_class:   'overlabel-apply',
				wrapper_class: 'overlabel-wrapper',
				hide_css:      { 'text-indent': '-10000px' },
				fade_css:      { 'text-indent': '0px', 'cursor': 'text', 'opacity': '0.4', 'filter': 'alpha(opacity=40)'  },
				show_css:      { 'text-indent': '0px', 'cursor': 'text', 'opacity': '1.0', 'filter': 'alpha(opacity=100)' },
				filter:        false
		 
			};
			
			$this.forms.form_loaded();
			$this.forms.events();
			
		},
		
		events: function() {
				$("#gform_1").submit(	function() { $("#input_2_4").val($("#input_1_4").val()); if ($('#choice_5_2').is(':checked')) { $this.forms.beta = true } }	);
				$(document).bind('gform_confirmation_loaded', 	$this.forms.submitted);
				$(document).bind('gform_post_render',			$this.forms.form_loaded);
		},
		
		submitted: function(event,form_id) {
			
			switch (form_id) {
				case 1:
					
					if ($this.forms.beta) {
					
						/* Transition to beta signup */
						$("#main,#screenshots").fadeOut();
						$("#gform_widget-4").slideDown();
						$("#sidebar").addClass("beta-signup");	
					
					} else {
						
						$("#sidebar,#main,#screenshots").stop().fadeOut();
						$("#signup_confirmation .newsletter_confirmation").show();
						
						setTimeout(function() {
						  $("#signup_confirmation").fadeIn(function() {
							$("#signup_confirmation .container").show('slide', {direction: 'right'}, 1000, function() {
								$("#signup_confirmation .return-link").slideDown();
							});
						  });
						},800);
						
					}
					
					break;
				case 2:
					
					$("#sidebar").fadeOut();
					$("#signup_confirmation .beta_confirmation").show();
					
					setTimeout(function() {
					  $("#signup_confirmation").fadeIn(function() {
						$("#signup_confirmation .container").show('slide', {direction: 'right'}, 1000, function() {
							$("#signup_confirmation .return-link").slideDown();
						});
					  });
					},800);
					
					break;
			}
			
		},
		
		form_loaded: function() {
			jQuery("#choice_5_1").attr("disabled","disabled");
			jQuery("label.overlabel, li.overlabel label.gfield_label").overlabel();
			jQuery("select, input:checkbox, input:radio, input:file, input:text, textarea").each( function() {
				if (jQuery(this).hasClass("uniform-processed")) {
					return;
				}
				jQuery(this).addClass("uniform-processed");
				jQuery(this).uniform();
			});
		}
		
	}
	
	this.page = {
		
		history: [],
		
		init: function() {
			$this.page.loaded();
			$this.page.events();
		},
		
		loaded: function() {
			
			addthis = undefined;
			$.getScript('http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4e8c982c7090445d&async=1', function() {
				addthis.init();
			});
			
			var page = $("#wrap").attr("slug");
			
			switch (page) {
				case 'home':
				case 'coming-soon':
					$this.page.transition.home();
					break;
				case 'about-us':
				case 'a-propos':
					$this.page.transition.about();
					break;
				case 'careers':
				case 'emplois':
					$this.page.transition.careers();
					break;
				default:
				case 'generic':
					$this.page.transition.generic();
					break;
			}
			
			$this.plugins.lightbox("#wrap");
			$this.forms.init();
			
		},
		
		events: function() {
			$('#screenshots li img').mouseover(		function() { $(this).hoverTransition({src: this.src.replace('_up','_over')}); }	);
			//$('#screenshots li img').live('mouseout',		function() { $(this).transition({src: this.src.replace('_over','_up')}); }	);
			$("a").live('click',	$this.page.click);
		},
		
		click: function(event,href) {
			
			/* Can be called both as a link click event or directly */
			if (href===undefined) {
				href = $(this).attr("href");
			}
			
			if (href===undefined) {
				return;
			}
			
			/* Don't process link clicks that don't go to this website or that are used by other plugins */
			if (href === undefined || href == "" || href.substr(0,1) == "#" || href=="javascript:;" || $(this).hasClass("lightbox") || (href.indexOf(config.site_url)==-1 && (href.indexOf("://")!=-1 || href.substr(0,4)=='mail')) && href != config.site_url) {
				return;
			}
			
			/* Fadeout the current page */
			$("#wrap, #content-info").fadeOut();
			
			return;
			
			//var ajax_response = function(response) {
			//	
			//	/* Only transition to the new page if the fadeout for the old page has finished */
			//	if ($("#wrap").is(":animated")) {
			//		setTimeout(function() {
			//			ajax_response(response);
			//		},100);
			//		return false;
			//	}
			//	
			//	/* Embed new page */
			//	$("#wrap").html($($(response).find("#wrap").html()));
			//	$("#wrap")
			//			.attr("slug",$(response).find("#wrap").attr("slug"))
			//			.attr("class",$(response).find("#wrap").attr("class"))
			//			.show();
			//	$("#content-info").delay(1000).fadeIn("fast");
			//	
			//	/* Play the transition */
			//	$this.page.loaded();
			//	
			//}
			//
			//$.ajax({dataType: 'html', url: href + "?RAW=1", success: ajax_response});
			//
			//$this.page.history.push(href);
			//
			//if (typeof history.pushState == 'function') {
			//	history.pushState({},"",href);
			//}
			//
			//return false;
			
		},
		
		transition: {
			
			home: function() {
				
				$(".ginput_counter:last-child").remove();
				
				/* Record height for animation, as we can't do this when they are hidden */
				var main_height			= $("#main").height();
				var screenshot_height 	= 114;//$("#screenshots").height();
				
				/* Hide by default */
				$("#sidebar,#banner,#main,#screenshots").hide();
				$("#sidebar").css({opacity: 0})
				
				/* Force window reload if this is an ajaxed request, since Gravity Forms doesn't properly support it */
				if ($this.page.history.length != 0) {
					return window.location.reload();
				}
				
				/* Slide in top bar and signup form */
				$("#banner").delay(100).show('slide', {direction: 'up', easing: 'easeOutExpo'}, 1000);
				$("#sidebar").delay(600).show('slide', {direction: 'right', easing: 'easeOutExpo'}, 1000);
				
				setTimeout(function() {
					$("#sidebar").css({opacity: 1.0})
				},650);
				
				/* Group rest of transitional animations in one timeout */
				setTimeout(function() {
					/* Fade in main text element, use opacity so space is reserved */
					$("#main").height(main_height).css({opacity: 0}).show();
					$("#main").animate({opacity: 1.0});
					
					/* Scan in the main context text */
					$("#main .container").hide().delay(800).slideDown(800);
					$("#main .wordfade_line").hide();
					
					setTimeout(function() {
					  $("#main .wordfade_line").fadeIn(200).delay(500).fadeOut(200)
					},500);
					
					/* Screenshots */
					setTimeout(function() {
						/* Show screenshots wrapper */
						$("#screenshots").height(screenshot_height);
						$("#screenshots").delay(600).slideDown();
						
						/* Fade in screenshots one by one */
						var wait = 800;
						$("#screenshots li").each(function() {
						  wait = wait + 300;
						  $(this).hide().delay(wait).fadeIn();
						});
					},1000);
					
				},1600);
				
			},
			
			about: function() {
				
				$("#main,#banner,#robot,#pictures li").hide();
				
				$("#banner").delay(100).show('slide', {direction: 'up', easing: 'easeOutExpo'}, 1000);
				
				$("#main, #robot").delay(500).fadeIn(500);
				
				/* Show pictures */
				  var delay = 1000;
				  $("#pictures li").each(function() {
					delay += 100;
					$(this).hide().delay(delay).fadeIn();
				  });
				
			},
			
			careers: function() {
				
				$("#amy,#main,#taleo,#banner").hide();
				
				$("#banner").delay(100).show('slide', {direction: 'up', easing: 'easeOutExpo'}, 1000);
				
				$("#amy").delay(500).fadeIn(500);
				$("#main").delay(750).fadeIn(500);
				$("#taleo").delay(1000).fadeIn(500);
				
			},
			
			generic: function() {
				
				$("#main,#banner").hide();
				
				$("#banner").delay(100).show('slide', {direction: 'up', easing: 'easeOutExpo'}, 1000);
				
				$("#main").delay(500).fadeIn(500);
				
			}
			
		}
		
	}
	
	this.hash = function() {
		var hash = window.location.href.substr(window.location.href.indexOf('#')+1);
		if (hash.indexOf('?')!==-1) {
			hash = hash.substr(0,hash.indexOf('?'));
		}
		return hash;
	}
	
	this.get = function() {
		if (window.location.href.indexOf('?')==-1) {
			return '';
		}
		return window.location.href.substr(window.location.href.indexOf('?')+1);
	}
	
	$this.init();
	
});

$(document).ready(function() {
	new base();
});
