/******************************************************

 by iNum (in-weon Hwang)  dcinside corp.

*****************************************************/


function CookieClass(){
	// 관련 변수 및 메소드 설명은 따로 설명서 파일에.
	//ClassName

		
		var addDay = {days : 0, hours : 0, minutes : 0};


		//셋팅 및 호출
		function getExpDate(days, hours, minutes) {
			var expDate = new Date( );
			if (typeof days == "number" && typeof hours == "number" && 
				typeof hours == "number") {
				expDate.setDate(expDate.getDate( ) + parseInt(days));
				expDate.setHours(expDate.getHours( ) + parseInt(hours));
				expDate.setMinutes(expDate.getMinutes( ) + parseInt(minutes));
				//return expDate.toGMTString( );
				return expDate;
			}
		}
		this.getExpDate = function(days, hours, minutes){
			return getExpDate(days,hours,minutes);
		}

		// utility function called by getCookie( )
		function getCookieVal(offset) {
			var endstr = document.cookie.indexOf (";", offset);
			if (endstr == -1) {
				endstr = document.cookie.length;
			}
			return unescape(document.cookie.substring(offset, endstr));
		}


		// primary function to retrieve cookie by name
		function getCookie(name) {
			var arg = name + "=";
			var alen = arg.length;
			var clen = document.cookie.length;
			var i = 0;
			while (i < clen) {
				var j = i + alen;
				if (document.cookie.substring(i, j) == arg) {
					return getCookieVal(j);
				}
				i = document.cookie.indexOf(" ", i) + 1;
				if (i == 0) break; 
			}
			return "";
		}
		   
		// store cookie value with optional details as needed
		function setCookie(name, value, expires, path, domain, secure) {
			document.cookie = name + "=" + escape (value) +
				((expires) ? "; expires=" + expires : "") +
				((path) ? "; path=" + path : "") +
				((domain) ? "; domain=" + domain : "") +
				((secure) ? "; secure" : "");
		}
		   
		// remove the cookie by setting ancient expiration date
		function deleteCookie(name,path,domain) {
			if (getCookie(name)) {
				document.cookie = name + "=" +
					((path) ? "; path=" + path : "") +
					((domain) ? "; domain=" + domain : "") +
					"; expires=Thu, 01-Jan-70 00:00:01 GMT";
			}
		}

		function initCookie(){
			deleteCookie("gallogId");
			deleteCookie("todayHit");
			deleteCookie("totalHit");
			deleteCookie("member");
		}



		//--------------------------

		this.getExp = function(){
			return getExpDate(addDay.days, addDay.hours, addDay.minutes);
		}

		this.setADay = function(days, hours, minutes){
			addDay.days = days;
			addDay.hours= hours;
			addDay.minutes= minutes;
		}

		//function setCookie(name, value, expires, path, domain, secure) {
		this.setCookie = function (name, value, expires, path, domain, secure){
			//setCookie(name, value, this.getExp(), null, _DOMAIN, true);
			//setCookie(name, value, this.getExp(), null, _DOMAIN, true);
			setCookie(name, value);
		}

		this.getCookie = function(name){
			return getCookie(name);
		}


		this.visitCheck = function(){
			// 주인 + 연속 : 카운트 x, 데이터갱신 x
			// 손님 + 연속 : 카운트 x , 데이터갱신 x
			// 주인 + 첫     : 카운트 x, 데이터갱신 o
			// 손님 + 첫     : 카운트 o, 데이터갱신 o

			var cF = false;
			var dF = false; 

//			alert(getCookie("MID"));
			if(G_ID == getCookie("MID")){ //G_ID  == getCookie("GID") == getCookie("MID")
				//alert("주인");
				setCookie("GID", G_ID);
				cF=true;
			}
			else if(false){ // G_ID  == getCookie("GID") && G_ID != getCookie("MID")
			
			}
			else if(false){ // ""  == getCookie("GID") && G_ID == getCookie("MID")			
				cF=true;
			}
			else if(G_ID != getCookie("GID")){
				//alert("첫방문");
				setCookie("GID", G_ID);
				cF=true;
			}else{
				//alert("방문중이시군요, 테스트를 위해 쿠키를 삭제합니다.");
				//deleteCookie("GID");
			}
			//alert(getCookie("GID"));
			
			if(cF || dF){
				var query = [];

				query[query.length] = {name:"mode", value:"SnG_STAT"};
				query[query.length] = {name:"CF", value:cF};
				query[query.length] = {name:"DF", value:dF};
				query[query.length] = {name:"gid", value:G_ID};
				
				AJCG.getAHAHData("/ajdb.php", function(){ COOM.receive_visitCheck(); }, query);
			}else{
				this.setStatus(_STATUS.today, _STATUS.total);
			}


		}

		this.receive_visitCheck = function(AHAH){
			var dObj = convertFunc(AJCG.getAHAH());
			//alert(dObj);
			if(dObj.result) {
				this.setStatus(dObj.data.today, dObj.data.total);
			}else {
//				alert("미갱신!");
				this.setStatus(_STATUS.today, _STATUS.total);
			}
		}

		this.setStatus = function(today, total){
			var t_wid = parseInt( (today / total * 100 ) / 2 );
			if( new String(today).length * 5 > t_wid) t_wid = new String(t_wid).length * 5;
			if( t_wid > 50) t_wid = 50;
			else if(t_wid < 13) t_wid=13;

			GEID('todayHitSpan').innerHTML =  today;
			GEID('totalHitSpan').innerHTML =  total;

			//alert(t_wid);
//			GEID('todayHit').style.width = t_wid + "px";
//			GEID('todayHit').innerHTML = "&nbsp;&nbsp;" + today;
//			GEID('totalHit').innerHTML = "&nbsp;&nbsp;" + total;		

//			GEID('todayHit').title = "오늘 방문자 수 : " + today;
//			GEID('totalHit').title = "총 방문자 수 : " + today;

		}


}
