function populate_date(month, day, year) {
  ge('date_month').value = month;
  ge('date_day').value = day;
  ge('date_year').value = year;
}

function ge(elem) {
  return document.getElementById(elem);
}

/*
 * Simple Ajax call method.
 *
 * From http://en.wikipedia.org/wiki/XMLHttpRequest
 */
function ajax(url, vars, callbackFunction) {
  var request =  new XMLHttpRequest();
  request.open("POST", url, true);
  request.setRequestHeader("Content-Type",
                           "application/x-www-form-urlencoded");

  request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {
      if (request.responseText) {
        callbackFunction(request.responseText);
      }
    }
  };
  request.send(vars);
}

function getCachedImage(pid, images, totalImages) {
	var createCachedImage = new Request.JSON({'url': 'index.php?ajax=true&type=list&pid=' + pid, onSuccess: function(imgData) {
		var imgName = 'images/tmp/' + pid + '_small.jpg';
		$('img-' + pid).empty();
		$('img-' + pid).adopt(
			new Element('a', {href: 'index.php?picker=photo&photo=' + pid, }).adopt(
				new Element('div', {'class': 'imgWrapperMinHeight150'}).adopt(
					new Element('img', {'style': 'background-image: url(/images/spinner.gif); background-repeat: no-repeat; background-position: center center;', id: 'img-' + pid, 'class': 'imgList', src: imgName, width: imgData[0], height: imgData[1]})
				)
			)
		);
		if (images.length > 0) {
			var progress = Math.round(300 - images.length / totalImages * 300);
			var togo = 300 - progress;

			var pFx = new Fx.Tween($('progressBarProgress'));
			var tFx = new Fx.Tween($('progressBarTogo'));
			pFx.start('width', progress + 'px');
			tFx.start('width', togo + 'px');
			
			var image = images.pop();
			getCachedImage(image, images, totalImages);
		} else {
			var pFx = new Fx.Tween($('progressBarProgress'));
			var tFx = new Fx.Tween($('progressBarTogo'));
			pFx.start('width', '300px');
			tFx.start('width', '0px');

			var fadeOut = new Fx.Tween($('progressBar'));
			fadeOut.start('height', '0px');
			fadeOut.addEvent('complete', function() {
				$('progressBar').dispose();				
			});
		}
    }});
	createCachedImage.send();
}

function getCachedBWImage(pid, images, totalImages, picker) {
	var createCachedImage = new Request.JSON({'url': 'index.php?ajax=true&type=listbw&pid=' + pid, onSuccess: function(imgData) {
		var imgName = 'images/tmp/' + pid + '_sm.jpg';
		$('img-' + pid).empty();
		eLink = new Element('a', {href: 'index.php?picker=' + picker + '&photo=' + pid, }).adopt(
				new Element('div').adopt(
						new Element('img', {id: 'img-' + pid, 'class': 'imgList', src: imgName, width: imgData[0], height: imgData[1]})
					)
				);

		// add loading insert to link
		eLink.addEvent('click', function(event) { loadingInsert(); });

		$('img-' + pid).adopt(eLink);

		if (images.length > 0) {
			var progress = Math.round(300 - images.length / totalImages * 300);
			var togo = 300 - progress;

			var pFx = new Fx.Tween($('progressBarProgress'));
			var tFx = new Fx.Tween($('progressBarTogo'));
			pFx.start('width', progress + 'px');
			tFx.start('width', togo + 'px');
			
			var image = images.pop();
			getCachedBWImage(image, images, totalImages, picker);
		} else {
			var pFx = new Fx.Tween($('progressBarProgress'));
			var tFx = new Fx.Tween($('progressBarTogo'));
			pFx.start('width', '300px');
			tFx.start('width', '0px');

			var fadeOut = new Fx.Tween($('progressBar'));
			fadeOut.start('height', '0px');
			fadeOut.addEvent('complete', function() {
				$('progressBar').dispose();				
			});
		}
		// initLoadingInsert();
    }});
	createCachedImage.send();
}

function getCachedImages(images){
	var progress = 0;
	var total = 300;
	var totalImages = images.length;
	var pB = new Element('div', {'style': 'border: 1px solid #ddd; padding: 8px; width: 320px;'});
	pB.set('html', 'Loading ... <br />');
	$('progressBar').set('style', 'height: 0px; overflow: hidden;');
	$('progressBar').adopt(pB);
	pB.adopt(
		new Element('img', {'id': 'progressBarProgress', 'src': 'images/empty.gif', 'style': 'width: ' + progress + 'px;'}),
		new Element('img', {'id': 'progressBarTogo', 'src': 'images/empty.gif', 'style': 'width: ' + total + 'px;'})	
	)
	
	var fadeIn = new Fx.Tween($('progressBar'));
	fadeIn.start('height', '64px');

	
	var image = images.pop();
	getCachedImage(image, images, totalImages);
}

function getCachedBWImages(images, picker){
	var progress = 0;
	var total = 300;
	var totalImages = images.length;
	
	var pB = new Element('div', {'id': 'wrapmetest', 'style': 'border: 1px solid #ddd; padding: 8px; width: 320px;'});
	
	pB.set('html', 'Loading ... <br />');
	$('progressBar').set('style', 'height: 0px; overflow: hidden;');
	$('progressBar').adopt(pB);
	pB.adopt(
		new Element('img', {'id': 'progressBarProgress', 'src': 'images/empty.gif', 'style': 'width: ' + progress + 'px;'}),
		new Element('img', {'id': 'progressBarTogo', 'src': 'images/empty.gif', 'style': 'width: ' + total + 'px;'})	
	)
	
	var fadeIn = new Fx.Tween($('progressBar'));
	fadeIn.start('height', '64px');

	
	var image = images.pop();
	getCachedBWImage(image, images, totalImages, picker);
}

function loadingInsert() {
	$('pleaseWait').setStyle('visibility', 'visible');
}

function uploadInsert() {
	$('uploadInsert').setStyle('visibility', 'visible');
}

function privacyInsert(cat, buy) {
	if(cat)
		$('choosePrivacyCat').setStyle('visibility', 'visible');
	else if(buy)
		$('choosePrivacyBuy').setStyle('visibility', 'visible');
	else 
		$('choosePrivacy').setStyle('visibility', 'visible');
}

function privacyHide() {
	$('choosePrivacy').setStyle('visibility', 'hidden');
	$('choosePrivacyCat').setStyle('visibility', 'hidden');
	$('choosePrivacyBuy').setStyle('visibility', 'hidden');
}

function initLoadingInsert() {
	$$('a[class=finishPicker]').each(function(eLink, index) {
		eLink.addEvent('click', function(event) {
			loadingInsert();
			// event.stop();
		});
	});
}


