<?php

function cloudfw_call_login_form_ajax() {
	require_once(TEMPLATEPATH . '/includes/functions/user_functions.php' );
	
	ob_start();
		cloudfw_login_form();
	ob_end_flush;
	$out = ob_get_contents();
	ob_end_clean();
	
	echo '
		<div id="login-container" class="cloudfw-modal content" style="display:none;">
			<div id="overlay"></div>
			<div id="content-container-in-modal" class="make-shadow">
			'.$out.'
			<div id="cloudfw-modal-ajax-content"></div>
			<a id="modal-container-close" href="#">Close</a>
			</div>
		</div>	
	';	
}

add_action	('wp_footer', 'cloudfw_call_login_form_ajax', 1000);


function cloudfw_load_javascripts_login(){
	wp_enqueue_script	('cf_login', 	TMP_URL.'/lib/js/login.js', array( 'jquery' ));
}

if (!is_admin()) 
	add_action	('wp_head',	'cloudfw_load_javascripts_login', 2);
	


function cloudfw_ajax_get_form() {
	
    $func = ( !empty( $_POST['func'] ) ) ? $_POST['func'] : false;
	
	if (!$func)
	
	 echo 'An Error';
	 
	else {
	
		require_once(TEMPLATEPATH . '/includes/functions/user_functions.php' );
		
		if (function_exists($func)):
		
			echo $func();
			
		else:
		
			echo 'Function Not Exists';
			
		endif;

	}

    exit;
}
add_action( 'wp_ajax_cloudfw_get_register_form', 'cloudfw_ajax_get_form' );
add_action( 'wp_ajax_nopriv_cloudfw_get_register_form', 'cloudfw_ajax_get_form' );



function cloudfw_add_user_sidebar_items($case = NULL){
	
	if (!(current_user_can('read'))){ 

?>
    <div id="user-ops-navigation">
        <?php if ($case == 'register') : ?>
        <a class="cloudfw-login" href="#/login"><?php _e('Dou you have an account?','cloudfw');?></a>
        <?php endif; ?>
        <a href="<?php echo home_url("wp-login.php?action=lostpassword"); ?>"><?php _e('Forgotten password?','cloudfw');?></a>
		<?php if (get_option('users_can_register') && $case == 'login') : ?>
        <a class="cloudfw-register" href="#/register"><?php _e('Register user','cloudfw');?></a>
        <?php endif; ?>

    </div>
    
   	<?php }else{ ?>

    <a href="<?php echo wp_logout_url(__url( get_permalink() )); ?>" class="button red block"><?php _e('Logout','cloudfw');?></a>
	
<?php
	}
}

add_action	('cloudfw_user_page_sidebar',	'cloudfw_add_user_sidebar_items', 10, 1);



function cloudfw_init_signup_extra($case = NULL, $current_user_id = NULL){

if ($case == 'register') {
	
	$facebook_datas = esc_html( $_POST['facebook'], 1 );
	$twitter_datas = esc_html( $_POST['twitter'], 1 );

}elseif($case == 'update') {

	$facebook_datas = get_the_author_meta( 'facebook', $current_user_id );
	$twitter_datas = get_the_author_meta( 'twitter', $current_user_id );
	
}

?>

    <p><strong><?php _e('Contact Information','cloudfw');?></strong></p>

    <p class="form-facebook form-sub-container">
            <label for="facebook"><?php _e('Your Facebook Page', 'cloudfw'); ?></label>
        <input class="login-input" name="facebook" type="text" id="facebook" value="<?php echo $facebook_datas; ?>" />
    </p>
    
    <div class="cf"></div>
    
    <p class="form-twitter form-sub-container">
        <label for="twitter"><?php _e('Your Twitter Username', 'cloudfw'); ?></label>
        <input class="login-input" name="twitter" type="text" id="twitter" value="<?php echo $twitter_datas; ?>" />
    </p> 
       
    <div class="cf"></div>
	
<?php
}

add_action	('cloudfw_signup_extra',	'cloudfw_init_signup_extra', 10, 2);


function cloudfw_init_signup_save($case = NULL, $user_id = NULL){
	global $error;
	
	if (!$user_id) return false;

	if ( !empty( $_POST['facebook'] ) )
		update_user_meta( $user_id, 'facebook', esc_attr( $_POST['facebook'] ) );
	if ( !empty( $_POST['twitter'] ) )
		update_user_meta( $user_id, 'twitter', esc_attr( $_POST['twitter'] ) );

}

add_action	('cloudfw_signup_save',	'cloudfw_init_signup_save', 10, 2);


?>