/**
  * @return SplashImageManager instance
  */
 public static function getInstance()
 {
     if (is_null(self::$_instance)) {
         self::$_instance = new SplashImageManager();
     }
     return self::$_instance;
 }
 /**
  * Retourne true, si la période d'inactivité de l'utilisateur a été atteinte.
  */
 public static function enough_idle_to_splash($lastSplash)
 {
     $siBean = SplashImageManager::getInstance()->get(1);
     // Si la variable n'est pas settée, c'est que l'utilisateur vient pour la 1ere fois.
     if (!isset($lastSplash)) {
         return true;
     }
     $endIdle = $lastSplash + $siBean->getWsi_idle_time() * 60;
     if (time() > $endIdle) {
         return true;
     } else {
         return false;
     }
 }
 /**
  * Fontion utililée dans le blog (dans le footer)
  */
 public function wsi_addSplashImageWpFooter()
 {
     // Chargement des données en base
     $configBean = ConfigManager::getInstance()->get();
     $siBean = SplashImageManager::getInstance()->get(1);
     // Si le plugin n'est pas activé dans ses options, on ne fait rien
     if ($configBean->isSplash_active() != 'true') {
         return;
     }
     // If not in First Load Mode, exit the function
     if ($configBean->isWsi_first_load_mode_active() == 'true') {
         return;
     }
     // Si la Splash Image n'est pas dans sa plage de validité, on ne fait rien
     if (WsiCommons::getdate_is_in_validities_dates() == "false") {
         return;
     }
     // If option selected, hide splash image on mobile devices
     if ($siBean->isWsi_hide_on_mobile_devices() == 'true') {
         if (is_mobile_browser()) {
             return;
         }
     }
     // If wsi_display_always option is activated, not paying attention to idle time
     if ($siBean->isWsi_display_always() != 'true') {
         // Si l'utilisateur n'a pas été inactif assez longtemps, on ne fait rien
         if (WsiCommons::enough_idle_to_splash($this->last_display) == false) {
             return;
         }
     }
     require "splash/content.inc.php";
 }
$siBean->setWsi_picture_link_target($_POST['wsi_picture_link_target']);
$siBean->setWsi_include_url($_POST['wsi_include_url']);
$siBean->setWsi_type($_POST['wsi_type']);
$siBean->setWsi_opacity($_POST['wsi_opacity']);
$siBean->setWsi_idle_time($_POST['wsi_idle_time']);
// Dates management
$siBean->setDatepicker_start($_POST['datepicker_start']);
$siBean->setDatepicker_end($_POST['datepicker_end']);
// Booleans management
$configBean->setSplash_active(isset($_POST['splash_active']));
$configBean->setWsi_first_load_mode_active(isset($_POST['wsi_first_load_mode_active']));
$siBean->setWsi_close_on_esc_function(isset($_POST['wsi_close_on_esc_function']));
$siBean->setWsi_close_on_click_function(isset($_POST['wsi_close_on_click_function']));
$siBean->setWsi_hide_cross(isset($_POST['wsi_hide_cross']));
$siBean->setWsi_disable_shadow_border(isset($_POST['wsi_disable_shadow_border']));
$siBean->setWsi_youtube_autoplay(isset($_POST['wsi_youtube_autoplay']));
$siBean->setWsi_youtube_loop(isset($_POST['wsi_youtube_loop']));
$siBean->setWsi_fixed_splash(isset($_POST['wsi_fixed_splash']));
$siBean->setWsi_display_always(isset($_POST['wsi_display_always']));
$siBean->setWsi_hide_on_mobile_devices(isset($_POST['wsi_hide_on_mobile_devices']));
// Valeurs des onglets
$siBean->setWsi_youtube($_POST['wsi_youtube']);
$siBean->setWsi_yahoo($_POST['wsi_yahoo']);
$siBean->setWsi_dailymotion($_POST['wsi_dailymotion']);
$siBean->setWsi_metacafe($_POST['wsi_metacafe']);
$siBean->setWsi_swf($_POST['wsi_swf']);
// Remove slash in HTML code.
$siBean->setWsi_html(stripslashes($_POST['wsi_html']));
ConfigManager::getInstance()->save($configBean);
SplashImageManager::getInstance()->save($siBean);
 /**
  * Install the database of WSI
  */
 public function wsi_install_db()
 {
     global $wpdb;
     global $wsi_db_version;
     require_once ABSPATH . 'wp-admin/includes/upgrade.php';
     switch ($this->get_current_wsi_db_version()) {
         case "1.0":
             //write here, what is nececery to go to the next version (2.0)
             $table_name_splashimage = SplashImageManager::tableName();
             $sql_splashimage = "CREATE TABLE " . $table_name_splashimage . " (\r\n\t\t\t\t\tid                        MEDIUMINT(9) NOT NULL AUTO_INCREMENT,\r\n\t\t\t\t\twsi_display_always        BOOLEAN,\r\n\t\t\t\t\twsi_idle_time             INT,\r\n\t\t\t\t\turl_splash_image          VARCHAR(255),\r\n\t\t\t\t\tsplash_image_width        INT,\r\n\t\t\t\t\tsplash_image_height       INT,\r\n\t\t\t\t\tsplash_color              VARCHAR(6),\r\n\t\t\t\t\tdatepicker_start          DATETIME,\r\n\t\t\t\t\tdatepicker_end            DATETIME,\r\n\t\t\t\t\twsi_display_time          INT,\r\n\t\t\t\t\twsi_fixed_splash          BOOLEAN,\r\n\t\t\t\t\twsi_picture_link_url      VARCHAR(255),\r\n\t\t\t\t\twsi_picture_link_target   VARCHAR(255),\r\n\t\t\t\t\twsi_include_url           VARCHAR(255),\r\n\t\t\t\t\twsi_close_esc_function    BOOLEAN,\r\n\t\t\t\t\twsi_hide_cross            BOOLEAN,\r\n\t\t\t\t\twsi_disable_shadow_border BOOLEAN,\r\n\t\t\t\t\twsi_type                  VARCHAR(20),\r\n\t\t\t\t\twsi_opacity               INT,\r\n\t\t\t\t\twsi_youtube               VARCHAR(255),\r\n\t\t\t\t\twsi_youtube_autoplay      BOOLEAN,\r\n\t\t\t\t\twsi_youtube_loop          BOOLEAN,\r\n\t\t\t\t\twsi_yahoo                 VARCHAR(255),\r\n\t\t\t\t\twsi_dailymotion           VARCHAR(255),\r\n\t\t\t\t\twsi_metacafe              VARCHAR(255),\r\n\t\t\t\t\twsi_swf                   VARCHAR(255),\r\n\t\t\t\t\twsi_html                  VARCHAR(255),\r\n\t\t\t\t\tPRIMARY KEY id (id)\r\n\t\t\t\t);";
             $table_name_config = ConfigManager::tableName();
             $sql_config = "CREATE TABLE " . $table_name_config . " (\r\n\t\t\t\t\tparam VARCHAR(255),\r\n\t\t\t\t\tvalue VARCHAR(255),\r\n\t\t\t\t\tPRIMARY KEY id (param)\r\n\t\t\t\t);";
             dbDelta($sql_config);
             dbDelta($sql_splashimage);
             $wpdb->insert($table_name_config, array('param' => 'splash_active', 'value' => get_option('splash_active') == 'true'));
             $wpdb->insert($table_name_config, array('param' => 'wsi_first_load_mode_active', 'value' => get_option('wsi_first_load_mode_active') == 'true'));
             $wpdb->insert($table_name_splashimage, array('url_splash_image' => get_option('url_splash_image'), 'splash_image_width' => get_option('splash_image_width'), 'splash_image_height' => get_option('splash_image_height'), 'splash_color' => get_option('splash_color'), 'datepicker_start' => get_option('datepicker_start'), 'datepicker_end' => get_option('datepicker_end'), 'wsi_display_time' => get_option('wsi_display_time'), 'wsi_picture_link_url' => get_option('wsi_picture_link_url'), 'wsi_picture_link_target' => get_option('wsi_picture_link_target'), 'wsi_include_url' => get_option('wsi_include_url'), 'wsi_type' => get_option('wsi_type'), 'wsi_opacity' => get_option('wsi_opacity'), 'wsi_idle_time' => get_option('wsi_idle_time'), 'wsi_close_esc_function' => get_option('wsi_close_esc_function') == 'true', 'wsi_hide_cross' => get_option('wsi_hide_cross') == 'true', 'wsi_disable_shadow_border' => get_option('wsi_disable_shadow_border') == 'true', 'wsi_youtube_autoplay' => get_option('wsi_youtube_autoplay') == 'true', 'wsi_youtube_loop' => get_option('wsi_youtube_loop') == 'true', 'wsi_fixed_splash' => get_option('wsi_fixed_splash') == 'true', 'wsi_display_always' => get_option('splash_test_active') == 'true', 'wsi_youtube' => get_option('wsi_youtube'), 'wsi_yahoo' => get_option('wsi_yahoo'), 'wsi_dailymotion' => get_option('wsi_dailymotion'), 'wsi_metacafe' => get_option('wsi_metacafe'), 'wsi_swf' => get_option('wsi_swf'), 'wsi_html' => get_option('wsi_html')));
             // Delete old options...
             delete_option('splash_active');
             delete_option('wsi_first_load_mode_active');
             delete_option('splash_test_active');
             delete_option('url_splash_image');
             delete_option('splash_image_width');
             delete_option('splash_image_height');
             delete_option('splash_color');
             delete_option('datepicker_start');
             delete_option('datepicker_end');
             delete_option('wsi_display_time');
             delete_option('wsi_picture_link_url');
             delete_option('wsi_picture_link_target');
             delete_option('wsi_include_url');
             delete_option('wsi_type');
             delete_option('wsi_opacity');
             delete_option('wsi_idle_time');
             delete_option('wsi_close_esc_function');
             delete_option('wsi_hide_cross');
             delete_option('wsi_disable_shadow_border');
             delete_option('wsi_youtube_autoplay');
             delete_option('wsi_youtube_loop');
             delete_option('wsi_fixed_splash');
             delete_option('wsi_youtube');
             delete_option('wsi_yahoo');
             delete_option('wsi_dailymotion');
             delete_option('wsi_metacafe');
             delete_option('wsi_swf');
             delete_option('wsi_html');
             //no break, because if the last version is 3.0, we must run this step and the next...
         //no break, because if the last version is 3.0, we must run this step and the next...
         case "2.0":
             $table_name_splashimage = SplashImageManager::tableName();
             $sql_update_wsi_html = "ALTER TABLE " . $table_name_splashimage . " MODIFY wsi_html LONGTEXT NULL;";
             $wpdb->query($sql_update_wsi_html);
         case "2.1":
             $table_name_splashimage = SplashImageManager::tableName();
             $sql_update = "ALTER TABLE " . $table_name_splashimage . " ADD COLUMN wsi_margin_top INT NULL;";
             $wpdb->query($sql_update);
         case "2.2":
             $table_name_splashimage = SplashImageManager::tableName();
             $sql_alter = "ALTER TABLE " . $table_name_splashimage . " ADD COLUMN wsi_hide_on_mobile_devices BOOLEAN NULL;";
             $wpdb->query($sql_alter);
             $sql_update = "UPDATE " . $table_name_splashimage . " SET wsi_hide_on_mobile_devices = 0;";
             $wpdb->query($sql_update);
         case "2.3":
             $table_name_splashimage = SplashImageManager::tableName();
             $wpdb->query("ALTER TABLE " . $table_name_splashimage . " ADD COLUMN wsi_close_on_click_function BOOLEAN NULL;");
             $wpdb->query("UPDATE " . $table_name_splashimage . " SET wsi_close_on_click_function = 0;");
             $wpdb->query("ALTER TABLE " . $table_name_splashimage . " DROP wsi_close_esc_function;");
             $wpdb->query("ALTER TABLE " . $table_name_splashimage . " ADD COLUMN wsi_close_on_esc_function BOOLEAN NULL;");
             $wpdb->query("UPDATE " . $table_name_splashimage . " SET wsi_close_on_esc_function = 0;");
         case "2.4":
             //nothing for the moment (it is the current version)
         //nothing for the moment (it is the current version)
         case "3.0":
             //do not exists
     }
     // Add or update the wsi db version.
     update_option("wsi_db_version", WSI_DB_VERSION);
 }
    /**
     * Fonction utilisée dans la partie Admin
     */
    public function wp_splash_image_options()
    {
        // L'utilisateur a-t-il les droits suffisants pour afficher la page
        if (!current_user_can('manage_options')) {
            wp_die(__("You do not have sufficient permissions to access this page.", 'wp-splash-image'));
        }
        $updated = false;
        $reseted = false;
        if (isset($_POST['action'])) {
            switch ($_POST['action']) {
                case 'update':
                    require "actions/UpdateAction.inc.php";
                    $updated = true;
                    break;
                case 'reset':
                    require "actions/ResetAction.inc.php";
                    $reseted = true;
                    break;
            }
        }
        // Pour le moment on ne charge que le 1er splash screen
        $configBean = ConfigManager::getInstance()->get();
        $siBean = SplashImageManager::getInstance()->get(1);
        ?>

	<div class="wsi-back wrap">

		<!-- Logo Info -->
		<div id="display_info">
            <a href="https://wordpress.org/plugins/wsi/" target="_blank">
                <img id="info_img" class="tooltipped" data-position="bottom"
                     data-tooltip="<?php 
        echo __('Infos', 'wp-splash-image');
        ?>
"
                     alt="<?php 
        echo __('Infos', 'wp-splash-image');
        ?>
"
                     src="<?php 
        echo WsiCommons::getURL();
        ?>
/style/info.png" />
            </a>
		</div>

		<!-- Logo Feedback -->
		<div id="display_feedback">
            <a target="_blank" href="https://gitter.im/ben-barbier/WP-Splash-Image">
                <img id="feedback_img" class="tooltipped" data-position="bottom"
                     data-tooltip="<?php 
        echo __('Chat', 'wp-splash-image');
        ?>
"
                     alt="<?php 
        echo __('Chat', 'wp-splash-image');
        ?>
"
                     src="<?php 
        echo WsiCommons::getURL();
        ?>
/style/chat_logo.png" />
            </a>
		</div>

		<!-- Logo "Buy me a Beer" -->
		<div id="display_buyMeABeer">
			<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CKGNM6TBHU72C" target="_blank">
				<img id="buyMeABeer_img" class="tooltipped" data-position="bottom"
                     data-tooltip="<?php 
        echo __('Buy me a Beer', 'wp-splash-image');
        ?>
"
                     alt="<?php 
        echo __('Buy me a Beer', 'wp-splash-image');
        ?>
"
                     src="<?php 
        echo WsiCommons::getURL();
        ?>
/style/beer.png" />
			</a>
		</div>

		<!-- Logo Reset -->
		<div id="display_reset" title="<?php 
        echo __('Reset', 'wp-splash-image');
        ?>
">
			<form method="post" action="<?php 
        echo $_SERVER['REQUEST_URI'];
        ?>
" style="height:32px;">
				<?php 
        wp_nonce_field('reset', 'nonce_reset_field');
        ?>
				<input type="hidden" name="action" value="reset" />
				<!-- Fix old tooltip problem -->
				<input type="image" id="reset_img" class="tooltipped" data-position="bottom"
                       data-tooltip="<?php 
        echo __('Reset original settings', 'wp-splash-image');
        ?>
"
                       src="<?php 
        echo WsiCommons::getURL();
        ?>
/style/reset.png" />
			</form>
		</div>

		<!-- Logo GitHub -->
		<img id="github_img1" alt="github" src="<?php 
        echo WsiCommons::getURL();
        ?>
/style/github/ForkMe_Blk.png" usemap="#github_map" />
		<img id="github_img2" alt="github" src="<?php 
        echo WsiCommons::getURL();
        ?>
/style/github/ForkMe_Wht.png" usemap="#github_map" />
		<map name="github_map"><area shape="poly" id="github_area" coords="8,0,46,0,141,92,141,133" href="https://github.com/ben-barbier/WP-Splash-Image" target="_blank" /></map>

		<h2>WP Splash Image</h2>

		<!-- Information message -->
		<?php 
        if ($updated) {
            WsiCommons::showMessage(__('Options Updated...', 'wp-splash-image'));
        }
        ?>
		<?php 
        if (WsiCommons::has_a_new_version()) {
            WsiCommons::showMessage(__('A new version of "WP Splash Image" is out !', 'wp-splash-image') . " (<a href='" . WsiCommons::getUpdateURL() . "'>" . __('update automatically', 'wp-splash-image') . "</a>)");
        }
        ?>

		<!-- ------ -->
		<!-- Forms  -->
		<!-- ------ -->

		<?php 
        require "forms/MainForm.inc.php";
        ?>

	</div>

	<script type="text/javascript">

		jQuery(document).ready(function ($) {

			// Récupération du type de splash
			<?php 
        if (isset($_POST['wsi_type'])) {
            ?>
				var wsi_type = '<?php 
            echo $_POST['wsi_type'];
            ?>
';
				<?php 
            $wsi_type = $_POST['wsi_type'];
            ?>
			<?php 
        } else {
            if ($siBean->getWsi_type() != "") {
                ?>
				var wsi_type = '<?php 
                echo $siBean->getWsi_type();
                ?>
';
				<?php 
                $wsi_type = $siBean->getWsi_type();
                ?>
			<?php 
            } else {
                ?>
				var wsi_type = 'picture';
				<?php 
                $wsi_type = $siBean->getWsi_type();
                ?>
			<?php 
            }
        }
        ?>

			// Gestion de l'affichage de la zone "block_splash_test_active"
			if($("#splash_active").attr("checked")=="checked") {
                $("#block_splash_test_active").css("display","table-row");
            } else {
                $("#block_splash_test_active").css("display","none");
            }
			$("#splash_active").click(function() {
				if($("#splash_active").attr("checked")=="checked") {$("#block_splash_test_active").fadeIn("slow");}
				else{$("#block_splash_test_active").fadeOut("slow");}
			});

			// Gestion de l'affichage des zones block_idle_time en fonction de "wsi_display_always"
			if($("#wsi_display_always").attr("checked")=="checked") {
				$("#block_idle_time").css("display","none");
			}else{
				$("#block_idle_time").css("display","block");
			}
			$("#wsi_display_always").click(function() {
				if($("#wsi_display_always").attr("checked")=="checked") {
					$("#block_idle_time").fadeOut("slow");
				}else{
					$("#block_idle_time").fadeIn("slow");
				}
			});

			// GitHub banner effect.
			$('#github_area').mouseover(function() {
				  $('#github_img2').fadeIn("400");
			});
			$('#github_area').mouseout(function() {
				  $('#github_img2').fadeOut("400");
			});

			// Color on select input radio
			function color_box(boxId) {
				$(".box_type").velocity({ backgroundColor: "#FFFFFF" }, 200);
				$(boxId).velocity({       backgroundColor: "#D9FFB2" }, 500);
			}

			$("#radio_picture").click(function() {     color_box("#box_picture")});
			$("#radio_youtube").click(function() {     color_box("#box_youtube")});
			$("#radio_yahoo").click(function() {       color_box("#box_yahoo")});
			$("#radio_dailymotion").click(function() { color_box("#box_dailymotion")});
			$("#radio_metacafe").click(function() {    color_box("#box_metacafe")});
			$("#radio_swf").click(function() {         color_box("#box_swf")});
			$("#radio_html").click(function() {        color_box("#box_html")});
			$("#radio_include").click(function() {     color_box("#box_include")});

			// Color on plugin init
			$("#box_<?php 
        echo $wsi_type;
        ?>
").velocity({ backgroundColor: "#D9FFB2" }, 500);

			// Splash Color field management
			$("#splash_color_demo").css("background-color", "#"+$("#splash_color").val());
			$("#splash_color").keyup(function() {
				$("#splash_color_demo").css("background-color", "#"+$("#splash_color").val());
			});

			// Fields filters
			$('#splash_color').keyfilter(/[0-9a-f]/i);
			$('#splash_image_height').keyfilter(/[\d\.]/);
			$('#splash_image_width').keyfilter(/[\d\.]/);

			// Live Preview
			$('#live_preview_button').click(function() {
				$.post('<?php 
        echo WsiCommons::getURL();
        ?>
/wsi/back/splash/demo.php', {
					url_splash_image:            $("#url_splash_image").val(),
					splash_image_width:          $("#splash_image_width").val(),
					splash_image_height:         $("#splash_image_height").val(),
					wsi_margin_top:              $("#wsi_margin_top").val(),
					splash_color:                $('#splash_color').val(),
					wsi_display_time:            $("[name='wsi_display_time']").val(),
					wsi_fixed_splash:            $("#wsi_fixed_splash:checked").is(":checked"),
					wsi_picture_link_url:        $("#wsi_picture_link_url").val(),
					wsi_picture_link_target:     $("#wsi_picture_link_target").val(),
					wsi_include_url:             $("#wsi_include_url").val(),
					wsi_close_on_esc_function:   $("#wsi_close_on_esc_function:checked").is(":checked"),
					wsi_close_on_click_function: $("#wsi_close_on_click_function:checked").is(":checked"),
					wsi_hide_cross:              $("#wsi_hide_cross:checked").is(":checked"),
					wsi_disable_shadow_border:   $("#wsi_disable_shadow_border:checked").is(":checked"),
					wsi_type:                    $("[name='wsi_type']:checked").val(),
					wsi_opacity:                 $("[name='wsi_opacity']").val(),
					wsi_youtube:                 $("#wsi_youtube").val(),
					wsi_youtube_autoplay:        $("#wsi_youtube_autoplay:checked").is(":checked"),
					wsi_youtube_loop:            $("#wsi_youtube_loop:checked").is(":checked"),
					wsi_yahoo:                   $("#wsi_yahoo").val(),
					wsi_dailymotion:             $("#wsi_dailymotion").val(),
					wsi_metacafe:                $("#wsi_metacafe").val(),
					wsi_swf:                     $("#wsi_swf").val(),
					wsi_html:                    $("#wsi_html").val()
				}, function(files) {
                    $('#live_preview_div').html(files);
                });
			});

            // Materializecss
            $('.tooltipped').tooltip({delay: 10});


            var pickadateConfig = {
                format:'yyyy-mm-dd',
                selectMonths: true, // Creates a dropdown to control month
                selectYears: 5, // Creates a dropdown of 5 years to control year
                onClose: function () { // Warning sur les dates de validités

                    var startDate = datepickerStart.pickadate('picker').get('select', 'yyyymmdd');
                    var endDate = datepickerEnd.pickadate('picker').get('select', 'yyyymmdd');
                    var currentDate = getCurrentDate();

                    if (needToDisplayDateAlertMessage(startDate, endDate, currentDate)) {
                        $("#box_datepickers_warning").velocity("fadeIn", { display: "inline" });
                    } else {
                        $("#box_datepickers_warning").velocity("fadeOut");
                    }

                }
            };
            var datepickerStart = $('#datepicker_start').pickadate(pickadateConfig);
            var datepickerEnd = $('#datepicker_end').pickadate(pickadateConfig);

            function getCurrentDate() {
                var today = new Date();
                var dd = today.getDate();
                var mm = today.getMonth() + 1; //January is 0!

                var yyyy = today.getFullYear();
                if (dd < 10) {
                    dd = '0' + dd
                }
                if (mm < 10) {
                    mm = '0' + mm
                }
                return '' + yyyy + mm + dd;
            }

            function needToDisplayDateAlertMessage (startDate, endDate, currentDate) {
                if (startDate && startDate > currentDate) {
                    return true;
                }
                if (endDate && endDate < currentDate) {
                    return true;
                }
                return false;
            }

            $('select').material_select();
			$('.modal-trigger').leanModal();

		});
	</script>

<?php 
    }