$(function(){
	initNavDrop();
	initGallery({
		holder:'.slideshow',
		list:'ul.slides',
		switcher:'.switcher ul > li',
		effect:'fade',
		autoRotation:10000
	});
	initAccordion();
	$('.main-g1 li.cat-elem> a:not(.all)').each(function(){		
		var _this = $(this);		
		var ind = $('.main-g1 li.cat-elem > a').index(_this);
		_this.mouseenter(function(){
			$('div.accordion ul > li').eq(ind).trigger('mouseenter');
		}).mouseleave(function(){
			$('div.accordion ul > li').eq(ind).trigger('mouseleave');
		}).click(function(){
			$('.main-g1 li').removeClass('active');
			$('div.accordion ul > li').eq(ind).find('a.tab').trigger('click');
			initTabs($('div.accordion ul'));
			_this.parent().addClass('active');
			return false;
		});
	});
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle'}, speed, easing, callback);	     
	};
});

function initTabs(obj){
	obj.each(function(){
		var _list = jQuery(this);
		var _links = _list.find('a.tab');
		_links.unbind('click');
		_links.each(function() {
			var _link = jQuery(this);
			var _href = _link.attr('href');
			var _tab = jQuery(_href);

			if(_link.parents('li').hasClass('active')) _tab.show();
			else _tab.hide();

			_link.click(function(){
				var ind = _links.index($(this));
				$('.main-g1 li').removeClass('active').eq(ind).addClass('active');
				_links.parents('li').each(function(){
					jQuery(jQuery(this).removeClass('active').find('a.tab').attr('href')).hide();
				});
				_link.parents('li').addClass('active');
				_tab.show();
				return false;
			});
		});
	});
}

function initAccordion(){
	$('div.accordion').each(function(){
		var hold = $(this);
		var li = hold.find('ul > li');
		var w = hold.find('.acc').width();
		var count = li.length;
		var liW = Math.floor(w/count) - hold.find('.acc').css('padding-right').replace("px", "");
		
		initTabs(li.parent());
		li.each(function(){
			var _this = $(this);
			var link = _this.find('> a');
			var slide = _this.find('> .slide');
			var text = slide.find('.text');
			var linkW = link.width();
			var slideW = slide.width();
			var openW = (w-slideW+liW)/count;
			var closeL = -(slideW-liW)/2;
			var bottom = text.height();
			_this.width(liW);
			slide.css({left:closeL});
			text.css({bottom:0});
			_this.mouseenter(function(){
				li.removeClass('open');
				_this.addClass('open');
				link.animate({'opacity': 0}, {queue:false, duration:300, complete:function(){
					link.hide();
				}});
				_this.stop().animate({width:slideW}, {queue:false, duration:700, complete:initText});
				slide.stop().animate({left:0}, {queue:false, duration:700});
				li.filter(':not(.open)').animate({width: openW}, {queue:false, duration:700});
			}).mouseleave(function(){
				if(!li.filter('.active').length){
					li.removeClass('open');
					text.css('bottom', 0);					
					li.stop().animate({width:liW}, {queue:false, duration:700});
					slide.stop().animate({left:closeL}, {queue:false, duration:700, complete:function(){
						link.show().animate({'opacity': 1}, {queue:false, duration:300});
					}});
				}
				else{
					li.filter('.active').stop().animate({width:slideW}, {queue:false, duration:700, complete:initText});
					li.filter(':not(.active)').stop().animate({width:openW}, {queue:false, duration:700});
					li.filter(':not(.active)').find('> a').show().animate({'opacity': 1}, {queue:false, duration:300});
				}
			});
			function initText(){
				text.css('bottom', bottom);				
				//text.animate({bottom:bottom}, {queue:false, duration:700});
			}
		});
	});
};

function initNavDrop(){
	var t;
	$('#nav > li').each(function(){
		var li = $(this);
		var drop = li.find('> .drop');
		if(drop.length){
			li.mouseenter(function(){
				drop.fadeIn(300);
			}).mouseleave(function(){
				drop.fadeOut(300);
			});
			drop.mouseenter(function(){
				clearTimeout(t);
			}).mouseleave(function(){
				drop.fadeOut(300);
			});
		}
	});
}

function initGallery(option){
	var hold = jQuery(option.holder);
	var duration = option.autoRotation;
	var switcher = option.switcher || false;
	var event = option.event || 'click';
	hold.each(function(){
		var _this = jQuery(this);
		var list = _this.find(option.list),
			count = list.children().length,
			w = list.parent().width(),
			_t,
			a = 0,
			r = a;

		if(option.prev && option.next){
			var prev = _this.find(option.prev).attr('rel', 'prev').click(animateSlide);
			var next = _this.find(option.next).attr('rel', 'next').click(animateSlide);
		}
		if(option.switcher){
			switcher = _this.find(switcher);
			switcher.eq(r).removeClass('active');
			switcher.eq(a).addClass('active');
			switcher.bind(event, function(){
				var ind = switcher.index(jQuery(this));
				animateSlide(ind);
				return false;
			});
		}
		if(option.autoRotation) runTimer();
		if(option.effect == 'fade') {
			list.children().css('opacity', 0);
			list.children().eq(a).css('opacity', 1).addClass('active');
		}
		if(option.stopOnHover && _t){
			list.mouseenter(function(){
				clearTimeout(_t);
			}).mouseleave(runTimer);
		}
		function runTimer(){
			_t = setTimeout(function(){
				animateSlide('next');
			}, duration);
		}
		
		function animateSlide(e){
			r = a;
			if(typeof e == 'string' && e == 'next') a++;
			else if(typeof e == 'number') a=e;
			else{
				if(e.target.rel == 'next') a++;
				else if(e.target.rel == 'prev') a--;
			}
			if(_t) clearTimeout(_t);
			if(a == count) a=0;
			else if(a == -1) a=count-1;
			list.children().eq(r).removeClass('active');
			list.children().eq(a).addClass('active');
			if(option.switcher){
				switcher.eq(r).removeClass('active');
				switcher.eq(a).addClass('active');
			}
			if(option.effect == 'fade'){
				list.children().eq(r).animate({opacity:0}, {queue:false, duration:700});
				list.children().eq(a).animate({opacity:1}, {queue:false, duration:700, complete:function(){
					if(option.autoRotation) runTimer();
				}});
			}
			else{
				list.animate({marginLeft:-w*a}, {queue:false, duration:700, complete:function(){
					if(option.autoRotation) runTimer();
				}});
			}
			return false;
		}
	});
};
