﻿/// 取广告内容
function GetContent(positionid) {
    var url = "/webservice/catolog/getadsinfoservice.asmx/GetAdsInfo";
    var Result = GetWebService(url, "positionid=" + positionid, "string");
    var ad_content = Result.getElementsByTagName("AD_Content")[0];
    //firefox 不支持selectSingleNode方法。
    var adsid = GetValue(ad_content, "AdsID");    
    var type = GetValue(ad_content, "Type");
    var content = GetValue(ad_content, "Content");

    var filename = null;
    if (type != "2") {
        filename = GetValue(ad_content, "FileName");
    }
    var target = "";
    var targetUrl = GetValue(ad_content, "URL");
    if (targetUrl != "#")
        target = "/aspx/adshow.aspx?prodid=" + positionid + "&adsid=" + adsid;
    else
        target = "";
    var width = GetValue(ad_content, "Width");  
    var height = GetValue(ad_content, "Height");  

    ShowAd(adsid, type, content, filename, target, width, height);

    WriteVisitLog(adsid, positionid);
}

function GetValue(ad_content, tagName) {
    return (ad_content != null && ad_content.getElementsByTagName(tagName)[0] != null) ?
        ad_content.getElementsByTagName(tagName)[0].firstChild.nodeValue : "";
}

/// 显示广告内容于页面上
function ShowAd(adsid, type, content, filename, target, width, height) {
    var ads_content = "";
    switch (type) {
        case "0":
            ads_content = GetImageAds(filename, target, width, height);
            break;
        case "1":
            ads_content = GetFlashAds(filename, target, width, height);
            break;
        case "2":
            ads_content = GetTextAds(target, content);
            break;
    }
    //alert(ads_content);
    document.write(ads_content);
}

/// 图片广告
function GetImageAds(filename, target, width, height) {
    var image_content = "";
    if (target != "") {
        image_content = "<a href=\"" + target + "\" target=\"_blank\">" +
                        "<img src=\"http://www.wireunion.com/uploadfiles/ad_files/" + filename + "\" width=\"" + width + "\" " +
                        " height=\"" + height + "\" border=\"0\" /></a>";
    }
    else {
        image_content = "<img src=\"http://www.wireunion.com/uploadfiles/ad_files/" + filename + "\" width=\"" + width + "\" " +
                        " height=\"" + height + "\" border=\"0\" />";
    }
    return image_content;
}

///动画广告
function GetFlashAds(filename, target, width, height) {

    var flash_content = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0' " +
                        "    width=\"" + width + "\" height=\"" + height + "\" id=\"ShockwaveFlash1\"> " +
                        "    <param name=\"movie\" value=\"/uploadfiles/ad_files/" + filename + "\" /> " +
                        "    <param name=\"quality\" value=\"high\" /> " +
                        "    <param name='wmode' value='transparent' /> " +
                        "    <embed src='/uploadfiles/ad_files/" + filename + "' quality='high' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' " +
                        "        type='application/x-shockwave-flash' width=\"" + width + "\" height=\"" + height + "\" wmode='transparent'></embed> " +
                        " </object>";
    return flash_content;
}

///文字广告
function GetTextAds(target, content) {
    var text_content = "";
    if (target != "") {
        text_content = "<a href=\"" + target + "\" target=\"_blank\">" + content + "</a>";
    }
    else
        text_content = content;
    return text_content;  
}

/// 写入广告位访问日志
function WriteVisitLog(adsid,positionid) {
    var url = "/webservice/catolog/writelogservice.asmx/WriteLogByDisplay";
    var result = GetWebService(url, "prodid=" + positionid + "&adsid=" + adsid, "string");
}