﻿//init page
jQuery(function(){
	initNav();
	initTabs();
	initSlide();
	initTabsmenu();
	initOpenClose();
});

//init open-close faq
function initOpenClose() {
	jQuery('.item').OpenClose({
		activeClass:'active',
		opener:'a.title',
		slider:'.text-holder',
		effect:'slide',
		animSpeed:500
	});
}

//init main top navigation
function initNav(){
	var set = jQuery('#nav');
	var link = set.find('>li', set);
	var drop;
	var dropH;
	link.css({ position: 'relative' })
	link.each(function(){
		var _this = jQuery(this);
		_this.bind(
			'mouseenter',
			function(){
				drop = _this.find('>ul');
				drop.removeAttr('style');
				dropH = drop.outerHeight(true);
				drop.css({ left: 0, height: 0 }).animate({ height: dropH }, { queue: false, duration: 250 });
			}
		)
		_this.bind(
			'mouseleave',
			function(){
				drop.animate({ height: 0 }, { queue: false, duration: 250 });
			}
		)
	})
	
}

//init tabs home page
function initTabs(){
	jQuery('ul.tabset').each(function(){
		var _list = $(this);
		var _links = _list.find('a.tab');
		var _tabHeight = 0;
		_links.each(function() {
			var _link = $(this);
			var _href = _link.attr('href');
			var _tab = $(_href);
			var _closeLink = _tab.find('.close');

			if(_link.hasClass('active')) _tab.show();
			else _tab.hide();

			_link.click(function(){
				if(!jQuery(this).hasClass('active')){
					_links.filter('.active').each(function(){
						$($(this).removeClass('active').attr('href')).hide();
					});
					_link.addClass('active');
					_tabHeight = _tab.outerHeight(true);
					_list.parents('.tabset-wrapper').animate({
						height: _tabHeight + _list.outerHeight(true)+1,
						marginTop: -_tabHeight-27
					},{ queue: false, duration: 500 })
					_tab.show();
					_list.addClass('opened');
				}
				return false;
			});
			_closeLink.bind('click', function(){
				_links.removeClass('active');
				_list.removeClass('opened');
				_list.parents('.tabset-wrapper').animate({
					height: _list.outerHeight(true),
					marginTop: -27
				},{ queue: false, duration: 500, complete: function(){
					_tab.hide();
					_list.parents('.tabset-wrapper').removeAttr('style');
				}})
				return false;
			})
		});
	});
}

//init open-close learn more
function initSlide(){
	var speed = 500;
	var click = false;
	jQuery('.slide-box').each(function(){
		var _this = jQuery(this);
		_this.append(jQuery('<span class="more-close"><a href="#">Close</a></span>'));
		var height = _this.height();
		_this.css({
			display: 'none',
			height: 0
		});
		var btn = _this.parent().find('.more > a');
		btn.click(function(){
			var _thisBtn = jQuery(this);
			if(click){
				jQuery('.slide-box').animate({ height: 0 },{queue: false,duration: speed});
				jQuery('.slide-box').parent().find('.more').slideDown(speed);
			}
			_this.css({display: 'block'}).animate({ height: height},{ queue: false, duration: speed});
			_thisBtn.parent().hide();
			click = true;
			return false;
		})
		var btnClose = _this.find('.more-close >a');
		btnClose.click(function(){
			_this.animate({ height: 0 },{queue: false,duration: speed});
			btn.parent().slideDown(speed);
			click = false;
			return false;
		})
	})
}

// init sidebar tabsmenu 
function initTabsmenu()
{
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)
	{
		if (sets[i].className.indexOf("tabmenu") != -1)
		{
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++)
			{
				if (links[j].className.indexOf("tab") != -1)
				{
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";

					links[j].onclick = function ()
					{
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							for (var i = 0; i < this.tabs.length; i++)
							{
								var tab = document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1));
								if (tab)
								{
									tab.style.display = "none";
								}
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";
							c.style.display = "block";
							return false;
						}
					}
				}
			}
		}
	}
}

// open-close plugin
jQuery.fn.OpenClose = function(_options){
	// default options
	var _options = jQuery.extend({
		activeClass:'active',
		opener:'.opener',
		slider:'.slide',
		animSpeed: 400,
		animStart:false,
		animEnd:false,
		effect:'fade',
		event:'click'
	},_options);

	return this.each(function(){
		// options
		var _holder = jQuery(this);
		var _slideSpeed = _options.animSpeed;
		var _activeClass = _options.activeClass;
		var _opener = jQuery(_options.opener, _holder);
		var _slider = jQuery(_options.slider, _holder);
		var _animStart = _options.animStart;
		var _animEnd = _options.animEnd;
		var _effect = _options.effect;
		var _event = _options.event;
		if(_slider.length) {
			_opener.bind(_event,function(){
				if(!_slider.is(':animated')) {
					if(typeof _animStart === 'function') _animStart();
					if(_holder.hasClass(_activeClass)) {
						_slider[_effect=='fade' ? 'fadeOut' : 'slideUp'](_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
						_holder.removeClass(_activeClass);
					} else {
						_holder.addClass(_activeClass);
						_slider[_effect=='fade' ? 'fadeIn' : 'slideDown'](_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
					}
				}
				return false;
			});
			if(_holder.hasClass(_activeClass)) _slider.show();
			else _slider.hide();
		}
	});
}
