/*--- news change function ---*/
function newsChange(){
	var stay_time = 6000; //in ms
	var change_speed = 800; //in ms
	$('div.news-rotate > ul').each(function(){
		var _hold = $(this);
		var _f = true;
		var _t;
		var _list = _hold.children();
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		
		_hold.mouseenter(function(){
			_f = false;
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			_f = true;
			if(_t) clearTimeout(_t);
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, stay_time);
		});
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_a != _ind){
				_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){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time);
			}
		}
		if(_f){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, stay_time);
		}
	});
	
	
}
var _id = 'slide-nav';
var _slideEl = 'div.slide';
var _duration = 500;
var _hide = false;

$(document).ready(function(){
	initTabs('.tabset');
	newsChange();
	$('#'+_id+' a.opener').click(function () {
		if (_hide) hideLevels(this);
		$(this).next(_slideEl).toggle(_duration);
		$(this).parent().parent().parent().toggleClass('active');
	});
	if(typeof($().galleryScroll) == 'function'){
		$('div.ad-gallery').galleryScroll({
			duration: 400,
			autoSlide: 6000,
			slideNum: "div.sw-inner"
		});
		
		$('div.g2').galleryScroll({
			duration: 1000,
			step: 6
		});
	}
	$('div.prod-image').each(function(){
		$(this).find('img').width('100%').reflect({
			height: 0.4,
			opacity: 0.3
		});
	});
});
/*--- tabs function ---*/
function initTabs(h_list) {
	$(h_list).each(function(_ind, _el) {
		
		var btn_h = $(_el);
		var _btn = $(_el).find('a.tab');
		var _a = 0;
		_btn.each(function(_ind, _el) {
			this._box = $('#'+_el.href.substr(_el.href.indexOf("#") + 1));
			if($(_el).hasClass('active')) {
				this._box.show();
				_a = _ind;
			}
			else {
				this._box.hide();
			}
			_el.onclick = function() {
				if(!$(this).hasClass('active')){
					_btn.get(_a)._box.hide();
					_btn.eq(_a).removeClass('active');
					this._box.show();
					$(this).addClass('active');
					_a = _ind;
				}
				return false;
			}
		});
	});
}
function hideLevels(_this){
	$('#'+_id+' li').each(function() {
		var _f = false;
		var _a = $('a', this).each(function(i, _el) {
			if (_el == _this) _f = true;
		});
		if (!_f)  {
			$(_slideEl, this).hide(_duration);
			$(this).removeClass('active');
		}
	});
}
