// Items erfassen
var items = {}, i=0;
var paper = Raphael("hexagonshow", 480, 288);
var mittelpunktX = 170;
var mittelpunktY = 80;
var hexsettings = [
	{
		positionX: 0,
		positionY: 5,
		hexanipositionX: 10,
		hexanipositionY: 10
	},
	{
		positionX: -115,
		positionY: 45,
		hexanipositionX: -30,
		hexanipositionY: -15
	},
	{
		positionX: 115,
		positionY: 45,
		hexanipositionX: -10,
		hexanipositionY: 15
	},
	{
		positionX: 0,
		positionY: -75,
		hexanipositionX: 90,
		hexanipositionY: 5
	},
	{
		positionX: 0,
		positionY: 85,
		hexanipositionX: -30,
		hexanipositionY: 20
	},
	{
		positionX: 115,
		positionY: -35,
		hexanipositionX: -10,
		hexanipositionY: 20
	},
	{
		positionX: -115,
		positionY: -35,
		hexanipositionX: 25,
		hexanipositionY: 20
	}
	];
var bilder = "resources/animation-de/";
$("#hexagonshow li").each(function(i){
	var item = {};
	item.hexpositionX = hexsettings[i].positionX;
	item.hexpositionY = hexsettings[i].positionY;
	item.hexurl = $("> a", this).attr("href");
	item.hextext = $("> a", this).text().toString();
	item.hexanipositionX = hexsettings[i].hexanipositionX;
	item.hexanipositionY = hexsettings[i].hexanipositionY;
	item.positionsX = Array();
	item.positionsY = Array();
	//console.log(120 + item.hexpositionX + item.hexanipositionX * 0.15);
	for(var u = 0;u<7;u++){
		item.positionsX[u] = (mittelpunktX + item.hexpositionX + item.hexanipositionX * (u/6)).toFixed(2);
		item.positionsY[u] = (mittelpunktY + item.hexpositionY + item.hexanipositionY * (u/6)).toFixed(2);
		//console.log(item.positionsX[u] + " | " + item.positionsY[u] );
	}
	item.hexactive = $(this).hasClass("active");
	item.image = $(this).attr("id");
	item.hexagon = paper.image(bilder + item.image + ".png", mittelpunktX, mittelpunktY, 140, 76).toBack().attr({cursor: "pointer"}).animate({translation: item.hexpositionX + " " + item.hexpositionY}, 1000, "<>");
	item.hexagon.click(function(event){
			location.href = item.hexurl;
	});
	items[i] = item;
	i++;
})
// Canvas zeichnen
window.setTimeout(function(){
		shuffleItems(0)
	}, 2000); 

function shuffleItems(pos){
	//console.log(items);
	var newObject = {};
	var j = 0;
	for (var i=pos; i< 7;i++){
		newObject[j] = items[i];
		//console.log(j + ": " + i);	
		j++;
	}
	var newPos = 7 - pos;
	for (var i=0; i < pos; i++){	
		newObject[newPos] = items[i];
		//console.log(newPos + ": " + i);
		newPos++;
	}
	items = newObject;
	//console.log(newObject);
	for (var i=0 in items){
		items[i].opacity = (1.0 - (0.1 * i)).toFixed(2);
		if(i==0){
			items[i].scale = 1.1;
		} else {
			items[i].scale = (1.07 - (0.07 * i)).toFixed(2);
		}
		moveToX = (items[i].positionsX[i]);
		moveToY = (items[i].positionsY[i]);
		items[i].hexagon.toBack().animate({opacity: items[i].opacity, scale: items[i].scale, x: moveToX, y: moveToY},250,"<>");
		//console.log(i + ": " + moveToX + "|" + moveToY)
		items[i].hexagon.mouseover(function(event){
			pos = parseInt(findPos(this));
			if(pos!=0){
				shuffleItems(pos);
			}
		});
	}
}

function findPos(hex){
	var ctr = "";
	for (var pos=0 in items){
		if (items[pos].hexagon == hex) {
			return pos;
		}
	}
	return ctr;
};

