// usage: openGallery(EventID);
// 18.11.2010 12:30 Werner Wrann, EOS Data Systems
// Needed CSS Styles Examples:
// .blockBg { position:fixed; top:0; left:0; width:100%; height:100 %; background-color:#000; z-index:500; opacity: 0.85 } - only IE7+ compatible!!!
// .containerBg { position:fixed; top:25%; left:25%; height:50%; width:50%; background-color:; z-index:501; }
// options = {
//		ShowTitle = true, // default true
//		ShowExtraClose = true, // default true
// 		beforeSuccess = function(), // default null
// 		afterSuccess = function(), // default null
// 		errorCallback = function(), // default null
// 		afterErrorCallback = function(), // default null
//		beforeClose = function(), // default null
//		afterClose = function() // default null
// }
function openGallery(eventID, options)
{
	// create Backgrounds, so the user my see that we do already work
    var bg = $(document.createElement('div')).addClass('blockBg');
    $(document.body).append(bg);

    var div = $(document.createElement('div')).addClass('containerBg');
    $(document.body).append(div);

	if (!options) options = {};
	var showTitle = options.ShowTitle;
	var showExtraClose = options.ShowExtraClose;
	var errorCallback = options.error;
	var afterErrorCallback = options.afterErrorCallback;
	if (!errorCallback) errorCallback = function(){
            alert('Something went wrong');
			bg.remove(); div.remove(); // remove everything so that we dont block the user input!
			if (afterErrorCallback) afterErrorCallback();
	};
	var beforeSuccess = options.beforeSuccess;
	var afterSuccess = options.afterSuccess;
	var beforeClose = options.beforeClose;
	var afterClose = options.afterClose;

	
		// click events
	var closeAction = function(){
		if (beforeClose) beforeClose();
		bg.remove();
		div.remove();
		if (afterClose) afterClose();
	};

	bg.click(function(){closeAction();}); // remove all elements
	
	// now do the script call
    $.ajax({
        url: 'http://res.ehf.eu/Gallery/?eid=' + eventID, // Gallery url with event ID - do not change
        dataType: 'script', // do not change
        success: function(){
			if (beforeSuccess) beforeSuccess();
            div.html(galleryData); // fill containerDIV with data
            div.galleria(); // create galleria
			
			var doShowTitle = (showTitle == undefined || showTitle);
			var doShowExtraClose = (showExtraClose == undefined || showExtraClose);
			
			if (doShowTitle && doShowExtraClose)
			{
				var topRow = $(document.createElement('center')).css('padding-bottom', '8px');
				if (doShowTitle && galleryTitle && galleryTitle.length > 0)
				{
					topRow.append('<strong>' + galleryTitle + '</strong>');
				}
				if (doShowTitle)
				{
					var cl = $(document.createElement('a')).html('Close').css('float', 'right').css('cursor', 'Pointer').css('padding-right', '5px').attr('href', 'javascript:void(0);');
					cl.click(function(){closeAction();});
					topRow.append(cl);
				}
				div.find('.galleria-stage').prepend(topRow);
			}
			
			if (afterSuccess) afterSuccess();
        },
        error: errorCallback
    });
}
