(function(window,$,undefined){
	var slideShowSpeed = 1500;
	var slideShowInterval = 8000;
	/* on dom ready */
	$(function(){
		initSlide();
		initMenu();
		fixMicrosoftBuggyScript();
		initApplicationForm();
		initPartners();
		initHomepageNews();
	});
	
	initSlide = function() {
		var cycleTimer;
		$("#slideshow .slide").hide();
		$("#slideshow .slide:first").fadeIn(slideShowSpeed).addClass('current');
		cycleTimer = setTimeout(cycleSlide,slideShowInterval);

		function cycleSlide() {
			clearTimeout(cycleTimer);
			var current = $("#slideshow .slide.current");
			var next = current.next();
			if ( next.size() == 0 )
				next = $("#slideshow .slide").first();
			current.fadeOut(slideShowSpeed);
			next.fadeIn(slideShowSpeed);
			current.removeClass('current');
			next.addClass('current');
			cycleTimer = setTimeout(cycleSlide,slideShowInterval);
		}
	}

	
	initPartners = function() {
		var max = 0;
		$(".partners p").css({height:'auto'});
		$(".partners li p").each(function(i,node){ max = Math.max(max,$(node).height());});
		$(".partners p").height(max);
	}
	
	fixMicrosoftBuggyScript = function(){
		/* remove the <base> tag injected by Microsoft news script */
		$("base").remove();
		$(".news table a").attr('target','_blank');
	}

	initApplicationForm = function() {
		$(":input[type='date']").datepicker({dateFormat:'yy-mm-dd'});
		$("#job-application form").submit(function(){
			var form = this;
			var valid = true;
			$(':input',form).each(function(i,node){
				if ( $(this).val() == '' ) {
					valid = false;
					$(this).focus();
					return false;
				}
			});
			if ( $(":input[name='privacy']:checked",form).val() != 1 ) {
				$(":input[name='privacy']:first",form).focus();
				valid = false;
			}
			if ( valid ) {
				$("#job-application").addClass('loading');
			}
			return valid;
		});
	}
	
	initHomepageNews = function(){
		var container = $("#news ul");
		var size = $("li",container).size();
		var height = $("li",container).height();
		var count = 0;
		/* duplicate last element */
		var el = $("li:first",container).clone().appendTo(container);
		var moveInterval = 5000;
		var moveSpeed = 1000;
		var t;
		
		t = setTimeout(moveOn,moveInterval);
		
		function moveOn() {
			clearTimeout(t);
			count++;
			$(container).stop();
			$(container).animate({
				top: ( count * height * -1 )
			},moveSpeed,'',function(){
				if ( count >= size ) {
					$(container).css({top:0});
					count = 0;
				}
			});
			setTimeout(moveOn,moveInterval);
		}
	}

	initMenu = function() {
		var menuTimers = [];
		var menuFadeDuration = 200;
		var menuFadeTimeout = 100;
		$(".menu-panel").detach().appendTo('body');
		$("#main-menu a,#main-menu span").hover(function(event){
			var menuID = $(this).attr("data-id");
			var pos = $(this).offset();
			var size = $(this).outerHeight();
			$("#menu-" + menuID ).css({
				top: pos.top + size,
				left: pos.left
			}).fadeIn(menuFadeDuration);
			clearTimeout(menuTimers[menuID]);
		},function(event){
			var menuID = $(this).attr("data-id");
			menuTimers[menuID] = setTimeout(function(){
				$("#menu-" + menuID ).fadeOut(menuFadeDuration);
			},menuFadeTimeout);
		});
		$(".menu-panel").hover(function(event){
			var menuID = $(this).attr("data-id");
			clearTimeout(menuTimers[menuID]);
		},function(event){
			var menuID = $(this).attr("data-id");
			menuTimers[menuID] = setTimeout(function(){
				$("#menu-" + menuID ).fadeOut(menuFadeDuration);
			},menuFadeTimeout);			
		});
	}
	
	openAnimatedWindow = function(id,action) {
		var w = document.createElement('section');
		var backdrop = document.createElement('div');
		var inner = document.createElement('div');
		var data = '';
		
		inner.className = 'inner';
		w.id = id;
		w.className = 'popup-window';
		backdrop.id = 'backdrop';
		$(w).append(inner);
		$('body').append(backdrop);
		$('body').append(w);
		$(backdrop).click(_remove);
		loadWindow(true);

		function _remove() {
			$(w).animate({top:-800},500,function(){
				$(w).remove()
				$(backdrop).fadeOut(200,function(){
					$(backdrop).remove();
				});
			});
		}
		
		function loadWindow(animate) {
			if ( animate ) {
				$(w).css({ top: -800 });
			} else {
				$(inner).height($(inner).innerHeight());				
			}
			$("html,body").animate({
				scrollTop : 0
			},500);
			$(inner).addClass('loading');
			$(inner).load(action,data,function(){
				$(inner).removeClass('loading');
				if ( animate ) {
					$(w).animate({top:0},500);
				} else {
					$(inner).animate({
						height : ($('form',inner).outerHeight() + 64)
					},500);
				}

				if ( $('.success',inner).size() > 0 ) {
					setTimeout(_remove,3000);
				}

				$('form',w).submit(function(){
					var form = this;
					var valid = true;
					$(':input',form).each(function(i,node){
						if ( $(this).val() == '' ) {
							valid = false;
							$(this).focus();
							return false;
						}
					});
					if ( $(":input[name='privacy']:checked",form).val() != 1 ) {
						$(":input[name='privacy']:first",form).focus();
						valid = false;
					}
					if ( valid ) {
						data = $(this).serialize();
						loadWindow(false);
					}
					return false;
				});
			});
		}
		return false;
	}
	
	var mwd = {
		openTicketWindow : function() {
			return openAnimatedWindow('ticket-window','/actions/ticket.cfm');
		},
		openContactWindow : function(extras) {
			return openAnimatedWindow('contact-window','/actions/contact.cfm?extras=' + extras);
		}
	}
	window.mwd = mwd;

})(window,jQuery);
