/**
 * Prekresli zalozky tak, aby se vesly do zadane sirky, pokud se tam 
 * vsechny nevejdou, zobrazi se tlacitko pro posunuti zobrazeni vpravo
 * pripadne vlevo.
 *
 * @param int bkm_cnt - pocet zalozek
 * @param string bkm_id - cast atributu id html elementu zalozky (cele id dostaneme takto: bkm_id + poradove cislo zalozky)
 * @param int from_bkm - poradove cislo zalozky, od ktere se zalozky zobrazi
 * @param int active_bkm - poradove cislo aktivni zalozky
 * @param int width - sirka, do ktere se zalozky zobrazuji
 * @return void
 */
function rePaintBookmarks(bkm_cnt,bkm_id,width)
{
	// obecne promenne
	var i,current_bkm_width, borders_width, width_increment;
	var width_difference = width;
	var display_bkm_cnt = 0;
	var from_bkm = parseInt(document.getElementById('current_first_bookmark').value);
	var active_bkm = parseInt(document.getElementById('current_active_bookmark').value);
	
	// pole sirek jednotlivych zalozek, ktere budou zobrazeny
	var bkm_width = new Array();
	for (i=from_bkm; i<bkm_cnt; i++) {
		current_bkm_width = getElementWidth(bkm_id+i);
		
		//alert(current_bkm_width);
		// minimum width
		//if (current_bkm_width<30) current_bkm_width=30;
		
		if (current_bkm_width > width_difference) {
			break;
		}
		
		bkm_width[i] = current_bkm_width;
		width_difference -= current_bkm_width;
		display_bkm_cnt++;
	}
	
	// z poctu zalozek, ktere budou zobrazeny, zjistime jakou sirku zaberou bordery zalozek a odecteme ji od width_difference
	borders_width = display_bkm_cnt + 1;
	width_difference -= borders_width;
	
	// z width_difference a poctu zobrazovanych zalozek spocitame, o jakou sirku je treba kazdou zalozku rozsirit, aby se 
	// akorat vesly do zadane sirky
	width_increment = Math.round(width_difference / display_bkm_cnt);
	
	// upravime sirky v poli zobrazovanych zalozek a spocitame kontrolni rozdil sirek
	width_difference = width;
	for (i=from_bkm; i<display_bkm_cnt+from_bkm; i++) {
		bkm_width[i] += width_increment;
		width_difference -= bkm_width[i];
	}
	width_difference -= borders_width;

	// pokud je rozdil sirek ruzny od nuly, upravime prvni zobrazovanou zalozku
	if (width_difference != 0) {
		bkm_width[from_bkm] += width_difference;
	}
	
	// zobrazeni sipek, pokud se do zadane sirky nevejdou vsechny zalozky
	if (from_bkm != 0) {
		document.getElementById('left_arrow').style.display = "block";
	}
	else {
		document.getElementById('left_arrow').style.display = "none";
	}
	if (bkm_cnt - from_bkm > display_bkm_cnt) {
		document.getElementById('right_arrow').style.display = "block";
	}
	else {
		document.getElementById('right_arrow').style.display = "none";
	}
	
	// prekresleni zalozek, ktere maji byt zobrazeny
	for (i=from_bkm; i<display_bkm_cnt+from_bkm; i++) {
		if (i == active_bkm) {
			document.getElementById(bkm_id+i).style.backgroundColor = "#203564";
			document.getElementById(bkm_id+i).style.backgroundImage = "url(\'/styles/product_detail_img/bkm_active_background.png\')";
			document.getElementById(bkm_id+i).style.paddingTop = "12px";
			document.getElementById(bkm_id+i).style.top = "0px";
			document.getElementById(bkm_id+i).style.border = "1px solid black";
			document.getElementById(bkm_id+i).style.cursor = "default";
		}
		else {
			document.getElementById(bkm_id+i).style.backgroundColor = "#779ec9";
			document.getElementById(bkm_id+i).style.backgroundImage = "url(\'/styles/product_detail_img/bkm_background.png\')";
			document.getElementById(bkm_id+i).style.paddingTop = "7px";
			if (from_bkm <= active_bkm && active_bkm < display_bkm_cnt + from_bkm) {
				document.getElementById(bkm_id+i).style.top = "5px";
			}
			else {
				document.getElementById(bkm_id+i).style.top = "0px";
			}
			document.getElementById(bkm_id+i).style.border = "none";
			document.getElementById(bkm_id+i).style.borderTop = "1px solid black";
			if (i > active_bkm) {
				document.getElementById(bkm_id+i).style.borderRight = "1px solid black";
				if (i == from_bkm) {
					document.getElementById(bkm_id+i).style.borderLeft = "1px solid black";
				}
			}
			else if(i < active_bkm) {
				document.getElementById(bkm_id+i).style.borderLeft = "1px solid black";
				if (i == display_bkm_cnt + from_bkm - 1) {
					document.getElementById(bkm_id+i).style.borderRight = "1px solid black";
				}
			}
			document.getElementById(bkm_id+i).style.cursor = "pointer";
		}
		
		document.getElementById(bkm_id+i).style.position = "relative";
		document.getElementById(bkm_id+i).style.borderBottom = "none";
		document.getElementById(bkm_id+i).style.backgroundRepeat = "repeat-x";
		document.getElementById(bkm_id+i).style.backgroundPosition = "top left";
		document.getElementById(bkm_id+i).style.width = bkm_width[i]+"px";
	}
	
	// skryti zalozek, ktere nemaji byt zobrazeny
	for (i=0; i<bkm_cnt; i++) {
		if (isNaN(bkm_width[i])) {
			document.getElementById(bkm_id+i).style.display = "none";
		}
		else {
			document.getElementById(bkm_id+i).style.display = "block";
		}
	}
	
	// zobrazeni aktualniho divu s vlastnostmi
	showActualElement('phone_description_block',active_bkm,bkm_cnt);
}

/**
 * Vrati potrebnou sirku elementu, aby se jeho obsah zobrazil v jednom radku
 *
 * @param string id - id elementu
 * @return int
 */
function getElementWidth(id) {
	var content = document.getElementById(id).innerHTML;
  var width = 0;

  // minimal length 12 symbols
  if (content.length>12)
	 width = Math.round(50 * Math.log(content.length) - 400/content.length);
  else
	 width = Math.round(50 * Math.log(12) - 400/12);
    
	return width;
}

/**
 * Nastavi aktualni poradova cisla zvolene zalozky a prvni zobrazene zalozky
 *
 * @param active_bkm - poradove cislo prave zvolene zalozky
 * @param first_bkm - poradove cislo zalozky, ktera se ma zobrazit jako prvni
 * @return void
 */
function setBookmarkOrdinalNumber(active_bkm,first_bkm)
{
	// nastavi poradove cislo aktualne zvolene zalozky
	if (active_bkm >= 0) {
		document.getElementById('current_active_bookmark').value = active_bkm;
	}
	
	// nastavi poradove cislo prvni zobrazene zalozky
	if (first_bkm >= 0) {
		document.getElementById('current_first_bookmark').value = first_bkm;
	}
}

/**
 * Zobrazi element s poradovym cislem actual_id a ostatni elementy skryje
 *
 * @param id - textova cast atributu id skupiny elementu
 * @param actual_id - poradove cislo elementu, ktery ma byt zobrazen
 * @param elem_cnt - celkovy pocet elementu
 */
function showActualElement(id,actual_id,elem_cnt)
{
	for (i=0; i<elem_cnt; i++) {
		if (actual_id == i) {
			document.getElementById(id+i).style.display = "block";
		}
		else {
			document.getElementById(id+i).style.display = "none";
		}
	}
}

/**
 * Zobrazi aktualni div s cenou (cenami) telefonu s pausalem (pausaly)
 *
 * @param id - textova cast atributu id skupiny elementu
 * @param actual_id - poradove cislo elementu, ktery ma byt zobrazen
 * @param elem_cnt - celkovy pocet elementu
 */
function showActualPrivilegePrices(id,actual_id,elem_cnt)
{
	for (i=0; i<elem_cnt; i++) {
		if (actual_id == i) {
			document.getElementById(id+i).style.display = "block";
			// styly zobrazeni ceny
			/*document.getElementById('prices_list_item'+i).style.backgroundImage = "url('styles/product_detail_img/price_arrow_blue.png')";*/
			document.getElementById('prices_list_item'+i).style.cursor = "default";
			document.getElementById('prices_list_item'+i).style.color = "#194F95";
		}
		else {
			document.getElementById(id+i).style.display = "none";
			// styly zobrazeni ceny
			/*document.getElementById('prices_list_item'+i).style.backgroundImage = "url('styles/product_detail_img/price_arrow_black.png')";*/
			document.getElementById('prices_list_item'+i).style.cursor = "pointer";
			document.getElementById('prices_list_item'+i).style.color = "black";
		}
	}
}
