oop=\"true\" quality=\"high\" " +
                    "pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" " +
                    "width=\"" + width + "\" height=\"" + height + "\"></embed>\n" +
                    "</object>";
          } else {
            elem.innerHTML = "<a href=\"" + resp[0][2] + "\" target=\"_blank\"><img src=\"" + resp[0][1] + "\" " +
                           "width=\"" + width + "\" height=\"" + height + "\" border=\"0\"/></a>";
          }
        
          // 출력된 광고는 ads 배열에 추가
          this.ads.push(resp[0][0]);
        }
			}
		}
		
		this.wild_ad_index ++;
		
		this.on_load();
	},
	
  onreadystatechange: function() {
			var ready = this.req.readyState;
			if (ready == 4) {		// 4 => READY_STATE_COMPLETE
			  this.update(this.req);
			}
	}
};

add_onload(WildAd.on_load.bindAsEventListener(WildAd));

function write_ad(domain, width, height) {
  // 동기적으로 XHR를 통해 ads 배열에 포함되지 않는 광고 질의 
	var req = get_xml_http_request();
	if (!req) return;
	
	req.open("POST", "/wild_ad/get_ad.php", false);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send("domain=" + domain + "&excepts=" + ads.join(","));

	if (req.status != 200)
	{
		document.write("request error<br />" + req.responseText);
	}
	else
	{
		var resp;
		try
		{
			resp = eval(req.responseText);
		}
		catch(e)
		{
			document.write("can't parse response<br />" + e);
			resp = false
		}
		
		if (resp)
		{		
			if (resp[0][0] > 0)
			{
				// 파일확장명을 고려해 마컵 document.write
				if (is_flash(resp[0][1]))
				{
					document.write("<object width=\"" + width + "\" height=\"" + height + "\">"
					+ "<param name=\"movie\" value=\"" + resp[0][1] + "\"></param>"
					+ "<param name=\"allowscriptaccess\" value=\"always\"></param>"
					+ "<embed src=\""+ resp[0][1] + "\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" "+ "width=\"" + width + "\" height=\"" + height + "\"></embed></object>");

				}
				else
				{
					document.write("<a href=\"" + resp[0][2] + "\" target=\"_blank\"><img src=\"" + resp[0][1] + "\" " +
												 "width=\"" + width + "\" height=\"" + height + "\" border=\"0\"/></a>");
				}
				
				// 출력된 광고는 ads 배열에 추가
				this.ads.push(resp[0][0]);	
			}
		}
	}		
}

function is_flash(filename)
{
	return (filename.toLowerCase().lastIndexOf(".swf") > 0);
}

function get_xml_http_request()
{
	var req = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest && !(window.ActiveXObject)) 
	{
		try 
		{ 
			req = new XMLHttpRequest();
		} 
		catch(e) 
		{
			req = false;
		}
	// branch for IE/Windows ActiveX version
	} 
	else if(window.ActiveXObject) 
	{
		try 
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e) 
		{
			try 
			{
					req = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e) 
			{
					req = false;
			}
		}
	}
	
	return req;
}

var ads = [];
var wild_ad_elems = [];

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

Function.prototype.bindAsEventListener = function(object) {
  var __method = this;
  return function(event) {
    return __method.call(object, event || window.event);
  }
}

function add_onload(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

var WildAd = {
  wild_ad_index: 0,
  wild_ad_elems: [],
  ads: [],

	insert_ad: function(domain_id, elem_id, width, height) {
	  this.wild_ad_elems.push({
	    domain: domain_id,
	    elem_id: elem_id,
	    width: width,
	    height: height
	  });
	},
	
	on_load: function() {
	  if (this.wild_ad_index >= this.wild_ad_elems.length) {
	    return;
	  }
	  
		this.req = get_xml_http_request();
		if (this.req) {		
      this.req.open("POST", "/wild_ad/get_ad.php", true);
      this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      this.req.onreadystatechange = this.onreadystatechange.bindAsEventListener(this);
      this.req.send("domain=" + this.wild_ad_elems[this.wild_ad_index].domain +
               "&excepts=" + this.ads.join(",") + 
               "&count=1");
    }
	},
	
	update: function(req)	{
	  var ad = this.wild_ad_elems[this.wild_ad_index];
		var elem = document.getElementById(ad.elem_id);
		if (!elem) {
		  this.wild_ad_index ++;
			return;
		}
		
		if (req.status != 200) {
			elem.innerHTML = "request error<br />" + req.responseText;
		} else {
			try {
				resp = eval(req.responseText);
			} catch(e) {
				elem.innerHTML = "can't parse response<br />" + e;
				this.wild_ad_index ++;
				return;
			}
			
			if (resp && resp[0]) {		
        if (resp[0][0] > 0) {
          var width = ad.width;
          var height = ad.height;
          
          // 파일확장명을 고려해 마컵 document.write
          if (is_flash(resp[0][1])) {
            elem.innerHTML = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" " +
                    "codebase=\"http://active.macromedia.com/flash4/cabs/swflash.cab#version=4,0,0,0\" " +
                    "width=\"" + width + "\" height=\"" + height + "\">\n" +
                    "<param name=\"movie\" value=\"" + resp[0][1] + "\">\n" +
                    "<param name=\"play\" value=\"true\">" +
                    "<param name=\"loop\" value=\"true\">" +
                    "<param name=\"quality\" value=\"high\">" +
                    "<embed src=\"" + resp[0][1] + "\" play=\"true\" loop=\"true\" quality=\"high\" " +
                    "pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" " +
                    "width=\"" + width + "\" height=\"" + height + "\"></embed>\n" +
                    "</object>";
          } else {
            elem.innerHTML = "<a href=\"" + resp[0][2] + "\" target=\"_blank\"><img src=\"" + resp[0][1] + "\" " +
                           "width=\"" + width + "\" height=\"" + height + "\" border=\"0\"/></a>";
          }
        
          // 출력된 광고는 ads 배열에 추가
          this.ads.push(resp[0][0]);
        }
			}
		}
		
		this.wild_ad_index ++;
		
		this.on_load();
	},
	
  onreadystatechange: function() {
			var ready = this.req.readyState;
			if (ready == 4) {		// 4 => READY_STATE_COMPLETE
			  this.update(this.req);
			}
	}
};

add_onload(WildAd.on_load.bindAsEventListener(WildAd));

function write_ad(domain, width, height) {
  // 동기적으로 XHR를 통해 ads 배열에 포함되지 않는 광고 질의 
	var req = get_xml_http_request();
	if (!req) return;
	
	req.open("POST", "/wild_ad/get_ad.php", false);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send("domain=" + domain + "&excepts=" + ads.join(","));

	if (req.status != 200)
	{
		document.write("request error<br />" + req.responseText);
	}
	else
	{
		var resp;
		try
		{
			resp = eval(req.responseText);
		}
		catch(e)
		{
			document.write("can't parse response<br />" + e);
			resp = false
		}
		
		if (resp)
		{		
			if (resp[0][0] > 0)
			{
				// 파일확장명을 고려해 마컵 document.write
				if (is_flash(resp[0][1]))
				{
					document.write("<object width=\"" + width + "\" height=\"" + height + "\">"
					+ "<param name=\"movie\" value=\"" + resp[0][1] + "\"></param>"
					+ "<param name=\"allowscriptaccess\" value=\"always\"></param>"
					+ "<embed src=\""+ resp[0][1] + "\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" "+ "width=\"" + width + "\" height=\"" + height + "\"></embed></object>");

				}
				else
				{
					document.write("<a href=\"" + resp[0][2] + "\" target=\"_blank\"><img src=\"" + resp[0][1] + "\" " +
												 "width=\"" + width + "\" height=\"" + height + "\" border=\"0\"/></a>");
				}
				
				// 출력된 광고는 ads 배열에 추가
				this.ads.push(resp[0][0]);	
			}
		}
	}		
}

function is_flash(filename)
{
	return (filename.toLowerCase().lastIndexOf(".swf") > 0);
}

function get_xml_http_request()
{
	var req = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest && !(window.ActiveXObject)) 
	{
		try 
		{ 
			req = new XMLHttpRequest();
		} 
		catch(e) 
		{
			req = false;
		}
	// branch for IE/Windows ActiveX version
	} 
	else if(window.ActiveXObject) 
	{
		try 
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e) 
		{
			try 
			{
					req = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e) 
			{
					req = false;
			}
		}
	}
	
	return req;
}


