// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


(function($) {

	$("a.image_popup").attach(Remote.Link, { dataType: "script" });
	$(".picture_serie").attach($.klass({
		initialize: function() {
			var element_item_id = $(this.element).attr("id").split("_");
			var item_id = element_item_id.pop();
			if (item_id) {
				//alert(item_id);
				var popup_setup_url = "/admin/editable/picture_series/" + item_id + "/popup"
				$.get(popup_setup_url, {}, function(data, type) {
					$("#image_popup_" + item_id).remove();
					$("body").append("<div id=\"image_popup_" + item_id + "\" class=\"image_popup\" style=\"display: none;\"></div>");
					$("#image_popup_" + item_id).html(data);
				})
			}
		}
	}));

	$.jquery = $.jquery || {};
	$.jquery.ux = $.jquery.ux || {};
	$.jquery.ux.ui = $.jquery.ux.ui || {};
	$.jquery.ux.ui.ImagePopupHelper = function() {
		return {
			getDimensions: function() {
				var dims = {};
				var dbw, dbh;

				if (window.innerHeight && window.scrollMaxY) {
					dbw = window.innerWidth + window.scrollMaxX;
					dbh = window.innerHeight + window.scrollMaxY;
				} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
					dbw = document.body.scrollWidth;
					dbh = document.body.scrollHeight;
				} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
					dbw = document.body.offsetWidth;
					dbh = document.body.offsetHeight;
				}

				if (self.innerHeight) {	// all except Explorer
					if(document.documentElement.clientWidth){
						dims.windowWidth = document.documentElement.clientWidth;
					} else {
						dims.windowWidth = self.innerWidth;
					}
					dims.windowHeight = self.innerHeight;
				} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
					dims.windowWidth = document.documentElement.clientWidth;
					dims.windowHeight = document.documentElement.clientHeight;
				} else if (document.body) { // other Explorers
					dims.windowWidth = document.body.clientWidth;
					dims.windowHeight = document.body.clientHeight;
				}

				dims.pageHeight = Math.max(dbh, dims.windowHeight);
				dims.pageWidth = Math.max(dbw, dims.windowWidth);

				return dims;
			},

			getPageScroll: function() {
				var scroll = {};

				if (self.pageYOffset) {
					scroll.y = self.pageYOffset;
					scroll.x = self.pageXOffset;
				} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
					scroll.y = document.documentElement.scrollTop;
					scroll.x = document.documentElement.scrollLeft;
				} else if (document.body) {// all other Explorers
					scroll.y = document.body.scrollTop;
					scroll.x = document.body.scrollLeft;
				}

				return scroll;
			}
		};
	}();

})(jQuery);

/**
 * Specific popup behaviour
 */
var defaultImagePopupBehaviour = $.klass({
	initialize: function() {
		$(".mask, .close_button, .wrap", this.element).bind("click", $.bind(this.onclose, this));
		$(".box", this.element).bind("click", function(e) { e.preventDefault(); return false; });
		$(window).resize($.bind(this.onresize, this));
		$(".prev_link", this.element).hide().bind("click", $.bind(this.onprevious, this));
		$(".next_link", this.element).hide().bind("click", $.bind(this.onnext, this));
		this.previous_images = [];
		this.next_images = [];
	},
	onshow: function(event, image_url, previous_images, next_images) {
		this.previous_images = previous_images;
		this.next_images = next_images;
		if (!$(".mask", this.element).is(":visible")) {
			this.recalculatePosition();
			this.element.show();
			$(".mask", this.element).show();
			$(".wrap", this.element).fadeIn("normal");
		}

		if (previous_images.length > 0) {
			$(".prev_link", this.element).show();
		} else {
			$(".prev_link", this.element).hide();
		}

		if (next_images.length > 0) {
			$(".next_link", this.element).show();
		} else {
			$(".next_link", this.element).hide();
		}
		var box = this;
		var img = new Image();

		$(img).load(function() {
			$(".box .the_image", this.element).animate({'width': img.width, 'height': img.height },
							750, '');
			$(".box", this.element).animate({'width': img.width, 'height': img.height + 100 },
							750, '', function(e) { box.onload(img);	});
		})
		.error(function() {
			box.onclose();
			alert('There was an error loading the image');
		})
		.attr('src', image_url);
	},
	onload: function(img) {
		$('.the_image', this.element).html("");
		$('.the_image', this.element).append(img).width(img.width)
			.height(img.height)
			.fadeIn('normal');
	},
	onclose: function() {
		var elem = this.element
		$(".wrap", this.element).fadeOut('normal', function(e) {
			elem.hide();
		});
	},
	onprevious: function() {
		var last = this.previous_images[this.previous_images.length -1];
		$('.the_image img', this.element).fadeOut('normal');
		$.get(last, null, null, "script");
		return false;
	},
	onnext: function() {
		var first = this.next_images[0];
		$('.the_image img', this.element).fadeOut('normal');
		$.get(first, null, null, "script");
		return false;
	},
	onresize: function() {
		if ($(this.element).is(":visible")) this.recalculatePosition();
	},
	recalculatePosition: function() {
		var ds = $.jquery.ux.ui.ImagePopupHelper.getDimensions();
		var ps = $.jquery.ux.ui.ImagePopupHelper.getPageScroll();

		$('.mask', this.element).width(ds.pageWidth)
		.height(ds.pageHeight);

		$('.wrap', this.element).css({
			'left': 0,
			'top': ps.y + (ds.windowHeight * 0.08)
		});
	}
});

