/*
 * Přepne na požadovaný layer
 */ 
function switchLayers(set_layer_state) {

	if (able_to_scroll) {
		var layer_to_reduce;
		var layer_to_expand;
		
		// Pokud je požadovaný layer shodný s aktivním, jen vytvoříme zavírací křížek.
		if (active_layer_state == set_layer_state) {
			createClosingCross(set_layer_state);
			return
		}
		
		if (set_layer_state == 0) {
			layer_to_reduce = active_layer_state;
			layer_to_expand = null;
		} else {
			layer_to_reduce = active_layer_state;
			layer_to_expand = set_layer_state;		
		}
		
		if (set_layer_state != 0) {
			layers.style.width = scroll_max_width + "px";	
		}
		
		scrollLayers(layer_to_reduce, layer_to_expand);	
							
		active_layer_state = set_layer_state;
	}
}


/*
 * Funkce pro ovládání scrollingu
 */
var visual = '';
function scrollLayers(layer_to_reduce, layer_to_expand) {
   visual = layer_to_expand;

	curr_scrolling_step = scroll_min;

	layer_to_reduce_id = "layer" + layer_to_reduce;
	layer_to_expand_id = "layer" + layer_to_expand;
	
	if (getElem(layer_to_reduce_id)) {
		layer_to_reduce_object = getElem(layer_to_reduce_id);
	} else {
		layer_to_reduce_object = null;		
	}
	
	if (getElem(layer_to_expand_id)) {
		layer_to_expand_object = getElem(layer_to_expand_id);
	} else {
		layer_to_expand_object = null;
	}
	scrolling = setInterval('interval_scrollLayers()', interval_speed);
}

/*
 * Interval scorllingu.
 */
function interval_scrollLayers() {
	
	able_to_scroll = false;

	if (layer_to_reduce_object != null) {
		reduce_width = ( (curr_scrolling_step * -1) + scroll_max );
		layer_to_reduce_object.style.width = reduce_width + "px";
	} else {
		reduce_width = 0;
	}

	if (layer_to_expand_object != null) {
		expand_width = curr_scrolling_step + scroll_min;
		layer_to_expand_object.style.width = expand_width + "px";
	} else {
		expand_width = 0;
	}
	
//	alert(layer_to_expand_object.style.width);
	
	curr_scrolling_step = curr_scrolling_step + scroll_step;
	
	if (curr_scrolling_step + scroll_step > scroll_max - 1) {
		stopScrolling();
    
      //alert(layer_to_expand_object.style.width);
	}
}

function stopScrolling() {
   // getElem('content').removeAttribute('class'); // IE neumi
   remClass('content', 'visual1', true);
   remClass('content', 'visual2', true);
   remClass('content', 'visual3', true);
   if(visual){
      addClass('content', 'visual'+visual, false);
      //alert(visual);
   }
   
	if (isElement(layer_to_expand_object)) {
		layer_to_expand_object.style.width = scroll_max + "px";	
	}
	if (isElement(layer_to_reduce_object)) {	
		layer_to_reduce_object.style.width = scroll_min + "px";
	}
	clearInterval(scrolling);	
	
	if (active_layer_state == 0) {
		layers.style.width = scroll_min_width + "px";
	}
	
	able_to_scroll = true;	
	
	createClosingCross(active_layer_state);	
}

/*
 * TODO:
 * Na zvoleném layeru vytvoříme zavírací křážek.
 * Ten vznikne úpravou divu active-zone.
 */
function createClosingCross(layer) {
	for (i=1; i<4; i++) {
		var curr_layer = getElem('layer' + i);
		curr = getElementsByClass('active-zone', 'div', 'layer'+i);
		if (curr.length > 0) {
			if (i == layer) {
				addClass(curr[0].id, 'off', false);
				curr[0].onclick =  function() { switchLayers(0); }
			} else {
				remClass(curr[0].id, 'off', true);
				curr[0].onclick =  function() { switchLayers(this.id.replace('active-zone-', '')); }				
				//curr[0].onmouseover =  function() { switchLayers(this.id.replace('active-zone-', '')); }				
			}			
		}
	}
}