var dummyHTML = "/js/white.html"

// --------------------
//   Event
// --------------------
// EventListener IE/Other Compatible

function addEvent(eventTarget, eventName, func){
	if(eventTarget.addEventListener){
		eventTarget.addEventListener(eventName, func, false);
	} else if(window.attachEvent){
		eventTarget.attachEvent('on'+eventName, function(){func.apply(eventTarget);});
	}
}





// --------------------
//   smartRollover
// --------------------

function smartRollover(){
	if(document.getElementsByTagName){
	    var preload = [];
		var images = document.getElementsByTagName("img");
		for(var i=0, src, l; i < images.length; i++){
			src = images[i].getAttribute("src");
			if(src.match("_off.")){
				images[i].src_rollout =  src;
				images[i].src_rollover =  src.replace("_off.", "_on.");
		        l = preload.length;
		        preload[l] = new Image();
		        preload[l].src = images[i].src_rollover;
				addEvent(images[i], "mouseout", function(){this.src = this.src_rollout;});
				addEvent(images[i], "mouseover", function(){this.src = this.src_rollover;});
			}
		}
	}
}
addEvent(window, 'load', smartRollover);


// --------------------
//   smoothScroll
// --------------------

var ss = {
  fixAllLinks: function() {
    // Get a list of all links in the page
    var allLinks = document.getElementsByTagName('a');
    // Walk through the list
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if ((lnk.href && lnk.href.indexOf('#') != -1) && 
          ( (lnk.pathname == location.pathname) ||
	    ('/'+lnk.pathname == location.pathname) ) && 
          (lnk.search == location.search)) {
        // If the link is internal to the page (begins in #)
        // then attach the smoothScroll function as an onclick
        // event handler
        ss.addEvent(lnk,'click',ss.smoothScroll);
      }
    }
  },
  smoothScroll: function(e) {
    // This is an event handler; get the clicked on element,
    // in a cross-browser fashion
    if (window.event) {
      target = window.event.srcElement;
    } else if (e) {
      target = e.target;
    } else return;

    // Make sure that the target is an element, not a text node
    // within an element
    if (target.nodeName.toLowerCase() != 'a') {
      target = target.parentNode;
    }

    // Paranoia; check this is an A tag
    if (target.nodeName.toLowerCase() != 'a') return;

    // Find the <a name> tag corresponding to this href
    // First strip off the hash (first character)
    anchor = target.hash.substr(1);
    // Now loop all A tags until we find one with that name
    var allLinks = document.getElementsByTagName('a');
    var destinationLink = null;
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if (lnk.name && (lnk.name == anchor)) {
        destinationLink = lnk;
        break;
      }
    }
    if (!destinationLink) destinationLink = document.getElementById(anchor);

    // If we didn't find a destination, give up and let the browser do
    // its thing
    if (!destinationLink) return true;

    // Find the destination's position
    var destx = destinationLink.offsetLeft; 
    var desty = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent && 
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }
    // Stop any current scrolling
    clearInterval(ss.INTERVAL);

    cypos = ss.getCurrentYPos();
    ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
    ss.INTERVAL =
setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);

    // And stop the actual click happening
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }

    if (e && e.preventDefault && e.stopPropagation) {
      e.preventDefault();
      e.stopPropagation();
    }
  },

  scrollWindow: function(scramount,dest,anchor) {
    wascypos = ss.getCurrentYPos();
    isAbove = (wascypos < dest);
    window.scrollTo(0,wascypos + scramount);
    iscypos = ss.getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(ss.INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  },

  getCurrentYPos: function() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },

  addEvent: function(elm, evType, fn, useCapture) {
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    // By Scott Andrew
    if (elm.addEventListener){
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent){
      var r = elm.attachEvent("on"+evType, fn);
      return r;
    } else {
      alert("Handler could not be removed");
    }
  } 
}

ss.STEPS = 20;
ss.addEvent(window,"load",ss.fixAllLinks);





// --------------------
//   styleswitcher
// --------------------

function setActiveStyleSheet(title) {
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
	setCookie();
}

function getActiveStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
	}
	return null;
}

function getPreferredStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1
			 && a.getAttribute("rel").indexOf("alt") == -1
			 && a.getAttribute("title")
			 ) return a.getAttribute("title");
	}
	return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function setStyleSheet(){
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
}


function setCookie(){
	var title = getActiveStyleSheet();
	createCookie("style", title, 365);
}

addEvent(window, 'load', setStyleSheet);
addEvent(window, 'unload', setCookie);
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);





// --------------------

//   confirm

// --------------------

var gConfirmObj;
var confirmN = 0;
var confirmWidth, confirmHeight;


function showConfirm(id){
	gConfirmId = id;
	var objBlock, objBg, objBody;

	objBlock = getObj(id);
	var blocks = objBlock.getElementsByTagName("div");
	for(var i=0; i<blocks.length; i++) {
		var obj = blocks[i];
		if(getClass(obj) == "bg") objBg = obj;
		if(getClass(obj) == "body") objBody = obj;
	}


	//IE6
	if(typeof document.documentElement.style.maxHeight == "undefined"){
		window.scrollTo(0,0);
		var container = getObj("container");
		container.style.overflow = "hidden";
		container.style.height = document.documentElement.clientHeight + "px";
		document.documentElement.style.overflow = "hidden";
		document.documentElement.style.height = document.documentElement.clientHeight + "px";
		objBg.style.width = "100%";
	}

	objBlock.style.visibility = 'visible';

	//amimetion
	if(objBody){
		if(!confirmWidth) confirmWidth = objBody.offsetWidth - 36;
		if(!confirmHeight) confirmHeight = objBody.offsetHeight;
		gConfirmObj = objBody;
		confirmAnimetion();
	}
	//objBlock.style.display = 'block';

	//IFRAME
	if(typeof document.documentElement.style.maxHeight == "undefined"){
		var elementIframe = document.createElement('iframe');
		elementIframe.src = dummyHTML;
		document.body.appendChild(elementIframe);

		elementIframe.id = id + "oIframe";

		elementIframe.style.position = "absolute";
		elementIframe.style.backgroundColor = "#ffffff";
		elementIframe.style.left = objBg.offsetLeft + "px";
		elementIframe.style.top = objBg.offsetTop + "px";
		elementIframe.style.width = objBg.offsetWidth + "px";
		elementIframe.style.height = objBg.offsetHeight + "px";
		elementIframe.style.zIndex = 4;
		elementIframe.style.display = "block";
		setClass(elementIframe, "bord");
	}
	objBlock.style.zIndex = 5;
}

function hideConfirm(id){
	var objBlock = getObj(id);
	objBlock.style.visibility = 'hidden';

	//IFRAME
	if(typeof document.documentElement.style.maxHeight == "undefined"){
		document.body.removeChild(getObj(id + "oIframe"));
	}

	//IE6
	if(typeof document.documentElement.style.maxHeight == "undefined"){
		var container = getObj("container");
		container.style.overflow = "";
		container.style.height = document.documentElement.clientHeight + "px";
		document.documentElement.style.overflow = "";
		document.documentElement.style.height = document.documentElement.clientHeight + "px";
	}
}

function confirmAnimetion(){
	var obj = gConfirmObj;
	var w, h;

	confirmN = (confirmN * 1000 + 1000 / 16) / 1000;
	w = Math.pow(Math.min(1, confirmN), 3) * confirmWidth;
	h = Math.pow(Math.min(1, confirmN), 3) * confirmHeight;

	obj.style.width = w + "px";
	obj.style.height = h + "px";
	obj.style["min-height"] = "0px";

//	alert(obj.offsetHeight);
	for(i=0; i<obj.childNodes.length; i++){
		var childObj = obj.childNodes[i];
		if(childObj.nodeType == 1){
			childObj.style["opacity"] = Math.max(0, confirmN - 1);
			childObj.style["-moz-opacity"] = Math.max(0, confirmN - 1);
			if(document.all){
				childObj.style.zoom = 1;
				childObj.style.filter = "alpha(opacity=" + Math.floor(Math.max(0, confirmN - 1) * 100) + ")";
			}
		}
	}


	if(confirmN < 2){
		setTimeout("confirmAnimetion()", 25);
	} else{
		obj.style["min-height"] = "250px";
		obj.style.width = "70%";
		confirmN = 0;
	}
}



// --------------------
//   Object
// --------------------

// Return Object IE/Other Compatible
function getObj(id){
	return document.all && document.all(id) || document.getElementById && document.getElementById(id);
}

// --------------------
//   Class IE/Other Compatible
// --------------------

function getClass(e){
	var c;
	if(document.all) c = e.getAttribute("className");
	else c = e.getAttribute("class");
	return (!c)?"":c;
}

function setClass(e, c){
	if(document.all) return e.setAttribute("className", c);
	else return e.setAttribute("class" , c);
}

function addClass(e, c){
	if(!getClass(e).match(c)) c = getClass(e) + " " + c;
	else c = getClass(e);
	setClass(e, c);
}

function removeClass(e, c){
	if(getClass(e)) setClass(e, getClass(e).replace(c, ""));
}





// --------------------
//   showLayer
// --------------------

function searchOptionShowSub(buttonObj, blockObj){
	var obj = buttonObj._obj;
	obj = obj.getElementsByTagName('p')[0];
	if(blockObj.move()) obj.style.display = "none";
	else obj.style.display = "block";
}


var showLayerObjects = new Array();

function showLayerInit(){
	var blocks = document.getElementsByTagName("div");
	for(var i=0; i<blocks.length; i++) {
		var obj = blocks[i];
		if(obj.getAttribute("rel")){
			if(obj.getAttribute("rel").match(/showLayer\[([^\]]+)\]/)){
				var id = RegExp.$1;
				var isShow = true;
				if(getClass(obj).match(/showLayerDefaultHide/)) isShow = false;
				eval(id + ' = new showLayer(obj, isShow, id)');
				showLayerObjects.push(eval(id));
			}
		}
	}
}

var showLayer = function(id){
	this.init.apply(this,arguments);
};

showLayer.prototype={
	_interval:25,
	_obj:null,
	_id:null,
	_padObj:null,
	_height : 0,
	_n : 1,
	_show : false,
	_resolution : 5,
	init:function(obj, isShow, id){
		this._obj = obj;
		this._id = id;
		this._height = this._obj.offsetHeight;
		this._obj.style.overflow = "hidden";
		this._show = isShow;
		if(isShow){
			this._obj.style.display = "block";
			this._obj.style.position = "relative";
			this._obj.style.top = "0px";
			this._obj.style.left = "0px";
		} else{
			//IE
			if(document.all){
				var padObj = document.createElement('img');
				this._obj.parentNode.insertBefore(padObj, this._obj);
				padObj.style.src = "..//common/img/clear.gif";
				padObj.style.height = "1px";
				this._padObj = padObj;
			}
		}
	},
	move:function(){
		if(this._padObj) this._padObj.style.display = "none";
		this._obj.style.position = "relative";
		this._obj.style.top = "0px";
		this._obj.style.left = "0px";
		this._obj.style.display = "block";
		if(this._n >= 1){
			this._show = !this._show;
			this._n  = 0;
			this.callback();
			return this._show;
		}
	},
	show:function(){
		if(!this._show){
			return this.move();
		}
	},
	hide:function(){
		if(this._show){
			this._obj.style.position = "relative";
			this._obj.style.top = "0px";
			this._obj.style.left = "0px";
			this._obj.style.height = "0px";
			this._obj.style.display = "none";
			this._show = !this._show;
			return false;
		}
	},
	callback:function(){
		f = this.event.update(this, this._obj);
		if(f) setTimeout(this.bind(this.callback), this._interval);
	},
	bind:function(method,arg){
		var _this=this;var _arg=(arg)?arg:[];
		return function(){
			method.apply(_this,_arg);
		}
	}
};

showLayer.prototype.event={
	update:function(t,o){
		var h;
		t._n = (t._n * 1000 + 1000 / t._resolution) / 1000;

		if(t._show){
			h = Math.pow(t._n, 2) * t._height;
		} else{
			h = (1 - Math.pow(t._n, 2)) * t._height;
		}

		o.style.height = h + "px";

		if(t._n >= 1){
			if(!t._show){
				o.style.display = "none";
			}
			else{
				o.style.position = "static";
				o.style.height = "auto";
				o.style.left = "1px";
				o.style.top = "1px";
			}
			return false;
		} else{
			return true;
		}
	}
};;

addEvent(window,"load", showLayerInit);





// --------------------
//   require
// --------------------

var requireObjs = new Object();
var requireClass = "indis";
var radioRequireClass = "indisCheck";

var regExp = new RegExp("^require\\[?([^\\]]*)\\]?\\[?([^\\]]*)\\]?$");

function requireInit(){	
	requireAdd(document.getElementsByTagName("input"));
	requireAdd(document.getElementsByTagName("select"));
	requireAdd(document.getElementsByTagName("textarea"));
}

function requireAdd(alltags){
	for(var i=0; i<alltags.length; i++) {
		var obj = alltags[i];
		if(obj.getAttribute("rel")){
			if(obj.getAttribute("rel").search(regExp) != -1){
				if(obj.type == "text" || obj.type == "password" || obj.type == "textarea"){
					addEvent(obj, "focus", requireOnFocus);
					addEvent(obj, "blur", requireOnBlur);
				} else if(obj.type == "select-one"){
					addEvent(obj, "change", requireOnBlur);
				} else if(obj.type == "radio" || obj.type == "checkbox"){
					var container = document.createElement("SPAN");
					obj.parentNode.insertBefore(container, obj);
					container.appendChild(obj);
					addEvent(obj, "click", requireOnBlur);
				}
				requireSet(obj);
			}
		}
	}
}

function requireOnFocus(event){
	event = event || window.event;
	var obj = event.target || event.srcElement;
	removeClass(obj, requireClass);
	removeClass(obj.parentNode, radioRequireClass);
}

function requireOnBlur(event){
	event = event || window.event;
	var obj = event.target || event.srcElement;
	requireSet(obj);
}

function requireSet(obj){
	if(obj.type == "text" || obj.type == "password" || obj.type == "textarea"){
		if(!obj.value) addClass(obj, requireClass);
		else removeClass(obj, requireClass);
	} else if(obj.type == "select-one"){
		if(obj.options[obj.selectedIndex].value == "") addClass(obj, requireClass);
		else removeClass(obj, requireClass);
	} else if(obj.type == "checkbox"){
		var isCheck = 0;
		var objs = new Array();

		obj.getAttribute("rel").search(regExp);
		var group = RegExp.$1;
		var number = RegExp.$2;
		var rel = RegExp.lastMatch;
		
		var inputs = document.getElementsByTagName("INPUT");
		for(var i=0; i<inputs.length; i++){
			if(inputs[i].getAttribute("rel") && inputs[i].getAttribute("rel").match("require\\[" + group + "\\]\\[" + number + "\\]")){
				if(inputs[i].checked){
					isCheck++;
					removeClass(inputs[i].parentNode, radioRequireClass);
				}
				else objs.push(inputs[i]);
			}
		}
		for(var i=0; i<objs.length; i++){
			if(isCheck >= number) removeClass(objs[i].parentNode, radioRequireClass);
			else addClass(objs[i].parentNode, radioRequireClass);
		}
	} else if(obj.type == "radio"){
		var isCheck = false;
		var objs = new Array();
		var inputs = document.getElementsByTagName("INPUT");
		for(var i=0; i<inputs.length; i++){
			if(inputs[i].name == obj.name){
				if(inputs[i].checked) isCheck = true;
				objs.push(inputs[i]);
			}
		}
		for(var i=0; i<objs.length; i++){
			if(isCheck) removeClass(objs[i].parentNode, radioRequireClass);
			else addClass(objs[i].parentNode, radioRequireClass);
		}
	}
}

addEvent(window, 'load', requireInit);