function PreloadImage(imgSrc, callback){
  var objImagePreloader = new Image();

  objImagePreloader.src = imgSrc;
  if(objImagePreloader.complete){
    callback(objImagePreloader);
    objImagePreloader.onload=function(){};
  }
  else{
    objImagePreloader.onload = function() {
      callback(objImagePreloader);
      //    clear onLoad, IE behaves irratically with animated gifs otherwise
      objImagePreloader.onload=function(){};
    }
  }
}

function ImageObject(url, name, description, alt) {
	this.url = url;
	this.name = name;
	this.description = description;
	this.alt = alt;
}

var gallery = {
	// These variables need to be set before init is called
	images : new Array(),
	areaId : '',
	thumbnailsId : '',
	basepath : '',
	t_height : 1,
	t_width : 1,

	// Internal variables
	_offset : 0,
	_curImage : -1,
	_area : undefined,
	_thumbnails : undefined,
	_oldImage : undefined,
	_newImage : undefined,
	_textarea : undefined,
	_textarea_title : undefined,
	_textarea_content : undefined,

	init: function () {
		var t = gallery;

		if (t.areaId == '' || t.thumbnailsId == '') return;

		t._area = document.getElementById(t.areaId);
		t._thumbnails = document.getElementById(t.thumbnailsId);

		// Create image elements
		var img = document.createElement('img');
		img.src = t.basepath + "/images/" + 'images/loading.gif';
		img.alt = "";

		var img2 = document.createElement('img');
		img2.src = t.basepath + "/images/" + 'images/loading.gif';
		img2.alt = "";

		$("#"+t.areaId)[0].appendChild(img);
		$("#"+t.areaId)[0].appendChild(img2);

		t._oldImage = $(img).hide();
		t._newImage = $(img2).hide();

		t._textarea = document.createElement('div');
		t._textarea.id = 'showcase_textarea';
		t._textarea_title = document.createElement('strong');
		t._textarea_content = document.createElement('p');

		t._textarea.appendChild(t._textarea_title);
		t._textarea.appendChild(t._textarea_content);
		$("#"+t.areaId)[0].appendChild(t._textarea);

		t.showThumbnails();
		gallery.showImage(0);

		$("#showcase_arrow_prev").fadeTo(200, 0.3);
		$("#showcase_arrow_next").fadeTo(200, 0.3);

		var containerHeight = $("#showcase_arrow_prev").parent().innerHeight();
		var containerWidth = $("#showcase_arrow_prev").parent().innerWidth();
		$("#showcase_arrow_prev").css("margin-top", containerHeight / 2 - $("#showcase_arrow_prev").outerHeight() / 2);
		$("#showcase_arrow_next").css("margin-top", containerHeight / 2 - $("#showcase_arrow_next").outerHeight() / 2);

		$("#"+t.areaId).hover(
			function() {
				$("#showcase_arrow_prev").fadeTo(100, 0.9);
				$("#showcase_arrow_next").fadeTo(100, 0.9);
				if ($(t._textarea).is(":visible")) {
					$(t._textarea).fadeTo(300, 0.8);
				}
			},
			function() {
				$("#showcase_arrow_prev").fadeTo(500, 0.3);
				$("#showcase_arrow_next").fadeTo(500, 0.3);
				if ($(t._textarea).is(":visible")) {
					$(t._textarea).fadeTo(500, 0.2);
				}
			}
		);
	},

	prevImage : function() {
		if (gallery._curImage - 1 < 0) {
			gallery.showImage(gallery.images.length - 1);
		} else {
			gallery.showImage(gallery._curImage - 1);
		}
	},

	nextImage : function() {
		if (gallery._curImage + 1 >= gallery.images.length) {
			gallery.showImage(0);
		} else {
			gallery.showImage(gallery._curImage + 1);
		}
	},

	scrollLeft : function() {
		var t = gallery;

		if (t._offset == 0) {
			t._offset = t.images.length-1;
		} else {
			t._offset -= 5;
		}

		t.scroll();
	},

	scrollRight : function() {
		var t = gallery;

		if (t._offset >= t.images.length - 6) {
			t._offset = 0;
		} else {
			t._offset += 5;
		}

		t.scroll();
	},

	scroll : function() {
		var t = gallery;

		if (t._offset < 0) {
			t._offset = 0;

		} else if (t._offset + 6 >= t.images.length) {
			t._offset = t.images.length - 6;
			if (t._offset < 0) {
				t._offset = 0;
			}
		}

		var ul = $("#subject_thumbnails_ul");
		if (ul.length <= 0) return;

		if (ul.is(':animated') == false) {
			var left = 0;
			if (ul.children().eq(t._offset).length > 0) {
				left = ul.children().eq(t._offset).position().left;
			}

			var time = (left + ul.position().left)/2;
			if (time < 0) time *= -1;

			ul.animate({left : -left + "px"}, { duration: 200 + time, queue: false });
		}
	},

	showImage : function(imgNr) {
		var t = gallery;
		if (imgNr >= t.images.length || imgNr < 0 || imgNr == t._curImage) return;


		var right = t._curImage < imgNr;

		t.focusThumbnail(imgNr);

		// Display image
		if (t._area != undefined) {
			t._area.style.backgroundImage = "url('images/loading.gif')";
		}

		// Remove old description
		while (t._textarea_title.childNodes.length > 0) {
			t._textarea_title.removeChild( t._textarea_title.firstChild );
		}
		while (t._textarea_content.childNodes.length > 0) {
			t._textarea_content.removeChild( t._textarea_content.firstChild );
		}


		// Show new description
		var img = t.images[t._curImage];
		if (img.name != "") {
			t._textarea_title.appendChild(document.createTextNode(img.name));
		}
		if (img.description != "") {
			t._textarea_content.appendChild(document.createTextNode(img.description));
		}
		if (img.name == "" && img.description == "") {
			$(t._textarea).hide();
		} else {
			$(t._textarea).show();
		}

		var tmp = t._oldImage;
		t._oldImage = t._newImage;
		t._newImage = tmp;

		var containerWidth = t._newImage.parent().innerWidth();
		var offset = containerWidth * 1.2;
		if (!right) offset *= -1;

		var left = containerWidth / 2 - t._oldImage.outerWidth() / 2 - offset;
		t._oldImage.animate({"left" : left}, {"duration" : "slow", "queue" : false});

		PreloadImage(t.basepath + "/images/" + t.images[t._curImage].url, function(image){ t._showImageCallback(right, image); });
	},

	_showImageCallback : function(direction, image) {
		var t = gallery;
		if (t._area != undefined) {
			t._area.style.backgroundImage = "none";

			t._newImage[0].src = t.basepath + "/images/" + t.images[t._curImage].url;
			t._newImage[0].alt = t.images[t._curImage].alt;

			var containerHeight = t._newImage.parent().innerHeight();
			var containerWidth = t._newImage.parent().innerWidth();
			var textareaHeight = $(t._textarea).outerHeight();

			var offset = containerWidth;
			if (!direction) offset *= -1;

			var posY = 0;
			if (image.height > containerHeight - textareaHeight) {
				posY = containerHeight / 2 - image.height / 2;
			} else {
				posY = containerHeight / 2 - textareaHeight / 2 - image.height / 2;
			}

			t._newImage.css("position", "absolute");
			t._newImage.css("top", posY);
			t._newImage.css("left", containerWidth / 2 - image.width / 2 + offset);
			t._newImage.fadeIn("slow");

			t._newImage.animate({"left" : containerWidth / 2 - image.width / 2}, {"duration" : "slow", "queue" : false});
		}
	},

	focusThumbnail : function(imgNr) {
		var t = gallery;

		if (imgNr >= gallery.images.length || imgNr < 0) return;

		var scrollRight = t._curImage < imgNr;
		var scrollLeft = t._curImage > imgNr;

		// Set new image nr
		t._curImage = imgNr;

		// Scroll if needed
		if (imgNr - t._offset < 0) {
			while (imgNr - t._offset < 0) {
				t._offset--;
			}

		} else if (imgNr - t._offset > 5) {
			while (imgNr - t._offset > 5) {
				t._offset++;
			}

		}

		if (scrollLeft) {
			if (imgNr > 0 && imgNr - t._offset == 0) {
				t._offset -= 1;
			}
		} else if (scrollRight && imgNr - t._offset == 5) {
			if (imgNr < t.images.length) {
				t._offset += 1;
			}
		}

		// Remove old focus, and add new
		$("#subject_thumbnails_ul").find(".active").removeClass("active");
		$("#subject_thumbnails_ul").children().eq(imgNr).addClass("active");

		t.scroll();
	},

	showThumbnails : function() {
		var t = gallery;

		if (t._thumbnails == undefined) return;

		while ( t._thumbnails.childNodes.length > 0 ) {
	        t._thumbnails.removeChild( t._thumbnails.firstChild );
	    }

		var thumbnails = t.images.length;
		if (thumbnails < 6) thumbnails = 6;
		for (var i = 0; i < thumbnails; i++) {
			var thumbnailLi = document.createElement('li');
			t._thumbnails.appendChild(thumbnailLi);

			var imgSrc = "";
			var imgNr = i;
			if (imgNr < t.images.length) {
				var link = document.createElement('a');
				link.href = "#";
				link.onclick = new Function("gallery.showImage("+imgNr+");return false;");

				var img = document.createElement('img');
				img.src = t.basepath + "/thumbs/" + t.images[imgNr].url;
				img.alt = t.images[imgNr].alt;
				//img.title = t.images[imgNr].name;

				thumbnailLi.appendChild(link);
				link.appendChild(img);
			}

			if (imgNr == t._curImage) {
				thumbnailLi.className = "active";
			}
		}
	},

	// addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	addEvent: function (obj, type, fn) {
		if (obj.addEventListener) obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e" + type + fn] = fn;
			obj[type + fn] = function () {
				obj["e" + type + fn](window.event);
			}
			obj.attachEvent("on" + type, obj[type + fn]);
		}
	}
};

function removeNoscript() {
	$('body').removeClass('noscript');
}

gallery.addEvent(window, 'load', removeNoscript);
gallery.addEvent(window, 'load', gallery.init);

