/*edit by wangsy*/
jQuery.extend({
	cnbb_loadJSON:function(url,cn,callback){
		var s = document.createElement("script");
		s.type = "text/javascript";
		s.src = url;
		window[cn] = callback;
		document.getElementsByTagName("head")[0].appendChild(s);
	}
	
});

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};



function cnbb_showad(data){

	/*
	当没有数据的时候，直接返回空
	*/
	if(data.datalist.length<=0){
		return;
	}
	
	/*
	读取该广告位的显示id,如果无ID,则为0
	*/
	var _showid=jQuery.cookie(data.todiv);
	if(_showid == null || _showid == undefined || (!_showid)){
		_showid=0;	
	}
	/*
	当该广告位的显示id大于数据读取过来的总长度，则显示第一个广告
	*/
	if(_showid>=data.datalist.length){
		_showid=0;	
	}
	
	//得到这个广告位的数据
	var item=(data.datalist)[_showid];
	
	//
	if(item.filetype=='img'){
		var o=jQuery("#"+data.todiv);
		o.html("<a href='"+item.url+"' target='_blank'><img src='"+item.src+"' border=0 width='"+item.width+"' height='"+item.height+"'/></a>");
	}else if(item.filetype=='swf'){
		jQuery("#"+data.todiv).html("<EMBED src='"+item.src+"' width='"+item.width+"' height='"+item.height+"'>	");
	}
	
	//将新的showid写入COOKIE中
	_showid++;
	jQuery.cookie(data.todiv, _showid,{expires: 1000});

	return;
}
