jQuery.noConflict();
jQuery(document).ready(function(){
	InitGallery();
	InitTabs();
});
function InitTabs(){
	var _duration = 300;
	var _list = jQuery('.tabset a');
		var _a = _list.index(_list.filter('active'));
		if (_a == -1){_a = 0}
	var _tabHolder = jQuery('.tab-holder');
	var _box = _tabHolder.find('div.tab-content');
	
	_box.css({opacity:0,zIndex:1});
	_list.removeClass('active').eq(_a).addClass('active');
	
	_tabHolder.find(_list.eq(_a).attr('href')).css({opacity:1,zIndex:2});
	var _sh = jQuery('<span><span>').appendTo(_tabHolder.parent());
	_sh.css({
		left:_list.eq(_a).position().left + 8, 
		width:_list.eq(_a).outerWidth(true) - 2,
		top:_tabHolder.position().top,
		display:'block',
		position:'absolute',
		zIndex:5,
		background:'#fff',
		height:1
	});
	_tabHolder.height(_tabHolder.find(_list.eq(_a).attr('href')).outerHeight(true));
	
	_list.click(function(){
		_box.css('zIndex',1).animate({opacity: 0}, {queue:false, duration:_duration});
		_list.removeClass(' active');
		jQuery(this).addClass(' active');
		var _l = jQuery(this).position().left;
		var _w = jQuery(this).outerWidth();
		_sh.css({	left: _l + 8 , width: _w - 2});
		var _tab = _tabHolder.find(jQuery(this).attr('href')).css('zIndex',2);
		_tab.animate({opacity: 1}, {queue:false, duration:_duration});
		_tabHolder.animate({height: _tab.outerHeight(true)}, {queue:false, duration:_duration});
		return false;
	});
};
function InitGallery(){
	jQuery('.visual').Gallery({
		speed: 800,
		autoRotation: 7000,
		list: 'ul.slider>li',
		pager: '.switcher ul',
		prev: 'a.btn-prev',
		next: 'a.btn-next',
		ieVersion: 6
	});
	jQuery('.promo-b').Gallery({
		speed: 800,
		autoRotation: false,
		list: 'ul.tabs>li',
		pager: 'ul.tabset',
		dinamicPagination: false,
		ieVersion: 6
	});
};
jQuery.fn.Gallery = function(_options){
	// default options
	var _options = jQuery.extend({
		speed: 1200,
		autoRotation: 4000,
		list: 'ul.fade>li',
		prev: 'a.prev',
		next: 'a.next',
		pager: 'ul.switcher',
		dinamicPagination: true,
		pause: '.pause',
		ieVersion: 6
	},_options);
	
	return this.each(function(){
		// options
		var _hold = jQuery(this);
		var _speed = _options.speed;
		var _autoRotation = _options.autoRotation;
		var _list = _hold.find(_options.list);
		var _prev = _hold.find(_options.prev);
		var _next = _hold.find(_options.next);
		var _pause = _hold.find(_options.pause);
		var _ie = _options.ieVersion;
		var _f = true;
		var _p = _options.dinamicPagination;
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) {_a = 0;_list.eq(_a).addClass('active')}
		var _i, _old = _a, _t;
	/*--------CREATING THUMBNAILS----------*/
		var _num = _hold.find(_options.pager);
		if (_p){	_num.empty();
			_list.each(function(i){
				jQuery('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_num);
			});
		};
		var _thumb = _num.find('a');
		_thumb.eq(_a).addClass('active');
	/*-------------------------------------------------*/
		if (jQuery.browser.msie && jQuery.browser.version < _ie){		_list.hide().eq(_a).show();
		}else{		_list.show().css({opacity:0}).eq(_a).css({opacity:1});	}
		
		if (_autoRotation) Run(_a);
		
		function Run(_a){
			_t = setTimeout(function(){
				_a++; if (_a >= _list.length){_a=0}
				ChangeFade(_a);
			}, _autoRotation);
		};
		function ChangeFade(_new){
			if(_new != _old){
				if(jQuery.browser.msie && jQuery.browser.version < _ie){
					_list.eq(_old).removeClass('active').hide();
					_list.eq(_new).addClass('active').show();
				}else{
					_list.eq(_old).removeClass('active').animate({opacity:0}, {queue:false, duration:_speed});
					_list.eq(_new).addClass('active').animate({opacity:1}, {queue:false, duration:_speed});
				}
				_thumb.eq(_old).removeClass('active');
				_thumb.eq(_new).addClass('active');
				_old=_new;_a=_new;
				if(_t) clearTimeout(_t);
				if (_autoRotation){if (_f){Run(_new);}}
			};
		};
		_pause.click(function(){
			_f = false;
			clearTimeout(_t);
			return false;
		});
		_thumb.click(function(){
			_i = _thumb.index(jQuery(this));
			ChangeFade(_i);
			_a = _i;
			return false;
		});
		_next.click(function(){
			_a++; if (_a == _list.length){_a=0}
			ChangeFade(_a);
			return false;
		});
		_prev.click(function(){
			_a--; if (_a == -1){_a = _list.length-1}
			ChangeFade(_a);
			return false;
		});
	});
};
