﻿/**
 * This variable is used in order to keep a track of all the opened galleries,
 * this way, if a gallery was opened once, it won't be duplicated in the DOM.
 */
var _CurrentlyOpenedGalleries = {};

/**
 * Opens an image gallery with the data located at the provided xml file path.
 * @param xmlFilePath: The file path at which the XML file containing the 
 *  gallery data is located. The XML of the gallery data should be represented
 *  as:
 *  <gallery>
 *	    <image>
 *		    <thumbnail>[thumbnailUrl]</thumbnail>
 *		    <fullImage>[fullImageUrl]</fullImage>
 *		    <description>[ImageDescription]</description>
 *	    </image>
 *  </gallery>
 */
function OpenGallery(xmlFilePath) {
    $.ajax({
        url: xmlFilePath
		, dataType: 'xml'
		, type: 'GET'

		, error: function(request, status, error) {
		    throw (status + ' : ' + error);
		}

		, success: function(data, status, request) {
		    if (!_CurrentlyOpenedGalleries[xmlFilePath]) {
		        _CurrentlyOpenedGalleries[xmlFilePath] = document.createElement('div');

		        $(data).find('image').each(function() {
		            var oThumb = document.createElement('a');
		            var oImg = document.createElement('a');
		            var oDesc = document.createElement('div');

		            oThumb.className = 'imgThumb';
		            oImg.className = 'imgFull';
		            oDesc.className = 'imgDesc';

		            oThumb.href = $(this).find('thumbnail').text();
		            oImg.href = $(this).find('fullImage').text();
		            oDesc.appendChild(document.createTextNode($(this).find('description').text()));

		            _CurrentlyOpenedGalleries[xmlFilePath].appendChild(oThumb);
		            _CurrentlyOpenedGalleries[xmlFilePath].appendChild(oImg);
		            _CurrentlyOpenedGalleries[xmlFilePath].appendChild(oDesc);
		        });
		        $(document.body).append(_CurrentlyOpenedGalleries[xmlFilePath]);
		    }

		    $(_CurrentlyOpenedGalleries[xmlFilePath]).mbGallery();
		}
    });
}

function OpenVideo(youtubeVideoId, title){
	if (!_CurrentlyOpenedGalleries[youtubeVideoId]) {
		_CurrentlyOpenedGalleries[youtubeVideoId] = document.createElement('div');
		$(_CurrentlyOpenedGalleries[youtubeVideoId]).append('<iframe src="http://player.vimeo.com/video/21218610?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff" width="500" height="281" frameborder="0"></iframe>');
		$(document.body).append(_CurrentlyOpenedGalleries[youtubeVideoId]);
		$(_CurrentlyOpenedGalleries[youtubeVideoId]).dialog({
			draggable: false
			, modal: true
			, resizable: false
			, width: '530px'
			, autoOpen: false
			, title: title
		});
	}
	$(_CurrentlyOpenedGalleries[youtubeVideoId]).dialog('open');
}
