
var Flash = {

    // Zona de Configuraciones prontus_id
    prontus_dir: '/noticias',
    swfVideo: '/flash/players/playerVideo.swf?'+(new Date()).getTime(),    // Ruta del player de video
    swfImgFade: 'image_fade/prontus_imgfade_8.2.swf', // Ruta del swf de imageFade
    objDefault: {
        quality: 'high',
        wmode: 'transparent',
        AllowFullScreen: 'true',
        allowScriptAccess: 'sameDomain'
    },

    // -----------------------------------------------------
    insertaImageFade: function(file, objid, wflash, hflash, target, img1, img2, img3, img4) {
        if(typeof img1 == 'undefined') {
            img1 = '';
        }
        if(typeof img2 == 'undefined') {
            img2 = '';
        }
        if(typeof img3 == 'undefined') {
            img3 = '';
        }
        if(typeof img4 == 'undefined') {
            img4 = '';
        }
        if(typeof target == 'undefined') {
            target = '';
        }
        var flashvars = {
            linkurl: file,
            urlartic: file,
            target: target,
            foto1: img1,
            foto2: img2,
            foto3: img3,
            foto4: img4 };
        var obj = {flashvars: flashvars};
        Flash.insertaFlash(Flash.prontus_dir +Flash.swfImgFade, objid, wflash, hflash, obj);
    },

    // -----------------------------------------------------
    insertaPlayerVideo: function(vurl, objid, wflash, hflash, img, ts, secc, tema, stem, titu) {
        if(typeof img == 'undefined') {
            img = '';
        }
        var flashvars = {
            VURL: vurl,
            VIMG: img,
            TS: ts,
            SECC: secc,
            TEMA: tema,
            STEM: stem,
            TITU: titu };
        var obj = {flashvars: flashvars};
        if(navigator.userAgent.indexOf('iPad') >= 0 && navigator.userAgent.indexOf('Mac OS X') >= 0) {
            var strHTML5 = '<video src="'+vurl+'" ' +
                'poster="'+img+'" ' +
                'width="'+wflash+'" ' +
                'height="'+hflash+'" ' +
                'controls="controls">' +
                'Tu navegador no soporta HTML5</video>';
            document.write(strHTML5);

        } else {
            Flash.insertaFlash(Flash.prontus_dir + Flash.swfVideo, objid, wflash, hflash, obj);
        }
    },

    // -----------------------------------------------------
    insertaFlash: function(movieUrl, objid, wflash, hflash, obj) {

        obj = Flash.merge(Flash.objDefault, obj);
        // Validacion de los parametros de entrada
        if (typeof movieUrl === 'undefined' || movieUrl == '' ||
            typeof objid === 'undefined' || objid == '' ||
            typeof wflash === 'undefined' || wflash == '' ||
            typeof hflash === 'undefined' || hflash == '') {
            return;
        }

        // Si tiene el plugin de media, utila ese mismo para insertar
        if(Flash.hasMedia()) {

            // Se revisa si tiene flashvars
            var flashvars;
            if ((typeof obj !== 'undefined') && (typeof obj.flashvars !== 'undefined')) {
                flashvars = obj.flashvars;
                delete obj.flashvars;
            }

            // Y finalmente se inserta en el
            $('#'+objid).media({
                width:      wflash,
                height:     hflash,
                src:        movieUrl,
                params:     obj,
                attrs:      obj,
                flashvars:  flashvars
            });

        // Se utiliza el método carretero
        } else {

            var strFlash = Flash.getStrFlash(movieUrl, objid, wflash, hflash, obj);

            // Se intenta con jquery
            if(Flash.hasJQuery()) {
                $('#'+objid).html(strFlash);

            // Si no funciona jquery sehace a la antigua
            } else {
                var midiv = document.getElementById(objid);
                if(typeof midiv !== 'undefined') {
                    midiv.innerHTML = strFlash;
                }
            }
        }
    },

    // -----------------------------------------------------
    getStrFlash: function(movieUrl, objid, wflash, hflash, obj) {

        var str = '';

        // Se revisa si tiene flashvars
        if(typeof obj !== 'undefined' && typeof obj.flashvars !== 'undefined') {
            obj.flashvars = Flash.generaFlashVars(obj.flashvars);
            //delete obj.flashvars;
        }

        // Se comienza con la creacion del string
        str = str + '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"';
        str = str + '  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0"';
        str = str + '  width="' + wflash + '"';
        str = str + '  height="' + hflash + '"';
        str = str + '  id="flash' + objid + '">';
        str = str + '  <param name="movie" value="' + movieUrl + '" />';
        for(var nomb1 in obj) {
            if(typeof obj[nomb1] !== 'undefined') {
                str = str +  '<param name="' + nomb1 + '" value="' + obj[nomb1] + '" />';
            }
        }
        str = str + '  <embed';
        str = str + '    src="' + movieUrl + '"';
        str = str + '    width="' + wflash + '"';
        str = str + '    height="' + hflash + '"';
        str = str + '    name="flash' + objid + '"';
        str = str + '    type="application/x-shockwave-flash"';
        str = str + '    pluginspage="http://www.macromedia.com/go/getflashplayer"';
        for(var nomb2 in obj) {
            if(typeof obj[nomb2] !== 'undefined') {
                str = str + '  ' + nomb2 + '="' + obj[nomb2] + '"';
            }
        }
        str = str + '    />';
        str = str + '</object>';
        return str;
    },

    // -----------------------------------------------------
    generaFlashVars: function(obj) {
        var str = '';
        for(var variable in obj) {
            if(typeof obj[variable] !== 'undefined') {
                str = str + variable + '=' + obj[variable] + '&';
            }
        }
        if(str != '') {
          str = str.substr(0, str.length - 1);
        }
        return str;
    },

    // -----------------------------------------------------
    merge: function(obj1, obj2) {
        if(typeof obj1 == 'undefined') {
            return obj2;
        } else if(typeof obj2 == 'undefined') {
            return obj1;
        }
        for(var attrname in obj2) {
            if(typeof obj2[attrname] !== 'undefined') {
                obj1[attrname] = obj2[attrname];
            }
        }
        return obj1;
    },

    // -----------------------------------------------------
    hasMedia: function() {
        if(!Flash.hasJQuery()) {
            return false;
        }
        if(jQuery().media) {
            return true;
        } else {
            return false;
        }
    },

    // -----------------------------------------------------
    hasJQuery: function() {
        if (typeof jQuery == 'undefined') {
            return false;
        } else {
            return true;
        }
    }
};

