﻿/*____________________________________________
|     _     _ ___      __ _                   |
|    (_)___(_) __|___ / _| |_ __ _ _ _ ___    |
|    | |_ /| \__ | _ \  _|  _/ _` | '_/ -_)   |
|    |_/__||_|___|___/_|  \__\__,_|_| \___|   |
|                                             |
|       ©2010 iziSoftware - Version 1.0       |
|_____________________________________________|

WECounterCoreJS : Affiche un compteur de visites

*/


//Démarrage
$(document).ready(function() {

    WECounterCoreJS.Initialize();

});

var WECounterCoreJS = {

    Initialize: function() {



        //evite les erreurs php en local
    if (window.location.protocol.match("file+")) {
            WECounterCoreJS.showNull();
            return false;
        }
        
        if (WEInfoPage.PHPVersion == "" || WEInfoPage.PHPVersion == 'undefined' || WEInfoPage.PHPVersion == null ) {
            alert("WECounter : "+WEEdSiteCommon.HtmlDecode(WEInfoPage.EditorTexts["NoPHP"])); // Impossible d'exécuter cette action. Le PHP n'est pas installé sur votre hébergement. Veuillez contacter votre hébergeur pour l'activer.
            return false;
        }

        // Option d'envoi AJAX 
        var CurDat = new Date().getTime(); //Actualisation du php sous IE
        var options = {
            beforeSubmit: this.showRequest,
            success: this.showResponse,
            url: WEInfoPage.RelativePath + 'WEFiles/Server/WECounter_v' + WEInfoPage.OEVersion + '.php',
            data: { PagePath: WEInfoPage.PageAssociatePath, Culture: WEInfoPage.Culture, Dat: CurDat }
        };

        $.ajax(options);
        return false;
    },

    showNull: function() {
        for (Elem in OEConfWECounter) {
            var html = "";
            for (i = 0; i < OEConfWECounter[Elem].Config.Lenght; i++) {
                html = html + "<div class='OESZ_Digit OESZG_" + Elem + "'>0</div>";
            }
            $("#" + Elem).html(html);
        }
    },
    
    showRequest: function(formData, jqForm, options) {
    },

    showResponse: function(data, statusText) {
        //alert(data);
        dataInit = data;
        try {
            data = data.substr(data.indexOf("{"), data.lastIndexOf("}") - data.indexOf("{") + 1);
            data = JSON.parse(data);
            for (ID in data) {
                value = data[ID]["Value"] + ""; //Conversion en string
                html = "";
                for (i = 0; i < (data[ID]["Lenght"] - value.length); i++) {
                    html = html + "<div class='OESZ_Digit OESZG_" + ID + "'>0</div>";
                }
                for (i = 0; i < value.length; i++) {
                    //alert(value.charAt(i));
                    html = html + "<div class='OESZ_Digit OESZG_" + ID + "'>" + value.charAt(i) + "</div>";
                }
                $("#" + ID).html(html);
            }
        }
        catch (Err) {
            alert("Error : " + Err.description + "\n\n" + dataInit); //Affichage de l'erreur lorsque l'affichage standard ne fonctionne pas + affichage du retour du php (en cas d'erreur php non parsable
        }


    }

}
