var FLOW_WIDTH = 656;
var MOVE_SPEED = 3;
var INFO_WIDTH = 150;

var velocity = 0;
var flow = new Object();
var flow_tr = new Object();

var img_count = 0;
var total_width = 0;
var ideal_tail = 0;

var pos = -0;
var slowdown = false;

var lastmove;
var freemove;

var mouse_div = '';


function getImageWidth(index) {
	
	return flow_tr.cells[index].firstChild.firstChild.width + 20;
	
}


function overflowLeft() {
	
	pos = pos + getImageWidth(0);
	flow_tr.appendChild(flow_tr.cells[0]);
	flow.style.left = pos + "px";
	
}

function overflowRight() {
	
	pos = pos - getImageWidth(img_count-1);
	flow_tr.insertBefore(flow_tr.cells[img_count-1], flow_tr.cells[0]);
	flow.style.left = pos + "px";
	
}


function handleOverflow() {
	
	var actual_left_tail = Math.abs(pos);
	var actual_right_tail = total_width - Math.abs(pos) - FLOW_WIDTH;
	
	if (actual_left_tail < ideal_tail) {
		//alert('doplnuju pravou: ' + ideal_tail);
		overflowRight();
		return 0;
	}
	
	//alert('ideal: ' + ideal_tail + ' actual: ' + actual_right_tail);
	if (actual_right_tail < ideal_tail) {
		//alert('doplnuju levou - actual tail' + actual_right_tail + ' ideal tail: '+ ideal_tail + ' pos: ' + pos + ' total_width: ' + total_width + ' flow width: ' + FLOW_WIDTH);
		overflowLeft();
		return 0;
	}
	
	return 1;
	
}


function move() {
	
	handleOverflow();
	
	pos += Math.ceil(MOVE_SPEED * velocity);
	
	flow.style.left = pos + "px";
	
	if (velocity != 0) {
		lastmove = window.setTimeout('move();',20);
	}
	
}

function velUp() {
	
	if (slowdown == false && Math.abs(velocity) < 1) {
		velocity = velocity * 1.5;
		window.setTimeout('velUp();',20);
	}
	
}

function velDown() {
	
	if (slowdown == true) {
		if (Math.abs(velocity) < 0.1) {
			velocity = 0;
			// pridano pro freemove
			freemove = window.setTimeout('freeMove();',2000);
		}
		
		
		if (Math.abs(velocity) > 0) {
			velocity = velocity  / 1.03;
			window.setTimeout('velDown();',20);
		}
	}
	
}

function startMoving(direction) {
	
	slowdown = false;
	
	// pridano pro freemove
	stopFreeMove();
	
	if (lastmove != null) {
		window.clearTimeout(lastmove);
	}
	
	if (direction == 'left') {
		velocity = + 0.1;
		window.setTimeout('move();',20);
		window.setTimeout('velUp();',20);
		
	} else {
		velocity = - 0.1;
		window.setTimeout('move();',20);
		window.setTimeout('velUp();',20);
	}
	
}

function stopMoving() {
	
	slowdown = true;
	window.setTimeout('velDown();',20);
	
}

function getTotalWidth() {
	
	for (x=0;x<=img_count-1;x++) {
		total_width += getImageWidth(x);
	}
	
}


function freeMove() {
	
	if (lastmove != null) {
		window.clearTimeout(lastmove);
	}
	
	velocity = -0.4;
	lastmove = window.setTimeout('move();',20);
	stopFreeMove();
}

function stopFreeMove() {
	
	if (lastmove != null) {
		window.clearTimeout(lastmove);
	}
	
	if (freemove != null) {
		window.clearTimeout(freemove);
	}
	
	velocity = 0;
}

function mouseAction() {
	if (mouse_div == 'out') {
		freeMove();
	} else if (mouse_div == 'in') {
		stopFreeMove();
	}
}

function flowLoad() {
	
	// nastavit pro js
	var gallery_block = document.getElementById('gallery_block')
	gallery_block.style.overflow = 'hidden';
	gallery_block.style.height = '81px';
	document.getElementById('button_left').style.display = 'block';
	document.getElementById('button_right').style.display = 'block';
	
	flow = document.getElementById('gallery_flow');
	flow_tr = document.getElementById('flow');
	
	img_count = flow_tr.cells.length;
	getTotalWidth();
	ideal_tail = 1.8 * (total_width/img_count);
	
	//while (!handleOverflow());
	
	//freeMove();
}


/* extension */


function roll(div, width, step) {
	
	div.style.width = Math.ceil(width) + 'px';
	
	opacity = (width * 100 / INFO_WIDTH)/10;
	
	div.style.opacity = (opacity)/10;
	div.style.filter = 'alpha(opacity=' + (opacity)*10 + ')';
	
	width += width * Math.sqrt(0.2*step);
	
	
	
	step += 0.2;
	
	if (width < INFO_WIDTH) {
		
		window.setTimeout(function () {roll(div, width, step)}, 25);
		
	} else {
		
		div.style.opacity = 1;
		div.style.filter = 'alpha(opacity=' + 100 + ')';
		div.style.width = Math.ceil(INFO_WIDTH) + 'px';
		
	}                                                
	
	
}


function showInfo(img) {
	
	var block = img.parentNode.nextSibling;
	
	block.style.display = 'block';
	
	roll(block, 1, 1);
	
	//block.style.width = '150px';
	
	
}

function hideInfo(img) {
	
	var block = img.parentNode.nextSibling;
	
	block.style.width = '0px';
	block.style.display = 'none';
}

