var car_width = 990;
var im_width = 415;
var count = 0; 
var subcount = 0; 
var radiusX =  (car_width )/2;
var radiusY = 0; 
var centerX = car_width - im_width; 
var centerY = -50;
var basespeed = 1;
var speed = 50;
var imageDivs = '';
var numberOfElements = 4;
var carousel = '';
var speedTest = '';
var Hmax = 400;
var Hmin = 100;
var docwidth = 1200;
var direction = 1;
var subpos = 0;
var msecs = 0;
var wait = 1000;
window.addEvent('domready', function(){
	carousel = $('carousel');
	/*carousel.addEvent('mousemove', onMouseMove.bindWithEvent( carousel ));*/
	/*docSize();*/
	startCarousel();
	gocarousel(0,'');
});

var roundcount = 0;
var roundcount_to = 0;
var lastslide = 0;
var clickedslide = 0;
var status = 'running';
var zoommax = 2;
var zoomp = 2;
var gourl = '';
function gocarousel(slidenum,url){
	lastslide = slidenum;
	roundcount_to = slidenum *100;
	if(url != ''){
		clickedslide = slidenum;
		wait = 5000;
		//roundcount_to = roundcount;
		//status = 'zooming';
		gourl = url;
		if(roundcount_to == roundcount){	
			if(status == 'running'){
				status = 'zooming';
				zoomp = 1;
				wait = 3000;
				gourl = url;return false;
			} else if(status == 'zooming'){
					document.location.href = (url);return false;
			}
		} else {
			status = 'running';
		}
	} else {
		wait = 3000;
		status = 'running';
	}
}

var startonce = false;
function startCarousel(){
	if(startonce == false){
		runCarousel();
		startonce = true;	
	}
}
var waiting = false;
function runCarousel(){
	var updatecarousel = false;
	if(roundcount - numberOfElements*100 == roundcount_to){roundcount = roundcount_to;}
	if(roundcount + numberOfElements*100 == roundcount_to){roundcount = roundcount_to;}
	
	if(roundcount_to != roundcount){
		var divr = roundcount_to - roundcount;
		if(divr < (-numberOfElements*100/2)){
			roundcount_to = roundcount_to + numberOfElements*100;
		} else if(divr > (numberOfElements*100/2)){
			roundcount_to = roundcount_to - numberOfElements*100;
		}
		roundcount = ((.5 * roundcount_to + 1.5 * roundcount)/2);
		var divr = roundcount_to - roundcount;
		if(divr > -2 && divr < 2){roundcount = parseInt(roundcount_to);}
//		alert(divr);
		updatecarousel = true;
		var currentDate = new Date()
		msecs = currentDate.getTime();
	} else {
		
		var currentDate = new Date()
		newmsecs = currentDate.getTime();
		if(status == 'zooming'){
			//zooming
			updatecarousel = true;
			if((newmsecs - msecs) > 2000){
				if(gourl != ''){	
					//alert(gourl);
					document.location.href = gourl;return false;
				}
			}
		} else {
			if(gourl == ''){
				var currentDate = new Date()
				newmsecs = currentDate.getTime();
				if((newmsecs - msecs) > wait){
					lastslide++;
					gocarousel(lastslide,'');
				}
			} else {
				gocarousel(clickedslide,gourl);
			}
				//alert('no timeoud');
		} 
		
	}
	
	if(updatecarousel == true){
		
		for(i=0; i < numberOfElements; i++){
			var factor = 1;
			if(status == 'zooming' && i == clickedslide ){
				if(zoomp < zoommax){zoomp = zoomp + .1;}
				factor = zoomp;
			}
	//		var rotation = Math.PI -Math.PI/2 +  ( Math.PI * 2 ) * ;
			var dangle = (-roundcount/100  +  i) * ( Math.PI * 2 ) / numberOfElements;
			var id = 'carousel_slide_' + i;
			posZ = ( Math.cos( dangle )* 500 + 1000 );
			if(posZ < 90){
				document.getElementById(id).style.display = 'none';
			} else {
			
				//imageDivsStyle = imageDivs[ i ].style; 
				//imageDivsStyle.position='absolute'; 
				ImWidth = ( Math.cos(  dangle )* factor*im_width/3 + 2*factor*im_width/3 );
				ImHeight = 295 *(ImWidth/im_width);	
				var d = (car_width - ImWidth)/2;
				posX = ( -Math.sin( dangle )* d  + d );
				posY = 0+(295-ImHeight)/2 ;//( Math.cos( rotation + dangle )* radiusY + radiusY );
			
			
				document.getElementById(id).style.zIndex = parseInt(posZ);
				document.getElementById(id).style.marginLeft = posX+"px"; 
				document.getElementById(id).style.marginTop = posY+"px"
				document.getElementById(id).style.width = ImWidth+"px";
				document.getElementById(id).style.height = ImHeight+"px";
				/*
				var image = 'carousel_slide_image_' + i;
				document.getElementById(image).style.width = (ImWidth - 28)+"px";
				document.getElementById(image).style.height = (ImHeight - 28)+"px";
				*/
				var label = 'carousel_slide_label_' + i;
				if(posZ < 1450){
					document.getElementById(label).style.display = 'none';
				} else {
					document.getElementById(label).style.display = 'block';
				}
				document.getElementById(id).style.display = 'block';
			}
		}
		
	}
	
	
		setTimeout('runCarousel();',100);
/*
	waiting = false;
	var currentDate = new Date()
	newmsecs = currentDate.getTime();
	count = count + direction * speed * (newmsecs - msecs + 5)/45;
	document.getElementById('bug').innerHTML = direction + ' - ' + speed + ' - ' +(newmsecs - msecs);
	msecs = currentDate.getTime();
	if(direction == -1 && count < 0){
		count = 100;
		subcount = subcount - 100;
		if(subcount < 0){subcount = 100 * (numberOfElements-1);}
		//alert('a');
		waiting = true;
		runners++;
		setTimeout('waitrunCarousel();',2400);
	} else if(direction == 1 && count > 100){
		count = 0;
		subcount = subcount + 100;
		if(subcount > (100 * (numberOfElements-1))){subcount = 0;}
		//alert('b');
		waiting = true;
		runners++;
		setTimeout('waitrunCarousel();',2400);
	} else {
			//	alert('c' + direction + ' s' + subcount);
		//DrunCarousel();
		waiting = true;
		runners++;
		setTimeout('waitrunCarousel();',40);
	}
*/	
}
var runners = 0;
function waitrunCarousel(){
	
	var currentDate = new Date()
	msecs = currentDate.getTime();
	if(waiting == true){
		runCarousel();
	}
}
function docSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
docwidth =  myWidth;
}
