function initTabs(){
	var _tabsArea = $('#tabs-area');
	var _links = _tabsArea.find('.additional-nav a');
	var _tabsContainer = _tabsArea.find('.tab-content:eq(0)');
	var _firstActive, _nowActive, _href;
	
	_links.each(function(){
		_link = $(this);
		if (_link.hasClass('active') && _tabsContainer.children().length == 0) {
			_href = _link.attr('href');
			var _num = _links.index(_link);
			$.ajax({url: _href, type:'POST', success: function(html){
					var _tab = $(html).appendTo(_tabsContainer);
					_tab.attr('rel', _num);
				}
			});
		}
		else if (_link.hasClass('active') && _tabsContainer.children().length != 0) {
			var _num = _links.index(_link);
			_tabsContainer.children().eq(0).attr('rel', _num);
		}
		_link.click(function(){
			if (!$(this).hasClass('active')){
				_links.each(function(){
					if ($(this).hasClass('active')) _nowActive = _links.index($(this));
				});
				_links.eq(_nowActive).removeClass('active');
				$(this).addClass('active');
				_nowActive = _links.index($(this));
				if (_tabsContainer.find('div[rel=' +_nowActive+ ']').length) {
					_tabsContainer.children().hide();
					_tabsContainer.find('div[rel=' +_nowActive+ ']').show();
				}
				else {
					_href = $(this).attr('href');
					$.ajax({
						url: _href,
						cache: false,
						success: function(html){
							_tabsContainer.children().hide();
							var _tab = _tabsContainer.append(html).children(':visible');
							_tab.attr('rel', _nowActive);
						}
					});
				}
			}
			return false;
			
		})
	})
	
}

/*--- main navigation ---*/
function initMenu(){
	if($.browser.msie && $.browser.version < 7){
		$('#nav li').mouseenter(function(){
			$(this).addClass('hover');
			if($(this).children('div.drop').length) hideSelectBoxes($(this).children('div.drop'));
		}).mouseleave(function(){
			$(this).removeClass('hover');
			if($(this).children('div.drop').length) showSelectBoxes($(this).children('div.drop'));
		});
	}
}
/*--- gallery ---*/
function initGallery(){
	var stay_time = 5000; //in ms or 'false' if not needed
	var change_speed = 600; //in ms
	$('div.gallery').each(function(){
		var _hold = $(this);
		var list_hold = _hold.find('ul.gallery-items');
		var _list = list_hold.children();
		var btn_prev = _hold.find('a.link-prev');
		var btn_next = _hold.find('a.link-next');
		var btn_play = _hold.find('a.link-pause');
		var _a = (_list.index(_list.filter('.active:eq(0)')) != -1)?(_list.index(_list.filter('.active:eq(0)'))):(0);
		var _f = true, _t;
		
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		var _btn = $('<ul></ul>');
		for(var i = 0; i < _list.length; i++){
			_btn.append('<li><a href="#">'+(i+1)+'</a></li>');
		}
		_hold.find('div.swicher').append(_btn);
		_btn = _btn.find('a');
		_btn.eq(_a).addClass('active');
		if(_f) btn_play.addClass('pause');
		else  btn_play.removeClass('pause');
		
		btn_prev.click(function(){
			if(_a > 0) changeEl(_a - 1);
			else changeEl(_list.length - 1);
			return false;
		});
		btn_next.click(function(){
			if(_a < _list.length - 1) changeEl(_a + 1);
			else changeEl(0);
			return false;
		});
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		btn_play.click(function(){
			if(_f){
				btn_play.removeClass('pause');
				_f = false;
				if(_t) clearTimeout(_t);
			}
			else{
				btn_play.addClass('pause');
				_f = true;
				if(_t) clearTimeout(_t);
				if(_f && stay_time){
					_t = setTimeout(function(){
						if(_a < _list.length - 1) changeEl(_a + 1);
						else changeEl(0);
					}, stay_time);
				}
			}
			return false;
		});
		if(_f && stay_time){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, stay_time);
		}
		
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				_list.eq(_a).removeClass('active').animate({opacity:0}, {queue:false, duration: change_speed});
				_list.eq(_ind).addClass('active').animate({opacity:1}, {queue:false, duration: change_speed});
				_a = _ind;
			}
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time+change_speed);
			}
		}
	});
}
$(document).ready(function(){
	initMenu();
	initGallery();
	initTabs();
});

/*--- show/hode select's ---*/
function hideSelectBoxes(object) {
	if ($.browser.msie && $.browser.version < 7) {
		var selects_list = $('select');
		object.each(function() {
			var _el = $(this);
			var t = _el.offset().top;
			var l = _el.offset().left;
			var w = _el.outerWidth();
			var h = _el.outerHeight();
			var el_selects = [];
			selects_list.filter(':visible').not(_el.find('select')).each(function(){
				var _select = $(this);
				var s_t = _select.offset().top;
				var s_l = _select.offset().left;
				var s_w = _select.outerWidth();
				var s_h = _select.outerHeight();
				var _ver = false, _hor = false;
				if((t - s_t > 0) ? (t - s_t < s_h) : (t - s_t + h > 0)) _ver = true;
				if((l - s_l > 0) ? (l - s_l < s_w) : (l - s_l + w > 0)) _hor = true;
				if(_ver && _hor) {
					_select.css('visibility', 'hidden');
					el_selects.push(this);
				}
			});
			this.sboxes = el_selects;
		});
	}
}
function showSelectBoxes(object) {
	if($.browser.msie && $.browser.version < 7){
		object.each(function() {
			if(this.sboxes.length > 0) {
				$(this.sboxes).css('visibility','visible');
			}
		});
	}	
}
