var ads = [];

function Ads_callBack(transpot)
{    
    try {
        //alert(transpot.responseText)
        var tempText = transpot.responseText.gsub(/\s/,' ');
        var tempAds = ('/*-secure-\n'+tempText+'\n*/').evalJSON();
        initAds(tempAds);
    }catch(e)
    {
        //alert(e);
    }
}

function showPopupAds(adsObj)
{
    if(info.viewPopupAds)return;
    var adsHTML = "";
    switch(adsObj.type)
    {
         case 1: 
            var orgContent = adsObj.content;
            var adsHtmlObj = adsObj;
            
            if(adsHtmlObj.title != '')
            {
                adsHtmlObj.content  = templates.PopupTextTitleAds.evaluate(adsHtmlObj);  
            }else if(adsObj.url != '')
            {
                adsHtmlObj.content  =  templates.PopupTextURLAds.evaluate(adsHtmlObj);  
            }
            adsHTML =  adsHtmlObj.content;  
            adsObj.content = orgContent;
            break;
        case 2:

            var isPic = new RegExp(/\w*\.(jpg|png|gif|jpeg)$/i);
            if (!isPic.test(adsObj.filePath))
            adsHTML = templates.FlashAds.evaluate(adsObj);
            else {
            adsHTML = templates.MMOAds.evaluate(adsObj); }
            break;
     } 
     
      var params= Object.extend({ 
            seed:Math.floor(Math.random()* 100000000),
            type:'viewPopupAds'
        }
        );
        new Ajax.Request( '/ads.aspx', {
            onSuccess:function(t){},
            onFailure:function(transpot){},
            method: 'post',
            parameters: params
        });
     
    var win=window.open("about:blank","_blank","width="+adsObj.width+",height="+adsObj.height);
    win.document.open();
    win.document.write(adsHTML);
    win.document.close();
            
}

function initAds(tempAds)
{    
    if(tempAds.ads.length == 0)return;
    tempAds.innerContentID = tempAds.contentID + "_content";

    var tempContent = "";
        
    if(tempAds.adsType == 'Ads_popup')
    {
        showPopupAds(tempAds.ads[0]);
        return;
    }
    tempAds.adsHeight = 'auto';
    tempAds.ads.each(
        function(a)
        {
            if(a.height != -1)
            {
                if(tempAds.adsHeight != 'auto')
                {
                    if(a.height > tempAds.adsHeight)tempAds.adsHeight = a.height;
                }else
                {
                    tempAds.adsHeight = a.height;   
                }
            }
        }
    )
    if(tempAds.adsHeight != 'auto' && tempAds.adsType == 'Ads_C')tempAds.adsHeight += 20;
    if(tempAds.adsHeight != 'auto')tempAds.adsHeight += 'px';
    switch(tempAds.adsType)
    {
        case 'Ads_C': tempContent = templates.AdsCTitle.evaluate(tempAds); break;
        default: tempContent = templates.AdsTitle.evaluate(tempAds); break;
    }
    
    $(tempAds.contentID).update(tempContent);
    tempAds.currentAds = 0;
    ads[ads.length] = tempAds;
    showAds(tempAds.adsType);
}

function clickAds(adsTypeName)
{
    try {
        var tempObj = $(ads).find(function(o){return o.adsType == adsTypeName});
        var adsObj = tempObj.ads[tempObj.currentAds];
        if (adsObj.onclick == undefined)
        {
            var email = MemberLogin.str_email == undefined ? "" : MemberLogin.str_email;
            adsObj.onclick = true;    
            var params= Object.extend({ 
			    seed:Math.floor(Math.random()* 100000000),
			    adsContractID:adsObj.id,
			    email:email,
			    type:'clickAds'
		    }
		    );
	        new Ajax.Request( '/ads.aspx', {
		        onSuccess:function(t){},
		        onFailure:function(transpot){},
		        method: 'post',
		        parameters: params
	        });
        }
    }catch(e)
    {
        alert(e);
    }
    
}

function hideFullscreenFlashLayer()
{
    try
    {
        $('Ads_fullscreen_content').hide();
    }
    catch(e){}
}

function replayFullscreenFlash()
{
    try
    {
        var adsType = $(ads).find(function(o){return o.adsType == 'Ads_fullscreen'});
        $('Ads_fullscreen_content').show();
        $('Ads_fullscreen_content').update(templates.FlashAds.evaluate(adsType.ads[adsType.currentAds]))
    }
    catch(e){}
}

function showAds(adsTypeName)
{
    var adsType = $(ads).find(function(o){return o.adsType == adsTypeName});
    var adsObj = adsType.ads[adsType.currentAds];
    adsObj.adsType = adsTypeName;
    switch(adsObj.type)
    {
        case 1: 
            var orgContent = adsObj.content;
            var adsHtmlObj = adsObj;
            if(adsHtmlObj.title != '')
            {
                adsHtmlObj.content  = templates.TextTitleAds.evaluate(adsHtmlObj);  
            }
            if(adsObj.url != '')
            {
                adsHtmlObj.content  =  templates.TextURLAds.evaluate(adsHtmlObj);  
            }
            $(adsType.contentID+"_content").update(adsHtmlObj.content);  
            adsObj.content = orgContent;
            break;
        case 2:
            //var isswf = new RegExp(/\w*\.swf$/i);
            var isPic = new RegExp(/\w*\.(jpg|png|gif|jpeg)$/i);
            if (!isPic.test(adsObj.filePath)) $(adsType.contentID + "_content").update(templates.FlashAds.evaluate(adsObj));
            else {
            //alert(templates.MMOAds.imageLink);
            $(adsType.contentID+"_content").update(templates.MMOAds.evaluate(adsObj)); }
            break;
        case 3: 
            if(!info.viewFullScreenAds){
                $(adsType.contentID+"_content").update(templates.FlashAds.evaluate(adsObj));
                 var params= Object.extend({ 
			        seed:Math.floor(Math.random()* 100000000),
			        type:'viewFullScreenAds'
		        }
		        );
                new Ajax.Request( '/ads.aspx', {
		            onSuccess:function(t){},
		            onFailure:function(transpot){},
		            method: 'post',
		            parameters: params
	            });
            }
            //window.setTimeout(replayFullscreenFlash,11000);
            break;
        default: 
            $(adsType.contentID+"_content").update(templates.TextAds.evaluate(adsObj));
    }
    if(adsType.ads.length > 1)
    {
        if(adsTypeName == 'Ads_fullstory')adsFullstoryTimeOut = window.setTimeout("showNextAds('" + adsTypeName + "')",adsObj.duration * 1000);
        else window.setTimeout("showNextAds('" + adsTypeName + "')",adsObj.duration * 1000);
    }   
}

function showNextAds(adsTypeName)
{
    var adsType = $(ads).find(function(o){return o.adsType == adsTypeName});
    if(adsType.ads.length -1  == adsType.currentAds)adsType.currentAds = 0;
    else adsType.currentAds += 1;
    showAds(adsTypeName);
}