<?php
try {
    if( isset($_GET["twcid"])) {

        require("twitter.php");
        $provider = "twitter";
        $key = "twcid";

    } else if (isset($_GET["fbid"])) {

        require("facebook.php");
        $provider = "facebook";
        $key = "fbid";

    } else if (isset($_GET["kc"]) ) {

        require("asociaciones.php");
        $provider = "asociaciones";
        $key = "kc";
    }

} catch (Exception $e) {

	echo '<div style="color: #ff0000;">';
	echo $e->getMessage();
	echo '<p>';
	echo __ ('Si no desaparece este mensaje p&oacute;ngase en contacto con los administradores.','buddypress');
	echo '</p>';
	echo '</div>';
	exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
get_header("oauth");

// REGISTER SCRIPT
wp_deregister_script( 'registrationScpt' );
wp_register_script( 'registrationScpt', get_stylesheet_directory_uri() .'/_inc/registration/oauth.js', '' );
wp_print_scripts( 'registrationScpt' );

// Post confirmed
$errorArray = array();
?>
    <style type="text/css">
        body {

            background-color:white;
        }

        .tabul {
            margin:0;
        }

        #twc-content {

            padding: 0 0 10px 0;
        }

        /*#vincular_email {

            background: transparent url('wp-content/themes/cvp-irontec/_inc/images/bg_status_menu_01.png') no-repeat;
            height: 27px;
            line-height: 105%;
            padding: 5px 18px;
            text-align: center;
            width:145px;
            color:white;
            cursor:pointer;
        }*/

    </style>
    <div id="bg"> </div>
    <div id="twc-content" style="">
        <div class="tunnel-counter">
            <ul id="tablist" class="tabul">
                <li class="tabtrigger">
                    <img id="tabtrigger_1" style="" src="<?php echo get_stylesheet_directory_uri() . "/registration/images/slider_counter_on.png"; ?>" />
                </li>
                <li class="tabtrigger">
                    <img id="tabtrigger_2" src="<?php echo get_stylesheet_directory_uri() . "/registration/images/slider_counter_grey.png"; ?>" />
                </li>
            </ul>
        </div> <!-- tunnel-counter -->

        <!-- TAB CONTENT INTRO -->
        <div id="mytab_0" class="tabcontent" style="display: block;">
            <div id="tunnel-box">
                <div class="tunnel-title"><h1><?php echo ucfirst($provider); ?> connect</h1></div>
                <div class="tunnel-action" style="min-height:0;">
                    <p>
                        <?php echo sprintf(__('Esta es la primera vez que intenta conectarse a <strong>kronikoensarea</strong> con su cuenta de %s', 'buddypress'), $provider); ?>
                    </p>
                    <p>
                        <?php echo __('Por favor indiquenos su email y escoja un nombre de usuario para completar el proceso.', 'buddypress'); ?>
                    </p>
                </div>
                <div class="tunnel-buttons">
                    <input type="button" value="<?php echo __('Continuar', 'buddypress'); ?>" id="tabcontinue_0" class="tunnel-next" />
                </div>
            </form>
            </div> <!-- tunnel-box -->
        </div>

        <!-- TAB CONTENT 1 -->
        <div id="mytab_1" class="tabcontent" style="display: none;">
            <div id="tunnel-box">
            <form method="POST" action="<?php echo site_url() . '/oauth?'.$key.'='.$_GET[$key]; ?>">
                <div class="tunnel-title"><h1><?php echo __('Email', 'buddypress'); ?></h1></div>
                <div class="tunnel-action" style="min-height:0;">
                        <input type="text" value="<?php echo strpos($user_obj->user_email, "changeme.") === false ? $user_obj->user_email : ""; ?>" id="signup_email" name="signup_email">

                        <div class="validate" style="margin-top:10px;display:none;">
                            <div class="default" style="color:red;">
                                <img src="<?php echo get_stylesheet_directory_uri().'/_inc/images/preloader.gif'; ?>" style="vertical-align:text-bottom;" />
                                <p><?php echo __('Comprobando email', 'buddypress'); ?></p>
                            </div>
                            <div class="response">
                                <p></p>
                            </div>
                            <div id="vincular" style="display:none;">
                                <p>
                                    <?php echo sprintf(__('Ya existe un usuario registrado con este Email. Puede vincular esta cuenta de %s
                                    al usuario existente o cambiar de correo electrónico.', 'buddypress'), $provider); ?>
                                </p>
                                <input type="button" value="<?php echo __('Vincular', 'buddypress'); ?>" id="vincular_email" class="tunnel-next" />
                            </div>
                        </div>
                    </div>
                    <div class="tunnel-buttons">
                        <input type="button" value="<?php echo __('Continuar', 'buddypress'); ?>" id="tabcontinue_1" class="tunnel-next" />
                    </div>
                </div>
            </form>
            </div> <!-- tunnel-box -->
        </div>

        <!-- Vincular email -->
        <div id="vincularEmailInfo" class="tabcontent" style="display: none;">
            <div id="tunnel-box">

                <div class="tunnel-title"><h1><?php echo __('Compruebe su email', 'buddypress'); ?></h1></div>
                <div class="tunnel-action" style="min-height:0;">
                    <p>
                        <?php echo sprintf(__('Acabamos de enviarle un email de validación. Por favor,
                        haga click sobre el enlace facilitado para vincular su cuenta de <strong>%s</strong>
                        y <strong>Kronikoen sarea</strong>', 'buddypress'), $provider); ?>
                    </p>
                </div>
                <div class="tunnel-buttons">
                    <input id="vincularFinalizar" type="button" value="<?php echo __('Finalizar', 'buddypress'); ?>" class="tunnel-next" />
                </div>
            </div> <!-- tunnel-box -->
        </div>

        <!-- TAB CONTENT 2 -->
        <div id="mytab_2" class="tabcontent" style="display: none;">

            <div id="tunnel-box">
                <form method="POST" action="<?php echo site_url() . '/oauth?'.$key.'='.$_GET[$key]; ?>">
                <div class="tunnel-title"><h1><?php echo __('Nombre de usuario', 'buddypress'); ?></h1></div>
                <div class="tunnel-action" style="min-height:0;">
                        <input type="text" value="<?php echo $user_obj->user_login; ?>" id="signup_displayname" name="signup_displayname">
                        <div class="validate" style="margin-top:10px;display:none;">
                            <div class="default" style="color:red;">
                                <img src="<?php echo get_stylesheet_directory_uri().'/_inc/images/preloader.gif'; ?>" style="vertical-align:text-bottom;" />
                                <p><?php echo __('Comprobando disponibilidad', 'buddypress'); ?></p>
                            </div>
                            <div class="response">
                                <p></p>
                            </div>
                        </div>

                </div>
                <div class="tunnel-buttons">
                    <input type="button" value="<?php echo __('Continuar', 'buddypress'); ?>" id="tabcontinue_2" class="tunnel-next" />
                </div>
                </form>

            </div> <!-- tunnel-box -->
        </div>

		<?php if (isset ($_GET['kc'])) : ?>
        <!-- TAB CONTENT 3 -->
        <div id="mytab_3" class="tabcontent" style="display: none;">

            <div id="tunnel-box">
                <form method="POST" action="<?php echo site_url() . '/oauth?'.$key.'='.$_GET[$key]; ?>">
                <div class="tunnel-title"><h1><?php echo __('Nombre' , 'buddypress'); ?></h1></div>
                <div class="tunnel-action" style="min-height:0; margin: 5px auto;">
					<input type="text" value="<?php echo ""; /*$user_obj->first_name*/; ?>" id="first_name" name="first_name">
                </div>
                <div class="tunnel-title"><h1><?php echo __('Apellidos' , 'buddypress'); ?></h1></div>
                <div class="tunnel-action" style="min-height:0; margin: 5px auto; margin-bottom: 20px;">
					<input type="text" value="<?php echo ""; /*$user_obj->last_name;*/ ?>" id="last_name" name="last_name">
					<div class="validate" style="margin-top:10px;display:none;">
                            <div class="default" style="color:red;">
                                <img src="<?php echo get_stylesheet_directory_uri().'/_inc/images/preloader.gif'; ?>" style="vertical-align:text-bottom;" />
                                <p><?php echo __('Comprobando disponibilidad', 'buddypress'); ?></p>
                            </div>
                            <div class="response">
                                <p></p>
							</div>
                    </div>
                </div>
                <div class="tunnel-buttons">
                    <input type="button" value="<?php echo __('Continuar', 'buddypress'); ?>" id="tabcontinue_3" class="tunnel-next" />
                </div>
                </form>

            </div> <!-- tunnel-box -->
        </div>
		<?php endif; ?>

        <!-- Fetch avatar -->
        <div id="fetchAvatar" class="tabcontent" style="display: none;">
            <div id="tunnel-box">
                <form method="POST" action="<?php echo site_url() . '/oauth?'.$key.'='.$_GET[$key]; ?>">
                <div class="tunnel-title"><h1 style="margin-top:0;"><?php echo __('Avatar', 'buddypress'); ?></h1></div>
                <div class="tunnel-action" style="min-height:0;">
                    <?php echo sprintf(__("¿Deseas usar tu avatar de %s en kronikoensarea?", "buddypress"), $provider); ?>
                    <br />
                    <?php

                        $url = "";
                        switch ($provider) {

                            case "twitter":

                                global $wpdb;
                                //Delete current user from wp_users
                                $q = sprintf("select user_id from %s WHERE meta_key = 'twcid' and meta_value = %d", $wpdb->usermeta, $_GET["twcid"]);
                                $userObj = $wpdb->get_row($q);

                                $user_obj = get_userdata($userObj->user_id);

                                $base = "http://api.twitter.com/1/users/profile_image/%%username%%?size=bigger";
                                $url = str_replace('%%username%%', $user_obj->twc_name ,$base);

                                break;

                            case "facebook":

                                $fb_uid = "";

                                if (isset($user_obj->tmp_facebook_uid)) {

                                    $fb_uid = $user_obj->tmp_facebook_uid;

                                } else if (isset($user_obj->facebook_uid)) {

                                    $fb_uid = $user_obj->facebook_uid;
                                }

                                $url = "http://graph.facebook.com/".$fb_uid."/picture?type=large";
                                break;

                            case "asociaciones":

                                $url = $user_obj->avatar;

                                if (strpos($user_obj->avatar, "http://") === false and strpos(base64_decode($user_obj->avatar), "http://") !== false) {

                                    $url = base64_decode($user_obj->avatar);
                                }

                                break;
                        }

                    if ($url) {
                    ?>
                        <div style="text-align:center;">
                            <input type="radio" name="fetchAvatar" value="1" checked="checked" /> <?php _e("Si", "buddypress"); ?>
                            <input type="radio" name="fetchAvatar" value="0" /> <?php _e("No", "buddypress"); ?>
                        </div>
                        <br />
                        <img src="<?php echo $url; ?>" height="50px" />
                    <?php
                    } else {
                    ?>
                        <div style="text-align:center;display:none;">
                            <input type="radio" name="fetchAvatar" value="1" /> <?php _e("Si", "buddypress"); ?>
                            <input type="radio" name="fetchAvatar" value="0" checked="checked" /> <?php _e("No", "buddypress"); ?>
                        </div>
                        <br />
                        <p><strong><?php _e("No se pudo importar tu avatar", "buddypress"); ?></strong></p>
                    <?php
                    }
                    ?>

                    <input type="hidden" name="avatar" value="<?php echo base64_encode($url); ?>" />
                </div>
                <div class="tunnel-buttons">
                    <input id="mensajeFinalizar" type="button" value="<?php echo __('Continuar', 'buddypress'); ?>" class="tunnel-next" />
                </div>
                </form>
            </div> <!-- tunnel-box -->
        </div>

        <!-- Vincular email -->
        <div id="revisarEmail" class="tabcontent" style="display: none;">
            <div id="tunnel-box">

                <div class="tunnel-title"><h1><?php echo __('Compruebe su email', 'buddypress'); ?></h1></div>
                <div class="tunnel-action" style="min-height:0;">
                    <p>
                        <?php echo sprintf(__('Acabamos de enviarle un email de validación. Por favor,
                        haga click sobre el enlace facilitado para finalizar el proceso de registro con <strong>%s</strong>', 'buddypress'), $provider); ?>
                    </p>
                </div>
                <div class="tunnel-buttons">
                    <input id="revisarFinalizar" type="button" value="<?php echo __('Finalizar', 'buddypress'); ?>" class="tunnel-next" />
                </div>
            </div> <!-- tunnel-box -->
        </div>

    </div>
</form>
</body>
</html>
