var tw;
var th;
var t2;
var ww=0;
var wh=0;
var wf=0;
var thumbid="thumb1";

function changeImage(imgId, imageUrl) {
	
	var imgTagElement = document.getElementById(imgId);
	imgTagElement.src = imageUrl;
	
}


function validate_field(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false;}
}
}

function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2) 
  {alert(alerttxt);return false;}
}
}


function checkSearch(thisform)
{
with (thisform)
{
	if (Manufacturer.value=="")
		if (validate_field(Name,"Neužpildytas laukas!")==false)
  		{
  			Name.focus();
  			return false;
  		}

}
}


function pause(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
} 

var tc;

function showcaption(obj, pos)
{
	var obj = document.getElementById(obj);
	var objname = obj.id;
	if (wf==0)
	{
	if ((ww==0)&&(wh==0))
		pos = pos + 3;
		obj.style.top=pos+"px";
	}
	if (pos<12)
		tc = setTimeout('showcaption(\''+objname+'\','+pos.toString()+')',1);
	else
		clearTimeout(tc);
}

function ChangeAlbumPhoto(thumb, caption, pict, pobj, obj, w, h)
{
	
	document.getElementById(thumbid).className="thumbs_inactive";
	thumb.className="thumbs_active";
	thumbid = thumb.id;
	imgcaption = document.getElementById('imagecaption');
	imgcaption.style.top="-3px";
	//document.getElementById('imagecaption').style.height="20px";
	imgcaption.innerHTML = caption;
	imgcaption.style.width="200px";
	imgcaption.style.top=-10-imgcaption.clientHeight+"px";
	
	//alert(document.getElementById('imagecaption').clientHeight);
	
	if (t2)
		clearTimeout(t2);
	if (tw)
		clearTimeout(tw);
	if (th)
		clearTimeout(th);
	//document.getElementById('loader').style.display = 'inline';
	obj.src='img/loading.gif';
	if (w > h)	{
		if (w >= 400) {
			var k = 400 / w;
			h = Math.ceil(h * k);
			w = Math.ceil(w* k);
		}
	}
	else
		if (h >= 400) {
			var k= (400 / h);
			w = Math.ceil(w* k);
			h = Math.ceil(h * k);
		}
		
	//alert(w.toString()+' '+ h.toString() );
	obj.style.filter='alpha(opacity = 0)';
	obj.style.opacity = 0;
	obj.src = pict;
	
	var ow = parseFloat(pobj.style.width);
	var oh = parseFloat(pobj.style.height);
	//alert(pobj.id);
	/*if (ow > (w+20)) {
	for (i=ow; i>(w+20); i--)
		pobj.width =  i;
	}
	else
	for (i=ow; i<(w+20); i++)
		pobj.width = i;
	*/
	if (isNaN(ow))
		ow = 400;
	if (isNaN(oh))
		oh = 400;
	//alert(oh.toString()+' '+h.toString());
	//alert(ow.toString()+' '+w.toString());
	if (h % 4 ==1)
		h = h - 1;
	else
	if (h % 4 ==2)
		h = h + 2;
	else
	if (h % 4 ==3)
		h = h + 1;
		
	if (w % 4 ==1)
		w = w - 1;
	else
	if (w % 4 ==2)
		w = w + 2;
	else
	if (w % 4 ==3)
		w = w + 1;
	//alert(oh.toString()+' '+h.toString());
	//alert(ow.toString()+' '+w.toString());
		
	ww=1;
	wh=1;
	setwidth(pobj.id, ow, w, 0);
	setheight(pobj.id, oh, h, 0);
	
	//setheight2(pobj.id, oh, h, 0);
	/*if (oh > (h+20)) {	
	for (i=oh; i>(h+20); i--)
		
		pobj.height = i;
	}
	else
	for (i=oh; i<(h+20); i++)
		pobj.height = i;
		*/
	//alert(pobj.id);
	
	obj.width = w;
	obj.height = h;
	
		var objname = obj.id;
		//alert('fadein('+objname+','+i.toString()+')');
	
		fadein(objname, 0, 100,1);
		//t = setTimeout('fadein(\''+objname+'\',0,100)',8);
		//setTimeout('fadein(\''+objname+'\',0,100)', 2000);
		showcaption(imgcaption.id, -10-imgcaption.clientHeight);
	
}

function setwidth(obj, oh, h, ml){
	var obj = document.getElementById(obj);
	var objname = obj.id;
	
	//alert(oh.toString()+' '+h.toString());
	
	obj.style.width=oh+"px";
	if (wh ==0){
	
	if (oh > h)
		oh = oh - 2;
	else
		if (oh < h)	
			oh = oh + 2;
	}
	
	if (oh!=h)
		{ 
			tw = setTimeout('setwidth(\''+objname+'\', '+oh.toString()+', '+h.toString()+')',ml);
			ww =1;
		}
	else
	{
		clearTimeout(tw);
		ww = 0;
		document.getElementById('loader').style.display = 'none';
	}
}

function setheight(obj, oh, h, ml){
	var obj = document.getElementById(obj);
	var objname = obj.id;
	
	//alert(oh.toString()+' '+h.toString());
	
	obj.style.height=oh+"px";
	
	if (oh > h)
		oh = oh - 2;
	else
		if (oh < h)
			oh = oh + 2;
	
	if (oh!=h){
		th = setTimeout('setheight(\''+objname+'\', '+oh.toString()+', '+h.toString()+')',ml);
		wh = 1;
	}
	else
	{
		clearTimeout(th);
		wh = 0;
	}
}

function setheight2(obj, oh, h, ml){
	var obj = document.getElementById(obj);
	var objname = obj.id;
	
	//alert(oh.toString()+' '+h.toString());
	
	
	
	if (oh > h)
	{
		for (i=oh; i>h; i--);
			obj.style.height=i+"px";
	}
	else
		for (i=oh; i<=h; i++)
			obj.style.height=i+"px";
			
		wh = 0;
	//}
}

function fadein(obj, p1, p2, ml)
{
	
	var obj = document.getElementById(obj);
	var objname = obj.id
	if ((ww==0)&&(wh==0))
	{
		obj.style.opacity = p1/100;
		obj.style.filter='alpha(opacity ='+p1.toString()+')';
		p1 = p1 + 4;
	}
	if (p1<p2)
	{
		t2 = setTimeout('fadein(\''+objname+'\','+p1.toString()+','+p2.toString()+')',ml);
		wf = 1;
	}
	else
	{
		clearTimeout(t2);
		wf = 0;
		
	}
	
}

function fadeout(obj, p1, p2, ml)
{
	
	var obj = document.getElementById(obj);
	var objname = obj.id
	obj.style.opacity = p1/100;
	obj.style.filter='alpha(opacity ='+p1.toString()+')';
	if ((ww==0)&&(wh==0))
		p1 = p1 - 1;
	if (p1>p2)
		t2 = setTimeout('fadeout(\''+objname+'\','+p1.toString()+','+p2.toString()+')',ml);
	else
		clearTimeout(t2);
	
}

var ts; 

function thumb_slide_right(thumbs){
	var obj = document.getElementById(thumbs);
	
	//if (ts)
	//	clearTimeout(ts);
		
	ts = setTimeout('moveThumbs(\''+obj.id+'\', -7)',500);
}

function thumb_slide_left(thumbs){
	var obj = document.getElementById(thumbs);
	
	//if (ts)
	//	clearTimeout(ts);
		
	ts = setTimeout('moveThumbs(\''+obj.id+'\', 7)',500);
}

function moveThumbs(obj, tpos)
{
	var obj = document.getElementById(obj);
	//alert(obj.style.left);
	
	if (obj.style.left=="")
		obj.style.left="0px";
	
	if (tpos<0) {
		if (parseInt(obj.style.left)+(((parseInt(document.getElementById('lastthumb').name)-5)*92))>-20)
			obj.style.left = (parseInt(obj.style.left) + tpos)+"px";
		ts = setTimeout('moveThumbs(\''+obj.id+'\', '+tpos.toString()+')',20);
	}
	else
	{
		if (parseInt(obj.style.left)<20)
			obj.style.left = (parseInt(obj.style.left) + tpos)+"px";
			
		ts = setTimeout('moveThumbs(\''+obj.id+'\', '+tpos.toString()+')',20);
	}
	
}
function stopThumbs()
{
	if (ts)
		clearTimeout(ts);
}