$(document).ready(function(){

	$('div.slides div.slide:not(:first) div.bg').each(function(){
		var imageurl = $(this).css('background-image').replace('url(', '').replace(')', '');
		$('<img/>')
			.attr('src', imageurl)
			.appendTo('#_Preload');
	});

	$('div.footer a').click(function(e){
		window.open('privacy.html','','menubar=0,resizable=0,scrollbars=1,width=450,height=350');
		e.stopPropigation();
	});

	$('div.slides div.slide:first, div.slider div.sleeve li:first').addClass('active');
	$('div.slides div.slide:not(:first)').css({opacity: 0, display: 'none'});
	
	if($.browser.msie){
		$('div.slides div.slide:not(:first) div.bg').css({opacity: 0, display: 'none'});
	}
	
	var buttons = $('div.slider div.buttons span'),
		animating = false,
		interval = null;
		
	buttons.click(function(){
		
		clearInterval(interval);
		
		var t = $(this),
			left = t.find('b').hasClass('left'),
			active = $('div.slider div.sleeve li.active'),
			next = left ? active.prev() : active.next();
			
		if(!next || !next.length){
			return;
		}
		
		move(next, left);
		
	});
	
	$('div.slider div.sleeve li').click(function(){
		
		clearInterval(interval);
		
		var t = $(this),
			parent = t.parent(),
			active = parent.find('li.active'),
			lis = parent.find('li');
			
		move(t, lis.index(t) < lis.index(active));
		
	});
	
	interval = setInterval(function(){
		var next = $('div.slider div.sleeve li.active').next();
		if(next && next.length){
			move(next, false);
		}
	}, 15000);
	
	function move(next, left){
		if(animating){
			return;
		}
	
		buttons.removeClass('disabled');
		
		var active = $('div.slider div.sleeve li.active'),
			nextnext = left ? next.prev() : next.next(),
			sleeve = $('div.slider div.sleeve'),
			ul = sleeve.find('ul'),
			lilist = ul.find('li'),
			count = ul.children().length,
			index = lilist.index(next);
			activeIndex = lilist.index(active);
			
		if(!nextnext || !nextnext.length){
			$('div.slider div.buttons span:' + (left ? 'first' : 'last')).addClass('disabled');
		}
		
		var hideTarget = $('div.' + active.attr('rel')),
			showTarget = $('div.' + next.attr('rel'));
		
		if($.browser.msie){
			hideTarget.find('div.content').hide();
		
			var hideBg = hideTarget.find('div.bg')[0],
				showBg = showTarget.find('div.bg')[0];
		
			hideTarget = $([]).add(hideTarget).add(hideBg);
			showTarget = $([]).add(showTarget).add(showBg);
		}
		
		hideTarget.fadeTo(750, 0, function(){
			hideTarget.hide();
			
			showTarget.show().fadeTo(750, 1, function(){
				
				if($.browser.msie){
					$('div.' + next.attr('rel') + ' div.content').show();
				}				

			});

			active.removeClass('active');
			next.addClass('active');

			$('div.page').removeClass('dark').addClass($('div.' + next.attr('rel')).attr('rel'));
			
			document.title = 'Vectorform - ' + next.find('strong').html().replace('&amp;', '&');
			
			animating = true;
			var offset = (index < count - 3 ? index : count - 4) * next.width();
			ul.animate({marginLeft: -1 * offset}, 'slow', null, function(){ animating = false; });			
			
		});

	};
	
});