var searchDetailBox = 0;
var expandStates = 0;
var expandHotels = 0;
var expandInfo = 0;
var menuDivId = '';
var termTable = '';

var liStateDispId = 'liStateDisp';
var liStateHideId = 'liStateHide';

var selects = new Array();

var lastProcess = new Date();


if (window.attachEvent) { 
	window.attachEvent("onload", init); 
} else {
	window.addEventListener('load',init,false); 
}



/*
 * Vola se po nacteni stranky
 */
function init()
{
	displayDetailSearch(0, null, '');
	menu();
	initMenu();
	changeTerms();
	initHotelPerexes();
}


mOption = function(arg)
{
	this.text = arg.text;
	this.value = arg.value;
	this.index = arg.index;
}

function toHTMLOption(arg)
{
	op = document.createElement('option');
	op.text = arg.text;
	op.value = arg.value;
	//op.index = arg.index;
	return op;
}

mSelect = function(select)
{
	this.id = select.id;
	this.select = select;
	this.data = new Array();
	this.length = this.select.length;
	for (i = 0; i < this.select.length; i++) { 
		//select.options[i].text = select.options[i].text + ': ' + i;	
		this.data[i] = new mOption(select.options[i]);

	}
	this.act = null;
	
};

function selectProcess(sel, htmlSel)
{
	
	// funguje jen ve FF - proto se to prozatim pro ostatni browsery neprovadi
	if(document.all)
		return;

	
 	// alert("aaaaaaaaaaaa");
	visible = 0;
	val = htmlSel.value;
	nowTime = new Date();
	if (nowTime.getTime() - 60 < lastProcess.getTime()) { 
		//document.getElementById('catalog').innerHTML += "=== " + val + "===";
		return;
	}
	lastProcess = nowTime;
	//document.getElementById('catalog').innerHTML += ': OK';
	actPos = 0;
	activeIndex = 0;
	//alert(val);
	// zjisteni, kolikaty radek se ma vypsat + mazani	
	delPos = 0;
	for (i = sel.length-1; i >= 0; i--) { 
		//document.getElementById('catalog').innerHTML += i + ': ' + sel.data[i].index + ', ';

		if (sel.data[i].value == val) { 
			actPos = sel.data[i].index;
			activeIndex = i;
			delPos++;
		}
		// else { 
		if (i - delPos >= 0) { 
			htmlSel.remove(i - delPos);
		}
		///}
	}
 		
	
	// vsechny polozky jsou odstraneny - krome vybrane polozky
	hV = Math.floor(visible/2);
	//return;
	
	from = sel.length + actPos + 1;
	to = 2 * sel.length + actPos;
	
	activePos = 0;
	//return;
	
	j = 1;
	i = 0;
	for (i = from; i < to; i++) { 
		pos = i % sel.length;
		//if (pos != 0) { 

			if (sel.data[pos].index == actPos) { 
				//htmlSel.options[j].selected = true;
				activePos = pos;
			} else { 
				htmlSel.options[j] = toHTMLOption(sel.data[pos]);			
				htmlSel.options[j].selected = false;	
				// document.getElementById('catalog').innerHTML += j + ', ';
						j++;
			}
			//htmlSel.options[j].index = j;

		//}

	}
	//htmlSel.options[sel.length - 1] = 
	
	//this.act = sel.data[activePos];
	//	htmlSel.selectedIndex = 1;
	//alert(activeIndex);
	//htmlSel.options[0] = toHTMLOption(sel.data[0]);
	//htmlSel.value = sel.data[activePos].value;
	//htmlSel.text = sel.data[activePos].text;
	
	//	document.getElementById('catalog').innerHTML += activePos + ', ' + activeIndex + ';' + htmlSel.value + '<br>';
	

}




function menu()
{
	i = 0;
	var regHid = /\bmHid\b/;
	var lis = document.getElementsByTagName("li");
	while (li = lis[i++]) { 
		if( regHid.test(li.className) ) { 
			if (li.hasChildNodes()) {
				li.childNodes[0].onclick = mTrik;
			}
		}
	}
}

function initMenu()
{
	i = 0;
	var regHid = /\bmHid\b/;
	menu = document.getElementById(menuDivId); 

	if (menu) { 
		var h2s = menu.getElementsByTagName("h2");
		while (h2 = h2s[i++]) { 

			if( regHid.test(h2.className) ) { 
				h2.onclick = mTrik;
			}
		}

		el = document.getElementById('menuStatesUl');		
		if (el) { 
			if (!expandStates) { 
				el.style.display = 'none';
			} else { 
				el.style.display = '';			
			}	
		}

		el = document.getElementById('menuHotelsUl');		
		if (el) { 		
			if (!expandHotels) { 
			  	el.style.display = 'none';
			 } else { 
			 	el.style.display = '';
			 }
		}
	    
		el = document.getElementById('menuInfoUl');
		if (el) { 		
			if (!expandInfo) { 
			  	el.style.display = 'none';
			 } else { 
			 	el.style.display = '';
			 }
		}	
			  
		el = document.getElementById('mAbec');
		if ( el ) { 
			listHidden('mAbec', 4, 'Zobrazit další země...', 'Skrýt další země...');		
		}
	}
}


function mTrik()
{
	uls = this.parentNode.getElementsByTagName('ul');
	i = 0;
	while (ul = uls[i++]) { 
		mListManip(ul);
	}
}		

function mListManip(ul)
{
	if (ul.style.display == '') { 
		ul.style.display = 'none';
	} else { 
		ul.style.display = '';
	}
}


function NoveOkno(cislo, title, sirka, vyska, left, top, root) {
	resizable = srollbars = 0;

	var x=screen.availWidth-10;
	var y=screen.availHeight-30;
	if (sirka > x) {
		sirka = x;
		left = 0;
		resizable = srollbars = 1;
	}
	if (vyska > y) {
		vyska = y;
		top = 0;
		resizable = srollbars = 1;
	}

	okno = window.open('', '', 'fullscreen=0,titlebar=0,locationbar=0,toolbar=0,scrollbars='+srollbars+',location=0,statusbar=0,menubar=0,resizable='+resizable+',hotkeys=0,width='+sirka+',height='+vyska);
  okno.moveTo(left,top);
  okno.document.open("text/html", "replace");
	okno.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">');
	okno.document.write('<HTML>');
	okno.document.write('<HEAD><title>'+title+'</title></HEAD>');
	okno.document.write('<BODY BOTTOMMARGIN="0" TOPMARGIN="0" LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0" BGCOLOR="#FFFFFF">');
	okno.document.write('<img src="'+root+'/getimage.php?i='+cislo+'" border=0 onclick="window.close(self)">');
	okno.document.write('</body></html>');		
	okno.document.close();
	okno.focus();
}
function WinOpen(theURL, winName, sirka, vyska) {
newWindow = window.open(theURL, winName, 'toolbar=0,directories=no,menubar=0,location=no,scrollbars=yes,resizable=yes,width='+sirka+',height='+vyska);	}

/**
  Zobrazeni detailniho vyhledavaciho formulare
*/
function displayDetailSearch(change)
{
	el = document.getElementById('formDetailBox');
	if (!el) { 
		return;
	}
	if (!change) { 
		if (searchDetailBox) {
			el.style.display = 'block';
		} else { 
			el.style.display = 'none';	
		}
	} else { 
		if (el.style.display == 'block') { 
			el.style.display = 'none';	
		} else { 
			el.style.display = 'block';
		}
	}				
	
}



/**
	itemPob, itemPoz
	
*/
function lastChange(typ) 
{
	var pozClass = 'itemPoz';
	var pobClass = 'itemPob';
	
	var pobReg = /\bitemPob\b/;
	var pozReg = /\bitemPoz\b/;
	i = 0;
	
	dbg = 0;
	nowClass = '';
	chPoby = document.getElementById('chPoby');
	chPozn = document.getElementById('chPozn');
	
	// kontroly na chcecked musi byt obracene, protoze dojde k 
	// zaskrtnuti a az pak zavolani
	
	//alert(chPoby.checked);
	
	if (typ == 'poby') {
		if (!chPoby.checked) { 
			if (dbg) alert('Pob - nezaskrtnute');
			pobD = '';			
			pobC = ''; 
			pobVis = 'visible';
			pobPos = 'static';		
			
			pozD = '';			
			pozC = '';
			pozVis = 'visible';
			pozPos = 'static';			
		} else { 
			if (dbg) alert('Pob - zaskrtnute');
			pobD = '';
			pobC = 'checked'; 
			pobVis = 'visible';
			pobPos = 'static';
			
			pozD = 'none'
			pozC = "";
			pozVis = 'hidden';
			pozPos = 'absolute';			
		}	
	} else { 
		if (!chPozn.checked) { 
			if (dbg) alert('Poz - nezaskrtnute');		
			pozD = '';			
			pozC = ''; 
			pozVis = 'visible';
			pozPos = 'static';		
			
			pobD = '';			
			pobC = "";	
			pobVis = 'visible';
			pobPos = 'static';		
		} else { 
			if (dbg) alert('Poz - zaskrtnute');		
			pozD = '';
			pozC = 'checked'; 
			pozVis = 'visible';
			pozPos = 'static';		
			
			pobD = 'none'
			pobC = "";	
			pobVis = 'hidden';
			pobPos = 'absolute';		
		}
	}
	
	tbl = document.getElementById('tblListPoor');	
		
	var trs = tbl.getElementsByTagName("tr");
	
	r = 1;
	while (tr = trs[i++]) { 
		if( pobReg.test(tr.className) ) { 
			// tr.style.visibility = pobVis;
			// tr.style.position = pobPos;
			tr.style.display = pobD;
			
			nowClass = pobClass;
			
		} else if( pozReg.test(tr.className) ) { 
			// tr.style.visibility = pozVis;
			// tr.style.position = pozPos;			
			tr.style.display = pozD;
						
			nowClass = pozClass;
		}
		
		if (tr.style.display == '') { 
			tr.className = nowClass + (r % 2 ? " lichy" : "");
			r++;
		}
	}	
	
	chPoby.checked = pobC;
	chPozn.checked = pozC;
}

function changeMiniOrder(index)
{
	ePocetDni = document.getElementById('ordPocetDni');
	ePocetNoci = document.getElementById('ordPocetNoci');
	eTransport = document.getElementById('ordTransport');
	eAccomodation = document.getElementById('ordAccomodation');	
	eMeal = document.getElementById('ordMeal');
	eFrom = document.getElementById('ordTermFrom');
	eTo = document.getElementById('ordTermTo');	
	ePoznamka = document.getElementById('ordPoznamka');	
	ePoznamkaCeniku = document.getElementById('ordPoznamkaCeniku');	
	
	ePocetDni.textContent = ordData[index].pocetDni;
	ePocetNoci.textContent = ordData[index].pocetNoci;
	eTransport.textContent = ordData[index].transport;
	eAccomodation.textContent = ordData[index].accomodation;	
	eMeal.textContent = ordData[index].meal;
	eTo.textContent = ordData[index].to;
	eFrom.textContent = ordData[index].fromShort;	
	ePoznamka.textContent = ordData[index].poznamka;
	ePoznamkaCeniku.textContent = ordData[index].poznamkaCeniku;
	
	ordData['index'] = index;
	
	f = document.getElementById('miniOrderForm');

	f.setAttribute('action', ordData[index].order);
	
	changeMiniOrderPrice();
}

/**
 * Function called onsubmit in bMiniOrderForm. This function
 * changes action to which data are sent, it adds GET parameter
 * "ordPocetOsob" to set how many people are going to holiday in
 * cOrder1 order form.
 * @return true to send form
 */
function checkpeople(){
	f = document.getElementById('miniOrderForm');
	
	ordPocetOsob = document.getElementById('ordPocetOsob').value;
	ix = ordData['index'];
	//alert('index:'+ix+' link:'+ordData[ix].order+'&people='+ordPocetOsob);
	actn = ordData[ix].order+'&ordPocetOsob='+ordPocetOsob;
	f.setAttribute('action', actn);
	return true;
}

function changeMiniOrderPrice()
{
	t = document.getElementById('ordTermId');
	p = document.getElementById('ordPocetOsob');
	
	term = t.selectedIndex;
	// alert(term);
	
	osob = p.options[p.selectedIndex].value;
	// alert(osob);
	
	price = ordData[term]['price'] * osob;
	
	document.getElementById('ordPrice').textContent = price;
	document.getElementById('ordPrice_euro').textContent = roundNumber(price / euro);
}


function changeTerms()
{
	i = 0;
	if (!termTable) { 
		return;
	}

	table = document.getElementById(termTable);

	if (!table)  { 
		return;
	}
	var hid = 'termHidRow';
	var reg = /\btermHidRow\b/;
	var trs = table.getElementsByTagName("tr");
	
	r = 1;
	while (tr = trs[i++]) { 
		if( reg.test(tr.className) ) { 
			if (tr.style.display == 'none') { 
				tr.style.display = '';
			} else { 
				tr.style.display = 'none';
			}
			nowClass = hid+" ";			
		} else { 
			nowClass = "";
		}
		
		if (tr.style.display == '') { 
			tr.className = nowClass + (r % 2 ? " lichy" : "");
			r++;
		}
	}
}	

function itemSelect(sel, item)
{
	
	
	if (selects[sel.id] == null) { 
		selects[sel.id] = new mSelect(sel);
	}
	/*
	if (selects[sel.id].act) { 
		alert(selects[sel.id].text);
	}
	*/

}

/* ================ problem ============= */



function selectPressed(sel)
{
	// document.getElementById('catalog').innerHTML += '<br />pressed';
	//if (selects[sel.id] == null) { 
	selects[sel.id] = new mSelect(sel);
	
	//}
	selectProcess(selects[sel.id], sel);
}


/** 
 manipuluje se seznamem zemi, co se vypisuji ve sloupecku
 - sbalovani/rozbalovani
*/
function listHidden(id, count, aTextOn, aTextOff)
{
	
	el = document.getElementById(id);
	if (el == null || count < 1) { 
		return;
	}
	
	childs = el.childNodes;
	liPos = 0;
	firstChild = -1;
	added = false;
	dispId = 0;
	for (i = 0; i < childs.length; i++) { 	
		// for (i in childs) { 
		ch = childs[i];	
		
		if ( ch.nodeName == 'LI' ) { 
			if (firstChild == -1) { 
				firstChild = i;
			}
			
			if (liPos == count) { 
				li = document.createElement('li');
				a = document.createElement('a');
				a.href = 'javascript:mAlphaStates("'+id+'", '+count+', "'+aTextOn+'", "'+aTextOff+'", 1);';
				a.style.fontWeight = 'bold';
				
				t = document.createTextNode(aTextOn);
				a.appendChild(t);
				li.id = liStateDispId;
				li.appendChild(a);
				
				// el.insertBefore(li, el.childNodes[firstChild]);
				
				ne = li.cloneNode(true);				
	
				// v naklonovanem zmenime hodnoty
				//ne.childNodes[1].textContent = aTextOff;
				ne.id = liStateHideId;
				ne.childNodes[0].textContent = aTextOff;
				ne.childNodes[0].href = 'javascript:mAlphaStates("'+id+'", '+count+', "'+aTextOn+'", "'+aTextOff+'", 0);';
				ne.style.display = 'none';
				
				el.insertBefore(ne, el.childNodes[firstChild]);		
				el.insertBefore(li, ch);
				added = true;
				dispId = i;
			} else if (liPos > count) { 
				// pridaly 2 polozky => +1
				
				ch.style.display = 'none';
				// el.childNodes[i - 2].style.display = '';
				if (added) { 
					el.childNodes[dispId+1].style.display = '';
					added = false;
				}
				
			}
			liPos++;
		}	
	}
}

/** 
  funkce listHidde pridava u odkazu volani teto funkci
  skryje/zobrazuje polozky
  @param	aTextOn		text, co rika, ze se maji zobrazit dalsi (jsou skryte)
  @param	aTextOff	text, co rika, ze se maji skryt (jsou zobrazene)
*/
function mAlphaStates(id, count, aTextOn, aTextOff, disp)
{
	el = document.getElementById(id);
	if (el == null || count < 1) { 
		return;
	}
	
	if (disp) { 
		document.getElementById(liStateDispId).style.display = 'none';
		document.getElementById(liStateHideId).style.display = '';
	} else { 
		document.getElementById(liStateDispId).style.display = '';
		document.getElementById(liStateHideId).style.display = 'none';
	}
	
	
	liPos = 0;
	firstChild = -1;
	oldText = '';
	for (i in el.childNodes) { 
		ch = el.childNodes[i];
		if ( ch.nodeName == 'LI' ) {
			if (liPos > count) { 
				if (ch.id != liStateDispId && ch.id != liStateHideId) { 
					if (ch.style.display == 'none') { 
						ch.style.display = '';
					} else { 
						ch.style.display = 'none';
					}		
				}
			
			}
			liPos++;			
		}	
	}
}

function goTo(targ)
{
	inp = document.getElementById('goto');
	inp.value = targ;
	f = document.getElementById('orderForm');
	f.submit();
}

/* ================ problem ============= */

function getElementsByClassName(className, tag, elm)
{
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

function initTourPerexes()
{	
	el = document.getElementById('content-in');
	if (el == null) { 
		return;
	}
	elems = getElementsByClassName("tourPerex", 'div', el);
 
	
	for (i in elems) { 
		per = elems[i];
		first = true;
		for (j in per.childNodes) { 
			ch = per.childNodes[j];

			if (ch.nodeName == 'P') { 
				parts = ch.id.split("_");
				displayInfo(parts[1], 0);
				break;
			}
		}
	}
}

function displayInfo(tI, hI, imgUrl, title)
{
	actId = 'tP_'+tI+'_'+hI;
	el = document.getElementById(actId);
	p = el.parentNode;
	for ( i in p.childNodes ) { 
		ch = p.childNodes[i];
		if (ch.nodeName == 'P' || ch.nodeName == 'p') {
 
			if (ch.id == actId) { 
				
				ch.style.display = 'block';
				//ch.style.backgroundColor = 'red';
			} else {

				ch.style.display = 'none';
				//ch.style.backgroundColor = 'blue';

			}
		}
	}
	// proc nefunguje v MSIE? http://msdn2.microsoft.com/en-us/library/ms534643.aspx
	if (imgUrl) { 
		img = document.getElementById('tI_img_'+tI);
		if (img) { 
			img.src = imgUrl
		}
	}
	
	if (title) { 
		document.getElementById('tT_'+tI).innerHTML = title;
	}
}

function initHotelPerexes()
{	
	line = 1;
	con = true;
	while ( con ) { 
		con = displayHotelInfo(line, 0);
		line++;
	}
		
}

function displayHotelInfo(lineId, tabId)
{
	i = 0;
	while ( true ) { 
		tab = document.getElementById('tab_'+lineId+'_'+i);

		if (!tab) { 
			return (i > 0);
		}

		perex = document.getElementById('perex_'+lineId+'_'+i);
		detail = document.getElementById('detail_'+lineId+'_'+i);
		
		if ( i == tabId ) { 
			perex.style.display = 'block';
			detail.style.display = 'block';
			tab.className = tab.className.replace(/pasive/g, 'active');
		} else { 
			perex.style.display = 'none';
			detail.style.display = 'none';
			tab.className = tab.className.replace(/active/g, 'pasive');
		}			
		
		i++;
	}
}

//rounds number to dec places
function roundNumber(num) {
  //round precision
  var dec = 0;
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}