﻿function obj_widget()
{
    var m_str_urlPopupExport = "/widgets/export.html?1=1";
    var m_str_urlToGoogleContent = "/widgets/google_content.html?1=1";
    
    
    this.export_notice = function(ean, title) { 
        return pv_xhrPop(m_str_urlPopupExport + '&widget=notice&ean=' + ean + '&title=' + title);
    }
    
    this.export_podcast = function(pod_id, pod_title) { 
        return pv_xhrPop(m_str_urlPopupExport + '&widget=podcast&pod_id=' + pod_id + '&pod_title=' + pod_title);
    }
    
    /*
    this.export_code = function(pod_id, pod_title) { 
        return pv_xhrPop(m_str_urlPopupExport + '&pod_id=' + pod_id + '&pod_title=' + pod_title);
    }
    */
    
    this.pop_googleWidget = function(google_url) {
        //alert('obj_widget.js : pop_googleWidget\n\ngoogle_url =\n' + google_url);
        return pv_xhrPop(m_str_urlToGoogleContent + '&url=' + google_url);
    }
    
    
    var pv_xhrPop = function(url) 
    { 
        print_pseudoPopUp(url, "", true, 2, "Chargement en cours...");
        return false;
    }
    
    
    
    //=============================================================================================
    // Page de tous les widgets : /widgets/index.html
    // Génération du code du widget Notice à la saisie d'un Ean 
    //=============================================================================================
    this.widgetNotice_genCode = function(root_url)
    {
	    var ean_ok = false;
    	
	    if (document.getElementById('widgetNotice_ean')) 
	    {
	        var ean = document.getElementById('widgetNotice_ean').value;
    	    
	        if (ean.length == 13) 
	        {
    	        if (this.widgetNotice_checkEAN(ean)) 
    	        {
	                ean_ok = true;
	                
	                var widget_url = root_url + '/widgets/notice.aspx?ean=';
	                
                    //var loading = '<div align="center" style="text-align:center;"><img src="/ds/i/loading-small.gif" /></div>';
	                //document.getElementById('widgetNotice').innerHTML = loading;
    	            
	                // Rechargement du code du widget dans le textarea : 
	                var code = '<!--[if IE]>' 
	                         + '<iframe src="' + widget_url + ean + '" width="220" height="250"/>'
	                         + '<![endif]-->'
	                         + '<!--[if !IE]><-->' 
	                         + '<object data="' + widget_url + ean + '" type="text/html" width="220" height="250">'
                             + 'alt : <a target="_blank" href="' + widget_url + ean + '">Widget Notice Mollat</a>'
                             + '</object>'
                             + '<!--><![endif]-->'
          
       
                    
	                document.getElementById('widgetNotice_code').value = code;
	                document.getElementById('widgetNotice_code').select();
                    document.getElementById('widgetNotice_guide').style.visibility = "visible";
	            }
	            else {
	                ean_ok = false;
	            }
	        }
	        else {
	            ean_ok = false;
	        }
	    }
	    else {
	        ean_ok = false;
	    }
    	
    	
	    if (!ean_ok) {
	        document.getElementById('widgetNotice_code').innerHTML = 'Veuillez saisir un Ean valide afin de pouvoir générer le code';
	        document.getElementById('widgetNotice_ean').select();
	        document.getElementById('widgetNotice_guide').style.visibility = "hidden";
	    }
    	
    }
    
    
    //=============================================================================================
    // Page de tous les widgets : /widgets/index.html
    // Vérification de l'Ean et rechargement du div contenant le widget Notice 
    //=============================================================================================
    this.widgetNotice_checkEAN = function(ean)
    {
        var xhr_url = '/widgets/ckeckEan.html?ean=' + ean;
        //  => /app_Ajax/frontOffice/AJAX_FO_CONSULTATION.aspx?sub=widget_ean
        
        var loading = '<div align="center" style="text-align:center;"><img src="/ds/i/loading-small.gif" /></div>';
        
        var myXHR = new httpRequest(xhr_url, "", "POST", true);
        myXHR.LoadContent("widgetNotice", loading, "");
        
        //alert(myXHR.xmlHttp.responseText);
        
        return (myXHR.xmlHttp.responseText.indexOf('<object') != -1);
    }
    
    
    
    //=============================================================================================
    // Page de tous les widgets : /widgets/index.html
    // Génération du code du widget Carrousel à la saisie d'au moins 5 Ean 
    //=============================================================================================
    this.widgetCarrousel_genCode = function(root_url)
    {
	    var ean_ok = false;
    	
	    if (document.getElementById('widgetCarrousel_ean')) 
	    {
	        var str_ean = document.getElementById('widgetCarrousel_ean').value;
	        var str_titre = document.getElementById('widgetCarrousel_titre').value;
	        var str_titreColor = document.getElementById('widgetCarrousel_titre_color').value;
	        var str_bgColor = document.getElementById('widgetCarrousel_bg_color').value;
	        
	        if (str_titre == 'Saisissez un titre pour votre carrousel') str_titre = '';
    	    
	        str_titre = encodeURIComponent(str_titre); // pour encoder les accents et caractères spéciaux dans l'url 
	        
	        if (str_titreColor == '') str_titreColor = '567191';
	        if (str_bgColor == '') str_bgColor = 'FFFFFF';
	        
            //-------------------------------------------------------------------------------------
            // Nettoyage de la chaîne des Ean : 
            // suppression des espaces : 
            var reg = new RegExp(" ", "g");
            str_ean = str_ean.replace(reg, '');
            
            // suppression des `;` inutiles : 
            reg = new RegExp("(;){2,}", "g");
            str_ean = str_ean.replace(reg, ';');
            
            // suppression du dernier `;` de fin de chaîne : 
            if (str_ean.charAt(str_ean.length - 1) == ';') {
                str_ean = str_ean.substr(0, str_ean.length - 1);
            }
            
            
            //-------------------------------------------------------------------------------------
            // Vérification de la validité des Ean saisis : 
            
            var str_ean_valid = this.widgetCarrousel_checkEan(str_titre, str_ean, str_titreColor, str_bgColor)
            var tabEan = str_ean_valid.split(';');
            var nbEan = tabEan.length;
            
	        if (nbEan >= 5) 
	        {
	            str_ean = str_ean_valid;
	            
                ean_ok = true;
                
                var widget_url = root_url + '/widgets/carrousel.aspx?titre=' + str_titre + '&color=' + str_titreColor + '&bg=' + str_bgColor + '&ean=';
                
	            
                //-------------------------------------------------------------------------------------
                // Rechargement du code du widget dans le textarea : 
                
                // Largeur et hauteur du widget complet en fonction du nb d'Ean : 
                var widgetWidth = this.widgetCarrousel_setWidth(nbEan);
                var widgetHeight = this.widgetCarrousel_setHeight(nbEan);

                // Hauteur de l'objet Flash contenu dans le widget en fonction du nb d'Ean : 
                var flashWidth = widgetWidth - 6;
                var flashHeight = widgetHeight - 60;
                
                var code = '<!--[if IE]>' 
	                     + '<iframe src="' + widget_url + str_ean + '&w=' + flashWidth + '&h=' + flashHeight + '" width="' + widgetWidth + '" height="' + widgetHeight + '"/>'
	                     + '<![endif]-->'
	                     + '<!--[if !IE]><-->' 
	                     + '<object type="text/html" width="' + widgetWidth + '" height="' + widgetHeight + '" ' 
                         + 'data="' + widget_url + str_ean + '&w=' + flashWidth + '&h=' + flashHeight + '">'
                         + 'alt : <a target="_blank" href="' + widget_url + str_ean + '">Widget Carrousel Mollat</a>'
                         + '</object>';
                
                document.getElementById('widgetCarrousel_code').value = code;
                document.getElementById('widgetCarrousel_code').select();
                document.getElementById('widgetCarrousel_guide').style.visibility = "visible";
                
            }
            else {
	            ean_ok = false;
            }
            
	    }
	    else {
	        ean_ok = false;
	    }
    	
    	
	    if (!ean_ok) {
	        // Textarea du code : 
	        document.getElementById('widgetCarrousel_code').innerHTML = 'Veuillez saisir au moins 5 Ean13 valides séparés par des `;`';
            document.getElementById('widgetCarrousel_guide').style.visibility = "hidden";
	        
	        // Champ text des Ean à saisir : 
	        document.getElementById('widgetCarrousel_ean').select();
	    }
    	
    }
    
    
    //=============================================================================================
    // Page de tous les widgets : /widgets/index.html => Carrousel 
    // Vérification de l'Ean et rechargement AJAX du div contenant le widget Carrousel 
    //=============================================================================================
    this.widgetCarrousel_checkEan = function(str_titre, str_ean, str_titreColor, str_bgColor)
    {
        var xhr_url = '/widgets/checkEanCarrousel.html?titre=' + str_titre + '&color=' + str_titreColor + '&bg=' + str_bgColor + '&ean=' + str_ean;
        //  => /app_Ajax/frontOffice/AJAX_FO_CONSULTATION.aspx?sub=widget_ean_carrousel 
        
        // Cet Ajax renvoie la chaîne (propre) des Ean valides, suivie d'une chaîne de séparation "###", 
        // suivie de la chaîne contenant la balise <object> et les messages associés 
        
        var loading = '<div align="center" style="text-align:center;"><img src="/ds/i/loading-small.gif" /></div>';
        
        var myXHR = new httpRequest(xhr_url, "", "POST", true);
        //myXHR.LoadContent("widgetCarrousel", loading, "");
        myXHR.sendPostReq();
        //alert(myXHR.xmlHttp.responseText);
        
        var tab = myXHR.xmlHttp.responseText.split('###');
        var str_ean_valid = tab[0];
        var response = tab[1];
        
        document.getElementById('widgetCarrousel').innerHTML = response;
        
        //return (myXHR.xmlHttp.responseText.indexOf('<object') != -1);
        
        return str_ean_valid;
    }
    
    
    //=============================================================================================
    // Redimensionnement du widget Carrousel en fonction du nb d'Ean apparant : 
    // Taille de base du widget pour le minimum de 5 Ean : L 460px  x  H 310px
    // Pour chaque Ean supplémentaire on ajoute 30px en largeur, et 12px en hauteur 
    //=============================================================================================
    
    this.widgetCarrousel_setWidth = function(param_nbEan)
    {
        var nbEan_min = 5;
        var min_width = 600; //460;
        var width_step = 30;

        return min_width + ((param_nbEan - nbEan_min) * width_step);
    }
    
    
    this.widgetCarrousel_setHeight = function(param_nbEan)
    {
        var nbEan_min = 5;
        var min_height = 390; //310;
        var height_step = 12;

        return min_height + ((param_nbEan - nbEan_min) * height_step);
    }
    
    
    
} // END function obj_widget() 
