// JavaScript Document

function toggleForm(form, title) {
	//
	//var form = $('#imageQuickForm');
	if (form.css("display") == "block") {
		// hide form
		form.css("display", "none");
		if (title) {
			title.attr("class", "");
		}
	} else {
		// show form
		form.css("display", "block");
		if (title) {
			title.attr("class", "white");
		}
	}
}

// resize functions
  function fitArea(target, wid, hei, getWid) {
    var originalWid = target.width();
	if (getWid > 0) {
		originalWid = getWid;
	}
	//var originalHei = target.height();
	var originalHei = 800;
	target.width(wid);
	var scaleRatio = wid/originalWid;
	target.height(Math.round(originalHei*scaleRatio));
	if (target.height() > hei) {
	  scaleRatio = hei/originalHei;
	  target.height(hei);
	  target.width(Math.round(originalWid*scaleRatio));
	}
  }
  
  function setDimensionsTofitArea(target, maxWid, maxHei, getWid, getHei) {
	//
	var scaleRatio = maxWid/getWid;
	var newHei = Math.round(getHei*scaleRatio);
	if (newHei > maxHei) {
	  scaleRatio = maxHei/getHei;
	  newWid = Math.round(getWid*scaleRatio);
	  target.attr("width", newWid);
	  target.attr("height", maxHei);
	} else {
	  target.attr("width", maxWid);
	  target.attr("height", newHei);	
	}
  }
  
  function fillArea(target, wid, hei, getWid) {
    var originalWid = target.width();
	if (getWid > 0) {
		//originalWid = getWid;
	}
	//var originalHei = target.height();
	var originalHei = 160;
	target.width(wid);
	var scaleRatio = wid/originalWid;
	target.height(Math.round(originalHei*scaleRatio));
	if (target.height() < hei) {
	  scaleRatio = hei/originalHei;
	  target.height(hei);
	  target.width(Math.round(originalWid*scaleRatio));
	}
  }

  function resizeWorkArea(){
  	//
	//$("#main").width($(window).width()-203);
  }
  
  function fitAllImages(){
	//
	var counter = 0;
	//
	$('.left-image').each(function() {
	  //
	  var left = $(this).children('img');
	  var right = $(this).next().children('img');
	  //
	  //if (counter < 1) { alert("width = "+parseInt($(right).css("width").replace("px", ""))); }
	  //
	  var leftOldWid = cssToInt($(left).css("width"));
	  var leftOldHei = cssToInt($(left).css("height"));
	  if ($(right).length) {
	  	var rightOldWid = cssToInt($(right).css("width"));
	  	var rightOldHei = cssToInt($(right).css("height"));
	  }
	  //
	  //if (leftOldWid > 0) {
		if ($(right).length) {
		  var leftProportion = leftOldWid / (leftOldWid+rightOldWid);
		  //alert(leftProportion);
		  var leftWid = ($(window).width()-211)*leftProportion;
		} else {
		  var leftWid = $(window).width()-246;
		}
		setDimensionsTofitArea($(left), leftWid, $(window).height()-107, leftOldWid, leftOldHei);
		if ($(right).length) {
			setDimensionsTofitArea($(right), $(window).width()-(211+leftWid), $(window).height()-107, rightOldWid, rightOldHei);
			$(this).next().css("marginLeft", cssToInt($(left).css("width"))+4);
		}	
	  //}							   
		counter++;						   
	});
	  
  }
  
  function cssToInt(value) {
	//  
	return parseInt(value.replace("px", "")) 
  }
  
  function position(target, container, align) {
  
	if (align=="center") {
		//alert(target.width());
	   target.css("margin-left", (container.width()/2)-(target.width()/2));
	   //target.css("margin-top", (container.height()/2)-(target.height()/2));
	}
}

function updateImageNav(opts) {
	//
	var caption = $('#image-holder').children(':eq('+opts.currSlide+')').children(':first').children(':first').attr("alt");
	$('#caption').html(caption);
	$('#counter').html((opts.currSlide+1)+' / '+opts.slideCount);
}


function enlarge(imageid) {
	//
	hideThumbGrid();
	$('#downloadbtn').css("display", "inline");
	addImgSrc(imageid);
	$('#image-holder').fadeIn(600);
	//
	firstImage = imageid;
	past = -1;
	pastSlide = imageid;
	$('#image-holder').cycle(imageid); 
}




function addNextSlide(curr,next,opts,dummy,noPast) {
	//
	if ($('#preload').css("display") == "block") {
		$('#preload').fadeOut(100);
	}
	if ($('#caption').length) {
		updateImageNav(opts);
	}
	//
	// if first image loaded is beyond the number of images loaded by PHP..
	if (firstImage >= num_images_init && firstImage == opts.currSlide) {
		addImgSrc(firstImage);
	}
	// if this is the very first image loaded, add the next one too
	if (firstImage == opts.currSlide) {
		pastSlide = opts.currSlide;
	}
	//
	var future = Math.max(num_images_init-firstImage, 1);
	//
	var nextSlide = opts.nextSlide-1;
	var nextSlideToLoad = nextSlide+future;
	var maxSlides = paths_to_add.length;
	if (nextSlideToLoad >= maxSlides) {
		nextSlideToLoad = future-(maxSlides-nextSlide);
	}
	//alert(firstImage+" / "+opts.currSlide+" / "+opts.nextSlide);
	addImgSrc(nextSlideToLoad);
	//
	preloadStarted = true;
	//
	if (noPast!=true) {
		addPrevSlide();
	}
	//
	if (future<num_images_init) {
		//
		var catchUp=num_images_init-(future+1);
		firstImage=catchUp;
		addNextSlide('','',opts,'',true);
	}
}

function addPrevSlide() {
	//	
	for (i=0; i<Math.ceil(num_images_init/2); i++) {
		pastSlide+=past;
		if (pastSlide<num_images_init) {	
			past = -1;
			pastSlide = paths_to_add.length+past;
		} 
		addImgSrc(pastSlide);
	}
}

function addImgSrc(imageid) {
	//
	var left = $('#image-holder').children(':eq('+imageid+')').children('.left-image').children(':first');
	if ($(left).length) {
		if ($(left).attr("src") == "") {
			$(left).attr("src", paths_to_add[imageid][0]);
			if (paths_to_add[imageid][1] != "") {
				var right = $(left).parent().next().children(':first');
				$(right).attr("src", paths_to_add[imageid][1]);
			}
		}
	}
}
