﻿if (!AV) {
    var AV = new function() {

    this.UrlBase = null;
    this.IdCapaVideoPrincipal = "Caracol_Main";
    this.IdProductoHogarEsencial = null;
    this.IdCapaContenedora = "content";
    this.NombreCapaFlash = "Caracol_Video";
    this.IdCapaOverlay = "Caracol_Overlay";
    this.FechaLanzamientoVideo = null;

        this.GuardarEvento = function(eventName) {
            $.ajax({ url: "PublicidadHogar.ashx?evento=" + encodeURIComponent(eventName) });
            try { urchinTracker('/AccionComercial/Caracol/' + eventName); } catch (e1) { }
        }

        function CerrarVideo() {
            swfobject.removeSWF(AV.NombreCapaFlash);
            $get(AV.IdCapaContenedora).removeChild($get(AV.IdCapaVideoPrincipal));
            document.body.removeChild($get(AV.IdCapaOverlay));
        }

        this.VideoTerminado = function() {
            AV.GuardarEvento('FinBannerHogar');
            CerrarVideo();
        }

        this.VideoCancelado = function() {
            this.FechaCancelacionVideo = new Date();
            this.FechaCancelacionVideo = this.FechaCancelacionVideo.getTime();
            segundos = (this.FechaCancelacionVideo - this.FechaLanzamientoVideo) / 1000;
            AV.GuardarEvento('VideoCancelado/' + segundos + ' segundos');
            CerrarVideo();
        }

        this.ShowFlashHogar = function() {
            var idCapaHogar = "prodHogar";
            if ($get(idCapaHogar) && swfobject.hasFlashPlayerVersion("9"))
                swfobject.embedSWF(this.UrlBase + "Swf/banner.swf", idCapaHogar, "249", "148", "9.0.0")

        }

        this.ShowBanner = function() {
            if (swfobject.hasFlashPlayerVersion("9")) {
                AV.GuardarEvento('InicioBannerHogar');

                var capaOverlay = document.createElement("DIV");
                capaOverlay.id = this.IdCapaOverlay;
                document.body.insertBefore(capaOverlay, document.body.firstChild);

                var capaCaracol = document.createElement("DIV");
                capaCaracol.id = this.IdCapaVideoPrincipal;

                var capaImagen = document.createElement("DIV");
                capaImagen.className = "cerrar";
                capaCaracol.appendChild(capaImagen);
                var imgCerrar = document.createElement("IMG");
                imgCerrar.alternateText = "Ir a Reale";
                imgCerrar.src = this.UrlBase + "img/Caracol/cerrarCaracol.gif";
                imgCerrar.onclick = function() { AV.VideoCancelado(); }
                capaImagen.appendChild(imgCerrar);

                var nombreCapaFlash = "Caracol_Video";
                var capaFlash = document.createElement("DIV");
                capaFlash.id = this.NombreCapaFlash;
                capaCaracol.appendChild(capaFlash);

                $get(this.IdCapaContenedora).insertBefore(capaCaracol, $get("content_sup"));

                swfobject.embedSWF(this.UrlBase + "swf/caracol_reale.swf", this.NombreCapaFlash, "950", "470", "9.0.0")
            }

        }

        this.Inicializar = function(urlBase, segundosVidaCookie, guid, idProductoHogarEsencial) {
            this.FechaLanzamientoVideo = new Date();
            this.FechaLanzamientoVideo = this.FechaLanzamientoVideo.getTime();
            this.IdProductoHogarEsencial = idProductoHogarEsencial;
            this.UrlBase = urlBase;
            this.ShowFlashHogar();
            var nombreCookie = "cookieMarkCaracol";
            if (!$.readCookie(nombreCookie)) {
                $.setCookie(nombreCookie, guid, { duration: (segundosVidaCookie / (24 * 3600)) });
                this.ShowBanner();
            }
            else {
                AV.GuardarEvento('SkipCookie');
            }
        }

        this.IrHogar = function() {
            AV.GuardarEvento('LinkHogar');
            window.location = this.UrlBase + "Producto/reale-hogar-28.aspx";
        }

        this.IrHogarEsencial = function() {
            AV.GuardarEvento('LinkHogarEsencial');
            window.location = this.UrlBase + 'Producto/reale-hogar-esencial-' + this.IdProductoHogarEsencial + '.aspx';
        }

        this.IrHogarEsencialDesdeVideo = function() {
            AV.GuardarEvento('LinkHogarEsencialDesdeVideo');
        }
    }
}