var _sti = setInterval;
var $$timer;
window.setInterval = function(callback,timeout,param) {
    var args = Array.prototype.slice.call(arguments,2);
    var _cb = function() {
        callback.apply(null,args);
    };
    $$timer = _sti(_cb,timeout);
}


//滚动对联

function scrollAd(width, height, pos_x, pos_y, url_1, url_2) {
    
	var cHeight = document.compatMode=="CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
	var cWidth = document.compatMode=="CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
	var px_1 = pos_x >= 0? pos_x : cWidth + pos_x - width;
	var px_2 = cWidth - px_1 - width;
    var py = pos_y >= 0? pos_y : cHeight + pos_y;
	var ddy = cHeight - py;
	var ddx = px_1 + width;
    var id1 = url_1 == '#'? null : loadingAd(width, height, px_1, py, url_1, 1);
	var id2 = url_2 == '#'? null : loadingAd(width, height, px_2, py, url_2, 1);
	var ids = [id1, id2];
    
	window.setInterval(playAd, 30, ids, ddx, ddy);

	var close = document.getElementById('close1');
    close.onclick = function() {
		clearInterval($$timer);
		for(i=0; i<ids.length; i++) {
		    if(ids[i] != null) {
			    ids[i].style.display = 'none';
			}
		}
		return false;
	}
}



function loadingAd($width, $height, $px, $py, $url, $i) {
    
    var idid = Math.ceil(Math.random()*10000) + 's';
	var img = '<a href="$url2$" target="_blank"><img style="width:$width$px;height:$height$px;border:none;margin:0;padding:0" src="$url1$" /></a>';
	var flash = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
              + ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0" '
			  + ' height="$height$" width="$width$"> '
			  + '<param name="movie" value="$url$" /> '
			  + '<param name="quality" value="high" /> '
			  + '<param name="wmode" value="opaque" /> '
			  + '<EMBED src="$url$" quality="high" WIDTH="$width$" HEIGHT="$height$" '
			  + ' wmode="opaque" TYPE="application/x-shockwave-flash" '
			  + ' PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED> '
			  + '</object>';

    var content = '', url11 = '', url22 = '';

	if ($url.indexOf('|') == -1) {
        content = flash;
    }else {
	    content = img;
	    urls = $url.split('|');
	    url11 = urls[0];
	    url22 = urls[1];
    }

    var temp = '<div id="$id$" style="width:$width$px; position:absolute; z-index:1000; top:$pos_y$px; left:$pos_x$px; border:1px solid #bfbfbf;">'
             + '<div style="width:$width$px; height:$height$px;">'+ content + '</div>'
			 + '<p style="height:16px; width:$width$px; line-height:16px; color:#000; margin:0; padding:0; font-size:12px;font-weight:normal;text-align:right;background:#eaeaea;">'
			 + '<a href="#" id="close1" style="color:#000; margin-right:7px; text-decoration:none;" onclick="return false">关&nbsp;&nbsp;闭</a></p></div>';

	
	var html = temp.replace(/\$width\$/gi, $width)
	               .replace(/\$height\$/gi, $height)
	               .replace(/\$pos_x\$/gi, $px)
	               .replace(/\$pos_y\$/gi, $py)
	               .replace(/\$url\$/gi, $url)
	               .replace(/\$id\$/gi, idid)
                   .replace(/\$url1\$/gi, url11)
	               .replace(/\$url2\$/gi, url22);

	document.write(html);
	return document.getElementById(idid);
    
}


function playAd(id, d_x, d_y) {
    
    var len = id.length;
	var delta = 0.1;
	var cHeight = (document.compatMode=="CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight);
    var sTop = (document.compatMode=="CSS1Compat" ? document.documentElement.scrollTop : document.body.scrollTop);
	var cWidth = document.compatMode=="CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
	for(i=0; i<len; i++) {
	    if(id[i] != null) {
            var top = parseInt(id[i].style.top);
			if (top != (cHeight + sTop - d_y)) {
	            var dy = (cHeight + sTop - d_y - top)*delta;
	            dy = (dy > 0? 1:-1)*Math.ceil(Math.abs(dy));
	            id[i].style.top = top + dy + 'px';
	        }
			if (i>0) {
				id[1].style.left = cWidth - d_x + 'px';
			}
		}        
	}

}





