function wait(ms) {
	var date = new Date();
	var curDate = null;
	do { curDate = new Date(); } while(curDate-date < ms);
} 

// =================== //
// gallery functions
function setOffset() {
	var thumbs = document.getElementById('thumbs');
	var cur = document.getElementById('current');
	var pid = document.getElementById('p_id');
	var offset = document.getElementById('offset');
	var num = 0;
	for(var i=0;i<thumbs.childNodes.length;i++) { 
		if (thumbs.childNodes[i].tagName == 'LI') { 
			num++;
			if (thumbs.childNodes[i].id == 'thumb-'+pid.value) { 
				cur.value = num; 
				thumbs.childNodes[i].className='active';
			} else {
				thumbs.childNodes[i].className='';
			};
		}
	}
	offset.value=eval( Math.floor((eval(cur.value)-1)/5)*5 );
}

function move(dir) {
	var thumbs = document.getElementById('thumbs');	
	var cur = document.getElementById('current');
	var pid = document.getElementById('p_id');	
	var offset = document.getElementById('offset');
	var thumbsbox = $('thumbs');
	var fx = thumbsbox.effects({duration: 1000, transition: Fx.Transitions.Cubic.easeOut});	
	var prev = $('prev');
	var next = $('next');
	var num = 0;
	var line = 5;
	var width = 97+2+1;
	var left = 1;
	var right = 1;

	for(var i=0;i<thumbs.childNodes.length;i++) { if(thumbs.childNodes[i].tagName == 'LI') { num++; } }

	if ((eval(offset.value)-line)<0) { left=0; prev.addClass('no'); } else { left=1; prev.removeClass('no'); }
	if ((eval(offset.value)+line)>num-1) { right=0; next.addClass('no'); } else { right=1; next.removeClass('no'); }

	if (dir==0) {
		thumbs.style.width = ((width) * num)+'px' ;
		thumbs.style.marginLeft = -eval(offset.value)*width+'px';
	} else {
		if ((dir==-1 && left) || (dir==1 && right)) {
			offset.value=eval(offset.value)+line*dir;
		}
			fx.start({
				'margin-left': -eval(offset.value)*width
			});
	}

	if ((eval(offset.value)-line)<0) { left=0; prev.addClass('no'); } else { left=1; prev.removeClass('no'); }
	if ((eval(offset.value)+line)>num-1) { right=0; next.addClass('no'); } else { right=1; next.removeClass('no'); }

}

function show(id) {
	var box = document.getElementById('photobox');
	var img = document.getElementById('photo');
	var offset = document.getElementById('offset');
	
	var photobox = $('photobox');
	var fx = photobox.effects({duration: 777, transition: Fx.Transitions.Cubic.easeOut});
	var photoimg = $('photo');
	var photoimg = $('curid');
	var photoimg = $('pid');

	var photo_url = './inc/ajax.php?id='+id+'&mode=1';
	var photoinfo_url = './inc/ajax.php?id='+id+'&mode=2';
	var curid_url = './inc/ajax.php?id='+id+'&mode=4';
	var pid_url = './inc/ajax.php?id='+id+'&mode=5';
	var dummy; 

	box.style.width = img.width+'px';
	box.style.height = img.height+'px';

// photo loading
	photobox.addClass('loading'); 
	var ajax_photo = new Ajax(photo_url, { 
		update: $('photobox'),
		method: 'get',
		onComplete: function() {
			var box = document.getElementById('photobox');
			var img = document.getElementById('photo');

			if (box.style.height!=(img.height+'px')) {
				fx.start({
					'width':  img.width,
					'height': img.height
				}).chain(function(){
					photobox.removeClass('loading');
				});
			} else {
				photobox.removeClass('loading');
			}
		},
		onCancel: function() {
			box.style.width = img.width+'px';
			box.style.height = img.height+'px';
			photobox.removeClass('loading');
		}
	});
	dummy = $random(0, 100);
	ajax_photo.request(dummy);
// end photo loading

// photoinfo loading
	var ajax_photoinfo = new Ajax(photoinfo_url, { 
		update: $('photoinfo'),
		method: 'get',
		onComplete: function() {
		},
		onCancel: function() {
		}
	});
	ajax_photoinfo.request();
// end photoinfo loading

// current num loading
	var ajax_curid = new Ajax(curid_url, { 
		update: $('curid'),
		method: 'get',
		onComplete: function() {
		},
		onCancel: function() {
		}
	});
	ajax_curid.request();
// end current num loading

// current photo id loading
	var ajax_pid = new Ajax(pid_url, { 
		update: $('pid'),
		method: 'get',
		onComplete: function() {
			setOffset(); 
			move();				
		},
		onCancel: function() {
		}
	});
	ajax_pid.request();
// end current photo id loading

	var thumbs = document.getElementById('thumbs');
	var cur = document.getElementById('current');
	var pid = document.getElementById('p_id');
	var offset = document.getElementById('offset');
	var num = 0;
	for(var i=0;i<thumbs.childNodes.length;i++) { 
		if (thumbs.childNodes[i].tagName == 'LI') { 
			num++;
			if (thumbs.childNodes[i].id == 'thumb-'+pid.value) { 
				cur.value = num; 
				thumbs.childNodes[i].className='active';
			} else {
				thumbs.childNodes[i].className='';
			};
		}
	}
	if (offset.value!=eval(Math.floor(eval(cur.value) / (5+1))*5));

}

//var first_p = document.getElementById('p_id').value;

function shownext()
{
	var pid = document.getElementById('p_id').value;
	var l = js_arr.length;
	var c = -1;//js_arr.indexOf(pid);
	for (var i=0; i<l; i++)
	{
		if (pid==js_arr[i])
		{
			c=i;
			break;
		}
	}
	//alert(js_arr[0]);
	if (c==-1) return;
	if (c==(l-1)) show(js_arr[0]);
	//else if (c==0) show(js_arr[l]);
	else show(js_arr[c+1]);
}

function showprev()
{
	//sleep(500);
	var pid = document.getElementById('p_id').value;
	var l = js_arr.length;
	var c = -1;//js_arr.indexOf(pid);
	for (var i=0; i<l; i++)
	{
		if (pid==js_arr[i])
		{
			c=i;
			break;
		}
	}
	//alert(js_arr[0]);
	if (c==-1) return;
	if (c==0) show(js_arr[l-1]);
	//else if (c==0) show(js_arr[l]);
	else show(js_arr[c-1]);
}
// end gallery functions
// =================== //
