function check(obj) {
	return !!(obj || obj === 0);
}

/* Codes below depends on jQuery */
jQuery.noConflict();
(function ($) {
	window.globalNav = function (mi, si, options) {
		var settings = $.extend({
				speed: 100,
				delay: 2000
			}, options),
			$globalNav = $('#globalNav'),
			$items = $globalNav.find('> ul > li'),
			timer;
		init();
		$items.bind('mouseenter', function () {
			var $this = $(this),
				$subMenu = this.$subMenu;
			if ($subMenu.length) {
				open();
			} else {
				closeAll();
			}
			$items.removeClass('globalnav-on');
			$this.addClass('globalnav-on');
		});
		$globalNav.bind('mouseenter', function () {
			window.clearTimeout(timer);
			timer = null;
		});
		$globalNav.bind('mouseleave', function () {
			timer = window.setTimeout(function () {
				openInit();
			}, settings.delay);
		});
		function closeAll() {
			if ($globalNav.hasClass('globalnav-sub-open')) {
				$globalNav.animate({'height': '34px'}, settings.speed, function () {
					$(this).removeClass('globalnav-sub-open');
				});
			}
			$items.removeClass('globalnav-on');
		}
		function open() {
			$globalNav.addClass('globalnav-sub-open').animate({'height': '55px'}, settings.speed);
		}
		function openInit() {
			var main,
				sub;
			if (mi && (mi > 0)) {
				main = $items[(mi - 1)];
				if (main) {
					if (main.$subMenu.length && !$globalNav.hasClass('globalnav-sub-open')) {
						open();
					}
					if (si && (si > 0) && main.$subMenu && main.$subMenu.length) {
						sub = main.$subMenu.find('> li')[(si - 1)];
						$(sub).addClass('globalnav-on');
					} else {
						closeAll();
					}
					$items.removeClass('globalnav-on');
					$(main).addClass('globalnav-on');
				}
			} else {
				closeAll();
			}
		}
		function init() {
			$items.each(function () {
				this.$subMenu = $(this).find('> ul');
			});

			openInit();
		}
	};

	window.initSubNav = function () {
		var parent = $('#subNav > ul'),
			len = arguments.length,
			i = 0,
			items,
			index,
			selected;
		if (!len) {
			return false;
		}
		for (; i < len; i++) {
			if (parent.length) {
				items = $('> li', parent);
				index = arguments[i] - 1;
				if (items.length && !!(index || index === 0)) {
					selected = items[index];
					if ($(selected).hasClass('has-child')) {
						$(selected).addClass('subnav-sub-on');
					} else {
						$(selected).addClass('subnav-on');
					}
					parent = $('> ul', selected);
				}
			}
		}
		subNavHover();
	}

	function subNavHover() {
		var items = $('#subNav li.has-child');
		items.bind('mouseenter', function () {
			$(this).addClass('subnav-hover');
		}).bind('mouseleave', function () {
			$(this).removeClass('subnav-hover');
		});
	}

	$.fn.tabs = function (options) {
		var settings = $.extend({
				tabs: 'li a',
				index: 0,
				active: 'tab-on',
				title: '¼±ÅÃµÊ',
				rolling: false,
				imgOnOff: false,
				delay: 3000
			}, options),
			url = document.location.href.split('#');
		return this.each(function () {
			var self = this,
				tabs = $(self).find(settings.tabs);
			self.index = settings.index;
			self.selectTab = function (index) {
				var this_tab = tabs[index],
					tab_img = $('img', this_tab);
				self.index = index;
				tabs.each(function () {
					var img = $('img', this);
					if (img.length && settings.imgOnOff) {
						img.attr('src', function () {
							var src = this.getAttribute('src');
							return src.replace('_on.', '_off.');
						});
					}
					$(this).parent().removeClass(settings.active).removeAttr('title');
					this.content.removeClass(settings.active);
				});
				if (tab_img.length && settings.imgOnOff) {
					tab_img.attr('src', function () {
						var src = this.getAttribute('src');
						return src.replace('_off.', '_on.');
					});
				}
				$(this_tab).parent().addClass(settings.active).attr('title', settings.title);
				this_tab.content.addClass(settings.active);
			};
			init();
			function roll() {
				self.timer = window.setInterval(function () {
					self.index = (++self.index >= tabs.length) ? 0 : self.index;
					self.selectTab(self.index);
				}, settings.delay);
			}
			function init() {
				var cont_id = url[1];
				tabs.each(function () {
					this.content = $(this.getAttribute('href', 2));
				});
				self.selectTab(self.index);
				tabs.bind('click', function (e) {
					e.preventDefault();
					self.selectTab(tabs.index(this));
				});
				if (cont_id && document.getElementById(cont_id)) {
					tabs.filter('[href="#' + cont_id + '"]').click();
				}
				if (settings.rolling) {
					roll();
					$(self).bind('mouseenter', function () {
						window.clearInterval(self.timer);
						self.timer = null;
					}).bind('mouseleave', function () {
						roll();
					});
				}
			}
		});
	};

	/* On DOM Ready */
	$(document).ready(function () {
	});
}(jQuery));
