﻿/*****************************************************
 *  Hoya JavaScript framework
 *  Copyright 2006 hoyakin.com
 *  (c) 2006 Daeho Kim <propolos@hotmail.com>
 *  For details, see the hoyakin web site: http://www.hoyakin.com 
 *
 *  본 코드는 아래의 오픈소스를 기반으로 확장 또는 새로 작성 되었습니다.
 *  Prototype.js [http://http://prototype.conio.net]
 *
 *  최초 작성자: 김대호
 *  최초 작성일: 2006. 07. 04
 *
 *  최종 작성자: 김대호
 *  최종 작성일: 2007. 01. 06
 *
 *
******************************************************/
var Hoya = {
	Version: '1.3',
	Hash: {},
	require: function(fileName) {
		document.write('<script type="text/javascript" src="'+fileName+'"></script>');
	},
	load: function() {
		if((typeof Prototype=='undefined') || (typeof Element == 'undefined') || 	(typeof Element.Methods=='undefined') ||	parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1]) < 1.5)
			throw("Hoya requires the Prototype JavaScript framework >= 1.5.0");

		$A(document.getElementsByTagName("script")).findAll( function(s) { return (s.src && s.src.match(/Hoya\.js(\?.*)?$/)); }).each( function(s) {
				var path = s.src.replace(/Hoya\.js(\?.*)?$/,'');
				var includes = s.src.match(/\?.*load=([\w.,]*)/);
				(includes ? includes[1] : '').split(',').each(
					function(include) { Hoya.require(path+include+'.js') });
		});
	}
}
Hoya.load();
Hoya.QueryString = $H(document.location.search.toQueryParams());

Date.Guid = function(key){
	var now = new Date();
	var yyyy = now.getFullYear();
	var mm = '0' + now.getMonth();
	mm = mm.substring(1,mm.length);
	var dd = '0' + now.getDate();
	dd = dd.substring(1,dd.length);
	var hh = '0' + now.getHours();
	hh = hh.substring(1,hh.length);
	var mi = '0' + now.getMinutes();
	mi = mi.substring(1,mi.length);
	var ss = '0' + now.getSeconds();
	ss = ss.substring(1,ss.length);
	var ms = now.getMilliseconds();
	return (key || 'GUID') + yyyy + mm + dd + hh + mi + ss + ms;
}

var _LUA_ = navigator.userAgent.toLowerCase()

var Browser = {
    UA : _LUA_,

	IsNN4 : ( ( document.layers ) ? true : false ),

    IsMSIE  : ( ( /msie/.test( _LUA_ ) ) && !( /opera/.test( _LUA_ ) ) ),
	IsMSIE4Up : ( (/msie/.test( _LUA_ ) ) && !( /opera/.test( _LUA_ ) )  && ( parseInt(navigator.appVersion) >= 4 ) ),
	IsMSIE4 : ( ( /msie/.test( _LUA_ ) ) && !( /opera/.test( _LUA_ ) )  && ( /msie 4/.test( _LUA_ ) ) ),
	IsMSIE5 : ( ( /msie/.test( _LUA_ ) ) && !( /opera/.test( _LUA_ ) )  && ( /msie 5/.test( _LUA_ ) ) ),
	IsMSIE6 : ( ( /msie/.test( _LUA_ ) ) && !( /opera/.test( _LUA_ ) )  && ( /msie 6/.test( _LUA_ ) ) ),
	IsMSXPSP2 : ( ( /msie/.test( _LUA_ ) ) && ( /windows nt 5.1/.test( _LUA_ ) )  && ( /sv1/.test( _LUA_ ) ) ),

    IsGecko : ( ( /gecko/.test( _LUA_ ) ) && !( /konqueror|safari|khtml/.test( _LUA_ ) ) ),

    IsOpera : ( ( window.opera ) && ( /opera/.test( _LUA_ ) ) ),
	IsOpera7 : ( ( window.opera ) && ( /opera/.test( _LUA_ ) ) && ( /opera 7/.test( _LUA_ ) ) || ( /opera\/7/.test( _LUA_ ) ) ),
	IsOpera5Or6 : ( ( window.opera ) && ( /opera/.test( _LUA_ ) ) && ( /opera 5/.test( _LUA_ ) ) || ( /opera\/5/.test( _LUA_ ) ) || ( /opera 6/.test( _LUA_ ) ) || ( /opera\/6/.test( _LUA_ ) ) ),

    IsKHTML : ( /konqueror|safari|khtml/.test( _LUA_ ) )
}

Element.addMethods({
  toHTML: function(element) {
    element = $(element);
    if (element.outerHTML) {
      return element.outerHTML.stripScripts();
    } else {
		var p = Builder.node('div');
		$(p).appendChild(element);
		return p.innerHTML;
    }
  }
});

//base 개체에 영향을 주지 않고 확장개체를 생성
Object.inherits = function(newObject, baseObject) {
	return Object.extend( Object.clone(baseObject), newObject );
}

Object.extend(String.prototype, {
	bytes: function(){
		return this.toArray().inject(
			0, 
			function(previousValue, value, index){
				if(escape(value).length > 4)
					return previousValue += 2;
				else
					return ++previousValue;
			}
		);
	},
	isNullOrEmpty: function(){
		if(this == null) return true;
		return /^([\s]*)$/.test(this);
	}
});


Hoya.RegExp = {
	Id: /^([\w\-_]+)$/gi,
	Image: /\.(png|gif|jpg|jpeg|bmp)$/gi
}

Hoya.Loader = {
	Items: $A(),
	push: function(iterator){
		Hoya.Loader.Items.push(iterator || Prototype.K);
	},
	exec: function(){
		Hoya.Loader.Items.each(function(value, idx){(value || Prototype.K)();});
	},
	run: function(){
		Event.observe(window, 'load', Hoya.Loader.exec);
	}
}

Hoya.Helper = {
	getHTML: function(sId,url,pars){		
		var myAjax = new Ajax.Updater(
					{success: sId}, 
					url, 
					{
						method: 'get', 
						parameters: pars, 
						onFailure: Hoya.Helper.reportError
					});
		
	},
	reportError: function(request)
	{
		alert('Sorry. There was an error.');
	},
	toStyleString: function(propertyString, value){
		return (!propertyString || !value || value == '')?'':propertyString + ': ' + value + '; ';
	},
    rewriteHTML: function(){
        var values = $A(arguments);
        values.each(function(node, index){
            if(node = $(node)){
                var html = node.innerHTML;
                node.innerHTML = html;
            }
        });
    },
    wrapFlash: function(parentNode, id, swf){
        if((parentNode = $(parentNode)) && window.ActiveXObject ){
            var html = '';
            html += '\r\n<object id="' + id + '_ie" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0">';
            html += '\r\n<param name="movie" value="' + swf + '" />';
            html += '\r\n<param name="quality" value="best" />';
            html += '\r\n<!-- Hixie method -->';
            html += '\r\n<!--[if !IE]> <-->';
            html += '\r\n<object id="' + id + '" type="application/x-shockwave-flash" data="' + swf + '">';
            html += '\r\n<param name="quality" value="best" />';
            html += '\r\n<param name="movie" value="' + swf + '" />';
            html += '\r\n<param name="FlashVars" value="playerMode=embedded" />';
            html += '\r\n' + parentNode.innerHTML;
            html += '\r\n</object>';
            html += '\r\n<!--> <![endif]-->';
            html += '\r\n</object>';
            parentNode.innerHTML = html;
        }
    },
    writeFlash: function(parentNode, id, swf, altTag){
        if((parentNode = $(parentNode)) && window.ActiveXObject ){
            html += '\r\n<object id="' + id + '" type="application/x-shockwave-flash" data="' + swf + '">';
            html += '\r\n<param name="quality" value="best" />';
            html += '\r\n<param name="movie" value="' + swf + '" />';
            html += '\r\n<param name="FlashVars" value="playerMode=embedded" />';
            html += '\r\n' + altTag;
            html += '\r\n</object>';
            parentNode.innerHTML = html;
        }
    },
    writeIEFlash: function(parentNode, id, swf, altTag){
        if((parentNode = $(parentNode)) && window.ActiveXObject ){
            var html = '';
            html += '\r\n<object id="' + id + '" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0">';
            html += '\r\n<param name="movie" value="' + swf + '" />';
            html += '\r\n<param name="quality" value="best" />';
            html += '\r\n' + altTag;
            html += '\r\n</object>';
            parentNode.innerHTML = html;
        }
    },
	getWindowFeatrueString: function(options){
		var sFeatures = '';
		if(Browser.IsMSIE){ // 익스플로어 계열
			if( ( typeof(options.type) !== 'undefined' ) && ( options.type == 'modal' || options.type == 'modeless' ) ){
				sFeatures += (options.height)?'dialogHeight:' + parseInt(options.height) + 'px' + ';':'';
				sFeatures += (options.left)?'dialogLeft:' + parseInt(options.left) + 'px' + ';':'';
				sFeatures += (options.top)?'dialogTop:' + parseInt(options.top) + 'px' + ';':'';
				sFeatures += (options.width)?'dialogWidth:' + parseInt(options.width) + 'px' + ';':'';
				sFeatures += (options.center)?'center:' + options.center + ';':'';
				sFeatures += (options.hide)?'dialogHide:' + options.hide + ';':'';
				sFeatures += (options.edge)?'edge:' + options.edge + ';':'';
				sFeatures += (options.help)?'help:' + options.help + ';':'';
				sFeatures += (options.resizable)?'resizable:' + options.resizable + ';':'';
				sFeatures += (options.scrollbars)?'scroll:' + options.scrollbars + ';':'';
				sFeatures += (options.status)?'status:' + options.status + ';':'';
				sFeatures += (options.unadorned)?'unadorned:' + options.unadorned + ';':'';
			}else{
				sFeatures += (options.height)?'height=' + parseInt(options.height):'';
				sFeatures += (options.left)?',left=' + parseInt(options.left):'';
				sFeatures += (options.location)?',location=' + options.location:'';
				sFeatures += (options.menubar)?',menubar=' + options.menubar:'';
				sFeatures += (options.resizable)?',resizable=' + options.resizable:'';
				sFeatures += (options.scrollbars)?',scrollbars=' + options.scrollbars:'';
				sFeatures += (options.status)?',status=' + options.status:'';
				sFeatures += (options.titlebar)?',titlebar=' + options.titlebar:'';
				sFeatures += (options.toolbar)?',toolbar=' + options.toolbar:'';
				sFeatures += (options.top)?',top=' + parseInt(options.top):'';
				sFeatures += (options.width)?',width=' + parseInt(options.width):'';
				sFeatures += (options.fullscreen)?',fullscreen=' + options.fullscreen:'';
			}
		}else if(Browser.IsGecko){ // 모질라 계열
			sFeatures += 'dependent=' + (options.type == 'modal' || options.type == 'modeless');
			sFeatures += ',modal=' + (options.type == 'modal');
			sFeatures += ',dialog=' + (options.type == 'modal' || options.type == 'modeless');
			sFeatures += (options.height)?',height=' + parseInt(options.height):'';
			sFeatures += (options.left)?',left=' + parseInt(options.left) + 'px':'';
			sFeatures += (options.location)?',location=' + options.location:'';
			sFeatures += (options.menubar)?',menubar=' + options.menubar:'';
			sFeatures += (options.resizable)?',resizable=' + options.resizable:'';
			sFeatures += (options.scrollbars)?',scrollbars=' + options.scrollbars:'';
			sFeatures += (options.status)?',status=' + options.status:'';
			sFeatures += (options.titlebar)?',titlebar=' + options.titlebar:'';
			sFeatures += (options.toolbar)?',toolbar=' + options.toolbar:'';
			sFeatures += (options.top)?',top=' + parseInt(options.top) + 'px':'';
			sFeatures += (options.width)?',width=' + parseInt(options.width):'';
			sFeatures += (options.fullscreen)?',fullscreen=' + options.fullscreen:'';
		}else{
		}
		return sFeatures;
	}
}

Hoya.Zoom = {
    Current: 2,
    LinkId: 'zoomBase',
    CssPath: 'http://newyd.hoyakin.com/css/open_content/common/',
    CssNames: $A(['ixcore_60.css','ixcore_70.css','ixcore.css','ixcore_95.css','ixcore_115.css']),
    zoom: function(effect){
        var link = $(Hoya.Zoom.LinkId);
        if(link != undefined){
            if(Cookie.accept()){
                Hoya.Zoom.Current = parseInt(Cookie.get('ZoomLevel'));
            }
            
            if(isNaN(Hoya.Zoom.Current)){Hoya.Zoom.Current = 2;}
            
            if(effect == 'in'){
                Hoya.Zoom.Current += 1;
            }else if(effect == 'out'){
                Hoya.Zoom.Current -= 1;
            }
            
            if(Hoya.Zoom.Current < 0){Hoya.Zoom.Current = 0;}
            else if(Hoya.Zoom.Current > 4){Hoya.Zoom.Current = 4;}
                    
            if(link.href != (Hoya.Zoom.CssPath + Hoya.Zoom.CssNames[Hoya.Zoom.Current])){
                link.href = Hoya.Zoom.CssPath + Hoya.Zoom.CssNames[Hoya.Zoom.Current];
            }
            
            if(Cookie.accept()){
                Cookie.set('ZoomLevel', Hoya.Zoom.Current.toString());
            }
        }
        
    }
}

Hoya.Cmd = {
	alert: function(msg, ele){
		alert(msg);
		if(ele = $(ele)){
			ele.focus();
			ele.select();
		}
	},
	showModal : function(sUrl, sName, params, options){
		Object.extend(options, {type:'modal'});
		var sFeatures = Hoya.Helper.getWindowFeatrueString(options);
		if(Browser.IsMSIE){
			return window.showModalDialog(sUrl, params, sFeatures);
		}else if(Browser.IsGecko){
			var oParams = params;
			window.open(sUrl, sName, sFeatures);
			return oParams;
		}else{
		}
	},
	showModeless : function(sUrl, sName, params, options){
		Object.extend(options, {type:'modeless'});
		var sFeatures = Hoya.Helper.getWindowFeatrueString(options);
		if(Browser.IsMSIE){
			return window.showModelessDialog(sUrl, params, sFeatures);
		}else if(Browser.IsGecko){
			var oParams = params;
			window.open(sUrl, sName, sFeatures);
			return oParams;
		}else{
		}
	},
	open : function(sUrl, sName, options){
	    if(!sName){sName = '__hoya_win';}
	    if(options){
		    Object.extend(options, {type:'open'});
		    var sFeatures = Hoya.Helper.getWindowFeatrueString(options);
		    return window.open(sUrl, sName, sFeatures);
		}else{
		    return window.open(sUrl, sName);
		}
	}
}

Hoya.Get = {
	allSelectOptions: function(select, ignore){
		var arr = [];
		if(!(select =$(select))) return arr;
		for(var loop1 = 0; loop1 < select.options.length; loop1++){
			if(ignore != select.options[loop1].value)	arr.push(select.options[loop1].value);
		}
		return arr;
	}
}

Hoya.PlugIn = {
	setFCKeditor: function(divId, toolbar, width, height, basePath){
		if(!$(divId)) return false;
		if(typeof(FCKeditor) != 'undefined'){
			var oFCKeditor;
			oFCKeditor = new FCKeditor( divId || 'htmlContents' ) ;
			oFCKeditor.BasePath = basePath || '/FCKeditor/' ;
			oFCKeditor.Width = width || '100%' ;
			oFCKeditor.Height = height || 400 ;
			oFCKeditor.ToolbarSet = toolbar || 'Default';
			oFCKeditor.ReplaceTextarea() ;
		}
	}
}

Hoya.Convert = {
    toSelectBox: function(tagParentNode, tagName, selectBoxId, selectBoxParentNode, sOptLabel){
        if((tagParentNode = $(tagParentNode)) && (selectBoxParentNode = $(selectBoxParentNode)) ){
            var objHtml = '';
            objHtml += '<select id="'+selectBoxId+'" name="'+selectBoxId+'" onchange="if(this.options[this.selectedIndex].value != \'\'){location.href=this.options[this.selectedIndex].value;}">';
            objHtml += '<optgroup label="'+sOptLabel+'" >';
            objHtml += '<option value="" >'+sOptLabel+'</option>';
            var lis = $A(tagParentNode.getElementsByTagName(tagName));
            objHtml = lis.inject(objHtml, function(html, value, index){
                                    var aObj = value.getElementsByTagName('A')[0];
                                    if(aObj){
                                        var optValue = aObj.href;
                                        var optText = aObj.innerHTML;
                                        html += '<option value=\''+optValue+'\'>'+optText+'</option>';
                                    }
                                    return html;
                                });
            objHtml += '</optgroup>';
            objHtml += '</select>';
            selectBoxParentNode.innerHTML = objHtml
        }
    },
    toImage: function(aTagName, imgSrc, imgId, imgAlt, imgLongdesc){
        var imgTag = '<img src="' + imgSrc + '" id="' + imgId + '" alt="' + imgAlt + '" ';
        if(imgLongdesc){
            imgTag +=  ' longdesc="' + imgLongdesc + '" ';
        }
        imgTag +=  ' />';
        $(aTagname).innerHTML = imgTag;
    }
}

Hoya.Event = {
    append: function(ele, eventName, func){
        if(ele = $(ele)){
		    Event.observe(ele, eventName, func);
        }
    },
    appendMouseOver: function(ele, func){
        Hoya.Event.append(ele,'mouseover',func);
    }
}
