/* =========================================

	Positive Theme - Login & Registration Functions

============================================*/


$(document).ready(function() {
	
	var thm_ajax_url = $("meta[name=theme_ajax_url]").attr('content');
	
	// CloudFW Registration Modals		
	var $login_box = $('#login-container');
	var $login_box_modal = $login_box.find('#content-container-in-modal');
	var $content_guide = $('#cloudfw-modal-ajax-content');
	var $message_box_interval, xhr;
	$login_box.hide();
	
	$('#modal-container-close').live('click',function(e){
		e.preventDefault();
		close_cloudfw_modal();			
	});
	
	// Show Modal
	function create_cloudfw_modal($loading){
		$login_box.children("#overlay").css('opacity',.5);
		if(!$.browser.msie) $login_box.fadeTo('fast',1); else $login_box.show();
		
		if($loading){ 
			$content_guide.html('');
			$('#cloudfw-login-container').hide();
			$('#modal-loading').remove();
			$login_box_modal.prepend('<div id="modal-loading"></div>');
		}
		
		$login_box_modal.css({marginTop: 0}).animate({marginTop: 100});
		
		//$login_box_modal.center()
		$login_box.css({height: $(document).height()});
		$.scrollTo(0, {duration: 500 });
		
		$(window).one('keyup',function(e) {
		  if (e.keyCode == 27) { close_cloudfw_modal(); }   // esc
		});
		
		clearInterval($message_box_interval);
		$message_box_interval = setInterval(function(){
			$('#cloudfw-modal-message-box').slideUp();
			clearInterval($message_box_interval);
		}, 5000);
		
		
	}
	
	// Close Modal
	function close_cloudfw_modal(){

		window.location.hash = '#/';
		if (xhr) xhr.abort();

		if(!$.browser.msie) {
			
			$login_box.fadeTo('normal',0, function(){
				$(this).hide();
				$content_guide.html('').hide();
				$('#cloudfw-login-container').show();
			});
			
		} else {
			$login_box.hide();
			$content_guide.html('').hide();
			$('#cloudfw-login-container').show();
		}
		
	}
	
	
	
	// Login Trigger
	$('a.cloudfw-login, .cloudfw-login a').live('click',function(e){
		e.preventDefault();			
		window.location.hash = '#/login';
					
	});
	

	// Register Trigger
	$('a.cloudfw-register, .cloudfw-register a').live('click',function(e){
		e.preventDefault();
		$element = $(this);
		window.location.hash = '#/register';

	});	
	
	// Catch :Submit Button When Sending Form
	$('.login_form').bind('submit',function(){
	
		$(this).find(":submit").hide().after("<span>Please Wait...</span>");
		
	});
	
	// Send Registration Form
	function send_reg_form_cloudfw_modal(){
		$('#cloudfw-user-registerform').bind('submit',function(){
		the_form = $(this);
		my_array = {func : 'cloudfw_register_form_send', action: 'cloudfw_get_register_form'};
		
		xhr = $.ajax({
			  url: thm_ajax_url,
			  cache: false,
			  type: 'post',
			  data: $.param(my_array, true)+"&"+the_form.serialize(),
			  success: function(data) {
				$('#modal-loading').fadeOut(function(){
					$content_guide.html(data).show();
					$(this).remove();
					create_cloudfw_modal();
					send_reg_form_cloudfw_modal();
					});
				}//end-success
			});//ajax-end
			
		create_cloudfw_modal(true);
		$('#cloudfw-user-registerform').unbind('submit');
		
		return false;
	}); // :submit
		

	}
	
	
	function send_update_form_cloudfw_modal(){
		$('#cloudfw-user-updateform').bind('submit',function(){
		the_form = $(this);
		my_array = {func : 'cloudfw_register_form_update', action: 'cloudfw_get_register_form'};
		
		xhr = $.ajax({
			  url: thm_ajax_url,
			  cache: false,
			  type: 'post',
			  data: $.param(my_array, true)+"&"+the_form.serialize(),
			  success: function(data) {
				$('#modal-loading').fadeOut(function(){
					$('#cloudfw-login-container').html(data).show();
					$(this).remove();
					create_cloudfw_modal();
					send_update_form_cloudfw_modal();
					});
				}//end-success
			});//ajax-end
			
		create_cloudfw_modal(true);
		$('#cloudfw-user-updateform').unbind('submit');
		
		return false;
	}); // :submit
		

}


		
$(window).hashchange( function(){
	
	var theHash = location.hash;
	if (theHash == '' || theHash == '#/') return false;
	
	switch(theHash) {
		case '#/login':
		case '#/profile':
		
		/* Login Functions */					
			$element = $(this);
			create_cloudfw_modal();
			$content_guide.html('');
			$('#cloudfw-login-container').show();
			$('#modal-loading').remove();

			send_update_form_cloudfw_modal();
		
		
		break;
		
		case '#/register':

		/* Register Functions */					
			create_cloudfw_modal(true);
		
			xhr = $.ajax({
			  url: thm_ajax_url,
			  cache: false,
			  type: 'post',
			  data: ({func : 'cloudfw_register_form', action: 'cloudfw_get_register_form' }),
			  success: function(data) {
				$('#modal-loading').fadeOut(function(){
					$content_guide.html(data).show();
					$(this).remove();
					create_cloudfw_modal();
					send_reg_form_cloudfw_modal();
					});
				}//end-success
			});//ajax-end
		
		break;
		
		case '#/losspass':
		
		break;	
	}
	
})
	
$(window).hashchange();
				
		
});


jQuery(window).bind("unload", function(){
	
});