//popupWin.js
var popupWin = $Class({
	$init : function(){
		var options = this.opt = this._getOptionSet( arguments[0] );
		this._base = $( options.id );

		if ( !this._base ) return;

		this.openArr = $$("#"+this.opt.id+" ."+this.opt.openClass);
		this.closeArr = $$("#"+this.opt.id+" ."+this.opt.closeClass);
		this._addEvent();

	},
	_getOptionSet : function(argu) {
		var option = {
			openClass: 'popup',
			closeClass: 'close'
		};
		if (typeof argu == "undefined") argu = new Object;
		for(var x in argu) option[x] = argu[x];
		return option;
	},
	_addEvent : function() {
		$Fn(this.open, this).attach(this.openArr, "click");
		$Fn(this.close, this).attach(this.closeArr, "click");
	},
	open : function(e){
		if(e.element.tagName == "A") var el =  e.element;
		else var el =  e.element.parentNode;

		var winLink = $Element(el).attr("href");
		var option = $Element(el).attr("rel");
		var winName =  $Element(el).attr("name");
		if(!winName) winName = "";
		if(!option) option = "";

		window.open(winLink, winName, option);
		e.stop();
	},
	close : function(){
		window.close();
	}
});

//rollOverImage.js
var rollOverImage = $Class({
	$init : function(){
		var options = this.opt = this._getOptionSet( arguments[0] );
		this._base = $( options.id );

		if ( !this._base ) return;

		this.imgArr = $$("#"+this.opt.id+" ."+this.opt.imgCss);
		this._addEvent();
	},
	_getOptionSet : function(argu) {
		var option = {
			imgCss : "_rollOver",
			imgOn : "on"
		};
		if (typeof argu == "undefined") argu = new Object;
		for(var x in argu) option[x] = argu[x];
		return option;
	},
	_addEvent : function() {
		var len = $A(this.imgArr).length();
		var arr = $A(this.imgArr).$value();
		$Fn(this.over, this).attach(arr, "mouseover");
		$Fn(this.out, this).attach(arr, "mouseout");
	},
	over : function(e){
		e = e.currentElement;
		var src = e.src;
		var on = this.opt.imgOn;
		src = src.replace(new RegExp('(^.+)(\..{3}$)'), "$1"+on+"$2");
		e.src = src;
	},
	out : function(e){
		e = e.currentElement;
		var src = e.src;
		var on = this.opt.imgOn;
		src = src.replace(new RegExp('(^.*)'+on+'(\..{3}$)'), "$1$2");
		e.src = src;
	}
});

var slideImage = $Class({
	$init : function(){
		this._base = $('teams');
		if(!this._base) return;
		
		this.onMove = false;
		this.currentIndex = 0;
		this.total;
		this.liAry = new Array();
		
		this._makeAry();
	},
	_makeAry : function(){
		
		this.liAry = $$('li a', this._base);
		
		this.imgW = 300;
		this.s = 5;
		
		this.total = this.liAry.length-1;
		this.removeAllChild(this._base);
		this.li;
		
		this._appendLi();
		
		this.currentElm = $Element(this.li).append(this.liAry[0]);
		$Element(this.currentElm._element.firstChild).css({left:'0px',position:'absolute'});
		
		this.nextElm;
		
	},
	chgImg : function(e,type){
		$Event(e).stop();
		
		if(this.onMove) return;
		
		this.onMove = true;
		
		var nextIndex;
		if(type == 'next'){
			nextIndex = ++this.currentIndex;
			if(nextIndex > this.total) nextIndex = 0;
		}else if(type == 'prev'){
			nextIndex = --this.currentIndex;
			if(nextIndex < 0) nextIndex = this.total;
		}
		this.currentIndex = nextIndex;
		
		this.nextElm = $Element(this.li).append(this.liAry[nextIndex]);
		
		var nextStartLeft = this.imgW;
		if(type == 'next') nextStartLeft *= -1;
		
		$Element(this.nextElm._element.firstChild).css({left:nextStartLeft+'px',position:'absolute'});
		
		this._setMove(type);
	},
	
	removeAllChild : function(node){
		while(node.hasChildNodes() ) {
			node.removeChild(node.lastChild);
		}
 	},
	
	_appendLi : function(){
		this.li = $('<li>');
		this.li.id = 'js_slideLi';
		$Element(this._base).append(this.li);
	},
	
	_move : function(type){
		
		var t1,t2 = 0;
		if(type == 'next'){
			t1 = this.imgW;
		}else{
			t1 = this.imgW * -1;
		};
		
		var currentLeft1 = parseInt($Element(this.currentElm._element.firstChild).css('left'));
		var tmpnn1 = (t1-currentLeft1)/this.s;
		if(tmpnn1 < 0){
			var nn1 = -(Math.ceil(-tmpnn1));
		}else{
			var nn1 = Math.ceil(tmpnn1);
		}
		var nextLeft1 = nn1+currentLeft1;
		$Element(this.currentElm._element.firstChild).css('left',nextLeft1+'px');
		
		var currentLeft2 = parseInt($Element(this.nextElm._element.firstChild).css('left'));
		var tmpnn2 = (t2-currentLeft2)/this.s;
		if(tmpnn2 < 0){
			var nn2 = -(Math.ceil(-tmpnn2));
		}else{
			var nn2 = Math.ceil(tmpnn2);
		}
		var nextLeft2 = nn2+currentLeft2;
		$Element(this.nextElm._element.firstChild).css('left',nextLeft2+'px');
		
		if(Math.abs(t1-nextLeft1) < 1){
			clearInterval(this.intvID);
			$Element(this.currentElm._element.firstChild).css('left',t1+'px');
			$Element(this.nextElm._element.firstChild).css('left',t2+'px');
			
			this._moveComplete();
		}
	},
	
	_setMove : function(type){
		var moveFn = $Fn(function(){this._move(type)},this).bind();
		this.intvID = setInterval(moveFn,10);
	},
	
	_moveComplete : function(){
		this.currentElm._element.parentNode.removeChild(this.currentElm._element);
		this.currentElm = this.nextElm;
		this.onMove = false;
	}

});

var SwitchMenu = $Class({
	$init : function(){
		var options = this.opt = this._getOptionSet( arguments[0] );
		this._base = $( options.id );

		if ( !this._base ) return;

		this.display();
		this._addEvent();
	},
	_getOptionSet : function(argu) {
		var option = {
			onClass : "on",
			offClass : "off",
			titleClass : "menu",
			menuList : false,
			animated : false,
			start : function(){ return -1 }
		};
		if (typeof argu == "undefined") argu = new Object;
		for(var x in argu) {
			option[x] = argu[x];
		}
		return option;
	},
	display : function() {
		this.menuArr = $$("#"+this.opt.id+" ."+this.opt.onClass);
		this.titleArr = $$("#"+this.opt.id+" ."+this.opt.titleClass);
		var index = this.opt.start();

		this.toggle(index);
	},
	toggle : function(index){
		var t = this;
		var menuArr = this.menuArr;
		var off = this.opt.offClass;
		var on = this.opt.onClass;
		var menuList = this.opt.menuList;
		var titleArr = this.titleArr;

		if(menuList) menuList = $$("#"+this.opt.id+" ."+menuList);
		$A(menuArr).forEach(function(v,i){
			var el = $Element(v);
			var titEl = $Element(titleArr[i]);

			if(index == i && el.hasClass(off)) {
				el.removeClass(off);
				titEl.css("cursor","default");
				if(menuList) t.listView(menuList[i]);
			}else{
				el.addClass(off);
				titEl.css("cursor","pointer");
				if(menuList) t.listHide(menuList[i]);
			}
		});
	},
	listHide : function(o){ //To do animated
		o.style.display = "none";
	},
	listView : function(o){ //To do animated
		o.style.display = "block";
	},
	onClick : function(e){
		var i = $A(this.titleArr).indexOf(e.currentElement);
		this.toggle(i);
	},
	_addEvent : function(){
		var titleArr = this.titleArr;
		$Fn(this.onClick, this).attach(titleArr, "click");
	}
});



var DispLoading = $Class({
	$init : function(){
		this.bodyElm = $Element(document.body);
		
		//this.setLoadingElm();
	},
	setLoadingElm : function(){
		//var elm = '<div style="position:absolute;background-color:#666666;">loading</div>';
		
		var div = $('<div>');
		//var loadingElm = $Element(div);
		div.className = 'awsloading';
		//loadingElm.css({'position':'absolute','background-color':'#666666','border':'solid 1px #000000','width':'160px','height':'84px'});
		//loadingElm.css('background:url(http://119.235.233.108/uxdc/img/common/extra/awsloading.gif)','repeat-x 0 0');
		//loadingElm.css({'position':'absolute','background-color':'#666666','border':'solid 1px #000000','width':'160px','height':'84px'});
		
		//var img = $('<img>');
		///img.src = 'http://119.235.233.108/uxdc/img/common/extra/awsloading.gif';
		//img.width = 160;
		//img.height = 84;
		
		//loadingElm.append(img);
		
		//loadingElm.html('loading');
		
		this.bodyElm.append(div);
		
		this._setElmCenter(loadingElm);
		//document.body.appendChild(elm);
	},
	_setLayerPos : function(elm){
		this._setElmCenter(elm);
	},
	_setElmCenter : function(elm){
		elm = $Element(elm);

		var wSize = this._getWindowSize();
		var pSize = this._getPageSize();
		var cSizeObj = this._getContentSize(elm);
		
		var st = document.documentElement.scrollTop || document.body.scrollTop;
		var sl = document.documentElement.scrollLeft || document.body.scrollLeft;
		elm.css({left:'0px',top:'0px'});
		elm.css({left:parseInt(pSize["2"]/2-cSizeObj["w"]/2 + sl)+"px",top:parseInt(pSize["3"]/2-cSizeObj["h"]/2 + st) + "px"});
	},
	_getPageSize : function(){

		var xScroll,yScroll;
	
		if(document.compatMode && document.compatMode != "BackCompat"){
			//Standard
			xScroll = document.documentElement.scrollWidth;
			yScroll = document.documentElement.scrollHeight;
		}else{
			//Quirks
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.compatMode && document.compatMode != "BackCompat") {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else{
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		var pageHeight = Math.max(windowHeight,yScroll);
		var pageWidth = Math.max(windowWidth,xScroll);

		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		//console.log(arrayPageSize)
		return arrayPageSize;
	},
	_getContentSize : function(elm){

		elm = $Element(elm);
		
		if(elm.css('display') == 'none'){
			elm.css('visibility','hidden');
			elm.show();
			var hideFlg = true;
		}
		
		var w = elm.width();
		var h = elm.height();
		if(hideFlg){
			elm.hide();
			elm.css('visibility','visible');
		}

		return {w:w, h:h};
	},

	_getWindowSize : function(){
		var wSize = new Object();
		if($Agent().navigator().ie) {
			wSize.w = document.documentElement.clientWidth;
			wSize.h = document.documentElement.clientHeight;
		} else {
			wSize.w = window.innerWidth;
			wSize.h = window.innerHeight;
		}

		return wSize;
	}

});