/**
 * @author	Jonathan Cochran <jono.cochran@gmail.com>
 *			COPYRIGHT (c) 2009
 * --------------------------------------------------------- */

$(document).ready(function() {

/**
 * BROWSER FIXES (IE5/6)
 * --------------------------------------------------------- */
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");	

/**
 * HEADER
 * --------------------------------------------------------- */	
	if ( $('.login').length > 0 ) { initLoginForm(); }
	function initLoginForm()
	{
		var $objLoginForm = $('#login-form');
		
		$('.login-close').click(function() { $('.login').trigger('click'); });
		initLogin();
		
		$('.login').click(function() {
			if ( !$objLoginForm.is(':visible'))
			{
				$objLoginForm.show().animate({ height : 200	}, 300, 'easeInOutExpo');
			}
			else
			{
				$objLoginForm.animate({ height : 0	}, 300, 'easeInOutExpo', function() { $(this).hide() });
			}
		});
		
		function initLogin()
		{			
			//Click to login
			$('.login-form').submit(function() {
				$.post(
					hostname +'user/login',
					{
						email : $('#email').val(),
						password : $('#password').val()
					},
					function(data) {
						if (data == "success")
						{
							window.location.href = hostname +'user';
							//window.location.reload(true);
						}
						else
						{
							if (data != "")
							{
								alert(data);
							}
						}
					}
				);
				return false;
			});
		}
	}

/**
 * HOMEPAGE
 * --------------------------------------------------------- */
	if ( $('#homepage-content').length > 0 ) { initHomepage(); }
	function initHomepage()
	{
		//Banner Images..
		$('.banner ul').slideImages('horizontal', 538, 283, 400, 'easeInOutExpo', 'timer=5000', '', $('.banner-prev a'), $('.banner-next a')); 
		
		//Jump to testimonials
		$('.homepage-misc a').click(function() {
			$(window).scrollTo( '#homepage-testimonials-wrap', 800, { easing: 'easeInOutExpo' } );
		});
	}

/**
 * SIGN UP
 * --------------------------------------------------------- */
	if ( $('#signup-content').length > 0 ) { initSignUp(); }
	function initSignUp()
	{
		
		$('.signup-agree .checkbox').click(function() {
			
			if ( $(this).hasClass('checkbox-selected') )
			{
				//$(this).removeClass('checkbox-selected');
			} 
			else
			{
				//$(this).addClass('checkbox-selected');
			}
			
		});
	
		$('.signup-button-free').click(function() {
			$('#signup-plan, #signup-more').fadeTo(500, .3);
			$('#signup-free h2').css('color', 'white');
			$('#signup-free-content').animate( { marginTop : '-142' }, 300, 'easeOutExpo');
		});
	
		$('.signup-free-cancel').click(function() {
			$('#signup-plan, #signup-more').fadeTo(300, 1);
			$('#signup-free h2').css('color', '#aaa');
			$('#signup-free-content').animate( { marginTop : '0' }, 300, 'easeInExpo');
		});
	
		//FREE
		$('.signup-free-submit').click(function() {
		
			//Agree to TOS...
			if ( ! $('.signup-agree .checkbox').hasClass('checkbox-selected') )
			{
				alert("You must read and agree to our TOS");
				return false;
			}
		
			//Some basic form checking...
			strEmail = $('.free-email').val();
			strPass  = $('.free-password').val();
			if ( strEmail.replace(/^\s+|\s+$/g,"") == "" || strPass.replace(/^\s+|\s+$/g,"") == "" )
			{
				alert('Email & Password both required');
				return false;
			}
		
			//Check email...
			if ( (strEmail.indexOf(".") < 2) || (strEmail.indexOf("@") == 0) )
			{
				alert('You must provide a valid email');
				return false;
			}
		
			//4 characters minimum password
			if ( strPass.length < 4)
			{
				alert('Password must be at least 4 characters');
				return false;
			}
		
			//Ajax signup
			$.post(
				hostname +'user/register',
				{
					email	 : strEmail,
					password : strPass 
				},
				function (response) {
					if (response == "success")
					{
						document.location.href = hostname +'user/pending';						
					}
					//Something went wrong...
					else
					{
						alert(response);
					}
				}
			);
		});
		
		//PLAN
		$('.signup-button-plan').click(function() {
			$('.signup-button-free').trigger('click');
		});
	}

/**
 * AJAX LOAD CONTENT
 * --------------------------------------------------------- */
	function loadContent(page, json)
	{
		var objContent = $('#content');
		objContent.fadeTo(200, 0, function() {
			$.post(
				hostname +'site/' + page,
				{
					json : $.toJSON(json)
				},
				function(html)
				{
					objContent
						.html(html)
						.fadeTo(500, 1, function() {
							eval(page)(name); 
							//removeLinks();
						});
					
				}
			);
		});
	}

/**
 * CALLBACKS (FROM AJAX)
 * --------------------------------------------------------- */

});
