/* 마우스 클릭 위치 기반 좌표 분석 */
/*
	typeof(window.innerWidth) : 비 IE ( number ) , IE ( undefined )
*/

var _c_http_host = "www.buxmovie.com";
var _c_align_type = "center";
var _c_host=document.location.protocol.indexOf("https")!=-1?"https://"+_c_http_host:"http://"+_c_http_host;
var _c_img = new Image();

var Init_Time = new Date().getTime();
createCookie("_c_time",Init_Time,"");

function createCookie(name,value,days) 
{
		if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) 
{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
}

function eraseCookie(name) 
{
		createCookie(name,"",-1);
}

function Rand_keyCreate()
{
		var _Rand_Array = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"); 
		var _key_rand1 = parseInt(Math.random()*25);
		var _key_rand2 = parseInt(Math.random()*25);
		var _key_rand3 = parseInt(Math.random()*25);

		var Today = new Date(); 
		var T_time = Today.getTime();
		T_time = Math.round(T_time / (1000)); // 현재시간 초로환산

		var Rand_Key = _Rand_Array[_key_rand1]+_Rand_Array[_key_rand2]+_Rand_Array[_key_rand3]+T_time;	
		return Rand_Key;
}

function MouseClick(e)
{
	var ax,ay,rx,px,window_width;
	var _scrOfX=0, _scrOfY=0;

	if(typeof(debug) == "undefined" ) debug = "N";
	if(!e) e=window.event;

	if(typeof(window.innerWidth) == "number") window_width =window.innerWidth;
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) window_width=document.documentElement.clientWidth;
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) window_width=document.body.clientWidth;
	

	if(document.body && (document.body.scrollLeft || document.body.scrollTop)) 
	{
			_scrOfY = document.body.scrollTop;
			_scrOfX = document.body.scrollLeft;
	} 
	else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
			_scrOfY = document.documentElement.scrollTop;
			_scrOfX = document.documentElement.scrollLeft;
	}

	// 클릭 좌표 ( 홈페이지가 center 정렬과 left 정렬일때 틀림 , 상대와 절대)
	if(_c_align_type == "left")
	{
			ax=e.clientX+ _scrOfX;
	}
	else if(_c_align_type == "center")
	{
			ax=e.clientX+ _scrOfX;
			ax=Math.round(ax-window_width/2);
	}
	ay=e.clientY+ _scrOfY;

	//px=Math.round(ax/window_width*100);
	//var now_time = new Date().getTime();


	// 쿠키에 기록
	var _c_uid = readCookie("_c_uid");
	if(_c_uid == "undefined" || _c_uid == null) 
	{
		_c_uid = Rand_keyCreate();
		createCookie("_c_uid",_c_uid,"");
		//createCookie("_c_order","1","");
	}

	// 시간차 계산
	//var _c_time = readCookie("_c_time");
	//diff_time = parseInt(now_time) - parseInt(_c_time);
	//createCookie("_c_time",new Date().getTime(),"");

	// 순서
	//var _c_order = readCookie("_c_order");

	// debug
	if(debug == "Y") alert("sc:"+_scrOfY+"K:"+_c_uid+"X: "+ax + " Y: "+ay + " window_width: "+window_width + " rx:" + rx + " px:"+px+" order: "+_c_order+" diff_time: "+Math.round(diff_time/1000));

	// send info to server
	//_c_img.src = _c_host+"/coordinate/coordinate.php?c_key="+_c_uid+"&c_x="+ax+"&c_y="+ay+"&s_width="+window_width+"&c_order="+_c_order+"&diff_time="+Math.round(diff_time/1000);
	_c_img.src = _c_host+"/coordinate.php?c_key="+_c_uid+"&c_x="+ax+"&c_y="+ay+"&s_width="+window_width;

	// 순서 +1
	//_c_order = parseInt(_c_order) + 1;
	//createCookie("_c_order",_c_order,"");	

	//coupon_body.location.href = _c_img.src;
}


document.onmousedown=MouseClick;
