/**
 * Item for galleries on the amenities and rooms pages
 * @param Object
 * - gallery:String - The gallery directory (without the filename for the xml file) also without the trailing slash
 * - container:String - Identifier for the 
 */
function PhotoGallery (o) {
	/* Save the options */
	o.reference = this;
	this.opt = o;
	this.xmlname = 'PhotoGallery.xml';
	this.container = jQuery(this.opt.container);
	this.albumBase = null;
	this.images = new Array();
	
	/* Tell the xml to start loading */
	jQuery.ajax({
		dataType : 'xml',
		url : this.opt.gallery + '/' + this.xmlname,
		success : function (data) {
			o.reference.onXMLLoad(data);
		}
	});
};
/**
 * Runs when the xml is loaded
 * @param Object
 */
PhotoGallery.prototype.onXMLLoad = function (data) {
	/* Pass the data to the parsing method */
	this.doGalleryParse(data);
};
/**
 * Parses the passed xml to create a gallery
 * @param Object - The XML to parse 
 */
PhotoGallery.prototype.doGalleryParse = function (xml) {
	/* Get a dom object from jquery */
	var dm = jQuery(xml);
	
	/* Find the album node for the basic information */
	var ref = this,
		album = dm.find('album'),
		imagenodes = album.find('img');
	this.albumBase = album.attr('lgpath');
	
	/* Loop through the found image nodes */
	imagenodes.each(function (idx, elem) {
		ref.images.push(jQuery(elem));
	});
	this.setup();
};
/**
 * Sets up the gallery once the data is loaded
 */
PhotoGallery.prototype.setup = function () {
	/* Go through the images array */
	var inc, tag, imgInfo;
	for (inc = 0; inc < this.images.length; inc++) {
		imgInfo = this.images[inc];
		tag = jQuery('<a rel="gallery-image-link" href="' + this.albumBase + imgInfo.attr('src') + '"><img src="' + this.albumBase + imgInfo.attr('src') + '" alt="' + imgInfo.attr('alt') + '" /></a>');
		this.container.append(tag);
	}
	
	/* Set the images up to use fancybox */
	jQuery('a[rel=gallery-image-link]').fancybox({
		titlePosition : 'inside',
		titleFormat : function (title, currentArray, currentIndex, currentOpts) {
			return '<span class="page-gallery-fancybox-title">' + (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
		}
	});
};
/**
 * Galleria overlay object
 * - container : jQuery object for the container
 */
function GalleriaOverlay (o) {
	/* Save a pointer to the object */
	o.reference = this;
	
	/* Find the xml location */
	o.xmlLocation = o.container.attr('href');
	o.container.click(function () {
		o.reference.onTileClick();
		return false;
	});
	
	/* Internalize the options */
	this.opt = o;
	this.pluginInitialized = false;
	jQuery.ajax({
		dataType : 'xml',
		url : this.opt.xmlLocation,
		success : function (data) {
			o.reference.onXMLLoad(data);
		}
	});
};
/**
 * Runs when the overlay element is clicked
 */
GalleriaOverlay.prototype.onOverlayClick = function () {
	this.doHide();
};
/**
 * Runs when the close link is clicked
 */
GalleriaOverlay.prototype.onCloseClick = function () {
	this.doHide();
};
/**
 * Runs when the tile on the regular page is clicked
 */
GalleriaOverlay.prototype.onTileClick = function () {
	this.doShow();
};
/**
 * Runs when the XML for the gallery items has been loaded
 */
GalleriaOverlay.prototype.onXMLLoad = function (data) {
	this.dm = jQuery(data);
	this.setup();
};
/**
 * Sets the gallery up -- this links the gallery button
 */
GalleriaOverlay.prototype.setup = function () {
	/* Attach the gallery overlay element */
	var _this = this,
		galleryDom = '<div class="galleria-overlay">'
		+ '<div class="overlay-wrapper">'
		+ '<div class="overlay-inner">'
		+ '<div class="close"><a href="#">CLOSE</a></div>'
		+ '<div class="images">'
		+ '</div>'
		+ '</div>'
		+ '</div>'
		+ '</div>';
	jQuery('body').append(galleryDom);
	this.elem = jQuery('div.galleria-overlay');
	this.elem.click(function (event) {
		if (!event.target) {
			event.target = event.srcElement;
		}
		if (event.target.className == 'galleria-overlay' || event.target.className == 'overlay-wrapper') {
			_this.onOverlayClick();
		};
		return false;
	});
	this.elem.find('div.close a').click(function (event) {
		_this.onCloseClick();
		return false;
	});
	
	/* Run through the images xml and attach the image tags */
	var imgs_container = this.elem.find('div.images'),
		imgs = this.dm.find('img'),
		albumLocation = this.dm.find('album').attr('lgpath');
	imgs.each(function (idx, elm) {
		imgs_container.append('<img src="' + albumLocation + jQuery(elm).attr('src') + '" alt="" />');
	});
};
/**
 * Shows the image overlay element
 */
GalleriaOverlay.prototype.doShow = function () {
	if (jQuery.browser != 'msie') {
		this.elem.css({
			'display' : 'inline',
			'opacity' : 0
		});
		if (!this.pluginInitialized) {
			this.pluginInitialized = true;
			this.elem.find('div.images').galleria({
				width : 480,
				height : 270
			});
		}
		this.elem.animate({
			'opacity' : 1
		}, 300);
	} else {
		this.elem.css({
			'display' : 'inline'
		});
	};
};
/**
 * Hides the image overlay element
 */
GalleriaOverlay.prototype.doHide = function () {
	if (jQuery.browser != 'msie') {
		var _this = this;
		this.elem.animate({
			'opacity' : 0
		}, 300, function () {
			_this.elem.css({
				'display' : 'none'
			});
		});
	} else {
		this.elem.css({
			'display' : 'inline'
		});
	};
};

