// µ¿ÀÛ°¡´ÉÇÑ ºê¶ó¿ìÀú ÆÇÁ¤
//
// @sample        if(chkAjaBrowser()){ location.href='nonajax.htm' }
// @sample        oj = new chkAjaBrowser();if(oj.bw.safari){ /* Safari ÄÚµå */ }
// @return        ¶óÀÌºê·¯¸®°¡ µ¿ÀÛ°¡´ÉÇÑ ºê¶ó¿ìÀú¸¸ true  true|false
//

function chkAjaBrowser() {
	var a, ua = navigator.userAgent;
	this.bw = {
	  safari    : ((a=ua.split('AppleWebKit/')[1])?a.split('(')[0]:0)>=124 ,
	  konqueror : ((a=ua.split('Konqueror/')[1])?a.split(';')[0]:0)>=3.3 ,
	  mozes     : ((a=ua.split('Gecko/')[1])?a.split(" ")[0]:0) >= 20011128 ,
	  opera     : (!!window.opera) && ((typeof XMLHttpRequest)=='function') ,
	  msie      : (!!window.ActiveXObject)?(!!createHttpRequest()):false
	}
	return (this.bw.safari||this.bw.konqueror||this.bw.mozes||this.bw.opera||this.bw.msie)
}

////
// ¼Û¼ö½Å ÇÔ¼ö
//
// @sample         	sendRequest('./about2.php', onloaded, '&prog=1')
// @param url      	¿äÃ»ÇÏ´Â ÆÄÀÏÀÇ URL
// @param callback 	¼Û¼ö½Å½Ã¿¡ ±âµ¿ÇÏ´Â ÇÔ¼ö ÀÌ¸§
// @param postparam	POST È£Ãâ½Ã parameter
//
function sendRequest(url, callback, postparam) {
	var oj = createHttpRequest();
	var b_is_dummy_param = false;
	if (b_is_dummy_param) {
		//-- cache¹æÁö¸¦ À§ÇÑ dummy parameterÃß°¡
		url += ((url.indexOf('?') == -1) ? '?' : '&') + (new Date().getTime());
	}
	oj.open(postparam ? 'POST' : 'GET', url, true);
	if (callback) { oj.onreadystatechange = function() { if ((oj.readyState == 4) && (oj.status >= 200 && oj.status < 300)) { callback(oj.responseText, oj, callback); } } }
	oj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//-- oj.setRequestHeader("Accept-Language","ko");
	try {
		oj.send(postparam);
	} catch(e) {}
}

////
// XMLHttpRequest ¿ÀºêÁ§Æ® »ý¼º
//
// @sample        oj = createHttpRequest()
// @return        XMLHttpRequest ¿ÀºêÁ§Æ®(ÀÎ½ºÅÏ½º)
//
function createHttpRequest() {
	var aHttp = null;
	if (window.ActiveXObject) {
		aHttp = new ActiveXObject("Msxml2.XMLHTTP");
		if (!aHttp) {
			aHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else if (window.XMLHttpRequest) {
		aHttp = new XMLHttpRequest();
	}
	return aHttp;
}

function ChangeLayer() {
	//-- cache À¯Çü
	this.enum_cache_style = {
		"none" : 0,
		"html" : 1,
		"element" : 2
	};
	//-- move À¯Çü
	this.enum_move_style = {
		"link" : 0,
		"window" : 1,
		"script" : 2
	};

	this.stop = true;
	this.item_count = 0;					// ¾ÆÀÌÅÛ°¹¼ö
	this.item_data = [];					// ¾ÆÀÌÅÛÁ¤º¸ [Layer±×·ì, Layer¹øÈ£, Data URL, Move URL]
	this.item_cache = [];
	this.item_img = [];
	this.last_idx = NaN;					// ÃÖÁ¾idx
	this.hd_display_layer = 0;		// µ¿ÀÛ timer handle

	//-- required user setting
	this.instance_name = '';			// instance¸í
	this.layerid = '';						// °Ô½Ã·¹ÀÌ¾îid
	this.layer_obj = null;				// °Ô½Ã·¹ÀÌ¾î

	//-- optional user setting
	this.start_idx = -1;					// ÃÊ±âidx
	this.currentspeed = 0;				// µ¿ÀÛ interval
	this.cache_style = this.enum_cache_style.element;
	this.move_style = this.enum_move_style.link;
	this.element_className = '';
	this.on_display_layer;				// display_layer() ÀÌÈÄ »ç¿ëÀÚÁöÁ¤ ÇÔ¼ö

	//-- user using method {
	this.set_item_data = function(item_data) {
		this.item_data = item_data;
		this.item_count = this.item_data.length;
	}
	this.get_random_idx = function () {
		return (Math.floor(Math.random() * this.item_count));
	}
	this.start = function() {
		if (this.item_count > 0) {
			this.stop = false;
			this.display_layer(this.start_idx);
		}
	}
	this.halt = function() {
		if (this.hd_display_layer > 0) {
			window.clearTimeout(this.hd_display_layer);
		}
	}
	this.display = function(p_idx) {
		this.halt();
		if (this.stop) { return; }
		if (!this.is_valid_idx(p_idx)) { return; }

		if (p_idx == this.last_idx) { return; }
		this.hd_display_layer = window.setTimeout(this.instance_name+'.display_layer(' + p_idx + ')', this.currentspeed);
	}
	this.move = function(p_idx) {
		if (!this.is_valid_idx(p_idx)) { return; }
		if (this.move_style == this.enum_move_style.link) {
				location.href = this.item_data[p_idx][3];
		}
		else if (this.move_style == this.enum_move_style.script) {
			try {
				eval(this.item_data[p_idx][3]);
			} catch(e) {}
		}
		else {
			window.open(this.item_data[p_idx][3]);
		}
	}
	this.next = function() {
		var idx = (this.last_idx + 1);
		idx = ((idx > (this.item_count - 1)) ? 0 : idx);
		this.display_layer(idx);
	}
	this.prev = function() {
		var idx = (this.last_idx - 1);
		idx = ((idx < 0) ? (this.item_count - 1) : idx);
		this.display_layer(idx);
	}
	//-- user using method }

	this.is_valid_idx = function(p_idx) {
		var idx = parseInt(p_idx);
		return ((idx >= 0) && (idx < this.item_count));
	}
	this.is_cached_idx = function(p_idx) {
		var b_using_cached = ((this.cache_style == this.enum_cache_style.html) || (this.cache_style == this.enum_cache_style.element));
		return ((b_using_cached && (typeof(this.item_cache[p_idx]) != 'undefined')) ? true : false);
	}

	this.display_layer = function(p_idx) {
		this.halt();
		if (this.stop) { return; }
		if (!this.is_valid_idx(p_idx)) { return; }

		//-- °³º°È­Ã³¸®
		if (typeof(this.on_display_layer) == 'function') { this.on_display_layer(p_idx, this); }

		/*--// °³º°È­Ã³¸® sample
		instance.on_display_layer = function (p_idx, obj) {
			var i, img_src, obj_img, b_is_new;
			//-- tab image Ã³¸®
			if (!obj.custom_item_img) {
				obj.custom_item_img = [];
				for (i = 0; i < obj.item_count; i++) { obj.custom_item_img[i] = document.getElementById(obj.item_data[i][0] + '_tab_' + obj.item_data[i][1]); }
			}
			for (i = 0; i < obj.item_count; i++) {
				obj_img = obj.custom_item_img[i];
				img_src = '/img/mainimg/' + obj.item_data[i][0] + ((p_idx == i) ? '_on_' : '_off_') + obj.item_data[i][1] + '.gif';
				b_is_new = (obj_img.src.toLowerCase().indexOf(img_src.toLowerCase()) == -1);
				if (b_is_new) { obj_img.src = img_src; }
			}
		}
		//--*/

		//-- ÇØ´ç ÄÁÅÙÃ÷ Ãâ·Â
		this.display_content(p_idx);
	}
	this.display_content = function(p_idx) {
		if (this.is_cached_idx(p_idx)) {
			this.write_content(p_idx);
			return;
		}
		var callback_get_content = function(p_context, p_oj, p_self_obj) {
			var parent_obj = p_self_obj.parent_obj;
			parent_obj.write_content(p_self_obj.req_idx, p_context);
		}
		callback_get_content.parent_obj = this;
		callback_get_content.req_idx = p_idx;

		var postparam = null;
		sendRequest(this.item_data[p_idx][2], callback_get_content, postparam);
	}
	this.write_cached_content = function (p_idx) {
		if (!this.is_cached_idx(p_idx)) { return; }
		if (this.cache_style == this.enum_cache_style.element) {
			if (true) {
				//-- last_idx¿¡ ÀÇÁ¸ÇÑ display
				if (this.is_cached_idx(this.last_idx)) { this.item_cache[this.last_idx].style.display = 'none'; }
				this.item_cache[p_idx].style.display = 'block';
			}
			else {
				for (var i = 0; i < this.item_count; i++) {
					if (this.is_cached_idx(i)) {
						this.item_cache[i].style.display = ((i == p_idx) ? 'block' : 'none');
					}
				}
			}
		}
		else if (this.cache_style == this.enum_cache_style.html) {
			this.layer_obj.innerHTML = this.item_cache[p_idx];
		}
		this.last_idx = p_idx;
	}
	this.write_content = function (p_idx, p_context) {
		if (!this.layer_obj) {
			this.layer_obj = document.getElementById(this.layerid);
			if (!this.layer_obj) { return; }
		}
		if (arguments.length == 2) {
			if (this.cache_style == this.enum_cache_style.element) {
				if (!this.is_cached_idx(p_idx)) {
					var content_layer_obj;
					content_layer_obj = document.createElement('div');
					content_layer_obj.setAttribute('id', this.layerid + '_' + p_idx);
					content_layer_obj.className = this.element_className;
					content_layer_obj.style.display = 'none';
					content_layer_obj.innerHTML = p_context;
					this.layer_obj.appendChild(content_layer_obj)
					this.item_cache[p_idx] = content_layer_obj;
					//--[drm:test] this.debug_title('[cached element:' + p_idx + ']')
				}
				this.write_cached_content(p_idx);
			}
			else if (this.cache_style == this.enum_cache_style.html) {
				if (!this.is_cached_idx(p_idx)) {
					this.item_cache[p_idx] = this.wrap_content(p_context);
					//--[drm:test] this.debug_title('[cached html:' + p_idx + ']')
				}
				this.write_cached_content(p_idx);
			}
			else {
				this.layer_obj.innerHTML = this.wrap_content(p_context);
				//--[drm:test] this.debug_title('[uncached write:' + p_idx + ']')
			}
		}
		else {
			this.write_cached_content(p_idx);
			//--[drm:test] this.debug_title('[cached write:' + p_idx + ']')
		}
		this.last_idx = p_idx;
	}
	this.wrap_content = function (p_context) {
		var pre_tag = '<div class="' + this.element_className + '">';
		var sur_tag = '</div>';
		return (pre_tag + p_context + sur_tag);
	}

	this.debug_title = function (p_msg) {
		document.title = p_msg;
	}
	this.get_prop_string = function (p_obj) {
		var s_ret = '';
		var s_val;
		for (var prop in p_obj) {
			if (typeof(prop) == 'function') { s_val = 'function()'; }
			else { s_val = eval('p_obj.' + prop); }
			s_ret += '[' + prop + ':' + s_val + ']\n';
		}
		return s_ret;
	}
}
