// JavaScript Document

function AutoCompleteDB()
{
	// set the initial values.
	this.bEnd = false;
	this.nCount = 0;
	this.aStr = new Object;
}

AutoCompleteDB.prototype.add = function(str)
{
	// increment the count value.
	this.nCount++;

	// if at the end of the string, flag this node as an end point.
	if ( str == "" )
		this.bEnd = true;
	else
	{
		// otherwise, pull the first letter off the string
		var letter = str.substring(0,1);
		var rest = str.substring(1,str.length);
		
		// and either create a child node for it or reuse an old one.
		if ( !this.aStr[letter] ) this.aStr[letter] = new AutoCompleteDB();
		this.aStr[letter].add(rest);
	}
}

AutoCompleteDB.prototype.getCount = function(str, bExact)
{
	// if end of search string, return number
	if ( str == "" )
		if ( this.bEnd && bExact && (this.nCount == 1) ) return 0;
		else return this.nCount;
	
	// otherwise, pull the first letter off the string
	var letter = str.substring(0,1);
	var rest = str.substring(1,str.length);
	
	// and look for case-insensitive matches
	var nCount = 0;
	var lLetter = letter.toLowerCase();
	if ( this.aStr[lLetter] )
		nCount += this.aStr[lLetter].getCount(rest, bExact && (letter == lLetter));
	
	var uLetter = letter.toUpperCase();
	if ( this.aStr[uLetter] )
		nCount += this.aStr[uLetter].getCount(rest, bExact && (letter == uLetter));
	
	return nCount;	
}

AutoCompleteDB.prototype.getStrings = function(str1, str2, outStr)
{
	if ( str1 == "" )
	{
		// add matching strings to the array
		if ( this.bEnd ) 
			outStr.push(str2);

		// get strings for each child node
		for ( var i in this.aStr )
			this.aStr[i].getStrings(str1, str2 + i, outStr);
	}
	else
	{
		// pull the first letter off the string
		var letter = str1.substring(0,1);
		var rest = str1.substring(1,str1.length);
		
		// and get the case-insensitive matches.
		var lLetter = letter.toLowerCase();
		if ( this.aStr[lLetter] )
			this.aStr[lLetter].getStrings(rest, str2 + lLetter, outStr);

		var uLetter = letter.toUpperCase();
		if ( this.aStr[uLetter] )
			this.aStr[uLetter].getStrings(rest, str2 + uLetter, outStr);
	}
}


function AutoComplete(aStr, oText, oDiv, nMaxSize)
{
	// initialize member variables
	this.oText = oText;
	this.oDiv = oDiv;
	this.nMaxSize = nMaxSize;
	
	// preprocess the texts for fast access
	this.db = new AutoCompleteDB();
	var i, n = aStr.length;
	for ( i = 0; i < n; i++ )
	{
		this.db.add(aStr[i]);
	}
			
	// attach handlers to the text-box
	oText.AutoComplete = this;
	oText.onkeyup = AutoComplete.prototype.onTextChange;
	oText.onblur = AutoComplete.prototype.onTextBlur;
}

AutoComplete.prototype.onTextBlur = function()
{
	this.AutoComplete.onblur();
}

AutoComplete.prototype.onblur = function()
{
	this.oDiv.style.visibility = "hidden";
}

AutoComplete.prototype.onTextChange = function()
{
	this.AutoComplete.onchange();
}

AutoComplete.prototype.onDivMouseDown = function()
{
	this.AutoComplete.oText.value = this.innerHTML;
}

AutoComplete.prototype.onDivMouseOver = function()
{
	this.className = "AutoCompleteHighlight";
}

AutoComplete.prototype.onDivMouseOut = function()
{
	this.className = "AutoCompleteBackground";
}

AutoComplete.prototype.onchange = function()
{
	var txt = this.oText.value;
	
	// count the number of strings that match the text-box value
	var nCount = this.db.getCount(txt, true);
	
	// if a suitable number then show the popup-div
	if ( (this.nMaxSize == -1 ) || ((nCount < this.nMaxSize) && (nCount > 0)) )
	{
		// clear the popup-div.
		while ( this.oDiv.hasChildNodes() )
			this.oDiv.removeChild(this.oDiv.firstChild);
			
		// get all the matching strings from the AutoCompleteDB
		var aStr = new Array();
		this.db.getStrings(txt, "", aStr);
		
		// add each string to the popup-div
		var i, n = aStr.length;
		for ( i = 0; i < n; i++ )
		{
			var oDiv = document.createElement('div');
			this.oDiv.appendChild(oDiv);
			oDiv.innerHTML = aStr[i];
			oDiv.onmousedown = AutoComplete.prototype.onDivMouseDown;
			oDiv.onmouseover = AutoComplete.prototype.onDivMouseOver;
			oDiv.onmouseout = AutoComplete.prototype.onDivMouseOut;
			oDiv.AutoComplete = this;			
		}
		this.oDiv.style.visibility = "visible";
	}
	else // hide the popup-div
	{
		this.oDiv.innerHTML = "";
		this.oDiv.style.visibility = "hidden";
	}
}

function createAutoComplete()
{
	var aNames =
	[
			"Ainaži", "Aizkraukle", "Aizpute", "Aknīste", "Aloja", "Alūksne", "Ape", "Auce", "Baldone", "Baloži", "Balvi", "Bauska", "Brocēni", "Cesvaine", "Cēsis", "Dagda", "Daugavpils", "Dobele", "Durbe", "Grobiņa", "Gulbene", "Ikšķile", "Ilūkste", "Jaunjelgava", "Jelgava", "Jēkabpils", "Jūrmala", "Kalnciems", "Kandava", "Kārsava", "Krāslava", "Kuldīga", "Ķegums", "Lielvārde", "Liepāja", "Limbaži", "Līgatne", "Līv�?ni", "Lub�?na", "Ludza", "Madona", "Mazsalaca", "Ogre", "Olaine", "Pāvilosta", "Piltene", "Pļaviņas", "Preiļi", "Priekule", "Rēzekne", "Rīga", "Rūjiena", "Sabile", "Salacgrīva", "Salaspils", "Saldus", "Saulkrasti", "Sigulda", "Seda", "Skrunda", "Smiltene", "Staicele", "Stende", "Strenči", "Subate", "Talsi", "Tukums", "Valdemārpils", "Valka", "Valmiera", "Vangaži", "Varakļāni", "Ventspils", "Viesīte", "Viļaka", "Viļāni", "Zilupe"
		];
	new AutoComplete(
		aNames, 
		document.getElementById('city'), 
		document.getElementById('sug'), 
		25
	);
}

function numbers(dimens,dir,pg){
var t_n = '';
if(dir>0){
var p=1;
}else{
var p=pg;
}
var xpag=0;
var dim_clean = new Array();
for(dick in dimens){
	if(isNumeric(dimens[dick])){
		dim_clean[xpag++] = dimens[dick];
	}
}
var ix=0
for (ix=0;ix<dim_clean.length;ix++)
{
var st = jQuery('#'+p).css('display');
if(st == 'none'){
	var k='';
}else{
	var k='act/';
}
	t_n += '<img src="userfiles/web/web_page/numbers/'+k+''+p+'.gif" alt="" onclick="pag('+p+')" />';
p++;}
return t_n;
}

function buildpage(divx){
var td = '';
var p=1;
var xpag=0;
var divpa = new Array();
for(dick in divx){
	if(isNumeric(divx[dick])){
		divpa[xpag++] = divx[dick];
	}
}
var nbs = numbers(divpa,0,p);
td += '<img hspace="20" src="userfiles/web/web_page/next.png" onclick="pag('+divpa[1]+')" />';
jQuery('.pagin').html([nbs, td].join());
//jQuery('#text div img').wrap('<div class="thumb-img"><div class="thumb-inner">' + '</div><div class="thumb-strip"></div><div class="thumb-zoom"></div></div>');
//jQuery(".thumb-img").bind("click", function(e){
 //     var img = jQuery('.thumb-inner img').attr('src');
	//  var w = jQuery('.thumb-inner img').attr('width');
	  //var h = jQuery('.thumb-inner img').attr('height');
	 // jQuery('#im_top').html(['<img src="'+img+'" alt="'+img+'" style="border:10px solid #333333;" />'].join());
	 // jQuery('#im_top').fadeIn("fast")
	 // jQuery('#im_top').css('width',w+'px');
	 // jQuery('#im_top').css('height',w+'px');
	 // jQuery('#im_top').css('top',(e.pageY-150)+'px');
	 // jQuery('#im_top').css('left','180px');
    //});
//jQuery('#im_top').bind("click", function(e){
	//jQuery(this).fadeOut();
//});
}

function pag(id){
var style = jQuery('#'+id).css('display');
var tdx='';
var p=1;
var l = divs.length;
if(style == 'none'){
	jQuery('#'+id).css('display','');
}else{
	jQuery('#'+id).css('display','none');
}
var cikls = new Array();
var poz=0;
for(dkd in divs){
	if(isNumeric(divs[dkd])){
		cikls[poz++] = divs[dkd];
	}
}
var ix=0
for (ix=0;ix<cikls.length;ix++)
{
if(cikls[ix]!=id){
	jQuery('#'+cikls[ix]).css('display','none');
}else
{
	if(p>1){
			tdx += '<img  src="userfiles/web/web_page/prev.gif" hspace="20" onclick="pag('+(p-1)+')" />';
			tdx += numbers(cikls,1,cikls.length);
			if(p<l && p!=cikls.length){
				tdx += '<img  hspace="20" src="userfiles/web/web_page/next.png" onclick="pag('+(p+1)+')" />';
			}
		}else{
			tdx += numbers(cikls,0,p);
			tdx += '<img  hspace="20" src="userfiles/web/web_page/next.png" onclick="pag('+(p+1)+')" />';
		}
}
p++;}
//for(zzk in divs){
//	if(isNumeric(divs[zzk]) && divs[zzk]!=id){
//		document.getElementById(divs[zzk]).style.display = 'none';
//	}else {
//		if(isNumeric(divs[zzk])){
//		if(p>1){
//			tdx += '<img  src="userfiles/web/w_page/registreties_iepirkumug-105.png" hspace="20" onclick="alert('+(p-1)+'); pag('+(p-1)+')" />';
//			if(p<l){
//				tdx += '<img  hspace="20" src="userfiles/web/w_page/nakosa_lpp.png" onclick="pag('+(p+1)+')" />';
//			}
//		}else{
//			tdx += '<img  hspace="20" src="userfiles/web/w_page/nakosa_lpp.png" onclick="pag('+(p+1)+')" />';
//		}
//		}
//	}
//p++;}
jQuery('.pagin').html([tdx].join());
}