/*
* Title: Initialze 
* Author: Yves Torres
* Haupt Applikation
*/

function nav(){
	$('#nav').find('> ul > li').each(function(){
		var ul = $(this).find('> ul');
		var $w = $(this).find('> a cufon').width()+20-parseInt(ul.css('padding-left'))-parseInt(ul.css('padding-right'));
		if($('html').hasClass('oldie'))
			ul.css({'width':$w+20});
		else
			ul.css({'min-width':$w});
	});
}

function teasers(){
	var teaser = $('#teaser');
	if(teaser.length>0){
		
		var width = 309, duration = 6000, cWidth = 927,
		
		first = teaser.find('ul'), second = first.clone(),container = first.wrap('<div style="width:'+cWidth+'px;height:145px;overflow:hidden;position:relative;" />')[0], numItems = first.find('li').length,
		totalWidth = width*numItems, curW = 0, restWay, int, timeOut, factor, restTime, left = 0, play = 1, calcDone=0;

		first.after(second);
		
		first.css({'position':'absolute',left:0,top:0,width:totalWidth});
		second.css({'position':'absolute',left:totalWidth,top:0,width:totalWidth});
		
		var run = function(amount,time) {
		  if(play) {
			if(curW>=totalWidth) {
				first.stop(true,true).css({left:0});
				second.stop(true,true).css({left:totalWidth});
				curW=0;
			}
			first.animate({left: "-="+amount}, time, 'linear');
			second.animate({left:  "-="+amount}, time, 'linear');
			curW+=amount;
			}
		}
				
		teaser.hover(function(){
			first.stop(); second.stop();
			clearInterval(int);
			clearTimeout(timeOut);
			play = 0;
		},function(){
			play = 1;
			
			left = Math.abs(parseInt(first.css('left')));
			for(i=1;i<11;i++){
				if(left<=width*i){
					restWay=(width*i)-left;
					calcDone=1;
				}
				if(calcDone) break;
			}
			calcDone=0;
			/*if(left<=width*4)
				restWay=(width*4)-left;
			if(left<=width*3)
				restWay=(width*3)-left;
			if(left<=width*2)
				restWay=(width*2)-left;
			if(left<=width)
				restWay=width-left;*/
			factor = width/duration;
			restTime = restWay/factor;
			curW-=restWay;
			
			run(restWay,restTime);
			
			timeOut = setTimeout(function(){
				run(width,duration);
				int = setInterval(function(){ run(width,duration); },duration);	
			},restTime);
		});
		
		int = setInterval(function(){ run(width,duration); },duration);	
		run(width,duration);
	}
}

function gallery(){
	if($('#car figure').length>0){
		
		var cont = $('#car figure'), imgs = cont.find('img'), lis = $('#car .thumbs li'), numImgs = imgs.length-1, curImg = 0, nextImg;
		
		lis.each(function(idx){
			var _this = $(this);
			$(this).find('> a').click(function(e){
				e.preventDefault();
				if(!_this.hasClass('active')){
					var _idx = idx;
					changeImg(_idx);
				}
				$(this).blur();
			});
		});

		var changeImg = function(num){
			var img = $('#img'+num);
			curImg = num;
			img.appendTo(cont);
			lis.removeClass('active');
			lis.eq(num).addClass('active');
			imgs.removeClass('active');
			img.addClass('active').css({opacity:0});
			img.animate({opacity:1},250);
		},
		next = function(){
			if(curImg==numImgs)nextImg=0;
			else nextImg=curImg+1;
			changeImg(nextImg);
		},
		prev = function(){
			if(curImg==0)nextImg=numImgs;
			else nextImg=curImg-1;
			changeImg(nextImg);
		};

		$(document)['bind']("keydown", function(event){
			var code = event.keyCode;
			return (code==39) ? next()
				: (code==37) ? prev()
				: true;
		});		
	}
}

$(document).ready(function(){
	cufon();
	setTimeout("nav()",100);
	setTimeout("teasers()",1000);
	gallery();
	$('#carlist').find('li:nth-child(3n)').css({'margin-right':0});
	$('.controls').find('li:nth-child(3n)').css({'margin-right':0});
});

























