var adm_id = 0;
function Adm(kind, src, href, width, height, adId) { 
    this.kind    = kind;
    this.src     = src;
    this.href    = href;
    this.width   = width;
    this.height  = height;
    this.loop    = null;
    this.play    = null;
    this.adId    = adId;
    this.style   = null;
	this.boxStyle= '';
    this.extfunc = null;
    this.wmode   = "transparent";
	this.id = "adm_obj_"+adm_id++;
    this.adObjId = '';
}

Adm.adm_f="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0";
Adm.ce = document.all?1:(document.getElementById?0:-1);
Adm.contentWidth = '780px';
Adm.contentAlign = 'center';
Adm.td_left = null;
Adm.td_right = null;
Adm.counterUrl="http://adsys.sinovision.net/sinovad.php?act=redirect&id=$1";
Adm.data = {};
Adm.add = function(id, width, height, containerId, boxStyle, keyword, listAll){
    Adm.data[id]={
        width:width, 
        height:height, 
        containerId:containerId, 
        boxStyle:boxStyle, 
        keyword:keyword, 
        listAll:listAll
    };
    if (containerId && $("#"+containerId).size() == 0){
        document.write('<div id="'+containerId+'"></div>');
    }
}

Adm.requireJsFile = function(url, onLoad) 
{
	if (!url) return null;

	var doc = document, head = doc.getElementsByTagName("head")[0], n = doc.createElement("script");

	n.setAttribute('type', "text/javascript");
	n.setAttribute('src', url);	
		
	var timer = setTimeout(function(){ 
		head.removeChild(n);
	}, 10 * 1000);

	var done = false;
	n.onload = n.onreadystatechange = function(){
		if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
			done = true;
			clearTimeout(timer);
			if (typeof onLoad == 'function'){
				onLoad();
			}
			head.removeChild(n);
		}
	};

	n.onerror = function(e) {
		clearTimeout(timer);
		head.removeChild(n);
	};

	head.appendChild(n);

	return n;
};

Adm.getData = function(){
	var key, val=[], data=[];
	for (key in Adm.data){
		val = [key, Adm.data[key]['listAll'], Adm.data[key]['keyword']];
		data[data.length] = val.join(",");
	}	
	document.write('<script type="text/javascript" src="http://adsys.sinovision.net/sinovad.php?info='+encodeURIComponent(data.join("|"))+'&time='+(new Date().getTime())+'"></script>');
}

Adm.prototype.outputByKey = function(key)
{
	if (!Adm.data[key]) return;
	if (Adm.data[key]['width']) this.width   = Adm.data[key]['width'];
	if (Adm.data[key]['height']) this.height   = Adm.data[key]['height'];
	if (Adm.data[key]['boxStyle']) this.boxStyle   = Adm.data[key]['boxStyle'];
    this.output(Adm.data[key]['containerId']);
};

Adm.prototype.output = function (containerId){
	var _adm = this;
	if (this.href && this.adId){
		var counterUrl = Adm.counterUrl.replace('$1', this.adId);
        if (counterUrl.indexOf("?") == -1)counterUrl+="?";
		else counterUrl+="&";
		this.href = counterUrl+"url="+encodeURIComponent(this.href);
	}
	switch (this.kind){
	    case "left":
		    this.createLeftRightTable();
            var ret = this.adm_media();
            if (ret){
                if (typeof ret == 'string'){
                    $(Adm.td_left).append(ret);
                }
                else {
                    Adm.td_left.appendChild(ret);
                    
                    var $iframe = $('iframe', ret);
                    if ($iframe.size() > 0){
                        $iframe[0].src = $iframe[0].src;
                    }
                }
                Adm.td_left.vAlign = "top";
            }
			break;
	    case "right":
		    this.createLeftRightTable();
            var ret = this.adm_media();
            if (ret){
                if (typeof ret == 'string'){
                    $(Adm.td_right).append(ret);
                }
                else {
                    Adm.td_right.appendChild(ret);
                    
                    var $iframe = $('iframe', ret);
                    if ($iframe.size() > 0){
                        $iframe[0].src = $iframe[0].src;
                    }
                }
                Adm.td_right.vAlign = "top";
            }
			break;
	    case "float_left":
			this.myload();
			break;
	    case "popup_mc": //显示在页面中心的覆盖内容的广告
			//this.popup_mc();
            this.popup_lb();
			break;
	    case "popup_rb": //从右下角升起的广告
			this.popup_rb();
			break;
	    case "normal":
		default:	        
            var ret = this.adm_media();
            if (ret){
		        document.write('<div id="'+this.id+'_style_div" style="'+this.boxStyle+'"></div>');
                if (typeof ret == 'string'){
                    $('#'+this.id+'_style_div').append(ret);
                }
                else {
                    $('#'+this.id+'_style_div')[0].appendChild(ret);
                }
                if (containerId) {
                    $("#"+containerId)[0].appendChild($('#'+this.id+'_style_div')[0]);
                }
                    
                var $iframe = $('iframe', ret);
                if ($iframe.size() > 0){
                    $iframe[0].src = $iframe[0].src;
                }
            }
			break;
	}
}

Adm.prototype.popup_rb = function ()
{
	var obj = this.createAd();
    if (!obj) return;
	obj.style.height = this.height;
	js.Dom.setZ(obj, 10000);
	
	this.addClose(obj);

	var _adm = this, w=0, h=0, y=0, first=true, is_hide = false;
	function resize()
	{
		if (w > 0 && h > 0){
			var vp = Adm.getScroll();
			var x = vp.width + vp.left - w - 10;
			var y = vp.height + vp.top;
			if (!first) y-=h+5;
			$(obj).css({left:x, top:y});
			$(obj).show();
		
			if (first){
				$(obj).animate({top: y-h-5}, "slow");				
				jQuery(window).resize(function(){if (!is_hide) resize()});
				jQuery(window).scroll(function(){if (!is_hide) resize()});
				first = false;	
			}
		}
		else {
			w = $(obj).width();
			h = $(obj).height();
			setTimeout(function(){resize();}, 300);
		}
	}

	resize();
}

//左下
Adm.prototype.popup_lb = function ()
{
	var obj = this.createAd();
    if (!obj) return;

    var cookieVal_c1 = js.Cookie.getCookie('adm_popup_lb_c1');
    var cookieVal_c2 = js.Cookie.getCookie('adm_popup_lb_c2');
    var cookieVal_c3 = js.Cookie.getCookie('adm_popup_lb_c3');
    var cookieVal_c4 = js.Cookie.getCookie('adm_popup_lb_c4');
    var thisUrl = document.URL;
    //alert(thisUrl);

    //需要24小时只显示一次的栏目
    var c1 = thisUrl.indexOf('news');
    var c2 = thisUrl.indexOf('clients');
    var c3 = thisUrl.indexOf('classified');
    var c4 = thisUrl.indexOf('blog');
    //alert("c1:"+c1+"c2:"+c2+"c3:"+c3+"c4:"+c4);
    
    if(c1>0 ){
        if (cookieVal_c1) {
            $(obj).hide();
            return;
        }
        js.Cookie.setCookie('adm_popup_lb_c1', 1, 3600 * 24);
    }else if(c2>0 ){
        if (cookieVal_c2) {
            $(obj).hide();
            return;
        }
        js.Cookie.setCookie('adm_popup_lb_c2', 1, 3600 * 24);
    }else if(c3>0 ){
        if (cookieVal_c3) {
            $(obj).hide();
            return;
        }
        js.Cookie.setCookie('adm_popup_lb_c3', 1, 3600 * 24);
    }else if(c4>0 ){
        if (cookieVal_c4) {
            $(obj).hide();
            return;
        }
        js.Cookie.setCookie('adm_popup_lb_c4', 1, 3600 * 24);
    }
    
    
	obj.style.height = this.height;
	js.Dom.setZ(obj, 10000);
	
	this.addClose(obj);

	var _adm = this, w=0, h=0, y=0, is_hide = false;
	function resize()
	{
		if (w > 0 && h > 0){
			var vp = Adm.getScroll();
			var x = 0;
			var y = vp.height + vp.top - h- 5;
			$(obj).css({left:x, top:y}).show();
		}
		else {
			w = $(obj).width();
			h = $(obj).height();
			setTimeout(function(){resize();}, 300);
		}
	}
	resize();
	
	jQuery(window).resize(function(){if (!obj.is_hide)resize(obj)});
	jQuery(window).scroll(function(){if (!obj.is_hide)resize(obj)});
}

Adm.prototype.popup_mc = function ()
{
	var obj = this.createAd();
    if (!obj) return;
	obj.style.height = this.height;
	js.Dom.setZ(obj, 10000);
	
	this.addClose(obj);
    //$(obj).css( position : "absolute");
	
	var _adm = this;
	var w=0, h=0, _scroll;
	function resize (obj)
	{
		if (w > 0 && h > 0){
			_scroll = Adm.getScroll();
			var x = Math.floor((_scroll.width - w)/2)+_scroll.left;
			var y = Math.floor((_scroll.height - h)/2)+_scroll.top;
			if (x-_scroll.left<10)x=_scroll.left+10;
			if (y-_scroll.top<10)y=_scroll.top+10;
			
			js.Dom.setXY(obj, x, y);
		}
		else {
			w = $(obj).width();
			h = $(obj).height();
			setTimeout(function(){resize(obj);}, 300);
		}
	}	
	resize(obj);
	
	jQuery(window).resize(function(){if (!obj.is_hide)resize(obj)});
	jQuery(window).scroll(function(){if (!obj.is_hide)resize(obj)});
}

Adm.prototype.addClose = function (obj)
{
    var div=document.createElement("div");
	obj.appendChild(div);
    var text=document.createTextNode("×");
	div.appendChild(text);
	div.style.position = "absolute";
	div.style.right = "5px";
	div.style.top = "5px";
	div.style.cursor = "hand";
	div.style.cursor = "Pointer";
	div.style.fontSize = "14px";
	div.style.border = "1px solid #f5f5f5";
	div.style.backgroundColor = "#333333";
	div.style.color = "#ffffff";
	div.style.padding = "0px 5px 1px 5px";
	js.Dom.setZ(div, 10001);
	js.Event.addEvent(div, 'click', function(){js.Dom.hideObj(obj); obj.is_hide = true; });
}

Adm.prototype.myload = function ()
{
	var obj = this.createAd();
    if (!obj) return;
	obj.style.zIndex = "10";

	y = window.screen.width>800 ? 120:120
	x = window.screen.width>800 ? 15:122

	obj.style.top=document.body.scrollTop+document.body.offsetHeight-y;
	obj.style.left=x;
	function leftmove()
	{ 
		obj.style.top=document.body.scrollTop+document.body.offsetHeight-y;
		obj.style.left=x;
		setTimeout(leftmove,50)
	}
	leftmove();
}

Adm.prototype.createAd = function (){    
    var ret = this.adm_media();
    if (ret){
        document.write('<div id="'+this.id+'_style_div" style="position:absolute;width:'+this.width+'"></div>');	
    
        if (typeof ret == 'string'){
            $('#'+this.id+'_style_div').append(ret);
        }
        else {
            $('#'+this.id+'_style_div')[0].appendChild(ret);
                    
            var $iframe = $('iframe', ret);
            if ($iframe.size() > 0){
                $iframe[0].src = $iframe[0].src;
            }
        }

        return document.getElementById(this.id+'_style_div');
    }
    
    return null;
}


Adm.prototype.adm_media = function (){
    if (this.adObjId){
        var $ret = $("#"+this.adObjId);
        if ($ret.size() > 0) {
            return $ret[0];
        }
        return null;
    }
    
    var s="";
	if (this.width)var width_text = ' width="'+this.width+'" ';
	if (this.height)var height_text = ' height="'+this.height+'" ';
    if(this.src.toLowerCase().indexOf(".swf")!=-1){
		if (this.href)s+='<button style="'+(this.width?('width:'+this.width+'px;'):'')+(this.height?('height:'+this.height+'px'):'')+'background:transparent;border:0px;padding:0px;cursor:hand" onclick="window.open(\''+this.href+'\',\'\',\'\')">';
		s+='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="'+Adm.adm_f+'" id="'+this.id+'" '+ width_text + height_text + '>';
		s+='<param name="movie" value="'+this.src+'" />';
		s+='<param name="quality" value="high" />';
        if(this.play)s+='<param name="play" value="'+this.play+'">';
		if(this.wmode!="")s+='<param name="wmode" value="'+this.wmode+'" />';
        if(this.loop)s+='<param name="loop" value="'+this.loop+'">';
		s+='<embed src="'+this.src+'" '+ width_text + height_text + ' id="'+this.id+'" name="'+this.id+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" ';
		s+=' wmode="opaque" ';
		if(this.loop)s+=' loop="'+this.loop+'" ';
		s+='></embed></object>';
		if (this.href)s+='</button>';
    }
    else if(this.src.toLowerCase().indexOf(".flv")!=-1){
		s='<object id="'+this.id+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+ width_text + height_text + '>';
		s+='<param name="movie" value="/mediaplayer.swf" />';
		s+='<param name="quality" value="high" />';
		s+='<param name="wmode" value="opaque" />';
		s+='<param name="allowfullscreen" value="true" />';
		s+='<param name="allowscriptaccess" value="always" />';
		s+='<param name="flashvars" value="repeat=list&autostart=true&shuffle=false&width='+this.width+'&height='+this.height+'&file='+this.src+'&image=/templates/images/video_img.jpg&callback=/video_callback.php" />';
		s+='<embed src="/mediaplayer.swf" '+ width_text + height_text + ' id="'+this.id+'" name="'+this.id+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always"';
		s+=' wmode="opaque" ';
		s+=' flashvars="repeat=list&autostart=true&shuffle=false&width='+this.width+'&height='+this.height+'&file='+this.src+'&image=/templates/images/video_img.jpg&callback=/video_callback.php" ';
		s+='></embed></object>';
    }
    else {
        s="<img ";
        if(this.style)s+=" style='"+this.style+"'";
        if(this.extfunc)s+=" "+this.extfunc+" ";
        s+=" id='"+this.id+"' src='"+this.src+"' border='0' " + width_text + height_text + ">";
		if (this.href)s="<a href='"+this.href+"' target='_blank'>"+s+"</a>";
    }
    return s;
}

Adm.prototype.createLeftRightTable = function (){
	if (!Adm.createLeftRightTable){
		Adm.createLeftRightTable = true;
		
		document.write('<div id="adm_table_div"></div>');
		var div = Amd.findObj("adm_table_div");
		div.style.position = "absolute";
		div.style.width = "100%";
		div.style.top = "100px";
		div.style.left = "0px";
		div.style.zIndex = "-1";
		
		var table = document.createElement("table");
		table.align = "center";
		table.border = "0px";
		table.cellSpacing = "0px";
		table.cellPadding = "0px";
		div.appendChild(table);
		var tb_body = document.createElement("tbody");
		table.appendChild(tb_body);
		tb_row = document.createElement("tr");
		tb_row.align="center"; 
		tb_row.valign="top";
		tb_body.appendChild(tb_row);
		var tb_cell_l = document.createElement("td");
		tb_cell_l.width = '100px'
		tb_row.appendChild(tb_cell_l);
		Adm.td_left = tb_cell_l;
		var tb_cell_c = document.createElement("td");
		tb_cell_c.width = Adm.contentWidth
		tb_row.appendChild(tb_cell_c);
		var tb_cell_r = document.createElement("td");
		tb_cell_r.width = '100px'
		tb_row.appendChild(tb_cell_r);
		Adm.td_right = tb_cell_r;
	}
}

Adm.findObj = function (n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=Adm.findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

Adm.getScroll = function() {
	var d = document, b = d.body;
	var mode = d.compatMode;
	
	var width = self.innerWidth;  // Safari
	var height = self.innerHeight; // Safari, Opera
	if (mode || jQuery.browser.msie) { // IE, Gecko, Opera
		width = (mode == 'CSS1Compat') ? d.documentElement.clientWidth : b.clientWidth;
		if (!jQuery.browser.opera ) height = (mode == 'CSS1Compat') ? d.documentElement.clientHeight : b.clientHeight;
	}
	
	return {
		left: Math.max(d.documentElement.scrollLeft, b.scrollLeft), 
		top: Math.max(d.documentElement.scrollTop, b.scrollTop), 
		width: width, 
		height: height
	};
}

