var rotateOrderLeft = {
	
	'front': 'left-45'
	,'left-45': 'left'
	,'left': 'left-back-45'
	,'left-back-45': 'back'
	,'back': 'right-back-45'
	,'right-back-45': 'right'
	,'right': 'right-45'
	,'right-45': 'front'
};

var rotateOrderRight = {
	
	'front': 'right-45'
	,'right-45': 'right'
	,'right': 'right-back-45'
	,'right-back-45': 'back'
	,'back': 'left-back-45'
	,'left-back-45': 'left'
	,'left': 'left-45'
	,'left-45': 'front'
};
var lastpos = 0;

$(document).ready(function() {
	$('a.nyroModal').nyroModal();
	$('#colors a').click(function() {
		var color = $(this).attr('rel');
		var where = $('div#container').attr('rel');
		var side = $('div#jackport-main').attr('rel');
		$('#jackport-main img').attr('src','/fileadmin/templates/jackport/picker/'+where+'/'+side+'/'+color+'.jpg');
		$('#jackport-main').attr('mycolor',color);
		return false;
	});

	$('div#arrow-view-left').bind('selectstart',function() { return false; });
	$('div#arrow-view-left').bind('mousedown',function() { return false; });
	$('div#arrow-view-right').bind('selectstart',function() { return false; });
	$('div#arrow-view-right').bind('mousedown',function() { return false; });
	$('div#arrow-view-left').dblclick(function(e) {
		e.preventDefault();
		return false;
	});
	$('div#arrow-view-right').dblclick(function(e) {
		e.preventDefault();
		return false;
	});
	
	$('div#arrow-view-left').click(function(e) {
		e.preventDefault();
		rotateLeft();
		return false;
	});
	
	$('#arrow-view-right').click(function(e) {
		e.preventDefault();
		rotateRight();
		return false;
	});
	
	$('a.selectlink').click(function() {
		$(this).parent('li').siblings('li').removeClass('first');
		$(this).parent('li').addClass('first');
		$(this).parent('li').addClass('first');
		$(this).parent('li').addClass('first');
		$(this).parent('li').addClass('first');
		$(this).parent('li').addClass('first');
			$('#start-dealer ul li:not(.first)').toggle();
		var href = $(this).attr('href');
		href = href.substr(1,href.length);
		var rel = $(this).attr('rel');
		$(rel).val(href);
		//alert($(rel).val());
		
		return false;
	});
	
	$('#start-dealer ul li:not(.first)').hide();
	
	$('#jackport-main').mousemove(function(e) {
		showanim = false;
		var pos = $(this).position();
		var pos2 = $('#container').position();
		var imgpos = e.clientX-pos.left-pos2.left;
		
		var newpos = parseInt(imgpos/15);
		
		//console.log(newpos);
		if (newpos != lastpos) {
			if (newpos < lastpos) rotateLeft();
			if (newpos > lastpos) rotateRight();
			lastpos = newpos;
		}
	});
	window.setTimeout("initAnimation()",1500);
	
});

var rotatecount = 8;
var rotated = 0;
var showanim = true;
function initAnimation() {
	if (showanim && rotatecount >  rotated) {
		rotated++;
		rotateRight();
		window.setTimeout("initAnimation()",300);
	}
}

function rotateRight() {
	var color = $('#jackport-main').attr('mycolor');
	var where = $('div#container').attr('rel');
	var side = $('div#jackport-main').attr('rel');
	var next = rotateOrderRight[side];
	if (where != 'home') return;
	$('#jackport-main img').attr('src','/fileadmin/templates/jackport/picker/'+where+'/'+next+'/'+color+'.jpg');
	$('div#jackport-main').attr('rel',next);
}

function rotateLeft() {
	var color = $('#jackport-main').attr('mycolor');
	var where = $('div#container').attr('rel');
	var side = $('div#jackport-main').attr('rel');
	var next = rotateOrderLeft[side];
	if (where != 'home') return;
	$('#jackport-main img').attr('src','/fileadmin/templates/jackport/picker/'+where+'/'+next+'/'+color+'.jpg');
	$('div#jackport-main').attr('rel',next);
}

