function replaceAll(txt, replace, with_this) {
    return txt.replace(new RegExp(replace, 'g'), with_this);
}

function htmlencode(s) {
    if (s != null) {
        s = "" + s; 
        s = replaceAll(s, "&", "&amp;"); 
        s = replaceAll(s, "\'", "&apos;"); 
        s = replaceAll(s, "\"", "&quot;"); 
    }
    return s; 
}

function createTag(name, params, innerHTML) {
    var xml = "<" + name; 
    if (params != null) {
        for (var k in params)
            xml += ' ' + k + '="' + htmlencode(params[k]) + '"'; 
    }
    if (innerHTML == undefined) {
        xml += "/>\n"; 
    } else {
        xml += ">\n"; 
        if (innerHTML != null)
            xml += innerHTML; 
        xml += "</" + name + ">\n"; 
    }
    return xml; 
}

function flashencode(s) {
    if (s == null) return null; 
    s = "" + s; 
    s = replaceAll(s, '&', '\uFF06'); 
    return s; 
}

function createFlash(swf, width, height, vars) {
    var flashvars = ''; 
    for (var k in vars) {
        if (flashvars != '')
            flashvars += '&'; 
        flashvars += k + '=' + flashencode(vars[k]); 
    }
    
    var flash = {
        classid:    "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000", 
        codebase:   "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0",
        width:      width,
        height:     height
    }; 
    var flashInner = ''; 
    
    var _embd = {
        type:       "application/x-shockwave-flash",
        pluginspage:"http://www.macromedia.com/go/getflashplayer",
        width:      width,
        height:     height
    };
    
    function setAttribute(k, v, ek) {
        var objParam = {
            name: k, 
            value: v
        };
        flashInner += createTag('param', objParam); 
        if (ek == undefined) ek = k.toLowerCase(); 
        _embd[ek] = v; 
    }
    setAttribute('movie', swf, 'src');
    setAttribute("allowScriptAccess", "sameDomain"); 
    setAttribute('bgcolor',   '#FFFFFF'); 
    setAttribute('quality',   "high"); 
    setAttribute('menu',      "false");
    setAttribute('flashvars', flashvars); 
    setAttribute('wmode',     "opaque"); 
    
    flashInner += createTag('embed', _embd); 
    return createTag('object', flash, flashInner); 
}

function createFRoll(items, width, height) {
    var vars = {
            borderwidth: width, 
            borderheight: height, 
            textheight: 30
        };
        
    var pics = '', links = '', texts = ''; 
    
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        var title = item.title; 
        var link = item.link; 
        var img = item.large; 
        // var img = 'http://www.hnny.gov.cn/' + item.large; 
        // var img = 'images/dx_03.gif'; 
        if (i != 0) {
            pics += '|'; 
            links += '|'; 
            texts += '|'; 
        }
        pics += img; 
        links += link; 
        texts += title; 
    }
    
    vars.pics = pics; 
    vars.links = links; 
    vars.texts = texts; 
    var flash = createFlash('images/pixviewer.swf', width, height, vars); 
    return flash; 
}
