var inMotion = false;
var env = {
	width: 830,
	height: 340,
	dia: 587,
	mar: 121.5,
	cardw: 485,
	cardh: 300,
	angfix: 45,
	sect: 90
};
var delay = 5000;
var motionAngle = 0;
var active = 0;
var step, leap;
var rotateInterval, autoInterval;
var targetAngle, finalAngle;
var cards, links, transit, depths;

function initCards(url, _delay){
	delay = _delay || 5000;
	$.ajax({
		url: url,
		dataType: 'json',
		success: parseit
	});
}

function parseit(data){
	links = [];
	$.each(data, function(i, item){
		if (i > 4) return false;
		$('#carousel').append(
			$('<div>').attr('class','card').attr('id','card'+i).append(
				$('<div>').attr('class','info').append(
					$('<div>').attr('class','title').html(item.title)
				).append(
					$('<div>').attr('class','details').html(item.details)
				).append(
					$('<div>').attr('class','more').html(item.more)
				).hide()
			).append(
				$('<img>').attr({src: item.path, alt: ''})
			).css('cursor', 'pointer').click(clickHandler)
		);
		links[i] = item.link ? item.link : '';
	});
	setCards();
	$('#prevcard').show().css('cursor', 'pointer').click(prevcard);
	$('#nextcard').show().css('cursor', 'pointer').click(nextcard);
	autoInterval = setInterval(auto, delay);
}

function getTrans(a){
	var trans = {};
	var scale = Math.sin(a * Math.PI / 180) * (a > 180 ? 0.25 : 0.5) + 0.5;//(1 - Math.abs((90-(a%180))/90)) * (a > 180 ? -0.25 : 0.5) + 0.5;
	trans.width = Math.round(scale * env.cardw);
	trans.height = Math.round(scale * env.cardh);
	trans.top = (env.height - trans.height) / 2;
	trans.left = env.mar + env.dia * Math.abs((180-a)/180) - trans.width / 2;
	trans.z = depths[Math.floor((a+env.angfix)/env.sect)];
	return trans;
}
function setCards(){
	cards = $('#carousel .card');
	transit = [90, 180, 0];
	depths = '1,2,1'.split(',');
	leap = 90;
	distribute();
	$('#card'+active).find('.info').slideDown(200);
}
function distribute(add){
	add = add || 0;
	cards.each(function(i,item){
		if (step > 0){
			if (transit[i]+add > 180){
				transit[i] = 360-leap;
			}
		} else {
			if (transit[i]+add < 0){
				transit[i] = 180+leap;
			}
		}
		var trans = getTrans(transit[i]+add)
		$(item).css({
			width: trans.width,
			height: trans.height,
			top: trans.top,
			left: trans.left,
			zIndex: trans.z
		});
	});
}
function rotate(){
	motionAngle += step;
	if (arrived()){
		motionAngle = targetAngle;
		checkFinal();
	} else {
		rotateInterval = setTimeout(rotate, 40);
		distribute(motionAngle);
	}
}
function getAngle(a){
	if (a == 180){
		return a;
	}
	return (180 + a) % 180;
}
function move(target){
	clearInterval(autoInterval);
	$('#card'+active).find('.info').slideUp(200);
	motionAngle = 0;
	finalAngle = target;
	targetAngle = (Math.abs(target) > leap) ? target/2 : target;
	step = (target > 0) ? 5 : -5;
	inMotion = true;
	rotateInterval = setTimeout(rotate, 40);
}
function arrived(){
	if (step > 0){
		return motionAngle >= targetAngle;
	} else {
		return motionAngle <= targetAngle;
	}
}
function checkFinal(){
	$.each(transit, function(i){
		transit[i] = (transit[i]+targetAngle)%360;
		if (Math.round(transit[i]/45)*45 == 90){
			active = i;
		}
	});
	distribute();
	if (Math.abs(finalAngle) > Math.abs(targetAngle)){
		finalAngle -= targetAngle;
		rotateInterval = setTimeout(rotate, 40);
		motionAngle = 0;
	} else {
		inMotion = false;
		$('#card'+active).find('.info').slideDown(200);
		autoInterval = setInterval(auto, delay);
	}
}
function clickHandler(ev){
	ev.stopPropagation();
	if (inMotion) return;
	var c = Number(ev.currentTarget.getAttribute('id').substr(4));
	if (c == active){
		if (links[active]){
			location.href = links[active];
		}
	} else {
		move(transit[active] - transit[c]);
	}
}
function auto(){
	move(-leap);
}

function nextcard(){
	if (inMotion) return;
	move(-leap);
}
function prevcard(){
	if (inMotion) return;
	move(leap);
}
