function urlencode(url) {
	var res = '';
	for(var i = 0; i < url.length; i ++) {
		res += (url.charCodeAt(i) > 255) ? encodeURIComponent(url.charAt(i)) : url.charAt(i);
	}
	return res;
}

function parseArgs(arg) {
	var rows = arg.split(/,/);
	var args = {};
	for(var i = 0; i < rows.length; i ++) {
		var row = rows[i].split(/:/);
		args[row[0]] = row[1];
	}
	return args;
}

function XenoMouseOver() {
	$(this).addClass('XenoMouseOver');
	this.blur();
}

function XenoMouseOut() {
	$(this).removeClass('XenoMouseOver');
	this.blur();
}

function XenoSubmit() {
	var frm;
	$(this).closest('form').each(function () {
		frm = this;
	});
	if (frm.onsubmit) {
		if (!frm.onsubmit()) return false;
	}
	$(frm).submit();
	return false;
}

function XenoMemo() {
	var $$ = $(this);
	$$.removeClass('XenoMemo');
	if(this.defaultText) {
		this.value = this.defaultText;
		this.defaultText = '';
	} else {
		this.value = '';
	}
	$$.unbind('focusin', XenoMemo);
	$$.caret(this.value.length);
}

function XenoShowLayerClick() {
	try {
		var $$ = $(this);
		var self = this;
		var showed = false;
		var clsed = false;
		var idx = -1;
		var $$menu = this.menu;
		var $$layer = this.layer;
		$$menu.each(function (i) {
			if (self == this) idx = i;
		});
		$$layer.each(function (i) {
			if ($(this).css('display') != 'none') {
				showed = true;
				if (idx == i) clsed = true;
			}
		});
		$$menu.removeClass('XenoMouseOver');
		$$.blur();
		if (clsed && this.showIndex == -1) {
			$$layer.slideUp();
			return false;
		}
		$$layer.hide();
		if (idx >= 0) {
			if (this.showIndex != -1) {
				$$layer.eq(idx).show();
				$$.addClass('XenoMouseOver');
			} else if (showed) {
				$$layer.eq(idx).show();
				$$.addClass('XenoMouseOver');
			} else if (!closed) {
				$$layer.eq(idx).slideDown();
				$$.addClass('XenoMouseOver');
			}
			if (this.callback) this.callback(idx);
		}
	} catch(e) {
		alert(e.message);
	}
	return false;
}

function XenoShowLayer(menu, layer, showIndex, callback) {
	if (!showIndex && showIndex != 0) showIndex = -1;
	var $$layer = $(layer);
	$$layer.each(function (i) {
		if (i == showIndex) {
			$(this).show();
		} else {
			$(this).hide();
		}
	});
	var $$menu = $(menu);
	$$menu.each(function (i) {
		this.menu = $$menu;
		this.layer = $$layer;
		if (callback) this.callback = callback;
		this.showIndex = showIndex;
		if (i == showIndex) {
			$(this).addClass('XenoMouseOver');
		}
	});
	$$menu.click(XenoShowLayerClick);
}

/*
function XenoSpinnerUp() {
	var input = this.parentNode.input;
	var value = input.value.replace(/[^\d\-]+/g, '') * 1 + 1;
	if (input.max == 0 || input.max) {
		if (value > input.max) value = input.max;
	}
	input.value = value;
	return false;
}

function XenoSpinnerDown() {
	var input = this.parentNode.input;
	var value = input.value.replace(/[^\d\-]+/g, '') * 1 - 1;
	if (input.min == 0 || input.min) {
		if (value < input.min) value = input.min;
	}
	input.value = value;
	return false;
}

function XenoSpinner() {
	var self = this;
	var params = this.title.split(/;/);
	var i;
	for (i = 0; i < params.length; i++) {
		var param = params[i].split(/:/);
		switch (param[0]) {
		case 'max':
			this.max = param[1] * 1;
			break;
		case 'min':
			this.min = param[1] * 1;
			break;
		}
	}
	this.title = '';
	$(this).css({
		'float': 'left'
	}).wrap('<div class="XenoSpinner"></div>');
	var spinner = $('<div class="XenoSpinnerButtons"></div>').each(function () {
		this.input = self;
	}).insertAfter(this);
	$('<a href="#" class="XenoSpinnerUp">▲</a>').click(XenoSpinnerUp).appendTo(spinner);
	$('<a href="#" class="XenoSpinnerDown">▼</a>').click(XenoSpinnerDown).appendTo(spinner);
}
*/

function XenoRolling() {
	var args = {'effect':'left', 'easing':'linear', 'duration':'normal', 'delay':1000, 'loop':'true'};
	if(!!this.title) {
		try {
			args = parseArgs(this.title);
		} catch(e) {}
	}
	this.title = '';
	if(!args.effect) args.effect = 'left';
	if(!args.easing) args.easing = 'linear';
	if(!args.duration) args.duration = 'fast';
	if(!args.delay) args.delay = 1000;
	if(!args.loop) args.loop = 'true';
	this.args = args;

	this.XenoRollingFlag = true;
	$(this).each(XenoRollingSet);
	var self = this;
	if(!!args.button) {
		$('a[name="' + args.button + '"]').click(function() {
			self.args.effect = this.hash.replace(/^#/, '');
			if(!self.XenoRollingFlag && self.args.loop == 'false') $(self).each(XenoRollingDo);
			return false;
		});
	}
	if(!this.XenoRollingFlag) return;
	if(args.loop == 'false') return;
	
	$(this).mouseenter(function() { this.XenoRollingStop = true; }).mouseleave(function() { this.XenoRollingStop = false; });
	this.XenoRollingStop = false;
	$(this).oneTime(this.args.delay, function() { $(this).each(XenoRollingDo); });
}

function XenoRollingSet() {
	var self = this;
	var size = 0;
	switch(this.args.effect) {
		case 'left':
		case 'right':
			size = $(this.parentNode).width();
			break;
		case 'up':
		case 'down':
			size = $(this.parentNode).height();
			break;
	}
	
	if(size == 0) { this.XenoRollingFlag = false; return; }
	
	var child = $('>:first', this)[0];
	var cnt = $('>*', this).length;
	var childSize = 0;
	switch(self.args.effect) {
		case 'left':
		case 'right':
			childSize = $(child).outerWidth(true);
			break;
		case 'up':
		case 'down':
			childSize = $(child).outerHeight(true);
			break;
	}
	if(childSize * cnt <= size) { this.XenoRollingFlag = false; return; }
	
	var visiblecnt = Math.ceil(size / childSize) + 4;
	if(cnt <= visiblecnt) { this.XenoRollingDiv = false; return }
	
	this.XenoRollingDiv = $('<div style="display:none;"></div>').appendTo(document.body)[0];
	$(this.XenoRollingDiv).append($('>*:gt(' + visiblecnt + ')', this));
}

function XenoRollingDo() {
	if(!!this.XenoRollingStop) {
		if(this.args.loop == 'true') $(this).oneTime(this.args.delay, function() { $(this).each(XenoRollingDo); });
		return;
	}
	if($(this).offset().left == 0) {
		if(this.args.loop == 'true') $(this).oneTime(this.args.delay, function() { $(this).each(XenoRollingDo); });
		return;
	}

	switch(this.args.effect) {
		case 'left':
			var atag = $('>:first', this);
			var size = atag.outerWidth(true);
			$(this).animate({'marginLeft':'-' + size + 'px'},
			{
				'queue':false,
				'duration': this.args.duration,
				'easing': this.args.easing,
				'complete': function () {
					if(!!this.XenoRollingDiv) {
						$(this.XenoRollingDiv).append(atag);
						$(this).append($('>:first', this.XenoRollingDiv)).css({'marginLeft':'0px'});
					} else {
						$(this).append(atag).css({'marginLeft':'0px'});
					}
					if(this.args.loop == 'true') $(this).oneTime(this.args.delay, function() { $(this).each(XenoRollingDo); });
				}
			});
			break;
		case 'right':
			var atag = $('>:last', this);
			var size = atag.outerWidth(true);
			$(this).css({'marginLeft':'-' + size + 'px'});
			if(!!this.XenoRollingDiv) {
				$(this.XenoRollingDiv).prepend(atag);
				$(this).prepend($('>:last', this.XenoRollingDiv));
			} else {
				$(this).prepend(atag).css({'marginLeft':'-' + size + 'px'});
			}
			$(this).animate({'marginLeft':'0px'},
			{
				'queue':false,
				'duration': this.args.duration,
				'easing': this.args.easing,
				'complete': function () {
					if(this.args.loop == 'true') $(this).oneTime(this.args.delay, function() { $(this).each(XenoRollingDo); });
				}
			});
			break;
		case 'up':
		case 'down':
			size = atag.outerHeight(true);
			break;
	}
}

function XenoBlink() {
	$(this).animate({
		opacity: 0.5
	},
	function () {
		$(this).animate({
			opacity: 1
		});
	});
}

function XenoResize() {
	this.cnt = ++this.cnt || 0;
	var w = $(this).width() - document.documentElement.clientWidth;
	var h = $(this).height() - document.documentElement.clientHeight;
	window.resizeBy(w, h);
	if (this.cnt > 5) return;
	if (w != document.documentElement.clientWidth || h != document.documentElement.clientHeight) $(this).oneTime(100, XenoResize);
}

function XenoLayer() {
	var args = {'effect':'normal', 'easing':'linear', 'duration':'normal'};
	if(!!this.title) {
		try {
			args = parseArgs(this.title);
		} catch(e) {}
	}
	this.title = '';
	if(!args.effect) args.effect = 'left';
	if(!args.easing) args.easing = 'linear';
	if(!args.duration) args.duration = 'fast';
	
	this.args = args;
	
	$(this).click(XenoLayerClick);
}

function XenoLayerClick() {
	if($(this).hasClass('XenoMouseOver')) {
		$(this).removeClass('XenoMouseOver');
		switch(this.args.effect) {
			case 'fade':
				$(this.hash).fadeOut(this.args.duration, this.args.easing);
				break;
			case 'slide':
				$(this.hash).slideUp(this.args.duration, this.args.easing);
				break;
			case 'normal':
			default:
				$(this.hash).hide();
				break;
		}
	} else {
		$(this).addClass('XenoMouseOver');
		switch(this.args.effect) {
			case 'fade':
				$(this.hash).fadeIn(this.args.duration, this.args.easing);
				break;
			case 'slide':
				$(this.hash).slideDown(this.args.duration, this.args.easing);
				break;
			case 'normal':
			default:
				$(this.hash).show();
				break;
		}
	}
	return false;
}

function XenoTab() {
	var args = {'effect':'normal', 'easing':'linear', 'duration':'normal'};
	if(!!this.title) {
		try {
			args = parseArgs(this.title);
		} catch(e) {}
	}
	this.title = '';
	if(!args.effect) args.effect = 'left';
	if(!args.easing) args.easing = 'linear';
	if(!args.duration) args.duration = 'fast';
	
	var box = this;

	$('.XenoTabMenu', this).each(function() {
		var menu = this;
		$('a', this).click(function() {
			if(!box.XenoTabNow) {
				var a = this;
				$('a', menu).removeClass('XenoMouseOver');
				$(this).addClass('XenoMouseOver');
				var minus = 1;
				switch(args.effect) {
					case 'scrollleft':
						minus = -1;
					case 'scrollright':
						var target = $(a.hash)[0];
						box.XenoTabNow = true;
						var size = $(target).width();
						$('.XenoTabLayer:visible', box).each(function() {
							if(this == target) return;
							$(this).animate({'marginLeft':size * minus}, {'easing':args.easing, 'duration':args.duration, 'complete':function() {
								$(this).hide().css({'marginLeft':0});
							}});
						});
						$(target).show().css({'marginLeft':- size * minus}).animate({'marginLeft':0}, {'easing':args.easing, 'duration':args.duration,
							'complete':function() { box.XenoTabNow = false; }
						});
						break;
					case 'scrollup':
						var minus = -1;
					case 'scrolldown':
						var target = $(a.hash)[0];
						box.XenoTabNow = true;
						var size = $(target).height();
						$('.XenoTabLayer:visible', box).each(function() {
							if(this == target) return;
							$(this).animate({'marginTop':size * minus}, {'easing':args.easing, 'duration':args.duration, 'complete':function() {
								$(this).hide().css({'marginTop':0});
							}});
						});
						$(target).show().css({'marginTop':- size * minus}).animate({'marginTop':0}, {'easing':args.easing, 'duration':args.duration,
							'complete':function() { box.XenoTabNow = false; }
						});
						break;
					case 'fade':
						var target = $(a.hash)[0];
						box.XenoTabNow = true;
						$('.XenoTabLayer:visible', box).each(function() {
							if(this == target) return;
							$('.XenoTabLayer:visible', box).fadeOut({'easing':args.easing, 'duration':args.duration});
						});
						$(target).fadeIn({'easing':args.easing, 'duration':args.duration,
							'complete':function() { box.XenoTabNow = false; }
						});
						break;
					case 'normal':
					default:
						var target = $(a.hash)[0];
						$('.XenoTabLayer:visible', box).each(function() {
							if(this == target) return;
							$(this).hide();
						});
						$(target).show();
						break;
				}
			}
			this.blur();
			return false;
		});
	});
}

$(window).load(function() {
	$('.XenoRolling').oneTime(100, XenoRolling);
});

$(function() {
	$('a.XenoLayerButton').each(XenoLayer);
	$('div.XenoTab').each(XenoTab);

	$('a.XenoNewWin').each(function() {
		this.opt = 'width=500px,height=500px,resizable=yes,scrollbars=no';
		if(!!this.title) {
			if(this.title.charAt(0) == '!') this.opt = this.title.replace(/^\!/, '');
			this.title = '';
		}
	}).click(function() {
		window.open(this.href, this.target, this.opt);
		return false;
	});

	if(!(/mobile/i.test(navigator.userAgent))) {
		$('a.XenoRollOverButton, button.XenoRollOverButton').mouseover(XenoMouseOver).mouseout(XenoMouseOut);
		$('a.XenoClickButton, button.XenoClickButton').toggle(XenoMouseOver, XenoMouseOut);
		try {
			$('input.XenoButton, a.XenoButton, button.XenoButton').button();
			$('input.XenoDateTime').each(function(i) {
				if(!this.id) this.id = 'atpdt' + i;
				$(this).AnyTime_picker(AnyTimePickerDateTimeDefaultOptions);
			});
			$('input.XenoDate').each(function(i) {
				if(!this.id) this.id = 'atpd' + i;
				$(this).AnyTime_picker(AnyTimePickerDateDefaultOptions);
			});
		} catch(e) {}
	}

	$('a.XenoSubmit').click(XenoSubmit);
	$('input.XenoMemo, textarea.XenoMemo').each(function () {
		if (this.alt) {
			this.defaultText = this.alt;
			this.alt = '';
		}
		$(this.form).submit(function() {
			$('input.XenoMemo, textarea.XenoMemo', this).val('');
		});
		$(this).focusin(XenoMemo);
	});
	//$('input.XenoPaddingFix').each(XenoPaddingFix);
	//$('input.XenoSpinner').each(XenoSpinner);
	//$('img.XenoBlink').everyTime(3000, XenoBlink);
	try {
		$('a.PrintButton').click(function() {
				$('div.PrintArea').printArea({'popWd':'700px','popHt':'1000px','mode':'popup'});
		});
	} catch(e) {}
	try {
		$('a.LightBox').lightBox({
			imageLoading: '/skin/default/script/lightbox/images/lightbox-ico-loading.gif',
			imageBtnClose: '/skin/default/script/lightbox/images/lightbox-btn-close.gif',
			imageBtnPrev: '/skin/default/script/lightbox/images/lightbox-btn-prev.gif',
			imageBtnNext: '/skin/default/script/lightbox/images/lightbox-btn-next.gif',
			imageBlank: '/_.gif'
		});
	} catch(e) {}
});

