/**
 * Groupalia js modules
 *
 * @author    Víctor González <victor.gonzalez@groupalia.com>
 * @copyright Copyright (c) 2011 Groupalia (http://www.groupalia.com)
 */
var GP = {};


/*
    HELPER TO CONVERT ELEMENTS FROM STRING TO OBJECT
*/
function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}

GP.cookieManager = {
    /*
        @description : Set a cookie in the browser (not working for private navigation) with a name, a value and an expires time set in days.
        @param name : Name of the cookie
        @param value : Value of the cookie
        @param expires : (OPTIONAL) Number of days until the cookie expires
        @usage: GP.cookieManager.setCookie('nombre_de_cookie', '1', '15')
    */
    setCookie: function (name, value, expires, path, domain, force) {
        var ARRcookies=document.cookie.split(";");
        var ARRcookiesX = new Array();
        for (i=0;i<ARRcookies.length;i++){
            var x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
            x=x.replace(/^\s+|\s+$/g,"");
            ARRcookiesX[i] = x;
        }
        if(name in oc(ARRcookiesX)) {
            if(force==true){
                var today = new Date();
                today.setTime(today.getTime());
                if ( expires )
                {
                    expires = expires * 1000 * 60 * 60 * 24;
                }
                var expires_date = new Date( today.getTime() + (expires) );
                document.cookie = name + "=" +escape( value ) +
                ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
                ( ( path ) ? ";path=" + path : ";path=/" ) +
                ( ( domain ) ? ";domain=" + domain : "" );
            }
        } else {
            var today = new Date();
            today.setTime(today.getTime());
            if ( expires )
            {
                expires = expires * 1000 * 60 * 60 * 24;
            }
            var expires_date = new Date(today.getTime() + (expires));
            document.cookie = name + "=" +escape( value ) +
            ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
            ( ( path ) ? ";path=" + path : ";path=/" ) +
            ( ( domain ) ? ";domain=" + domain : "" );
        }
        
    },
    /*
        @description : Check if a cookie with a certain value is setted in the browser
        @param name : Name of the cookie
        @param value : Value of the cookie
        @usage: GP.cookieManager.getCookie('nombre_de_cookie', '1')
    */
    getCookie: function (name, value) {
        var i,x,y,ARRcookies=document.cookie.split(";");
        for (i=0;i<ARRcookies.length;i++){
            x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
            y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
            x=x.replace(/^\s+|\s+$/g,"");
            if (x==name && y==value)
            {
                return unescape(y);
            }
        }
        return null;
    },
    /*
        @description : Triggers a javascript function if a cookie with certain value and name is setted in the browser
        @param name : Name of the cookie
        @param value : Value of the cookie
        @param exec : Name of the function that wants to be executed
        @usage: GP.cookieManager.triggerCookie('nombre_de_cookie', '1', funcion_a_ejecutar())
    */
    triggerCookie: function(name, value, exec) {
        if(this.getCookie(name, value)){
            var toexec = exec;
            eval(exec);
        }
    },
    /*
        @descripcion : Set a cookie for the subscription lightbox
        @param expires : Number of days until the cookie expires
        @usage : GP.cookieManager.cookieSubscription('1')
    */
    cookieSubscription: function(expires) {
        if(this.getCookie('subscribe_cookie', 'false') == null){
            this.setCookie('subscribe_cookie', 'true', expires);
        }
    },
    /*
        @descripcion : Clears a cookie from the browser that matches a certain name and value
        @param name : Name of the cookie
        @param value : Value of the cookie
        @usage : GP.cookieManager.clearCookie('nombre_de_cookie', '1')
    */
    clearCookie: function(name, value) {
        if(this.getCookie(name, value)){
            this.setCookie(name,'',-1, true);
        }
    }
};

var i18n = {
    "Close": "Cerrar"
};

GP.translator = {
    /*
        @descripcion : Translate a string defined in the i18n array
        @param str : String to be translated
        @usage : GP.translator.trans('String to translate')
    */
    trans: function(str){
        if(typeof(i18n) != 'undefined' && i18n[str]){
            return i18n[str];
        }
        return str;
    }
}

GP.modal = {
    /*
        @descripcion : Opens a modal window given an url
        @param url : Can be a complete url or a hidden div in the web (Ex. http://www.google.com or #hiddendiv)
        @usage : GP.modal.openModal('#hiddendiv')
    */
    openModal: function(url){
        jQuery.nmManual(url);
    },
    /*
        @descripcion : Opens a modal window given an url and assign the close event to an specified id
        @param url :  Can be a complete url or a hidden div in the web (Ex. http://www.google.com or #hiddendiv)
        @param closeid : Selector of the element that closes the modal window
        @usage : GP.modal.openCustomModal('#hiddendiv', '.close_button')
    */
    openCustomModal: function(url, closeid){
        jQuery.nmManual(url);
        jQuery(closeid).live('click', function(){
            jQuery.nmTop().close();
        });
    },
    /*
        @descripcion : Opens a modal window that calls via ajax the subscription panel
    */
    modalSubscription: function(url, cloud_url){
        if (GP.cookieManager.getCookie('subscribe_cookie', 'true') != null){
            var options = {
                width: 669,
                height: 460,
                zIndexStart: 100,
                minWidth: 669,
                minHeight: 424,
                callbacks: {
                    beforeShowCont: function(nm) {
                        GP.cookieManager.setCookie('subscribe_cookie', 'false', 15, '/', '', true);
                        jQuery('.nyroModalCloseSL').live('click', function(){
                            jQuery.nmTop().close();
                        });    
                        jQuery('.nyroModalCont').addClass('Subscription');
                    },
                    afterShowCont: function(nm) {
                        jQuery('#form-suscribe-caption').ready(function(){
                            GP.cookieManager.setCookie('subscribe_cookie', 'false', 15, '/', '', true);
                            var subscribeForm = new VarienForm('form-suscribe-caption', true);
                        });
                    }
                }
            }
            var url = url;
            var url = url.split('/');
            var urllightbox = cloud_url+url[3]+'/ajax/subscriptionlightbox';
            jQuery(document).ready(function(){
                jQuery.nmManual(urllightbox, options);
            });
        }
    }
};
