var a = {
	init:function(){
		a.slides.init();
	},
	slides:{
		timer:null,
		interval:10.5, // seconds
		transition:0.7, // seconds
		init:function(){
			$('.slides').each(function(i,e){
				index = $(e).siblings('.index');
				slides = $(e).find('.slide');
				if (slides.length > 0) {
					$(e).siblings('.left').click(function(){ a.slides.previous($(e),1); });
					$(e).siblings('.right').click(function(){ a.slides.next($(e),1); });
					slides.each(function(j,s){
						$(s).data({'mb3_index':j});
						p = $('<img src="http://static.mb3online.com/img/slides.index.png" width="8" height="8" />');
						index.append(p);
						p.click(function(){ a.slides.goTo($(s)); });
					});
				}
			});
			
			$(window).load(function(e){ a.slides.start(); });
		},
		start:function(){
			var slideCount = $('.slides .slide').length;
			$('.e .left, .e .right').css({opacity:0,'margin-top':'-20px',visibility:'visible'});
			$('.e .holder').css({visibility:'visible',opacity:0});
			$('.e .index').css({visibility:'visible',opacity:0});
			$('.e .loading').animate({opacity:0,'margin-top':'-30px'},function(){
				if (slideCount > 1) $('.e .left, .e .right').animate({opacity:1,'margin-top':'0px'});
				$('.e .holder').animate({opacity:1});
				$('.e .index').animate({opacity:1});
				a.slides.goTo($('.e .slides .active'));
			});
		},
		previous:function(el,pause){
			if (typeof pause == 'undefined') pause = 0;
			holder = el.find('.holder');
			next = holder.find('.slide.active').prev();
			if (next.length == 0) next = holder.find('.slide').last();
			a.slides.goTo(next,pause);
		},
		next:function(el,pause){
			if (typeof pause == 'undefined') pause = 0;
			holder = el.find('.holder');
			next = holder.find('.slide.active').next();
			if (next.length == 0) next = holder.find('.slide').first();
			a.slides.goTo(next,pause);
		},
		goTo:function(el,pause){
			if (typeof el == 'undefined') el = '';
			if (typeof pause == 'undefined') pause = 0;
			if ($(el).length == 0) return;
			
			clearTimeout(a.slides.timer);
			a.slides.timer = null;
							
			slides = el.parents('.slides');
			holder = el.parents('.holder');
			active = el.siblings('.active');
			left = el.position().left;
			holder.animate({left:'-'+left+'px'},(a.slides.transition*1000),'easeInOutQuint',function(){
				active.removeClass('active');
				el.addClass('active');
				if (!pause) a.slides.timer = setTimeout(function(){ a.slides.next(slides,0); },a.slides.interval*1000);
				else a.slides.timer = setTimeout(function(){ a.slides.next(slides,0); },15000);
			});
			
			index = el.data().mb3_index;
			imgs = $(el).parents('.slides').siblings('.index').find('img');
			imgs.animate({opacity:0.3});
			$(imgs[index]).animate({opacity:1.0});
		}
	}
};
$(document).ready(function(){ a.init(); });

