﻿if(typeof cj == "undefined")
{
	cj = {};
}
cj.evt = {
	guid : 1,
	add : function (element, type, handler)
	{
		if(element.addEventListener){
			element.addEventListener(type, handler, false);
		}else if (element.attachEvent)
		{
			element.attachEvent("on" + type, handler);
		}else{
			if (!handler.$$guid)
			{
				handler.$$guid = this.guid++;
			}
			if (!element.events)
			{
				element.events = {};
			}
			var handlers = element.events[type];
			if (!handlers)
			{
				handlers = element.events[type] = {};
				if(element["on" + type]){
					handlers[0] = element["on" + type];
				}
			}
			handlers[handler.$$guid] = handler;
			element["on" + type] = this.handleEvent;
		}
	},
	remove : function(element, type, handler)
	{
		if (element.removeEventListener)
		{
			element.removeEventListener(type, handler, false);
		}else
		{
			if (element.events && element.events[type])
			{
				delete element.events[type][handler.$$guid];
			}
		}
	}
}
cj.util = {
	getMousePos : function(ev){
		var ev = ev ? ev : window.event;
		return (ev.pageX && ev.x) ? {"x":ev.pageX, "y":ev.pageY} : {"x":ev.clientX+this.getScrollPos().left, "y":ev.clientY+this.getScrollPos().top}
	},
	getScrollPos : function(){
		var top = left = 0;
		top = document.documentElement.scrollTop || document.body.scrollTop;
		left = document.documentElement.scrollLeft || document.body.scrollLeft;
		return {"top" : top, "left" : left}
	},
	getWindowSize : function(){
		var width = height = 0;
		width = document.documentElement.clientWidth || document.body.clientWidth;
		height = document.documentElement.clientHeight || document.body.clientHeight;
		return {"width" : width, "height" : height}
	},
   	getElementPos : function(elm){
   		var log = '<br>';
   		var top = left = 0;
   		while(elm != null){
	   		top += elm.offsetTop;
	   		left += elm.offsetLeft;
   			elm = elm.offsetParent;
   		}
   		return {"top" : top, "left" : left}
   	},
	loadJs : function(src){
		var js = document.createElement("script");
		js.src = src;
		js.type = "text/javascript";
		document.getElementsByTagName("head")[0].appendChild(js);
	},
	fix_ieflash : function() {
		var objects = document.getElementsByTagName("object"); 
		for (var i=0;i<objects.length;i++)
			objects[i].outerHTML = objects[i].outerHTML;
	},
	popMsg : {
		div : null,
		move : 10,
		timer : null,
		holdTime : 3 * 1000,
		init : function(msg, pos, size, holdTime){
			if (typeof(holdTime)!="undefined"){
				this.holdTime = holdTime * 1000;
			}
			this.clear();
			this.size = size;
			this.div = document.createElement("div");
			this.div.className = "popMsg";
			this.div.innerHTML = msg;
			document.body.appendChild(this.div);
			this.div.style.left = pos.left + "px";
			this.div.style.top = pos.top + "px";
			this.show(1);
		},
		show : function(step){
			if(this.div.offsetWidth < this.size.width){
				this.div.style.width = Math.abs((this.size.width/this.move)*step) + "px";
				this.div.style.height = Math.abs((this.size.height/this.move)*step) + "px";
				this.timer = setTimeout( function(){ cj.util.popMsg.show(++step) }, 1 );
			}else{
				this.timer = setTimeout( function(){cj.util.popMsg.clear()}, this.holdTime);
			}
		},
		clear : function(){
			if (this.div){
				clearTimeout(this.timer);
				this.timer = null;
				document.body.removeChild(this.div);
				this.div = null;
			}
		}
	}
}
cj.roller = {
	holdtime : 6000,
	rolltime : 70,  /*這個數字決定速度*/
	ar : 0.99,
	gap : 7,
	init : function(boxid, itemcontainerid){
		var box = document.getElementById(boxid);
		if (!box){
			return;
		}
		var items = document.getElementById(itemcontainerid);
		if(!items)	return;
		box.items = items.getElementsByTagName('table');
		if(!box.items || box.items.length==0){
			return;
		}
		box.index = 0;
		box.timer = null;
		
		box.div1 = document.createElement("div");
		box.div1.style.cssText = "position:absolute; top:0px; left:0px;";
		box.div2 = box.div1.cloneNode(true);
		box.div3 = box.div1.cloneNode(true);
		box.appendChild(box.div1);
		box.appendChild(box.div2);
		box.appendChild(box.div3);
		
		this.roll(box);
	},
	roll : function(obj){
		while(obj.div1.childNodes[0])
			obj.div1.removeChild(obj.div1.childNodes[0]);
		obj.div1.appendChild(obj.items[obj.index].cloneNode(true));
		obj.div1.style.top = 0 + "px";
		
		if(obj.items.length <2){
			return;
		}
		while(obj.div2.childNodes[0])
			obj.div2.removeChild(obj.div2.childNodes[0]);
		obj.div2.appendChild(obj.items[(obj.index+1)%obj.items.length].cloneNode(true));
		obj.div2.style.top = obj.div1.offsetHeight + 10 + "px";
		
		if(obj.items.length>2){
			obj.index  = (obj.index == obj.items.length -1) ? 0 : obj.index +1;
			while(obj.div3.childNodes[0])
				obj.div3.removeChild(obj.div3.childNodes[0]);
			obj.div3.appendChild(obj.items[(obj.index+1)%obj.items.length].cloneNode(true));
			obj.div3.style.top = 2*obj.div1.offsetHeight + this.gap + 10 + "px";
			
			obj.timer = setTimeout(function(){cj.roller.rollNext(obj, cj.roller.ar)}, cj.roller.holdtime);
		}
	},
	rollNext : function(obj, ar){
		var top = Math.floor(Math.max(0, obj.div2.offsetTop* ar));
		obj.div1.style.top = top - obj.div1.offsetHeight - this.gap + "px";
		obj.div2.style.top = top + "px";
		obj.div3.style.top = top + obj.div1.offsetHeight + this.gap + "px";
		if(!top){
			this.roll(obj);
		}else{
			obj.timer = setTimeout(function(){cj.roller.rollNext(obj, ar*cj.roller.ar)}, this.rolltime);
		}
	}
}
cj.fade = {
	fps : 7,
	otype : false,
	start : function(obj, time, current, end){
		var steps = (end - current) / (time * this.fps) ;
		this.getotype(obj);
		if (this.otype != 'none'){
			this.setfade(obj, current);
			this.dofade(steps, obj, current, end);
		}
	},
	dofade : function (steps, obj, current, end){
		var dir = (steps > 0);
		current += steps;
		this.setfade(obj, current);
		
		if (dir ^ (current - end > 0))
		{
			setTimeout(
				function()
				{
					cj.fade.dofade(steps, obj, current, end);
				},
				1000 / this.fps
			);
		}else{
			this.sdo=0;
		}
	},
	setfade : function (obj, value){
		if (!this.otpye){
			this.getotype(obj);
		}
		switch(this.otype)
		{
			case 'ie':
				obj.filters.alpha.opacity = Math.floor(value * 100);
			break;
			
			case 'khtml':
				obj.style.KhtmlOpacity = value;
			break;
			
			case 'moz':
				obj.style.MozOpacity = (value == 1) ? 0.9999999 : value;
			break;
			
			default:
				obj.style.opacity = (value == 1) ? 0.9999999 : value;
			break;
		}
	},
	getotype : function(obj){
		if (typeof obj.filters == 'object'){
			this.otype = (obj.filters.length >0
				&& typeof obj.filters.alpha == 'object'
				&& typeof obj.filters.alpha.opacity == "number")
				? 'ie' : 'none';
		}else if(typeof obj.style.opacity != 'undefined'){
			this.otype = 'w3c';
		}else if (typeof obj.style.MozOpacity != 'undefined'){
			this.otype = 'moz';
		}else if (typeof obj.style.KhtmlOpacity != 'undefined'){
			this.otype = 'khtml';
		}else{
			this.otype = false;
		}
		return false;
	}
}
cj.slidshow = {
	index : 0,
	objId : "slidshow",
	obj : new Object(),
	imgs : new Array(),
	rolltime : 10, //每隔10秒下一張(必需大於淡出入的秒數)
	init : function(){
		if (typeof slidImgs == "undefined" || slidImgs == ""){
			return;
		}
		this.obj = document.getElementById(this.objId);
		if (!this.obj){
			return;
		}
		
		//clear
		while(this.obj.childNodes[0]){
			this.obj.removeChild(this.obj.childNodes[0]);
		}
		var a = slidImgs.split(',');
		var top = 0;
		for (var i=0; i<a.length; i++){
			this.imgs[i] = document.createElement("img");
			this.imgs[i].src = a[i];
			this.imgs[i].style.cssText = "position:absolute; top: 0px; left: 0px; filter:alpha(opacity=0); visibility:hidden";
			this.obj.appendChild(this.imgs[i]);
			cj.fade.setfade(this.imgs[i], 0);
		}
		cj.fade.setfade(this.imgs[0], 1);
		this.imgs[0].style.visibility = 'visible'
		if(a.length>1)
			setTimeout("cj.slidshow.roll()", 5* 1000); //第一次等5秒就開始
	},
	roll : function(){
		this.index  = (this.index == this.imgs.length -1) ? 0 : this.index +1;
		var pre = (this.index) ? this.index -1 : this.imgs.length -1;
		for (i=0; i<this.imgs.length; i++){
			if(i==this.index || i==pre){
				this.imgs[i].style.visibility = 'visible';
			}else{
				this.imgs[i].style.visibility = 'hidden';
			}
		}
		//原來的圖 : 1--> 總共1秒， 0.8->0透明度從0.8->0
		//cj.fade.setfade(this.imgs[pre], 0);
		cj.fade.start(this.imgs[pre], 1, 0.9, 0); //前一張淡出執行1秒
		//下一張圖 : 2--> 總共2秒， 0.2->1透明度從0.2->1
		cj.fade.start(this.imgs[this.index], 3, 0.1, 1); //下一張淡入3秒
		setTimeout("cj.slidshow.roll()", this.rolltime * 1000);
	}
}
cj.form = {
	chk : function(f, fields){
		if(!f)	return false;
		if(typeof f.lang == "undefined" || !f.lang){
			alert("lang not set");
			return false;
		}
		var lang = f.lang.value;
		if( typeof cj.form.msg == "undefined"){
			alert("cj.form.msg not set or lang.js not load");
			return false;
		}
		
		for (var i=0; i<fields.length; i++){
			var a = fields[i].split(',');
			var obj = f[a[0]];
			if(!obj){
				alert(a[0] + " not set");
				return false;
			}
			var type = a[1];
			var title = obj.getAttribute("title");
			if(!title){
				alert(a[0] + " title not set");
				return false;
			}
			var pos = cj.util.getElementPos(obj);
			pos.left +=  50;
			switch (type){
				case "num":
					obj.value = cj.form.trim(obj.value);
					if( !cj.form.isNumber(obj.value)){
						obj.focus();
						alert(title + cj.form.msg.not_number[lang]);
						return false;
					}
				case "text":
					if( obj.value == "" ){
						obj.focus();
						alert(title + cj.form.msg.not_null[lang]);
						return false;
					}
				break;
				case "email":
					if(!cj.form.isEmail(obj.value)){
						obj.focus();
						alert(cj.form.msg.valid[lang] + title);
						return false;
					}
				break;
			}
		}
		return true;
	},
	isEmail : function(elm){
		re = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
		return elm.match(re)
	},
	trim : function(obj){
		return obj.replace(/\s/g, '');
	},
	msg : {
		not_null : {
			zh_tw	: " 必填 ",
			en		: " is required! "
		},
		valid : {
			zh_tw	: " 請輸入有效的 ",
			en		: " Please specify valid "
		},
		day : {
			zh_tw	: "日期",
			en		: "Date"
		},
		not_number : {
			zh_tw	: " 不是數字",
			en		: " is not number"
		}
	}
}
cj.calendar = {
	init : function(yid, mid, did, aid){
		var a = document.getElementById(aid);
		if(!a)	return;
		a.y = document.getElementById(yid);
		a.m = document.getElementById(mid);
		a.d = document.getElementById(did);
		if(!a.y || !a.m || !a.d)	return;
		
		//select onchange --hide
		cj.evt.add(a.y, "change", function(){a.hide()});
		cj.evt.add(a.m, "change", function(){a.hide()});
		cj.evt.add(a.d, "change", function(){a.hide()});
		
		a.closeH = 13;
		a.bolder = 2;
		a.boxW = 30;
		a.boxH = 25;
		
		a.draw = cj.calendar.draw;
		a.drawBox = cj.calendar.drawBox;
		a.show = cj.calendar.show;
		a.hide = cj.calendar.hide;
		a.lastMonth = cj.calendar.lastMonth;
		a.nextMonth = cj.calendar.nextMonth;
		a.drawCaption = cj.calendar.drawCaption;
		a.setDate = cj.calendar.setDate;
		a.setH = cj.calendar.setH;
		a.setTheDay = cj.calendar.setTheDay;
		
		cj.evt.add(a, "click", function(){a.show()});
		a.style.cursor = "pointer";
		
		a.win = document.createElement("div");
		a.win.id = "calendar";
		var width = 7*a.boxW + 2*a.bolder + 6;
		a.win.style.width = width + "px";
		a.win.style.top = (cj.util.getElementPos(a)).top + a.offsetHeight + 5 + "px";
		a.win.style.left = (cj.util.getElementPos(a)).left + a.offsetWidth - width +  "px";
		
		//drag
		a.win.attrib = "dragable";
		cj.evt.add(a.win, "mousedown", cj.drag.start);
		
		if(document.attachEvent){
			a.win.mask = document.createElement("iframe");
			a.win.mask.style.cssText = "position:absolute; display:none; border:0px;";
			a.win.mask.style.width = a.win.style.width;
			a.win.mask.style.top = a.win.style.top;
			a.win.mask.style.left = a.win.style.left;
			document.body.appendChild(a.win.mask);
		}
		
		//close
		a.closer = document.createElement("div");
		a.closer.parent = a;
		a.closer.id = "calCloser";
		a.closer.style.top = 1 + "px";
		a.closer.style.right = 1 + "px";
		cj.evt.add(a.closer, "click", function(evt){
			var evt = evt || window.event;
			tgt = evt.target || evt.srcElement;
			tgt.parent.hide();
			});
		a.win.appendChild(a.closer);
		
		//last
		a.last = a.drawBox('calLast', 0, 0, 1, 1, "&#60;&#60");
		a.last.parent = a;
		a.last.style.cursor = "pointer";
		a.last.onclick = a.lastMonth;
		a.win.appendChild(a.last);
		
		//caption
		a.caption = a.drawBox('calCaption', 0, 1, 5, 1, "");
		a.win.appendChild(a.caption);
		document.body.appendChild(a.win);
		
		//next
		a.next = a.drawBox('calNext', 0, 6, 1, 1, "&#62;&#62");
		a.next.parent = a;
		a.next.style.cursor = "pointer";
		a.next.onclick = a.nextMonth;
		a.win.appendChild(a.next);
		
		//nav
		a.nav = new Array();
		var days = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
		for (var i=0; i<days.length; i++){
			a.nav[i] = a.drawBox("calNav", 1, i, 1, 1, days[i]);
			a.win.appendChild(a.nav[i]);
		}
		document.body.appendChild(a.win);
	},
	draw : function(){
		this.setTheDay();
		this.drawCaption();
		this.setH();
		
		if (typeof this.box != "undefined"){
			for(i in this.box){
				this.win.removeChild(this.box[i]);
			}
			this.box = null;
		}
		this.box = new Array();
		for (var i=0; i<7*this.rows; i++){
			id = (i%7==0 || i%7==6) ? "calweekend" : "";
			var day = (i - this.firstWeekDay) + 1;
			if (day < 1 || day > this.monthDay)
				day = "";
			this.box[i] = this.drawBox(id, Math.floor(i/7)+2, (i%7), 1, 1, day);
			this.box[i].parent = this;
			if (day){
				this.box[i].style.cursor = "pointer";
				this.box[i].onclick = this.setDate;
			}
			if (day == this.d.value)
					this.box[i].id = "caltheday";
			this.win.appendChild(this.box[i]);
		}
	},
	setTheDay : function(){
		this.monthDay = cj.calendar.solarDays(this.y.value, this.m.value);
		this.firstWeekDay = (new Date(this.y.value + "/" + this.m.value + "/1")).getDay(); 
	},
	setH : function(){
		this.rows =  Math.ceil((this.monthDay + this.firstWeekDay)/7);
		this.win.style.height = 2*this.bolder + this.closeH + (this.rows+2)*(this.boxH+1) + "px";
		if(typeof this.win.mask != "undefined"){
			this.win.mask.style.height = this.win.style.height;
		}
	},
	drawCaption : function(){
		this.caption.innerHTML = this.y.value + "-" + this.m.value;
	},
	show : function(){
		if(typeof this.win.mask != "undefined"){
			this.win.mask.style.display = "block";
		}
		this.win.style.display = "block";
		this.draw();
		return false;
	},
	hide : function(){
		if(typeof this.win.mask != "undefined")
			this.win.mask.style.display = "none";
		this.win.style.display = "none";
	},
	setDate : function(){
		this.parent.d.selectedIndex = this.innerHTML - 1;
		this.parent.hide();
		if(typeof this.parent.d.onchange == "function")
			this.parent.d.onchange();
	},
	lastMonth : function(ev){
		if (this.parent.y.selectedIndex == 0 && this.parent.m.selectedIndex == 0)	return;
		if (this.parent.m.selectedIndex == 0){
			this.parent.y.selectedIndex --;
			this.parent.m.selectedIndex = 11;
		}else{
			this.parent.m.selectedIndex --;
		}
		this.parent.monthDay = cj.calendar.solarDays(this.parent.y.value, this.parent.m.value);
		this.parent.firstWeekDay = (new Date(this.parent.y.value + "/" + this.parent.m.value + "/1")).getDay(); 
		this.parent.draw();
	},
	nextMonth : function(){
		if (this.parent.y.selectedIndex >= this.parent.y.options.length && this.parent.m.selectedIndex >= this.parent.m.options.length){
			return false;
		}
		if (this.parent.m.selectedIndex == 11){
			this.parent.y.selectedIndex++;
			this.parent.m.selectedIndex = 0;
		}else{
			this.parent.m.selectedIndex++;
		}
		this.parent.monthDay = cj.calendar.solarDays(this.parent.y.value, this.parent.m.value);
		this.parent.firstWeekDay = (new Date(this.parent.y.value + "/" + this.parent.m.value + "/1")).getDay(); 
		this.parent.draw();
	},
	drawBox : function (id, t, l, w, h, htm){
		var obj = document.createElement("div");
		obj.id = id;
		obj.style.top = this.bolder + this.closeH + t*(this.boxH+1) + "px";
		
		obj.style.left = this.bolder + l*(this.boxW+1) + "px";
		obj.style.width = w * (this.boxW+1) - 1 + "px";
		obj.style.height = h * (this.boxH+1) -1 + "px";
		obj.innerHTML = htm;
		return obj;
	},
	solarDays : function (y,m){
		var solarMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		if( m == 2){
			return (( y%4 == 0) && (y%100 !=0) || (y%400 == 0)) ? 29 : 28;
		}else{
			return solarMonth[m-1];
		}
	}
}
cj.dropdownMenu = {
	timer : null,
	menus : [],
	ar : 0.99,
	init: function(menu, item, dir){
		if(typeof dir == 'undefined') dir = 'y';
		for (var i=0; i<menu.length; i++){
			mm = document.getElementById(menu[i]);
			sm = document.getElementById(item[i]);
			if (mm&&sm){
				mm.timer = null;
				mm.op = false;
				var width = Math.max(100, sm.offsetWidth) + 20;
				var height = sm.offsetHeight;
				var index = this.menus.length;
				this.menus[index] = mm;
				mm.index = index;
				mm.appendChild(sm);
				sm.style.width = width + 'px';
				sm.style.height = height + 'px'
				mm.sm = sm
				
				if(dir=='y'){
					sm.style.top = cj.util.getElementPos(mm).top + mm.offsetHeight + "px";
					sm.style.left = cj.util.getElementPos(mm).left + "px";
				}else{
					if(document.all){
						sm.style.left = mm.offsetWidth + "px";
						sm.style.top = mm.offsetTop + 'px';
					}else{
						sm.style.top = cj.util.getElementPos(mm).top + "px";
						sm.style.left = cj.util.getElementPos(mm).left + mm.offsetWidth + "px";
					}
				}
				
				
				mm.onmouseover = function(){ cj.dropdownMenu.show(this.index) }
				mm.onmouseout = function(ev){
					var ev = ev || window.event;
					var to = ev.relatedTarget || ev.toElement
					while(to != null){
						if(to == this){
							return false;
						}
						to = to.parentNode;
					}
					cj.dropdownMenu.hide(this.index);
						
				}
			}
		}
	},
	show : function(index){
		var mm = this.menus[index];
		mm.op = true;
		for(i=0; i<this.menus.length; i++){
			if(this.menus[i].op){
				this.menus[i].sm.style.visibility = 'visible';
			}else{
				clearTimeout(this.menus[i].timer);
				this.menus[i].sm.style.visibility = 'hidden';
			}
		}
	},
	hide : function(index){
		var mm = this.menus[index];
		mm.op = false;
		mm.timer = setTimeout(function(){mm.sm.style.visibility = 'hidden'}, 1000);
	}
}
cj.evt.add(window, "load", function(){
	var floater = document.getElementById("floater");
	var calBase = document.getElementById("calBase");
	if (floater && calBase){
		var top = 410;
		var left = -50;
		function setFloaterPos(){
			floater.pos = cj.util.getElementPos(calBase);
			floater.style.left = floater.pos.left + left + "px";
			floater.style.top = floater.pos.top + top + cj.util.getScrollPos().top + "px";
		}
		setFloaterPos();
		cj.evt.add(window, "scroll", function(){setFloaterPos()});
		cj.evt.add(window, "resize", function(){setFloaterPos()});
	}
});