/**
 * 大部分代码Copy于render.js
 * 
 * @author jicheng.li
 */

XN.dom.ready(function(){
	//loadFile方式从引擎取回json//代码在尾部
	if(XN.env.domain == 'renren.com'){
		var shaft_url = 'http://shaft.jebe.renren.com/show?a=1000000061,1,3,1&r=http://www.renren.com&type=single';		
	}else{
		var shaft_url = 'http://shaft.jebe.renren.com/show?a=1000010029,1,3,1&r=http://www.kaixin.com&type=single';
	}
	
	if (XN) {
        var domain = XN.env.domain;
        var $element = $element;
        var each = XN.array.each;
        var isBlank = XN.string.isBlank;
    }
	
    var staticURI = 'http://jebe.xnimg.cn/';
    var templete = {};
    
    
    templete[1] = function(ad){ //文字广告
		var bHigh = (ad.adzoneid == '1000000054' || ad.adzoneid == '1000000055')
        var str = '<a target="_blank" href="' + ad.click_url + '">' +
        ad.text1.replace(/%%\(.*?\)(.*?)%%/g, '<span style="color:red">$1</span>') +
        '</a>';
        if (bHigh) 
            str = '<p>' + str + '</p>'
        return str;
    };
    
    templete[2] = function(ad){  
        if (/\.swf$/.test(ad.media_uri)) { //flash广告
            var html = '<div style="position:relative;">';
            
            html += '<embed src="' + ad.media_uri ;
            var if_click = parseInt(ad.flash_click_type, 0);
            
            if (isNaN(if_click)) {
                if_click = 0;
            }
            
            if (if_click == 1) {
                html += '?' + ad.flash_click_value + '=' + encodeURIComponent(ad.click_url);
            }
            html += '" type="application/x-shockwave-flash" ' + 'width="' +  ad.width + '" height="' + ad.height +
			 '" wmode="transparent" FlashVars="xname=' + (typeof jebe_username != 'undefined' &&  jebe_username ? jebe_username : '您好') + '">';                       
            html += '</embed>';
            
            if (if_click == 0) {
                html += '<div style="' + (XN.browser.IE ? 'filter:alpha(opacity=1);background-color:#fff;' : '') + 'width:' + ad.width + 'px; height: ' + ad.height + 'px;cursor:pointer;position: absolute; left: 0px; top: 0px; z-index: 999;" onclick="window.open(\'' + ad.click_url + '\');"></div>';
            }
            
            html += '</div>';
            
            return html;
        }else{  //图片广告
            return ['<a target="_blank" href="' + ad.click_url + '">', '<img width="' + ad.width + '"', 'height="' + ad.height + '"', 'src="' + ad.media_uri + '"', ' />', '</a>'].join('');
        }
    };
    
    templete[3] = function(ad){
        return '';
    };

 	
    function buildAD(obj){

		var div = $('huge-ad');
        var bLoad = false;
		var v = obj.ads[0];
        v.adzoneid = obj.adzoneid;
        if (v.media_uri && !/^http:\/\//.test(v.media_uri)) {
            v.media_uri = staticURI + v.media_uri; //TODO staticURI
        }
        if (v.format == 2) { //是图片和FLASH广告
            bLoad = true;
        }
		
        var cbFun = function(){
            var html = '';
            var v = obj.ads[0];
                var fun = templete[v.format];
                if (typeof fun == 'function') 
                    div.innerHTML = fun(v);
        }
		
        if (!bLoad) {
			cbFun();
		}else {
			beginLoad(obj.ads[0], cbFun);
		}
    };
	
    function beginLoad(ad, cbFun){ //单个广告
        var url = ad.media_uri;
		if(!url) {cbFun();return;}
        if (/swf$/i.test(url)) { //FALSH
            var sid = ad.adzoneid + 'swf'
            var html = '<embed id="' + sid + '" wmode="transparent" type="application/x-shockwave-flash" src="'+url+'"/>'
            var el = document.createElement('div');
			var jj = $('jebe_con_load')
			if (!jj) {
					jj = document.createElement('div');
					jj.style.height = '0px';
					jj.style.overflow = 'hidden';
					jj.id = 'jebe_con_load';
					document.body.appendChild(jj);
			}
			jj.appendChild(el);
            el.innerHTML = html;
            var swfTimer = setInterval(function(){
                var fla = $( sid )
                if (!fla) {
					clearInterval(swfTimer);
					return;
				}
		//try{   //曾经被地木注释掉的，现在又发现兼容错误   //后来发现是引擎错误（没有吧新版shaft更到外网）
                var n =  fla.PercentLoaded() ||  0;
		//}catch(e){n=0}
                //var n = fla.PercentLoaded ? fla.PercentLoaded() : 0;
                if (n >= 100) {
					el.innerHTML = ''
                    clearInterval(swfTimer);
                    cbFun();
                }
            }, 10)
            //InterTimer.push(swfTimer)
        }else { //图片
		    var img = new Image()
            img.onload = function(){   //又是异步！！！只能这样了（为了IE6添加的代码）
				setTimeout(cbFun,0); 
			};
            img.src = url;
        }
        
        
    }


    //setTimeout(function(){
        XN.loadFile({file:shaft_url,type:'js'}, function(){
            buildAD(json.result[0]);
        });
    //},0);

});

