/**
 * Copyright 2007 SK Communications. All rights reserved
 * @since 2007.03.26
 * @author okjungsoo
 * @requires prototype.js, Cookie.js
 * 
 * Copyright 구문을 출력해주는 클래스입니다. Copyright 마지막에 
 * http://stategloos.egloos.com/stat/stat.tiff과 뒤의 파라미터를 사용하여 어느페이지가
 * 어느정도 호출되었는지를 측정하는데 사용됩니다. 
 */

var Copyright = {
	copyrightTemplate: new Template(
		"	<div id=\"footer\">" +
		"	Copyrightⓒ <a href=\"http://corp.nate.com/\" target=\"_blank\">SK Communications.</a> All rights reserved.  &nbsp; " +
		"	  <a href=\"http://corp.nate.com/\" target=\"_blank\">회사소개</a>" +
		"	 | <a href=\"http://www.egloos.com/rules/provision.php\">이용약관</a>" +
		"	 | <a href=\"http://www.egloos.com/rules/privacy.php\"><strong>개인정보취급방침</strong></a>" +
		"	 | <a href=\"#\" onclick=\"window.open('http://www.egloos.com/emailreject.php','','width=400, height=270');\">이메일 수집거부</a>" +
		"	 | <a href=\"http://www.egloos.com/support.php\">고객센터</a>" +
		"	</div>" +
		"	#{statImage}"
	),
	
	statImgTemplate: new Template(
		"<img " +
			"src=\"http://stategloos.egloos.com/stat/stat.tiff?cp_url=[#{spchost}_ndr.egloos.com/#{spcuri}]\" " +
		"width=\"0\" height=\"0\" border=\"0\">"
	),
	
	
	initialize: function() {
		Event.observe(window, 'load', function(){
			var footerDiv = $('footer');
			if(footerDiv) {
				this.appendStat(footerDiv);
			}else {
				var bodyTag = document.getElementsByTagName("body")[0];
				
				var divElm = $(document.createElement("div"));
				divElm.update(this.getStatTag());
				
				bodyTag.appendChild(divElm);
			}
		}.bindAsEventListener(this));
	},
	
	
	/**
	 * 현재 페이지의 위치에 따라서, Copyright을 출력해 줍니다. 
	 * @deprecated /template/home_header.php를 사용하시기 바랍니다. 
	 * @param isTemplateOnly Copyright.js를 import한 것만으로 stat 이미지가 생성이 됩니다. 
	 * 			필요에 의해서 StatTag를 추가하시기를 원한다면 인자를 넘겨주세요. 
	 */
	print: function(isTemplateOnly) {
		if(typeof(isTemplateOnly) == 'undefined') {
			isTemplateOnly = true;
		}
		var statTag = (isTemplateOnly? "": this.getStatTag());
		var html = this.copyrightTemplate.evaluate({
			statImage: statTag
		});
	    document.writeln(html);
	}, 
	
	appendStat: function(footerDiv) {
		new Insertion.After(footerDiv, this.getStatTag());
	},

	/**
	 * 현재 페이지의 위치에 따라서, Copyright을 출력해 줍니다. 
	 */
	getStatTag: function() {
	    if(typeof(document.location.href) == "unknown") {
	        url = "http://egloos.egloos.com";
	    }else {
	        var url = document.location.href;
	    }
	    url = url.replace(/http\:\/\//gi,"");
	    var host = url.substring(0, url.indexOf("."));

		var spchost;
		var spcuri = "";
	
		switch(host){
			case "www":
				spchost = "www";
				spcuri = this._getSpcURIinWWW(url);
				break;
			case "valley":
				spchost = "valley";
				spcuri = this._getSpcURIinValley(url);
				break;
			case "garden":
				spchost = "garden";			
				break;
			case "finder":
				spchost = "finder";
				if(this.include(url, "/per_finder.php")) {
					spcuri = "per/";
				}
				break;
			default:
				spchost = "egloo";
				spcuri = this._getSpcURI(url);
				break;
		}

		// pcid cookie set
		this.setPCID();

		return this.statImgTemplate.evaluate({'spchost':spchost, 'spcuri':spcuri});
	}, 	
	
	_getSpcURIinWWW: function(url){
		var spcuri = "";

		if(this.include(url, "/login")) {
			spcuri = "login/";
		}else if(this.include(url, "/post")) {
			spcuri = "post/";
		}else if(this.include(url, "/recent")) {
			spcuri = "recent/";
		}else if(this.include(url, "/popular")) {
			spcuri = "popular/";
		}else if(this.include(url, "/adm/")) {
			spcuri = "admin/";
			if(this.include(url, "/adm/photo/")) {
				spcuri += "photo/";
			}else if(this.include(url, "/adm/stat/"))	{
				spcuri += "stat/";
			}
		}else if(this.include(url, "/egloo/")) {
			spcuri = "egloo/";
			if(this.include(url, "/egloo/insert.php")) {
				spcuri += "post/";
			}
		}else if(this.include(url, "/photo/")) {
			spcuri = "egloo/";
			if(this.include(url, "/photo/album_insert.php")) {
				spcuri += "photo/";
			}
		}		
		return spcuri;
	}, 
	
	_getSpcURIinValley: function(url){
		var spcuri = "";		

		if(this.include(url, "/gd_valley.php") 
				|| this.include(url, "/garden")) {
			spcuri = "garden/";
		}else if(this.include(url, "/theme") 
				|| this.include(url, "/eg_valley.php?slt=theme")) {
			spcuri = "theme/";
		}else if(this.include(url, "/trackback")
				|| this.include(url, "/weeklytheme.php")) {
			spcuri = "trackback/";
		}else if(this.include(url, "/tag") 
				|| this.include(url, "/eg_valley.php?slt=tag")) {
			spcuri = "tag/";
		}else if(this.include(url, "/emotion")) {
			spcuri = "emotion/";
		}else if(this.include(url, "/hotpotato")) {
			spcuri = "hotpotato/";
		}else if(this.include(url, "/review")) {
			spcuri = "review/";
		}else if(this.include(url, "/lifelog")) {
			spcuri = "lifelog/";
		}else if(this.include(url, "/center")) {
			spcuri = "center/";
		}else if(this.include(url, "/my_valley.php") 
				|| this.include(url, "/frm/") 
				|| this.include(url, "/my")) {
			spcuri = "my/";
			if (this.include(url, "/adm")) {
				if (this.include(url, "/admin_announce.php")) {
					spcuri += "admin_alert/";
				} else if (this.include(url, "/comment.php")) {
					spcuri += "admin_comment/";
                } else if (this.include(url, "/egloo.php")) {
                    spcuri += "admin_egloo/";
                } else if (this.include(url, "/rss.php")) {
                    spcuri += "admin_rss/";
                } else if (this.include(url, "/tag.php")) {
                    spcuri += "admin_tag/";
                } else if (this.include(url, "/recommend.php")) {
                    spcuri += "admin_recommend/";
				}
			} else if(this.include(url, "/announce_list.php")) {
			    spcuri += "alert/";
			} else if (this.include(url, "/comment_tracking.php")) {
			    spcuri += "comment/";
			} else if (this.include(url, "/post_list.php")) {
			    if (this.include(url, "c=garden")) {
			        spcuri += "garden/";
			    } else if (this.include(url, "c=rss")) {
			        spcuri += "rss/";
			    } else if (this.include(url, "c=tag")) {
			        spcuri += "tag/";
			    } else {
			        spcuri += "addlink/";
			    }
			} else if (this.include(url, "/recommend.php")) {
			    if (this.include(url, "con=attention")) {
			        spcuri += "popular/";
			    }
			    else {
			        spcuri += "recommend/";
			    }
			} else if (this.include(url, "/lifelog_list.php")) {
			    spcuri += "lifelog/";
			} else if (this.include(url, "/checkpost_list.php")) {
			    spcuri += "checkpost/";
			}
		} else if (this.include(url, "/?url=")) {
            spcuri += "?url=/";
        }
		return spcuri;
	}, 
	
	_getSpcURI: function(url){
		var spcuri = "";		
		if(this.include(url, "/photo")) {
			spcuri = "photo/";
		}else if(this.include(url, "/garden")) {
			spcuri = "garden/";
		}else if(this.include(url, "/lifelog")) {
			spcuri = "lifelog/";
		}
		return spcuri;		
	}, 
	
	/**
	 * Copyright의 setPCID와 시간을 이용해서, Cookie의 pcid항목을 설정합니다. 
	 */
	setPCID: function() {
		if(Cookie.getCookie("pcid") == null || Cookie.getCookie("pcid") == ""){
			var cookieEval = new Date();
			cookieEval = cookieEval.getTime();

			for(var i=0; i < 5; i++) {
				cookieEval = cookieEval + Math.random().toString().charAt(2);
			}
			var uv_expired_data = new Date(2011,1,1);
			Cookie.setCookie("pcid", cookieEval , uv_expired_data, "/", ".egloos.com", false);
		}
	}, 
	
	include: function(str, pattern) {
		return str.indexOf(pattern) > -1;
	}
};

Copyright.initialize();
