if (typeof Ferengi == "undefined") {
    var Ferengi = {
        Version: "1.0"
    };
    Ferengi.namespace = function() {
        var a = arguments,
			o = null,
			i, j, d;
        for (i = 0; i < a.length; ++i) {
            d = a[i].split(".");
            o = Ferengi;
            for (j = (d[0] == "Ferengi") ? 1 : 0; j < d.length; ++j) {
                o[d[j]] = o[d[j]] || {};
                o = o[d[j]];
            }
        }
        return o;
    };
};
Ferengi.namespace("Pxp");
// Creating a class
Ferengi.Pxp.Beneficio = new Class({
    Implements: Options,
    options: {
        ProjectId: ""
		, ContentId: ""
		, AttachId: ""
		, ObjectType: ""
		, ProjectContent: ""
		, Text: ""
		, EsBeneficioValido: null
    },
    initialize: function(options) {
        this.setOptions(options);
        // initialize commands goes here
    }

});
Ferengi.Pxp.Beneficio.implement({
    load: function(o) {
	
		
		if(this.options.EsBeneficioValido != true) o.style.display = 'none';
		if(this.options.EsBeneficioValido == true) o.style.display = 'block';
	
        var url = "/ProjectRegistration.aspx?Id=" + this.options.ProjectId + "&amp;ProjectContent=" + this.options.ProjectContent + "&amp;ObjectType=" + this.options.ObjectType + "&amp;AttachId=" + this.options.AttachId + "&amp;ContentId=" + this.options.ContentId + "&amp;Grisado=si";
        url = url + "&amp;TB_iframe=true&amp;height=550&amp;width=650";
        if (this.options.Text != "") var html = "<div class='botonbeneficiodiv botonTextoDinamico'><A class=iframebox title='' href='" + url + "' NeedsAuthentication='true'> <span>" + this.options.Text + "</span> </A></div>";
        else var html = "<div class='botonbeneficiodiv'><A class=iframebox title='' href='" + url + "' NeedsAuthentication='true'><IMG alt='' src='/Images/boton_suscribite_beneficio.png'> </A></div>";

        $(o).set('html', html);

        var host = window.location.host;
        var sectionId = "12"; //sectionId de CV
        
        if (host.contains('.uy') || host.contains('cablevisionuruguay')) {
            sectionId = "240"; //sectionId de CV Uruguay
        }
        else
            if (host.contains('.py') && host.contains('cablevision')) {
                sectionId = "461"; //sectionId de CV Paraguay
            }
            else
                if(host.contains('.py') && host.contains('fibertel')) {
                    sectionId = "436"; //sectionId de FT Paraguay
                }
                else
                    if (host.contains('fibertel')) {
                        sectionId = "96"; //sectionId de FT 
                    }
        
        //esto es para chequear que el contenido esté destacado actualmente en Beneficios de CV/FB sino no muestra el botón de imprimir cupón
		
		if(this.options.EsBeneficioValido == null)
		{
			urlToCheck = "/CheckCurrentHighlight.ashx?ObjectType=Section&ObjectId=" + sectionId + "&TargetZone=Beneficios&ContentId=" + this.options.ProjectContent;
			var myAjaxFeed = new Request({
				url: urlToCheck,
				method: 'get',
				onSuccess: function(rpta) {
				if (!rpta.contains('0')) {o.style.display = 'block';}
				}
			});
			myAjaxFeed.send();
		}
    }
});



window.addEvent("domready", function() {
    //Busca todos los divs que tengan como clase Ferengi_Pxp_Beneficio por si hay varios beneficios en la pagina
    //<DIV class=Ferengi_Pxp_Beneficio ferengi_pxp_beneficio="ProjectId=28&amp;ProjectContent=708&amp;AttachId=1195">- Imprimí -</DIV>
    var oo = $$('.Ferengi_Pxp_Beneficio');
    for (var i = 0; i < oo.length; i++) {
        var o = oo[i];

        var oparams = {};
        var attrs = o.getAttribute('Ferengi_Pxp_Beneficio')
        var arrAttrs = attrs.split(/&/g)
        for (var i2 = 0; i2 < arrAttrs.length; i2++) {
            var attr = arrAttrs[i2]
            var arrPair = attr.split(/=/);
            if (arrPair.length > 1) {
                var name = arrPair[0];
                var value = arrPair[1];
                if (name == "ProjectId") oparams.ProjectId = value;
                if (name == "ContentId") oparams.ContentId = value;
                if (name == "AttachId") oparams.AttachId = value;
                if (name == "ObjectType") oparams.ObjectType = value;
                if (name == "ProjectContent") oparams.ProjectContent = value;
                if (name == "EsBeneficioValido") oparams.EsBeneficioValido = ("true" == value);
                if (name == "Text") oparams.Text = value;
            }
        }
        var gp = new Ferengi.Pxp.Beneficio(oparams);
        gp.load(o);
        
    }
});


