var dev=""; 
function eBookOpener() {
    this.maxWin	= null;
	this.winWidth        = null;
    this.winHeight       = 0;
	this.winLimitWidth   = 1024;
    this.winLimitHeight  = 768;
	this.winStyle        = null;
	this.winStyleName    = null;
    this.albumID; 
	this.dirPage = 1;
    this.eBookUrl;
	this.ViewerStyleList = new Array("eBookFull", "eBookCustomer", "eBookDefault", "FullScreen");
	
    this.GetWinStyle     = GetWinStyle;
    this.OpenBook        = OpenBook;
	
	this.reMax="N";
	this.User = null;
	this.bUrl = null;
	this.reBook = null;
	
	this.SearchKey="";
	this.SearchType=null;
	this.Search_year1 = null;
	this.Search_month1 = null;
	this.Search_day1 = null;
	this.Search_year2 = null;
	this.Search_month2 = null;
	this.Search_day2 = null;
	this.ClassInit = null;
	this.HTMLFILE = 0;
	this.Group = null;
	this.subGroup = null;
	this.BroswerCheck = "IE";
	this.IEver = 5;
	this.openChk = 1;
	this.openType = "WIN";
// 20100427 
	this.iPhone = iPhone;
	this.chg_script = chg_script;
	this.GetValue = GetValue;
//20110104
	this.GetIdentifier = GetIdentifier; 

	this.iPhoneStore = "http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?cc=kr&id=421995461&mt=8&alreadyRedirected=1";
	this.iPadStore = "http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?cc=kr&id=424776264&mt=8&alreadyRedirected=1";
	this.androidMarket = "";
	this.smart_chk = "";
}
// 20100427 
function iPhone() {
	var osx = window.navigator.appVersion;
	if(osx.indexOf("iPhone") > 0 || osx.indexOf('iPad') > 0 || osx.indexOf('iPod') > 0 || osx.indexOf("Android") > 0)  {
		if(osx.indexOf("iPhone") > 0)
			return "iPhone";
		else if(osx.indexOf('iPad') > 0)
			return "iPad";
		else if(osx.indexOf('iPod') > 0)
			return "iPod";
		else if(osx.indexOf("Android") > 0)
			return "Android";
	} else
		return "";
}
//20110330
function chg_script(js,pid, del) 
{
	var pLoad = false;
	var head = document.getElementsByTagName("head")[0];
	var script = document.createElement("script"); 

	if (head.getElementsByTagName("script").length > 1) 
	{ 
		for (var i = (head.childNodes.length - 1); i >= 0; i--)
		{ 
		
			if (head.childNodes[i].nodeName == 'SCRIPT' && head.childNodes[i].id == pid) 
            { 
				if(del) {
					head.removeChild(head.childNodes[i]); 
				} else {
					pLoad = true;
					break; 
				}
			} 
		}
	} 
	if(!pLoad) {
		script.src = js; 
		script.type = 'text/javascript'; 
		script.id= pid;
		head.appendChild(script); 
	}
}
//
function GetValue(url, res) 
{ 
	try{
		
		new Ajax.Request( url , { 
			onSuccess: function( transport ) { 
					var domain =  "http://"+transport.responseText+"/autoalbum/connectBook.html" + res;
					try{	
						location.href="SoftBook:"+domain;
					}catch(e){alert('false');}
			} 
		} ); 

	}catch(e){
		setTimeout("this.GetValue('"+url+"', '"+res+"')", "500");
	}

} 


//20110330
function GetIdentifier(url,aid,uid,device,js,sq,ws,wn) {
	try{
		var ajx = new Ajax.Request( url , { 
			onSuccess: function( transport ) { 
				var getStr = transport.responseText;
				if(!getStr) {
					this.chg_script(js,'smartSc', 1); 
					return;
				}

				var gu = getStr.indexOf("||");				
				var lgu = getStr.lastIndexOf("||");
				var identifier = getStr.substring(0,gu);
				
				var tStr = getStr.substring(gu+2);
				var gu2 = tStr.indexOf("||");
				var aname = tStr.substring(0, gu2);
				
				var tStr2 = tStr.substring(gu2+2);
				
				var gu3 = tStr2.indexOf("||");
				var dbnum = tStr2.substring(0, gu3);
				
				var tStr3 = tStr2.substring(gu3+2);
				var gu4 = tStr3.indexOf("||");
				var site = tStr3.substring(0, gu4);
				
				var tStr4 = tStr3.substring(gu4+2);
				var gu5 = tStr4.indexOf("||");
				var cdomain = tStr4.substring(0, gu5);

				var tStr5 = tStr4.substring(gu5+2);
				var gu6 = tStr5.indexOf("||");
				var smart_app_name = tStr5.substring(0, gu6);
				
				var tStr6 = tStr5.substring(gu6+2);
				var app_url = tStr6;

				if(this.iPhone() == "Android") {
					identifier = identifier.toLowerCase();
				}
				switch(device) {
					case "iPhone" : 
								device_name = "¾ÆÀÌÆù"; 
								app_name="¾Û"; 
								break;
					case "iPad" : 
								device_name = "¾ÆÀÌÆÐµå"; 
								app_name="¾Û"; 
								
								break;
					case "Android" : 
								device_name = "¾Èµå·ÎÀÌµå";
								app_name ="¾îÇÃ";  
								break;
					default : 
								device_name = "½º¸¶Æ®Æù,Å¸ºí·¿PC"; 
								app_name="¾îÇÃ";
				}

				
				if(identifier == "BROWSER") {
					
				} else {
					try{
						
						if(confirm("'"+device_name+"'¿ë \""+  smart_app_name + "\" " + app_name + " ÀÌ ¼³Ä¡ µÇ¾î ÀÖÀ»°æ¿ì \"½ÂÀÎ\" ¶Ç´Â \"È®ÀÎ\"  ¹öÅÏÀ» ÅÍÄ¡½Ã ¿­¶÷ÀÌ °¡´ÉÇÕ´Ï´Ù. \n¼³Ä¡ ÆäÀÌÁö·Î ÀÌµ¿Àº \"Ãë¼Ò\" ¹öÅÏÀ» ´­·¯ÁÖ¼¼¿ä")) {
							//alert(identifier+"://"+aid+"/"+aname+"/"+dbnum+"/"+cdomain+"/"+uid+"/"+site);	
							location.href= identifier+"://"+aid+"/"+aname+"/"+dbnum+"/"+cdomain+"/"+uid+"/"+site;
							
						} else {
							location.href = app_url;
						}
					}catch(e){}
				}

				
			}
		});
	}catch(e){
		//alert("error:"+e);
		var obj = this;
		setTimeout(function(){obj.GetIdentifier(url,aid,uid,device,js,sq,ws,wn);},500);
		//setTimeout("this.GetIdentifier('"+url+"','"+aid+"','"+uid+"'"+device+"'+'"+js+"')", "500");
	}


}

//
function browserVersion(){    

	this.BroswerCheck = (function x(){})[-5]=='x'?'FF3':(function x(){})[-6]=='x'?'FF2':/a/[-1]=='a'?'FF':'\v'=='v'?'IE':/a/.__proto__=='//'?'Saf':/s/.test(/a/.toString)?'Chr':/^function \(/.test([].sort)?'Op':'Unknown' ;
	if(this.BroswerCheck == "IE")
		this.IEversionCheck();
}

function IEversionCheck()
{
	var ver = 5;
	if( navigator.appName.indexOf("Microsoft") > -1 ) // IE?
	{
		if( navigator.appVersion.indexOf("MSIE 6") > -1) // IE6?
			ver= 6;
		else if(navigator.appVersion.indexOf( "MSIE 7") > -1) // IE7?IE7? 
			ver = 7;
		else if(navigator.appVersion.indexOf("MSIE 8") > -1) // IE8?IE8? 
			ver = 8;
	}

	this.IEver = ver;
}
function GetWinStyle() { 
	this.smart_chk = this.iPhone();
	var smart = this.smart_chk;

	if(this.maxWin == "Y") {
		var tmpFullScreen = ""
		this.winStyle = 0;
	}
	else var tmpFullScreen = ""  ;   

    if (this.winStyle == null ) { this.winStyle = 2; }
	if(( screen.width <= this.winLimitWidth || screen.height <= this.winLimitHeight) ) {  this.winStyle = 0 ; }
    
    switch ( this.winStyle ) {              
      case 0  : //  Default window 
                this.winStyleName  = this.ViewerStyleList[0]; 
                this.winTop     = 0 ;                                   
                this.winLeft    = 0 ;
                this.winWidth   =  screen.width  ;
                this.winHeight  =  screen.height ;
               
				if ( screen.width  != screen.availWidth  ) { this.winWidth  = screen.availWidth } // TaskBar Width
                if ( screen.height != screen.availHeight ) { this.winHeight = screen.availHeight } // TaskBar Heigh

				if(this.BroswerCheck=="Saf") 
					this.winHeight -= 25;

				this.maxWin = "Y";
				
				break;
      
      case 1  : //  customer window 
                this.winStyleName  = this.ViewerStyleList[1]; 
                if(this.winWidth < this.winLimitWidth)    {  this.winWidth  = this.winLimitWidth; }
                if(this.winHeight < this.winLimitHeight)  {  this.winHeight  = this.winLimitHeight; }
                this.winLeft    = (screen.width  - this.winWidth) / 2;
                this.winTop     = (screen.height - this.winHeight) / 2;
                break;
    
      case 2 : // user customer window environemnt
                this.winStyleName  = this.ViewerStyleList[2]; 
                this.winWidth      = this.winLimitWidth;  
                this.winHeight     = this.winLimitHeight;  
                this.winLeft       = (screen.width  - this.winWidth) / 2;
                this.winTop        = (screen.height - this.winHeight) / 2;
				
                break;
            
      case 3 :  // Full Screen  
                this.winStyleName  = this.ViewerStyleList[3]; 
                this.winWidth      = screen.width;  
                this.winHeight     = screen.height;  
                this.winLeft       = 0;
                this.winTop        = 0;
                break; 
      default : ;                            
    }
	/*
	if(smart.length > 0) {
		this.winHeight += 100;
		alert(this.winHeight);
	}
	*/
	this.winStyle    = "top=" + this.winTop + ", left=" + this.winLeft + ", width=" + this.winWidth + ", height=" + this.winHeight + ", directories=no, location=no, menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no, copyhistory=no" + tmpFullScreen ;
  }
 


function OpenBook() {
    // NOTE : process v1.6 , v1.8 param type for naver
	var bro_status_chk=null;
	this.GetWinStyle();
	var eBookClient = null;
	try
	{
		
		var u = this.eBookUrl;
		if(u.indexOf('view.html') > 0)
			var formPage = u.replace('view.html', 'connectBook.html');
		else if(u.indexOf('bbb.html') > 0)
			var formPage = u.replace('bbb.html', 'connectBook.html');

		var resOpt = "Falbum="+this.albumID+"&FmaxWin="+this.maxWin+"&FdirectPageNum="+this.dirPage+"&Fbhi="+this.winHeight+"&UserDumy="+this.User+"&scw="+screen.width+"&sch="+screen.height+"&bUrl="+this.bUrl+"&openerHTML="+this.HTMLFILE+"&SearchKey="+this.SearchKey+"&SearchType="+this.SearchType+"&Search_year1="+this.Search_year1+"&Search_month1="+this.Search_month1+"&Search_year2="+this.Search_year2+"&Search_month2="+this.Search_month2+"&fspGroup="+this.Group+"&subGroup="+this.subGroup;
		var sQuery2 = formPage + "?" + resOpt;
		var basePath = formPage.replace("connectBook.html", "");
		var jqueryJS = basePath + "js/JQuery/jquery-1.4.2.min.js";
		this.chg_script(jqueryJS, 'jquerySc', 0);
		
		var smart = this.smart_chk;
		if(smart.length > 0) {		
			var prototypeJS = basePath+"js/prototype/prototype.js";
			this.chg_script(prototypeJS, 'prototypeSc', 0);
				
			var smartJS = basePath+"common/lib/smart.html?UserDumy=" + this.User + "&album_id="+this.albumID+"&smart="+smart;			
			if(smart == "iPad") {
				var IdentifierURL = basePath+"common/lib/getIdentifier.html?UserDumy="+this.User+"&album_id="+this.albumID+"&device=tpc&smart="+smart;
				smartJS += "&device=tpc";
			} else { 
				var IdentifierURL = basePath+"common/lib/getIdentifier.html?UserDumy="+this.User+"&album_id="+this.albumID+"&smart="+smart;
			}
		
			smartJS += "&sq="+encodeURL(sQuery2);
			smartJS += "&ws="+encodeURI(this.winStyle);
			smartJS += "&wn="+encodeURI(this.winStyleName);
		
			//var gi = this.GetIdentifier(IdentifierURL,this.albumID, this.User, smart, smartJS, sQuery2, this.winStyle,this.winStyleName);
			//this.chg_script(smartJS,'smartSc', 1); 
			
			//eBookClient = window.open("about:blank", this.winStyleName +"_"+ Math.round(Math.random() * 100), this.winStyle);		
			if(this.openType == "WIN") {
				eBookClient = window.open("about:blank","_blank",'fullscreen=yes, scrollbars=auto');		
				eBookClient.document.open();
				eBookClient.document.write("<script language='javascript' type='text/javascript' src='"+smartJS+"' ></script>");
				eBookClient.document.close();
			} else {
				smartJS += "&direct=yes";
				location.href = smartJS;
			}
			this.openChk = eBookClient.closed;
			this.openChk = 0;
		}
		else if(this.openType == "WIN") {
			eBookClient = window.open(sQuery2, this.winStyleName +"_"+ Math.round(Math.random() * 100), this.winStyle);			
			this.openChk = eBookClient.closed;
		} else {
			location.href = sQuery2;
			this.openChk = 0;
		}
		
	
	}
	catch (e)
	{
		//alert("ÆË¾÷Ã¢ÀÌ Â÷´ÜµÇ¾î Ã¥ÀÚ¸¦ ¿­Áö ¸øÇÏ¿´½À´Ï´Ù.\n\n¾Æ·¡ \"ÀüÀÚºÏ º¸±â\" ¹öÆ° ¶Ç´Â Ã¥ÀÚ Ç¥Áö¸¦ Å¬¸¯ ÇÏ°Å³ª\n\nÀ§ ³ë¶õ Ç¥½ÃÁÙÀÇ ÆË¾÷ Â÷´ÜÀ» ÇØÁ¦ ÇÏ¿© ÁÖ½Ã¸é Ã¥ÀÚ¸¦ ¿­¶÷ ÇÏ½Ç¼ö ÀÖ½À´Ï´Ù.");
		bro_status_chk = "block";
	}	
	
	if(this.reMax == "Y") {
		self.opener = self;
		self.close(); 
	}

	if(this.ClassInit==1) {
		OpenEBook = new eBookOpener();
	}

	return bro_status_chk;
}  

OpenEBook = new eBookOpener();
function encodeURL(str){
    var s0, i, s, u;
    s0 = "";                // encoded str
    for (i = 0; i < str.length; i++){   // scan the source
        s = str.charAt(i);
        u = str.charCodeAt(i);          // get unicode of the char
        if (s == " "){s0 += "+";}       // SP should be converted to "+"
        else {
            if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){       // check for escape
                s0 = s0 + s;            // don't escape
            }
            else {                  // escape
                if ((u >= 0x0) && (u <= 0x7f)){     // single byte format
                    s = "0"+u.toString(16);
                    s0 += "%"+ s.substr(s.length-2);
                }
                else if (u > 0x1fffff){     // quaternary byte format (extended)
                    s0 += "%" + (oxf0 + ((u & 0x1c0000) >> 18)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else if (u > 0x7ff){        // triple byte format
                    s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else {                      // double byte format
                    s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
            }
        }
    }

    return s0;
}
