﻿/**
 * Flash가 사용하는 JS control
 * @author SSen
 */
function FlashControl()
{
	this.loginURL = "";
	this.joinURL = "";
	this.swfFolder = "";
	this.jsFolder = "";
	this.zinizineURL = "";
	this.loungeURL = "";
	this.ziniIntroURL = "";
	
	/*
	 *
	 * Login Control
	 *
	 *
	 */
	/** 로그인 중인지 확인 */
	this.ing = false;
	/** 로그인에 영향을 받는 컨테이너들의 리스트 */
	this.ingContainers = new Array();
	/** 로그인 팝업 */
	this.loginWindow;

	
	this.moveToZinizine = function()
	{

		///alert("CallZinine");
		document.location.href = this.zinizineURL;
	}
	
	this.moveToZiniIntro = function()
	{
			this.ing = true;
			this.loginWindow = window.open(this.ziniIntroURL, "loginWindow", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=555,height=272");
	}
	
	
	this.moveToLounge = function()
	{
		document.location.href = this.loungeURL;
	}
	/*
	 *
	 * Core
	 *
	 *
	 */
	/**
	 * 쿠키를 가져온다
	 * @param {String} name
	 */
	this.getCookie = function(name)
	{
		var Found = false;
		var start;
		var end;
		var i = 0;
		
		while (i <= document.cookie.length) {
			start = i;
			end = start + name.length;
			
			if (document.cookie.substring(start, end) == name) {
				Found = true;
				break;
			}
			i++;
		}
		
		if (Found == true) {
			start = end + 1;
			end = document.cookie.indexOf(";", start);
			if (end < start) {
				end = document.cookie.length;
			}
			return document.cookie.substring(start, end);
		}
		return "";
	}
	/**
	 * ie 인지 아닌지 확인한다.
	 * @return {Boolean} true:IE, false:Firefox or Opera, webkit
	 */
	this.isIE = function()
	{
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return true;
		}
		else {
			return false;
		}
	}
	/**
	 * swf object 를 가져온다.
	 * @param {String} movie 의 name 을 입력
	 * @return {Object} 검색어에 해당하는 해당하는 swf
	 */
	this.getSwf = function(name)
	{
		if (this.isIE()) {
			return document[name];
		}
		else {
			return document[name];
		}
	}
	/**
	 * scroll 의 x, y 위치를 가져온다
	 * @return {Array} page scroll x, y
	 */
	this.getPageScroll = function()
	{
		var yScroll;
		
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		}
		else 
			if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
			}
			else 
				if (document.body) {// all other Explorers
					yScroll = document.body.scrollTop;
				}
		
		arrayPageScroll = new Array('', yScroll)
		return arrayPageScroll;
	}
	
	/**
	 * page 의 width, height 를 가져온다
	 * @return {Array} page width, height and window width, height
	 */
	this.getPageSize = function()
	{
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else 
			if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			}
			else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) { // all except Explorer
			if (document.documentElement.clientWidth) {
				windowWidth = document.documentElement.clientWidth;
			}
			else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		}
		else 
			if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			}
			else 
				if (document.body) { // other Explorers
					windowWidth = document.body.clientWidth;
					windowHeight = document.body.clientHeight;
				}
		
		// for small pages with total height less then height of the viewport
		if (yScroll < windowHeight) {
			pageHeight = windowHeight;
		}
		else {
			pageHeight = yScroll;
		}
		
		// for small pages with total width less then width of the viewport
		if (xScroll < windowWidth) {
			pageWidth = xScroll;
		}
		else {
			pageWidth = windowWidth;
		}

		if(top.location.href.indexOf("/fr/uc") != -1 || top.location.href.indexOf("/~") != -1){
			if(windowWidth > 960){
				windowWidth = 1044;
			}
			if(windowHeight < 670){
				windowHeight = 730;
			}
		}
		return [pageWidth, pageHeight, windowWidth, windowHeight];
	}
	

	
	/**
	 * 로그인을 시작함
	 * @return {void}
	 */
	this.loginStart = function()
	{
		if (!this.ing) {
			this.ing = true;
			this.loginWindow = window.open(this.loginURL, "loginWindow", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=555,height=272");
		}
	}
	this.joinStart = function()
	{
		var sw = screen.availWidth;
		var sh = screen.availHeight;

		//창의 위치 
		var px=(sw-840)/2;
		var py=(sh-850)/2;

		window.open(this.joinURL, "joinWindow", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=840,height=850,left="+px+"; top="+py+"");
	}
	/**
	 * 로그인이 정상적으로 완료되면 호출
	 * @return {void}
	 */
	this.loginComplete = function(type)
	{
		if (this.ing) {
			this.ing = false;
			this.loginWindow = null;
			var container = "";
			for (var f = 0; f < this.ingContainers.length; f++) {
				container = this.ingContainers[f];
				this.getSwf(container).loginCallback(true, type);
			}
		}
	}
	/**
	 * 로그인이 비정상 종료, 취소되면 호출
	 * @return {void}
	 */
	this.loginCancel = function()
	{
		if (this.ing) {
			this.ing = false;
			this.loginWindow = null;
			var container = "";
			for (var f = 0; f < this.ingContainers.length; f++) {
				container = this.ingContainers[f];
				this.getSwf(container).loginCallback(true);
			}
		}
	}
	/**
	 * 로그인을 중지한다. 팝업을 닫음
	 * @return {void}
	 */
	this.loginStop = function()
	{
		if (this.ing) {
			this.ing = false;
			if (this.loginWindow != null) {
				this.loginWindow.close();
				this.loginWindow = null;
			}
		}
	}
	
	
	/*
	 *
	 *
	 * Panel Control
	 *
	 *
	 *
	 */
	/** 현재 panel 이 하나라도 열려있는지 여부 */
	this.isContainerOpen = false;
	/* *********************************************************************
	 * public interface
	 ********************************************************************* */
	/**
	 * 상품 정보창을 연다
	 * @param {String} brandID
	 * @param {String} itemID
	 */
	this.openProductInfo = function(brandID, itemID)
	{
		var flashvars = {
			panelControlName: this.instanceName,
			moduleName: "productInfo",
			brandID: brandID,
			itemID: itemID
		};
		this.open(flashvars);
	}
	/**
	 * 에디터 뷰어를 연다
	 * @param {String} brandID
	 * @param {String} itemID
	 * @param {int} page
	 */
	this.openContentViewer = function(brandID, itemID, page)
	{
		var flashvars = {
			panelControlName: this.instanceName,
			moduleName: "contentViewer",
			brandID: brandID,
			itemID: itemID,
			page: page
		};
		this.open(flashvars);
	}
	/**
	 * VOD 뷰어를 연다
	 * @param {String} brandID
	 * @param {String} itemID
	 */
	this.openVODViewer = function(brandID, itemID)
	{
		var flashvars = {
			panelControlName: this.instanceName,
			moduleName: "vodViewer",
			brandID: brandID,
			itemID: itemID
		};
		this.open(flashvars);
	}
	/**
	 * panel을 닫는다
	 * @return {void}
	 */
	this.close = function()
	{
		try{
			document.getElementById("hideswf").style.display ="inline";
		}catch(e){
		}
		if (this.isContainerOpen) {
			this.isContainerOpen = false;
			
			var container = this.getDiv();
			container.innerHTML = '';
			container.style.display = "none";
			container.style.visibility='hidden';
			try{
				var i = this.ingContainers.indexOf("flashPanelContainer");
			}catch(e) {

			}

			this.ingContainers.splice(i, 1);
			window.onresize = null;
			window.onscroll = null;
		}
	}
	/**
	 * 현재 panel이 하나라도 열려있는지 확인
	 * @return {Boolean}
	 */
	this.isWindowOpen = function()
	{
		return this.isContainerOpen;
	}
	/* *********************************************************************
	 * event
	 ********************************************************************* */
	/**
	 * window scroll handle
	 * @param {FlashWindowControl} control
	 * @return {void}
	 */
	this.scrollHandle = function(control)
	{
		var scroll = function()
		{
			control.pos(); 
		}
		return scroll;
	}
	/**
	 * window resize handle
	 * @param {FlashWindowControl} control
	 * @return {void}
	 */
	this.resizeHandle = function(control)
	{
		var resize = function()
		{
			control.resize();
			control.pos();
		}
		return resize;
	}
	/* *********************************************************************
	 * utils
	 ********************************************************************* */
	/**
	 * offsetLeft 참고...
	 * board frame 의 위치정렬 - swf container 의 위치에 상대정렬 된다.
	 * @return {void}
	 */
	this.boardAlign = function()
	{
		var frame = document.getElementById(this.iframe);
		var box = this.container();
		var l;
		var t;
		var w;
		var h;
		if (this.layerType == "faq") {
			l = 186;
			t = 180;
			w = 678;
			h = 461;
		}
		else {
			l = 145;
			t = 225;
			w = 615;
			h = 303;
		}
		frame.style.left = (box.offsetLeft + l) + "px";
		frame.style.top = (box.offsetTop + t) + "px";
		frame.style.width = w + "px";
		frame.style.height = h + "px";
	}
	/**
	 * window size 에 맞게 flash 크기를 조절한다
	 * @return {void}
	 */
	this.resize = function()
	{
		var size = this.getPageSize();
		var container = this.getDiv();
		var ie = this.isIE();
		var w = (ie) ? size[2] : size[2] - 3;
		var h = (ie) ? size[3] : size[3] - 3;
		container.style.width = w + "px";
		container.style.height = (h-100) + "px";
		
	
		return container;
	}
	/**
	 * scroll 위치에 맞게 y 위치를 조절한다
	 * @return {void}
	 */
	this.pos = function()
	{
		var pos = this.getPageScroll();
		var container = this.getDiv();
		container.style.top = pos[1] + "px";
		container.style.left = (-80) + "px";  // 콘텐츠 뷰어 좌측 좌표
		return container;
	}
	/**
	 * container div 를 가져온다
	 * @return {DIV Element} document.getElementById(container name)
	 */
	this.getDiv = function()
	{
		return document.getElementById("flashPanelDiv");
	}
	/**
	 * panel 을 연다
	 * @param {Object} flashvars
	 * @return {void}
	 */
	this.open = function(flashvars)
	{
			try{
				document.getElementById("hideswf").style.display ="none";
			}catch(e){
			}
	
		if (!this.isContainerOpen) {
			this.isContainerOpen = true;
			
			var container = this.resize();
			container.style.display = "block";
			container.innerHTML = '<div id="flashPanelContainer"></div>';
			this.pos();
			
			flashvars.container = "flashPanelContainer";
			
			this.ingContainers.push("flashPanelContainer");
			
			var params = {
				base: this.swfFolder,
				allowScriptAccess: "sameDomain",				
				allowFullScreen: "true",				
				wmode: "transparent"
			};
			
			window.onresize = this.resizeHandle(this);
			window.onscroll = this.scrollHandle(this);
			var attribute = {};
			swfobject.embedSWF(this.swfFolder + "flashContainer.swf?date=" + (new Date().getTime()), "flashPanelContainer", "100%", "100%", "10.0.0", this.jsFolder + "expressInstall.swf", flashvars, params,attribute);
		}
	}
}