jQuery.noConflict();
function QComicUtils()
{
	_if_debug = true;
	_debug_pri = {'low':1,'normal':2,'high':3};

	function debug(msg,context)
	{
		if( !_if_debug ) return;
		if(!jQuery.browser.mozilla) return;
		if(typeof(console)=='undefined') return;

		if( typeof(msg) == 'object' )
		{
			var tmp = '';
			for( idx in msg )
			{
				tmp+= idx+'='+msg[idx]+',';
			}
			msg = tmp;
		}

		console.debug('['+context+']'+msg);
	};

	function getObj(params)
	{
		var id = params['id'];
		var obj = document.getElementById(id);
		debug('id='+id+',obj='+obj,'getObj');
		return obj;
	};

	function getFlashObj(params)
	{
		var id = params['id'];
		var obj = null;
		var browser  = getBrowser();
		if( browser == 'ie' )
		{
			obj = getObj(params);
		}
		else
		{
			obj = window[id];
			if( !obj )
			{
				obj = document.getElementsByName(id)[0];
			}
		}
		debug('browser='+browser+',id='+id+',obj='+obj,'getFlashObj');
		return obj;
	};

	function getBrowser()
	{
		if (navigator.appName.indexOf('Microsoft') != -1)
			return 'ie';
		else
			return 'ff';
	};

	function getDomain()
	{
		debug('location.href='+location.href,'getDomain');

		var tmp = location.href.substr(7);
		var pos = tmp.indexOf('/');
		if( pos ) tmp = tmp.substring(0,pos);
		else tmp = tmp.substr(0);

		var tmp_arr = tmp.split('.');
		var domain = tmp_arr[1]+'.'+tmp_arr[2];
		debug('domain='+domain,'getDomain');

		return domain;
	};

	/*function getScrollTop()
	{
		var scrollTop;
		if (document.documentElement && document.documentElement.scrollTop)
		{
			scrollTop = document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			scrollTop = document.body.scrollTop;
		}
		else if (window.pageYOffset)
		{
			scrollTop = window.pageYOffset;
		}
		return scrollTop;
	}*/
	function getScrollTop()
	{
		var scrollPos; 
		if (typeof window.pageYOffset != 'undefined')
		{ 
			scrollPos = window.pageYOffset; 
		} 
		else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
		{ 
			scrollPos = document.documentElement.scrollTop; 
		} 
		else if (typeof document.body != 'undefined')
		{ 
			scrollPos = document.body.scrollTop; 
		} 
		return scrollPos;
	}

	function getMousePosition(params)
	{
		debug('[getMousePosition]params[id]='+params['id']);
		var res = (params['res']) ? params['res'] : _ids;

		var obj = getObj({'id':res[params['id']]['id']});
		debug('[getMousePosition]obj='+obj);
		debug('[getMousePosition]obj.clientWidth='+obj.clientWidth+',obj.clientHeight='+obj.clientHeight);
		debug('[getMousePosition]obj.scrollWidth='+obj.scrollWidth+',obj.scrollHeight='+obj.scrollHeight);

		var pos_x = 0;
		var pos_y = 0;
		var scroll_top = getScrollTop();
		if( window.event && params['force_center']!=1 )
		{
			/* x */
			var pos_x = window.event.clientX;
			/* y */
			var element_h = res[params['id']]['height'] ? res[params['id']]['height'] : 0;
			var pos_y = window.event.clientY + scroll_top - element_h;
		}
		else
		{
			var pos_x = Math.ceil(document.body.clientWidth / 2) - Math.ceil(res[params['id']]['width'] / 2);
			var pos_y = Math.ceil(window.screen.availHeight / 2) - Math.ceil(res[params['id']]['height'] / 2) + scroll_top - 100;
			debug('[getMousePosition]document.documentElement.scrollLeft='+document.documentElement.scrollLeft+',scroll_top='+scroll_top);
			debug('[getMousePosition]window.screen.availWidth='+window.screen.availWidth+',window.screen.availHeight='+window.screen.availHeight);
			debug('[getMousePosition]pos_x='+pos_x+',pos_y='+pos_y);
		}

		return {'pos_x':pos_x,'pos_y':pos_y};
	};

	function getElementPosition(params)
	{
		var obj = getObj({'id':_ids[params['id']]['id']});
		debug('[getElementPosition]params[id]='+params['id']+',obj='+obj);
		debug('[getElementPosition]obj.clientLeft='+obj.clientLeft+',obj.clientTop='+obj.clientTop+',obj.clientWidth='+obj.clientWidth+',obj.clientHeight='+obj.clientHeight);
		debug('[getElementPosition]obj.offsetLeft='+obj.offsetLeft+',obj.offsetTop='+obj.offsetTop);
		debug('[getElementPosition]obj.scorllLeft='+obj.scrollLeft+',obj.scrollTop='+obj.scrollTop);

		var scroll_top = getScrollTop();
		var pos_x = Math.ceil(document.body.clientWidth / 2) - Math.ceil(_ids[params['id']]['width'] / 2);
		var pos_y = scroll_top + Math.ceil(_ids[params['id']]['height'] / 2) - 100;
		debug('[getElementPosition]document.documentElement.scrollLeft='+document.documentElement.scrollLeft+',scroll_top='+scroll_top);
		debug('[getElementPosition]window.screen.availWidth='+window.screen.availWidth+',window.screen.availHeight='+window.screen.availHeight);
		debug('[getElementPosition]pos_x='+pos_x+',pos_y='+pos_y);

		return {'pos_x':pos_x,'pos_y':pos_y};
	};

	function replaceCode(code)
	{
		code = code.replace(/[+]/g,'%2B');
		code = code.replace(/[ ]/g,'+');
		return code;
	};

	function displayElement(params)
	{
		debug('[displayElement]enter');
		debug(params);

		var obj = getObj({'id':params['id']});
		if( obj )
		{
			if( params['flag'] == 1 )
			{
				obj.style.display = '';
			}
			else if( params['flag'] == 2 )
			{
				obj.style.display = 'none';
			}
		}
	};

	function copyToClip(obj)
	{
		var rng = document.body.createTextRange();
		rng.moveToElementText(obj);
		rng.scrollIntoView();
		rng.select();
		rng.execCommand('Copy');
		rng.collapse(false);
	}

	this.debug = debug;
	this.copyToClip = copyToClip;
	this.getFlashObj = getFlashObj;
	this.replaceCode = replaceCode;
	this.getScrollTop = getScrollTop;
}
var qcomic_utils = new QComicUtils();
