$(document).ready(function(){
	 $('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  
	});
	
	//$("#frm_signup").validationEngine();				   		   
	//When you click on a link with class of poplight and the href starts with a # 
	$('a.poplight[href^=#]').click(function() {
		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size
		var next_url = ($(this).attr('title') == undefined) ? BASE_URL+'user/index' : $(this).attr('title');
		$('#signin_redirect_url').val(next_url);
		return showPopups(popID,popURL);		
		
	});
	
	
	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	  	$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  
	}); //fade them both out
		
		return false;
	});
 
	
});
$('#btn_reg_splashpages').click(function(){ 
                              var position = $(this).offset();
							  //position image							  
							  $('#spinner').css({ top: position.top - 15, left: position.left + $(this).width() + 30 }).fadeIn();
							  reg_splash_pages();
						  });

function reg_splash_pages() {
	if (validate('form20')) {
		$.ajax({
			url: BASE_URL+'user/handle_registration_na',
			type: 'POST',
			dataType: 'json',
			data: $('#new_splash_signup_form').serialize(),
			success: function(response) {
				if (response.status == 'success') {
					$('#reg-signup_form').css("display","none").fadeOut(1000);
					$('#reg_success').css("display","block").fadeIn(1000);
					$('#spinner').fadeOut();
					return ;
				} else {
					if(response.error_no == 1)
					  $('#email_error').html(response.errodesc).css({'color':'#F00','font-size':'11px','text-align':'left'}); 
					if(response.error_no == 2)
					  $('#phone_error').html(response.errodesc).css({'color':'#F00','font-size':'11px','text-align':'left'}); 
					$('#spinner').fadeOut();
					
				}
			}
		});	
	} else {
		$('#spinner').fadeOut();
		return false;
	}
};


$('#new_splash_signup_form').keypress(function(e) {
	if (e.which == 13) {
		var position = $('#btn_reg_splashpages').offset();
	    //position image							  
	    $('#spinner').css({ top: position.top - 15, left: position.left + $('#btn_reg_splashpages').width() + 30 }).fadeIn();
		reg_splash_pages();
	}
});
$('#btn_reg_submit').click(function(){ 
	  var position = $('#btn_reg_submit').offset();
	  //position image
	  
	  $('#spinner').css({ top: position.top -15, left: position.left + $('#btn_reg_submit').width() + 31 }).fadeIn();
		 reg_submit();
 });

function reg_submit() {
	if (validate('form')) {
		$.ajax({
			url: BASE_URL+'user/handle_register',
			type: 'POST',
			dataType: 'json',
			data: $('#new_signup_form').serialize(),
			success: function(response) {
				if (response.status == 'success') {
					$('#reg-signup_form').css("display","none").fadeOut(1000);
					$('#reg_success').css("display","block").fadeIn(1000);
					$('#spinner').fadeOut();
					$('#download_guide').css('display','block').fadeIn(1000);
					return ;
				} else {
					if(response.error_no == 1)
					  $('#email_error').html(response.errodesc).css({'color':'#F00','font-size':'11px','text-align':'left'}); 
					if(response.error_no == 2)
					  $('#phone_error').html(response.errodesc).css({'color':'#F00','font-size':'11px','text-align':'left'}); 
					$('#spinner').fadeOut();
				}
			}
		});	
	} else {
		$('#spinner').fadeOut();
		return false;
	}
};

$('#new_signup_form').keypress(function(e) {
	if (e.which == 13) {
		var position = $("#btn_reg_submit").offset();
	    //position image							  
	    $('#spinner').css({ top: position.top - 15, left: position.left + $("#btn_reg_submit").width() + 30 }).fadeIn();
		reg_submit();
	}
});

function showPopups(popID,popURL){

		$('#login_messages').html('');
		var query= popURL.split('?');
		var dim= query[1].split('&');
		var popWidth = dim[0].split('=')[1]; //Gets the first query string value
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) });
		//alert($('#' + popID+' a.close').length);
		if($('#' + popID+' a.close').length  == 0){
		  //$('#' + popID+' a.close:first').remove();
		  $('#' + popID).prepend('<a href="#" class="close"><span class="btn_close" title="Close Window"></span></a>');
		}
		
			//Fade in the Popup and add close button
		
		
		//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
		
		//Apply Margin to Popup
		$('#' + popID).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
		return false;
		
}
