
$(document).ready(function ()
{
	/*Navigation*/
	$('#navigation .elem').hover(function ()
	{
		$('#navigation .elem ul').hide();
		var ul = $(this).find('ul');
		if (ul.width()<$(this).width() || ul.width()<205)
		{
			var new_width = parseInt($(this).width()) - 2;
			ul.width(new_width + 'px');
			new_width = new_width-4;
			if (new_width<205) new_width = 205;
			ul.width(new_width-2 + 'px')
			ul.find('li div').width(new_width + 'px');
		}
		ul.show()
	},
	function ()
	{
		$(this).find('ul').hide();
	});
	
	$('#navigation .elem ul li div').hover(function ()
	{
		$(this).addClass('hover');
	},
	function ()
	{
		$(this).removeClass('hover');
	});
	
	var speed = 1;
	
	/*Questions*/
	$('.main-content .content .question .about h4 a,.main-content .content .question .questionschema h4 a').click(function ()
	{
		var container = $(this).parent().parent().find('div.container');
		$(this).toggleClass('open');
		container.slideToggle(container.height() * speed);
		return false;
	});

	$('.main-content .column .help a').click(function ()
	{
		var container = $(this).parent().find('div.container');
		$(this).toggleClass('open');
		container.slideToggle(container.height() * speed);
		return false;
	});
	
	$('.main-content .step-list li a img').hover(function ()
	{
		if ($(this).hasClass('active') == false) $(this).attr('src',$(this).attr('src').replace('.gif','.hover.gif'));
		return false;
	},
	function ()
	{
		if ($(this).hasClass('active') == false)  $(this).attr('src',$(this).attr('src').replace('.hover.gif','.gif'));
		return false;
	});
	
	$('.main-content .step-list li a img').click(function ()
	{
		var $this = $(this);
		var $ul = $this.parent().parent().parent().parent();
		
		var same = $ul.find('li a img.active').parent().attr('class') == $this.parent().attr('class');
		
		$ul.find('li a img').each(function ()
		{
			var $this = $(this);
			$this.removeClass('active');
			$this.attr('src', $this.attr('src').replace('.hover.gif','.gif'));
		});
		
		var questionId = $ul.find('.step-name').attr('title');
		
		if (!same)
		{
			$this.addClass('active');
			$this.attr('src', $this.attr('src').replace('.gif','.hover.gif'));
			
			var answerValue = $this.parent().attr('class');
			
			answer.save(questionId, answerValue);
		}
		else
		{
			answer.clear(questionId);
		}
		
		return false;
	});
	
	$('.main-content .open-close li a.print').click(function ()
	{
		window.print();
		return false;
	});
	
	/*Feedback*/
	$('.feedback-form .submit-holder').click(function ()
	{
		var name = $('#feedback-form input[name=name]').val();
		var email = $('#feedback-form input[name=email]').val();
		var feedback = $('#feedback-form textarea').val();
		
		if (!name)
		{
			alert('You have to type in your name');
			return false;
		}
		if (!email || /^[^ ]+?@[^ ]+?\.[a-z]{2,6}$/.test(email) == false)
		{
			alert('You have to type in your e-mail correctly');
			return false;
		}
		if (!feedback)
		{
			alert('You have to type in some feedback');
			return false;
		}
		$('#feedback-form').submit();
		return false;
	});
	
	
	
	
	
	
});


var auth = {
	shown: null,
	emailCleared: false, 
	loginCleared: false,
	signIn: function(scrollToTop, message)
	{
		auth.openPopup('signIn', message, scrollToTop);
	},
	signUp: function(scrollToTop, message)
	{
		auth.openPopup('signUp', message, scrollToTop);
	},
	forgotPassword: function()
	{
		auth.openPopup('forgotPassword');
	},
	openPopup: function(show, message, scrollToTop)
	{
		if (auth.shown == show)
		{
			$('#loginBox').removeClass('active');
			auth.shown = null;
			return;
		}
		
		auth.shown = show;

		$('#loginBox').addClass('active').find('.message').html(message);
		
		if (show == 'signUp')
		{
			$('#signInForm').hide();
			$('#signUpForm').show().find('input[name=signup_name]').focus();
			$('#forgotPasswordForm').hide();
		}
		else if (show == 'signIn')
		{
			$('#signInForm').show().find('input[name=auth_email]').focus();
			$('#signUpForm').hide();
			$('#forgotPasswordForm').hide();
		}
		else if (show == 'forgotPassword')
		{
			$('#signInForm').hide();
			$('#signUpForm').hide();
			$('#forgotPasswordForm').show().find('input[name=fp_email]').focus();;
		}
		
		if (scrollToTop)
		{
			window.scrollTo(0, 0);
		}
	},
	requestSignIn: function()
	{
		var $form = $('#signInForm');
		
		var email = $form.find('input[name=auth_email]').val();
		var password = $form.find('input[name=auth_password]').val();
		
		if (!email || !password)
		{
			message.show('Invalid email and/or password', null, 3000);
			return false;
		}
		
		if (!auth.loginCleared)
		{
			var onComplete = function(response)
			{
				if (response)
				{
					auth.requestSignIn();
					auth.loginCleared = true;
					$form.submit();
				}
				else 
				{
					message.show('Invalid email and/or password', null, 3000);
				}
			}
			
			$.post(global_url + 'ajax/verifyLogin', { email: email, password: password }, onComplete, 'json');
			
			return false;
		}
		
		return true;
	},
	createUser: function()
	{
		var $form = $('#signUpForm');
		
		var name = $form.find('input[name=signup_name]').val();
		if (!name)
		{
			message.show('Please type your name', null, 3000);
			return false;
		}
		
		var email = $form.find('input[name=signup_email]').val();
		if (!email)
		{
			message.show('Please type your email', null, 3000);
			return false;
		}
		
		if (!/^[^ ]+?@[^ ]+?\.[a-z]{2,6}$/.test(email))
		{
			message.show('The email you entered is not valid', null, 3000);
			return false;
		}
		
		var password = $form.find('input[name=signup_password]').val();
		if (!password)
		{
			message.show('Please choose a password', null, 3000);
			return false;
		}
		
		var confirm_password = $form.find('input[name=signup_confirm_password]').val();
		if (!confirm_password)
		{
			message.show('Please confirm your password', null, 3000);
			return false;
		}
		
		if (password != confirm_password)
		{
			message.show('Password and confirm password are not equal', null, 3000);
			return false;
		}
		
		if (!auth.emailCleared)
		{
			var onComplete = function(response)
			{
				if (response)
				{
					//auth.createUser();
					auth.emailCleared = true;
					document.getElementById('signUpForm').submit();
				}
				else 
				{
					message.show('The email you entered is already in use', null, 3000);
				}
			}
			
			$.post(global_url + 'ajax/isEmailInUse', { email: email }, onComplete, 'json');
			
			return false;
		}
		
		return true;
	},
	requestPassword: function()
	{
		var $form = $('#forgotPasswordForm');
		var email = $form.find('input[name=fp_email]').val();
		
		if (!email)
		{
			message.show('Please type your email');
			$form.find('input[name=fp_email]').focus();
			return;
		}
		
		var onComplete = function(response)
		{
			$('#signInForm').find('input[name=auth_email]').val(email);
			message.show(response.message, null, 3000);
		}
		
		$.post(global_url + 'ajax/requestPassword', { email: email }, onComplete, 'json');
	}
	
}

var answer = {
	save: function(questionId, answerValue)
	{
		var params = {
			questionId: questionId,
			answer: 	answerValue
		}
		
		$.post(global_url + 'ajax/saveAnswer', params, answer.saved, 'json');
	},
	clear: function(questionId)
	{
		answer.save(questionId, 0);
	},
	saved: function(response)
	{
		if (!response.error)
		{
			message.show('Answer saved', global_url + 'image/icon/check.gif');
		}
		message.show(response.message, null, 3000);
	}
}

var message = {
	initialized: false,
	delay: 1500,
	timeout: null,
	init: function()
	{
		message.initialized = true;
		$('#messageBox')
			.mouseover(message.mouseOver)
			.mouseout(message.mouseOut);
	},
	show: function(msg, icon, delay)
	{
		if (!message.initialized) message.init();
		
		var iconMarkup = '';
		if (icon) iconMarkup = '<img src="' + icon + '" />';
		
		var $mb = $('#messageBox');
		$mb.css('top', '-45px');
		$mb.find('span.message').html(msg);
		$mb.find('span.icon').html(iconMarkup);
		$mb.animate({ top: '0px' }, 200);
		
		message.hide(delay || message.delay);
	},
	hide: function(delay)
	{
		clearTimeout(message.timeout);
		message.timeout = setTimeout(function(){
			$('#messageBox').animate({ top: '-45px' }, 200);
		}, delay);
	},
	mouseOver: function()
	{
		clearTimeout(message.timeout);
	},
	mouseOut: function()
	{
		message.hide(message.delay);
	}
}

var exportExcel = {
	getPath: function(){
		$('ul.open-close li a.export').addClass('exporting');
		$.get(global_url + 'export', {}, exportExcel.download, 'json');
	},
	download: function(response){
		$('ul.open-close li a.export').removeClass('exporting');
		
		if (!response.error) location.href = response.path;
	}
}