/* --------------------------------------------------------------------------------------
 * common.js
 * version 1.10
-------------------------------------------------------------------------------------- */
EventObserve(window, 'load', fncSwapImage, false);

function EventObserve(elem, name, func, cap) {
	if(elem.addEventListener){
		elem.addEventListener(name, func, cap);
	}else if(elem.attachEvent){
		elem.attachEvent('on' + name, func);
	}
}


/* --------------------------------------------------------------------------------------
 * Swap Images
 *   fncSwapImage
-------------------------------------------------------------------------------------- */
function fncSwapImage(){
	var imgs = document.getElementsByTagName('img');

	for(var i=0; i<imgs.length; i++){
		if (comHasClass(imgs[i], 'swap')) {
			// FileName Split.
			var tempSrc = imgs[i].src;
			var paths = tempSrc.split("/");
			var fileNameFull = paths[paths.length - 1];
			var names = fileNameFull.split(".");
			var extension = names.pop();
			var fileName = names.join('.');
			if(fileName.indexOf("_off") != -1) {
				var onFileNameFull = fileName.replace(/_off/, "_on") + "." + extension;
			}else {
				var onFileNameFull = fileName + "_on" + "." + extension;
			}

			// submit 'On' and 'Off' fileName split.
			imgs[i].offSrc = imgs[i].src;
			imgs[i].onSrc = imgs[i].src.replace(fileNameFull, onFileNameFull);

			// Events.
			imgs[i].onmouseover = function(){
				this.src = this.onSrc;
			}
			imgs[i].onmouseout = function(){
				this.src = this.offSrc;
			}
		}
	}
}

/* get className */
function comHasClass(id, className) {
	if (typeof id == 'string') {
		var elem = document.getElementById(id);
	} else {
		var elem = id;
	}
	if (!elem) return false;
	var elementClassName = elem.className;
	if (elementClassName.length == 0) return false;
	if (elementClassName == className ||
		elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
		return true;
	return false;
}
/* add className */
function comAddClass(id, addClassStr) {
	if (typeof id == 'string') {
		var elem = document.getElementById(id);
	} else {
		var elem = id;
	}
	if (!elem) return;
	if(elem.className == ''){
		elem.className = addClassStr;
	} else if (comHasClass(elem, addClassStr)) {
		elem.className += ' ' + addClassStr;
	}
}
/* remove className */
function comRemoveClass(id, removeClassStr) {
	if (typeof id == 'string') {
		var elem = document.getElementById(id);
	} else {
		var elem = id;
	}
	var elem = document.getElementById(id);
	if (!elem) return;
	if (!comHasClass(elem, removeClassStr)) return;
	var classes = elem.className.split(' ');
	var defaultClassName= '';
	for (var i=0; i < classes.length; i++){
		if (classes[i] != removeClassStr) {
			defaultClassName += classes[i] + ' ';
		}
	}
	elem.className = defaultClassName;
}

/* --------------------------------------------------------------------------------------
 * Window Open
 *   fncWinOpen, fncWinLiquidOpen, fncWinFixOpen
 *   @param arguments[1]  src
 *   @param arguments[2]  width
 *   @param arguments[3]  height
-------------------------------------------------------------------------------------- */
//_blank Window Base
function fncWinOpenBase(src, type) {
	var width  = arguments[2] != undefined ? arguments[2] : 600;
	var height = arguments[3] != undefined ? arguments[3] : 500;
	switch (type) {
	case 'lequid':
		var param = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
		break;
	case 'fix':
		var param = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no";
		break;
	default:
		var param = "toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes";
	break;
	}
	var wo = window.open(src, "_blank", param + ",width=" + width + ",height=" + height);
	wo.window.focus();
}

//_blank Window normal
function fncWinOpen(src) {
	fncWinOpenBase(src, '', arguments[1], arguments[2]);
}
//_blank Window lequid
function fncWinLiquidOpen(src) {
	fncWinOpenBase(src, 'lequid', arguments[1], arguments[2]);
}
//_blank Window fix
function fncWinFixOpen(src) {
	fncWinOpenBase(src, 'fix', arguments[1], arguments[2]);
}

/* --------------------------------------------------------------------------------------
 * Window Close
 *   fncWinClose
-------------------------------------------------------------------------------------- */
function fncWinClose(){
	self.window.close();
}

/* --------------------------------------------------------------------------------------
 * Window Change
 *   fncWinChange
 *   @param arguments[1]  src
-------------------------------------------------------------------------------------- */
function fncWinChange(src){
	if(fncWinCheckOpener()){
		popupWindow (src);
	}else{
		window.opener.location.href = src;
	}
}
function fncWinChangeAndClose(src){
	if(fncWinCheckOpener()){
		popupWindow (src);
	}else{
		window.close();
		window.opener.location.href = src;
	}
}


//check Window Opener
function fncWinCheckOpener() {
	return (!window.opener || window.opener.closed) ? true : false;
}

//fncWinCheckOpener -> open Window
function popupWindow(url) {
	fncWinOpen(url);
}


/* --------------------------------------------------------------------------------------
 * Smooth Scroll
 *   fncSmoothScroll
 *   @param id  target element id
 *   @param arguments[1]  speed
 *   @param arguments[2]  interval
 *   @param arguments[3]  acceleration
-------------------------------------------------------------------------------------- */
function fncSmoothScroll(id) {
  id = id.replace(/^#/, '');
  this.speed = arguments[1] != undefined ? arguments[1] : 10;
  this.interval = arguments[2] != undefined ? arguments[2] : 10;
  var acceleration = arguments[3] != undefined ? arguments[3] : 0.3;
  var targetElem = document.getElementById(id);
  if (!targetElem) return;
  this.targetPos = targetElem.offsetTop;
  var obj = targetElem;
  if (obj.offsetParent) {
    while (obj = obj.offsetParent) {
      this.targetPos += obj.offsetTop;
    }
  }
  var docHeight = document.body.offsetHeight;
  var winHeight = document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight;
  var scrHeight = document.documentElement.scrollTop || document.body.scrollTop;
  this.scrLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
  if (this.targetPos + winHeight > docHeight) {
    this.targetPos = docHeight - winHeight;
  }
  this.position = 0;
  if (this.targetPos < scrHeight) {
    this.position = scrHeight;
    } else {
    this.position = (this.targetPos - scrHeight) * acceleration;
  }
  this.moveObj = (function (_this) {
    return function(){
      var move = (_this.targetPos - _this.position) / _this.speed;
      if (parseInt(move) == 0) {
        move = (_this.position > _this.targetPos) ? -1 : 1;
      }
      var point = parseInt(_this.position + move);
      scrollTo(_this.scrLeft, point);
      if ((_this.targetPos > _this.position && _this.targetPos > point) ||
        (_this.targetPos < _this.position && _this.targetPos < point)) {
        setTimeout(function () { _this.moveObj(); }, _this.interval);
      }
      _this.position = point;
    };
  })(this);
  this.moveObj();
}

/* --------------------------------------------------------------------------------------
 * Add Bookmark
 *   fncAddBookmark
 *   @param arguments[1]  html tags
-------------------------------------------------------------------------------------- */
function fncAddBookmark(tags) {
	if(navigator.userAgent.indexOf('AppleWebKit')==-1){
		if(navigator.userAgent.indexOf('Opera')!=-1){
			document.write('<a href="' + location.href + '" rel="sidebar" title="' + document.title +'">' + tags + '</a>');
		}else{
			document.write('<a href="#" onClick="addBookmarkCore(document.title, location.href);return false;">' + tags + '</a>');
		}
	}
}

function addBookmarkCore(title,url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else if( document.all ) {
		window.external.AddFavorite(url, title);
	} else if( window.opera && window.print ) {
		return true;
	}
}

/* --------------------------------------------------------------------------------------
 * Page Print
 *   fncWindowPrint
 *   @param arguments[1]  html tags
-------------------------------------------------------------------------------------- */
function fncPagePrint(tags){
 var isIEMac = ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Mac') != -1));
 if(!isIEMac){
  document.write('<a href="javascript:print();">' + tags + '</a>');
 }
}




