// DOM ready
jQuery(function(){
	initTabs();
	initChangeLang();
	loadLang();
});

function loadLang(){
	if(jQuery.cookie('language')){
		var lang = jQuery.cookie('language');
		var links = jQuery('ul.lang a');
		var link = links.filter('[title='+lang+']');
		var currTab = jQuery('#'+link.attr('rel'));
		var par = currTab.parent();
		var otherTabs = par.find('div.tab-lang');
		links.removeClass('active');
		link.addClass('active')
		otherTabs.hide();
		currTab.show();
		jQuery('a.opener').hide();
		jQuery('a.oth-lang').show();
	}
	else{
		jQuery('a.oth-lang').hide();
	}
}

// tabs init
function initTabs() {
	/* jQuery('ul.tabset1').jqueryTabs({
		addToParent:false,
		holdHeight:false,
		activeClass:'active',
		tabLinks:'a.tab',
		fadeSpeed:0,
		event:'click'
	}); */
	jQuery('ul.tabset2').jqueryTabs({
		addToParent:false,
		holdHeight:false,
		activeClass:'active',
		tabLinks:'a.tab',
		fadeSpeed:0,
		event:'mouseenter'
	});
}

// open close
function initChangeLang(){
	var a;
	var othLang = jQuery('a.oth-lang');
	jQuery('a.opener').each(function(){
		var link = jQuery(this);
		var box = jQuery(link.attr('href'));
		box.css({opacity:0, 'display':'block'});
		link.click(function(){
			a = jQuery(this);
			if(link.hasClass('active')){
				hide(link, box);
			}
			else{
				link.addClass('active');
				box.animate({opacity:1, top:0}, {queue:false, duration: 1000});
			}
			return false;
		});
	});
	jQuery('ul.lang a.tab').click(function(){
		hide(a, jQuery(a.attr('href')));
		jQuery.cookie('language', jQuery(this).attr('title'));
		othLang.show();
		a.hide();
	});
	othLang.click(function(){
		jQuery('a.opener').trigger('click');
		return false;
	});
	function hide(link, box){
		link.removeClass('active');
		box.animate({opacity:0, top:'100%'}, {queue:false, duration: 1000});
	}
}

// jquery tabs plugin
jQuery.fn.jqueryTabs = function(_options){
	// default options
	var _options = jQuery.extend({
		addToParent:false,
		holdHeight:false,
		activeClass:'active',
		tabLinks:'a.tab',
		fadeSpeed:300,
		event:'click'
	},_options);

	return this.each(function(){
		var _holder = jQuery(this);
		var _fadeSpeed = _options.fadeSpeed;
		var _activeClass = _options.activeClass;
		var _addToParent = _options.addToParent;
		var _holdHeight = _options.holdHeight;
		var _tabLinks = jQuery(_options.tabLinks, _holder);
		var _tabset = (_addToParent ? _tabLinks.parent() : _tabLinks);
		var _event = _options.event;
		var _animating = false;

		// tabs init
		_tabLinks.each(function(){
			var _tmpLink = jQuery(this);
			var _tmpTab = jQuery('#'+_tmpLink.attr('rel'));
			var _classItem = (_addToParent ? _tmpLink.parent() : _tmpLink);
			if(_tmpTab.length) {
				if(_classItem.hasClass(_activeClass)) _tmpTab.show();
				else _tmpTab.hide();
			}
		});

		// tab switcher
		function switchTab(_switcher) {
			if(!_animating) {
				var _link = jQuery(_switcher);
				var _newItem = (_addToParent ? _link.parent() : _link);
				var _newTab = jQuery('#'+_link.attr('rel'));
				if(_newItem.hasClass(_activeClass)) return;

				var _oldItem = jQuery(_addToParent ? _tabset : _tabLinks).filter('.'+_activeClass);
				var _oldTab = jQuery('#'+ jQuery(_addToParent ? _oldItem.children('a') : _oldItem).attr('rel'));
				if(_newTab.length) {
					_animating = true;
					if(_oldItem.length) {
						_newItem.addClass(_activeClass);
						_oldItem.removeClass(_activeClass);

						var _parent = _oldTab.parent();
						if(_holdHeight) _parent.css({height:_parent.height()});

						_oldTab.fadeOut(_fadeSpeed,function(){
							_newTab.fadeIn(_fadeSpeed,function(){
								_animating = false;
							});
							if(_holdHeight) _parent.css({height:'auto'});
						});
					} else {
						_newItem.addClass(_activeClass);
						_newTab.fadeIn(_fadeSpeed,function(){
							_animating = false;
						});
					}
				}
			}
		}
		
		function closeTab(_switcher){
			if(!_animating) {
				var _link = jQuery(_switcher);
				var _newItem = (_addToParent ? _link.parent() : _link);
				var _newTab = jQuery('#'+_link.attr('rel'));
				_newItem.removeClass(_activeClass);
				_newTab.hide();
			}
		}

		// control
		_tabLinks.each(function(){
			if(_event == 'mouseenter'){
				jQuery(this).bind({
					'mouseenter': function(){
						switchTab(this);
					},
					'mouseleave': function(){
						closeTab(this);
					}
				});
			}
			else{
				jQuery(this).bind(_event, function(){
					switchTab(this);
					return false;
				});
			}
		});
	});
}

jQuery.cookie = function (key, value, options) {
	if (arguments.length > 1 && String(value) !== "[object Object]") {
		options = jQuery.extend({}, options);
		if (value === null || value === undefined) options.expires = -1;
		if (typeof options.expires === 'number') {
			var days = options.expires, t = options.expires = new Date();
			t.setDate(t.getDate() + days);
		}
		value = String(value);
		return (document.cookie = [
			encodeURIComponent(key), '=',
			options.raw ? value : encodeURIComponent(value),
			options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		].join(''));
	}
	// key and possibly options given, get cookie...
	options = value || {};
	var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
	return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
