/*
	JS interface for the Flash image gallery
*/
var Gallery = {
	locale: "fi_FI",
	/*
		URL patterns for the gallery XML and the SWF
		Variables:
			%%LOCALE%% - locale
			%%INSTANCEID%% - gallery instance ID (only for the XML pattern)
			%%XMLURL%% - XML URL (only for the SWF pattern)
	*/
	xmlUrlPattern: "%%INSTANCEID%%",
	swfUrlPattern: "/stc/flash/gallery.swf?xmlUrl=%%XMLURL%%",
	
	
	/*
		Overlay and Flash settings
	*/
	overlay: {
		active: true
	},

	flash: {
		size: {width: 800, height: 600},
		minVersion: "8",
		bgColor: "#000000"
	},

	
	// Display gallery
	openInstance: function(iid){
		// Create URLs using patterns
		var xmlUrl = Gallery.xmlUrlPattern.replace(/%%LOCALE%%/g, Gallery.locale).replace(/%%INSTANCEID%%/g, iid);
		var swfUrl = Gallery.swfUrlPattern.replace(/%%LOCALE%%/g, Gallery.locale).replace(/%%XMLURL%%/g, escape(xmlUrl));
		
		// Create overlay
		if(Gallery.overlay.active){
			$("body").eq(0).append("<div id=\"galleryOverlay_" + iid + "\" class=\"galleryOverlay\"></div>");
			
			// Attach click handler
			$("#galleryOverlay_" + iid).click(function(event){
				Gallery.close();
			});
			
			// Fade to view
			$("#galleryOverlay_" + iid).css("opacity", "0");
			$("#galleryOverlay_" + iid).fadeTo("slow", 0.7);
		}
		
		// Create gallery wrapper element
		$("body").eq(0).append("<div id=\"galleryWrapper_" + iid + "\" class=\"galleryWrapper\"></div>");
		
		// Stop click event propagation
		$("#galleryWrapper_" + iid).click(function(event){
			event.stopPropagation();
		});
		
		// Attach scroll and resize event handlers
		Gallery.adjustPositionAndSize($("#galleryOverlay_" + iid), $("#galleryWrapper_" + iid));
		$(window).resize(function(){
			Gallery.adjustPositionAndSize($("#galleryOverlay_" + iid), $("#galleryWrapper_" + iid));
		});
		
		$(window).scroll(function(){
			Gallery.adjustPositionAndSize($("#galleryOverlay_" + iid), $("#galleryWrapper_" + iid));
		});
		
		
		// Inject Flash using SWFObject
		var so = new SWFObject(swfUrl, "flashGallery_" + iid, Gallery.flash.size.width, Gallery.flash.size.height, Gallery.flash.minVersion, Gallery.flash.bgColor);
		so.addParam("wmode", "transparent");
		so.addParam("allowFullScreen", "true");
		so.write("galleryWrapper_" + iid);
	},
	
	
	// Function to handle window scroll and resize events
	adjustPositionAndSize: function(overlay, gallery){
		var pageSelector = document;
		var top = $(pageSelector).scrollTop() + ($(window).height() / 2 - Gallery.flash.size.height / 2);
		$(overlay).width($(window).width());
		$(overlay).height($(window).height());
		$(overlay).css("top", $(window).scrollTop());
		$(overlay).css("left", $(window).scrollLeft());
		$(gallery).width(Gallery.flash.size.width);
		$(gallery).height(Gallery.flash.size.height);
		$(gallery).css("top", top + "px");
		$(gallery).css("left", ($(window).scrollLeft() + ($(window).width() / 2) - (Gallery.flash.size.width / 2)) + "px");
	},
	
	// Close gallery the ugly way: by removing the elements from the DOM :) (Hey! It works!)
	close: function(){
		$(".galleryWrapper").remove();
		$(".galleryOverlay").fadeOut(function(){
			$(".galleryOverlay").remove();
		});
	}
}

function openGallery(iid){
	Gallery.openInstance(iid);
}
