//全局變量，
//檢查提取或解析XML廣告配置文件時是否出現錯誤
var isError = false;
//廣告鏈接和實體鏈接
var linkUrl, entityUrl;
var xmlPath = "/AdPreview/";

//獲取XML文件名
function getXMLFilePath()
{
    var ret = xmlPath + "ad_";
    ret += ad_siteid;
    ret += "_";
    ret += ad_nodeid;
    ret += ".xml";
    return ret;
}

//解析廣告的XML配置文件
function parseXMLFile()
{
    var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
    var root;
    xmlDoc.async = false;
    try
    {
        xmlDoc.load(getXMLFilePath());
        root = xmlDoc.documentElement;
        if(root.childNodes.item(0).childNodes.item(0).text == "1")
        {
            isError = true;
            return;
        }
        linkUrl = root.childNodes.item(1).childNodes.item(0).text;
        entityUrl = root.childNodes.item(2).childNodes.item(0).text;
    }
    catch(ex)
    {
        isError = true;
    }
}

//檢查是否有none置標(由編輯器中插入的代表此頁面不需要廣告)
function hasNoneTag()
{
	try{
    var noneTag = document.all("adv_div_none");
    if(noneTag == null)
        return false;
    else
        return true;
    }
    catch(ex){return false;}
}

//檢查是否有edit置標(由編輯器指定的廣告位置)
function hasEditTag()
{
	try{
    var editTag = document.all("adv_div_edit");
    if(editTag == null)
        return false;
    else
        return true;
    }catch(ex){return false;}
}

//將edit置標中的demo標志去掉
/*
function dropDemoInEditTag()
{
    var demoTag = document.all("adv_div_demo");
    if(demoTag != null)
        demoTag.style.display = "none";
}
*/

//獲取廣告實體的類型
function getEntityType()
{
    var pos = entityUrl.lastIndexOf(".");
    var entityType = entityUrl.substring(pos + 1);
    return entityType;
}

function isFlash(entityType)
{
    if(entityType == "swf")
        return true;
    return false;
}

//獲取最終要顯示的廣告實體的HTML源代碼
function getDisplayContent(vAlign)
{
    var result = "";
    if(isFlash(getEntityType()))
    {
        result += "<OBJECT align=" + vAlign + " classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://big5.xinhuanet.com/gate/big5/active.macromedia.com/flash2/cabs/swflash.cab#version=5,0,0,0\">";
        result += "<PARAM NAME=movie VALUE=\"" + entityUrl + "\">";
        result += "<PARAM NAME=quality VALUE=high>";
        result += "<EMBED src=\"" + entityUrl + "\" quality=high TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://big5.xinhuanet.com/gate/big5/www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\"></EMBED>";
        result += "</OBJECT>";
    }
    else
    {
        result += "<a href=\"" + linkUrl + "\">";
        result += "<img src=\"" + entityUrl + "\" border=0 align=" + vAlign + ">";
        result += "</a>";
    }
    return result;
}

//進行初始化
function initAll()
{
    //dropDemoInEditTag();
    if(hasNoneTag())
        isError = true;
        
        
    if(hasEditTag())
        adLayerObject = document.all("adv_div_edit");
    else
        adLayerObject = document.all("adv_div_default");
        
}

//主函數入口
function adMain()
{
    initAll();
    parseXMLFile();
    if(isError)
    {
    	try{
        	document.all("adv_div_edit").style.display = "none";
        }catch(ex){}
        return;
    }

    
    var vAlign;
    //var adLayer = document.createElement("DIV");
    if(hasEditTag())
    {
        vAlign = document.all("adv_div_edit").align;
        //adv_div_edit.replaceNode(adLayer);
        //adLayer.innerHTML = getDisplayContent(vAlign);
        adv_div_edit.outerHTML = getDisplayContent(vAlign);
        //adv_div_edit.style.display = "inline";
    }
    else
    {
        vAlign = "left";
        adv_div_default.innerHTML = getDisplayContent(vAlign);
        document.all("adv_div_default").style.display = "inline";
    }
}

function showAd(nAdType, nAdPosition, nStyle)
{
	adMain();
}