$(document).ready(function(){
	initTabs();
	initEvents();
	ieHover('#menu > li');
	fadeGall();
});
function fadeGall(){
	var _hold = $('.featured-stories');
	var hold_list = _hold.find('.stories');
	var _list = hold_list.children();
	var _btn = _hold.find('.pagination ul a');
	var btn_prev = _hold.find('.pagination .prev');
	var btn_next = _hold.find('.pagination .next');
	var btn_play = _hold.find('.pagination .pause');
	var _timer;
	var _f = false;
	var _active = 0;
	var change_time = 6000;
	var change_speed = 800;
	_btn.each(function(_ind){
		if($(this).parent().hasClass('active')) _active = _ind;
		this.onclick = function(){
			_changeEl(_ind);
			return false;
		}
	});
	hold_list.css({
		position: 'relative',
		overflow: 'hidden'
	});
	_list.css({
		position: 'absolute',
		top: 0,
		left: 0,
		zIndex: 1,
		opacity: 0
	});
	_list.eq(_active).css({
		position: 'relative',
		zIndex: 2,
		opacity: 1
	});
	if(!btn_play.parent().hasClass('play')) _f = true;
	if(_f) { _timer = setTimeout(function(){ _nextEl();}, change_time);}
	_btn.parent().removeClass('active').eq(_active).addClass('active');
	
	//function nextEl
	function  _nextEl(){
		var _ind = _active;
		if(_ind < _list.length -1 ){ _ind++;}
		else{ _ind = 0;}
		_changeEl(_ind);
	}
	btn_next.click(function(){
		var _ind = _active;
		if(_ind < _list.length -1 ){ _ind++;}
		else{ _ind = 0;}
		_changeEl(_ind);
		return false;
	});
	btn_prev.click(function(){
		var _ind = _active;
		if(_ind > 0 ){ _ind--;}
		else{ _ind = _list.length -1;}
		_changeEl(_ind);
	});
	btn_play.click(function(){
		if(_f){
			if (_timer) { clearTimeout(_timer);}
			_f = false;
			$(this).parent().addClass('play');
		}
		else{
			$(this).parent().removeClass('play');
			_f = true;
			if (_timer) { clearTimeout(_timer);}
			_timer = setTimeout(function(){ _nextEl();}, change_time);
		}
		return false;
	});
	//function changeEl
	function _changeEl(_ind) {
		if(_ind != _active) {
			if (_timer) { clearTimeout(_timer);}
			hold_list.stop().animate({height: _list.eq(_ind).outerHeight()}, change_speed/2);
			_list.eq(_active).css({zIndex: 1, position: 'absolute'}).stop().animate({opacity: 0}, change_speed);
			_list.eq(_ind).css({zIndex: 2, position: 'relative'}).stop().animate({opacity: 1}, change_speed, function(){
				hold_list.height('auto');
			});
			_btn.eq(_active).parent().removeClass('active');
			_btn.eq(_ind).parent().addClass('active');
			_active = _ind;
			if(_f) { _timer = setTimeout(function(){ _nextEl();}, change_time);}
		}
	}
}
/*--- ie hover ---*/
function ieHover(_list) {
	if ($.browser.msie && $.browser.version < 7) {
		$(_list).hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
	}
}

/*--- tabs ---*/
function initTabs() {
	$('.tabset').each(function(_ind, _el) {
		var btn_h = $(_el);
		var _btn = $(_el).find('a.tab');
		var _a = 0;
		_btn.each(function(_ind, _el) {
			var _box = $('#'+_el.href.substr(_el.href.indexOf("#") + 1));
			if($(_el).hasClass('active')) {
				_box.show();
				_a = _ind;
			}
			else {
				_box.hide();
			}
			_el.onclick = function() {
				if(!$(this).hasClass('active')){
					$('#'+ _btn.get(_a).href.substr(_btn.get(_a).href.indexOf("#") + 1)).hide();
					_btn.eq(_a).removeClass('active');
					$('#'+ this.href.substr(this.href.indexOf("#") + 1)).show().height(230);
					$('.'+ this.href.substr(this.href.indexOf("#") + 1)).show();
					$(this).addClass('active');
					
					_a = _ind;
				}
				return false;
			}
		});
	});
}

/*--- event divs ---*/
function initEvents() {
	$('.date').each(function(_ind, _el) {
		var date_h = $(_el);
		var _date = $(_el).find('a.event');
		var _a = 0;
		_date.each(function(_ind, _el) {
			var _box = $('#'+_el.href.substr(_el.href.indexOf("#") + 1));
			//var _box = $(_el).find('div.event');
			if($(_el).hasClass('active')) {
				_box.show();
				_a = _ind;
			}
			else {
				_box.hide();
			}
			_el.onclick = function() {
				if(!$(this).hasClass('active')){
					$('#'+ _date.get(_a).href.substr(_date.get(_a).href.indexOf("#") + 1)).hide();
					_date.eq(_a).removeClass('active');
					$('#'+ this.href.substr(this.href.indexOf("#") + 1)).show().height(145);
					$(this).addClass('active');
					_a = _ind;
				}
				return false;
			}
		});
	});
}