var _ih_version = '3.0.0';
var _ih_modified = '20061124';
var _ih_sidx = '';
var _ih_ua   = '1';
var _ih_key  = '193c062c14de6d4ac923a0d522e1878c';
var _ih_frame= '';

_ih_org_error_handler = window.onerror;

function _ih_urlescape( _str ) {
    var str = escape(_str);
    var ch;
    while((ch=str.indexOf("+")) > 0) str = str.substr(0, ch) + "%2B" + str.substr(ch+1, str.length);
    while((ch=str.indexOf("/")) > 0) str = str.substr(0, ch) + "%2F" + str.substr(ch+1, str.length);
    while((ch=str.indexOf("&")) > 0) str = str.substr(0, ch) + "%26" + str.substr(ch+1, str.length);
    while((ch=str.indexOf("?")) > 0) str = str.substr(0, ch) + "%3F" + str.substr(ch+1, str.length);
    return str;
}

function _ih_setcookie(name,value){
	var argc = _ih_setcookie.arguments.length;
	var argv = _ih_setcookie.arguments;

	var expires = new Date();
	var expiresmin = ( argc > 2) ? argv[2]:null;
	var path = ( argc > 3) ? argv[3]:null;

	if(expiresmin != null){
		var now = new Date();
		expires.setTime(now.getTime() + expiresmin * 60 * 1000);
	}

	//alert(expires.toGMTString());

	document.cookie = name + "=" + _ih_urlescape(value) +
		((expiresmin == null) ? "" : ("; expires =" + expires.toGMTString())) +
		((path == null) ? "" : ("; path =" + path));
}


function _ih_getcookie (name) {
	var dcookie = document.cookie;
	dcookie =dcookie.replace(/%5F/g,"_");
	var cname = name + "=";
	var clen = dcookie.length;
	var cbegin = 0;
	while (cbegin < clen) {
		var vbegin = cbegin + cname.length;
			if (dcookie.substring(cbegin, vbegin) == cname) {
				var vend = dcookie.indexOf (";", vbegin);
				if (vend == -1) vend = clen;
			return unescape(dcookie.substring(vbegin, vend));
		}
		cbegin = dcookie.indexOf(" ", cbegin) + 1;
		if (cbegin == 0) break;
	}
	return "";
}

function _ih_user(){
	if(typeof(_ih_ua) != "undefined" && typeof(_ih_ua) != "unknown" && _ih_ua == 1){
		var usr	= _ih_getcookie("as_iu");
		var sex	= _ih_getcookie("as_is");
		var age	= _ih_getcookie("as_ia");
		return 	'&usr='+usr+'&sex='+sex+'&age='+age;
	} else {
		return '';
	}
}

function _ih_get_writer(url,referer,rnd){
	url		= _ih_urlescape(url);
	referer = _ih_urlescape(referer);
	ss		= escape(screen.width + 'x' + screen.height);
	usr		= _ih_user();

	//debug
	var host = 'http://log' + _ih_sidx + '.wsos.co.kr';
	//var host = 'http://danalysis.wsos.co.kr';

	var writer = '';
	writer = host + '/analysis/writer.php?rnd='+ Math.random()
		+'&k=' + _ih_key + '&s=' + _ih_sidx + '&url=' + url + '&referer=' + referer + '&ss=' + ss + usr;
	return writer;
}

function _ih_writer(){

	window.onerror = window._ih_org_error_handler;

	var url = '';
	var referer = '';
	var isframe = false;
	var doc = '';
	var session = '';
	var sessionreferer = '';
	
	session = _ih_getcookie("__w_ses_");
	sessionreferer = _ih_getcookie("__w_rfr_");

	url = self.document.location.href;
	referer = self.document.referrer;

	if( ( typeof(top.document) != "unknown" && typeof(top.document) != "undefined" ) && referer == top.document.location.href) {
		if(session == "y" && sessionreferer != ""){
			referer = sessionreferer;
		} else if(top.document.referrer != ""){
			isframe = true;
			referer = top.document.referrer;
			doc = top.document;
		}
	} else if( ( typeof(parent.document) != "unknown" && typeof(parent.document) != "undefined" ) && referer == parent.document.location.href) {
		if(session == "y" && sessionreferer != ""){
			referer = sessionreferer;
		} else if(parent.document.referrer != ""){
			isframe = true;
			referer = parent.document.referrer;
			doc = parent.document;
		}
	} else {
		if(referer == "" && session == "y" && sessionreferer != ""){
			referer = sessionreferer;
		}
	}

	if(isframe){
		if(referer != "" && (typeof(_ih_frame) == "undefined" || typeof(_ih_frame) == "unknown" || _ih_frame != "Y")){
			var scripts;
			var frame = 0;
			if (doc.all || doc.getElementById){
				if (doc.all)
					scripts = doc.all.tags("script")
				else if (doc.getElementById)
					scripts = doc.getElementsByTagName("script")
				for (i=0;i< scripts.length;i++){
					if((pos = scripts[i].src.indexOf("analysis.php")) > 0 || (pos = scripts[i].src.indexOf("analysis.js")) > 0){
						frame = 1; break;
					}
				}
			}
			if(frame == 0) url = doc.location.href;
		}		
	}

	if(session != "y"){
		_ih_setcookie("__w_ses_","y",null,"/");
	}
	_ih_setcookie("__w_rfr_",url,30,"/");

	if( referer=="undefined" || referer=="unknown" ) referer='';
	var writer = _ih_get_writer(url,referer,true);

	if(_ih_bjs) {
		if(_ih_bmsie) {
			_ih_track_image.src=writer;
		} else {
			document.write('<i' + 'mg height="0" width="0" border="0" ' + 'src="' + writer + '"/>');
			//setTimeout("_ih_track_image.src=writer;",1);
		}
	} else {
		document.write('<i' + 'mg height="0" width="0" border="0" ' + 'src="' + writer + '"/>');
	}

	//_ih_adprint();
}

function _ih_adprint(){
	document.writeln('<d' + 'iv id="_ih_ad_" height="0" width="0"></div>');
	setTimeout("_ih_admsg();",1000);
}

function _ih_admsg(){
	if(_ih_bjs && _ih_track_image.width == '1'){
		var d = document.getElementById("_ih_ad_");
		if(d){
			sHtml  = '<a href="http://www.wsos.co.kr">';
			sHtml += '<span style="font-size:9px;font-family:Arial;color:blue;TEXT-DECORATION: none">';
			sHtml += 'Powered By WSOS</span></a>';
			d.innerHTML = sHtml;
		}
	}
}

function _ih_click(url){
	var referer = self.document.location.href;
	var writer = _ih_get_writer(url,referer,true);
	_ih_track_image.src=writer;
}

function _ih_error_handler(message,url,line){
	_ih_writer();
	return true;
}

function _ih_monitor_get(url,referer,load){

	url		= _ih_urlescape(url);
	referer = _ih_urlescape(referer);

	//debug
	var host = 'http://log' + _ih_sidx + '.wsos.co.kr';
	//var host = 'http://danalysis.wsos.co.kr';

	var writer = '';
	writer = host + '/analysis/monitor.php?rnd='+ Math.random()
		+'&k=' + _ih_key + '&s=' + _ih_sidx + '&url=' + url + '&referer=' + referer + '&l=';
	return writer;
}

function _ih_monitor(load){

	var url = '';
	var referer = '';
	var doc = '';

	url = self.document.location.href;
	referer = self.document.referrer;

	if( referer=="undefined" || referer=="unknown" ) referer='';
	_ih_monitor_writer = _ih_monitor_get(url,referer,load);
}

function _ih_monitor_access(load){

	var writer = _ih_monitor_writer + load;
	if(_ih_bjs) {
		if(_ih_bmsie) {
			_ih_monitor_image.src=writer;
		} else {
			_ih_monitor_image.src=writer;
		}
	}
}

function _ih_monitor_ping(){
	_ih_monitor_access('p');
	setTimeout("_ih_monitor_ping()",1000*60);
}

function _ih_monitor_onload(){
	_ih_monitor_load = true;
	_ih_monitor();
	//_ih_monitor_access('l');
	setTimeout("_ih_monitor_ping()",1000*60);
}

function _ih_monitor_onunload(){
	if(_ih_monitor_load){
		_ih_monitor_access('u');
		_ih_monitor_load = false;
	}
}

window.onerror = _ih_error_handler;

var _ih_bmsie = (document.all)?true:false;
var _ih_bjs	= (window.screen)?true:false;
var _ih_track_image = new Image();

_ih_writer();

