var Mbx_galeriePresse = new Class({
	initialize : function()
	{
		this.initMultibox();
		this.initDownloads();
	},
	
	initMultibox : function()
	{
		//vorbereitung
		$('gallery').getElements('.popup').each(function(el) {
			el.set(
				'href', 
				el.getElement('img').get('src').replace(/height=100/g, 'width=520')
			);
		});

		//zusaetzliche verlinkung fuer lupen-elemente
		$('gallery').getElements('div.imagelink').each(function(el) {
			var lens = el.getElement('div');
			lens.setStyle('cursor', 'pointer');
			lens.addEvent('click', function(cEv) {
				cEv.target = el.getElement('a');
				el.getElement('a').fireEvent('click', cEv);
			}.bind(el));
		});
		
		//multibox initialisieren
		var initMultiBox = new multiBox({
			initialSize: {w:550, h:250},
			mbClass: '.popup',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
			container: $(document.body),//where to inject multiBox
			descClassName: 'multiBoxDesc',//the class name of the description divs
			path: './Files/',//path to mp3 and flv players
			useOverlay: true,//use a semi-transparent background. default: false;
			maxSize: {w:520, h:1024},//max dimensions (width,height) - set to null to disable resizing
			addDownload: false,//do you want the files to be downloadable?
			addRollover: false,//add rollover fade to each multibox link
			addOverlayIcon: false,//adds overlay icons to images within multibox links
			addChain: false,//cycle through all images fading them out then in
			recalcTop: false,//subtract the height of controls panel from top position
			addTips: true,//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
			downloadMDD: true,
			fixedTop: 50
			/*((window.getHeight() / 2) - ((Number(this.contentObj.height) + this.contentObj.xH) / 2) - this.container.getStyle('border').toInt() + window.getScrollTop()) + this.options.offset.y;*/
		});
	},
	
	initDownloads : function()
	{
		//aktuelle downloads holen und input-elemente entsprechend setzen
		this.setupDownloadStati();
		
		//event fuer submit-button
		$('galerie_download').addEvent('click', function(cEv) {
			this._clickDownload(cEv);
		}.bind(this));
		
		//events fuer checkboxen
		$('gallery').getElements('input[type=checkbox]').each(function(tmpEl) {
			var el = tmpEl;
			el.addEvent('click', function(cEv) {
				this._clickGalleryImage(cEv, el);
			}.bind(this));
		}.bind(this));
	},
	
	setupDownloadStati : function()
	{
		var getDownloadsRequest = new Request({
			url: conf['dirs']['ajax'] + '/ajax.getGalleryDownloads.php', 
			method: 'get', 
			onSuccess: function(response) {
		    	var downloads = response.split(',');
		    	$('gallery').getElements('input[type=checkbox]').each(function(tmpEl) {
		    		if(downloads.contains(tmpEl.get('value')))
		    		{
		    			tmpEl.set('checked', true);
		    		}
		    		else
		    		{
		    			tmpEl.set('checked', false);
		    		}
		    	});
			}
		});
		getDownloadsRequest.send();
	},
	
	_clickDownload : function(p_event)
	{
		new Event(p_event).stop();
		
		window.open('/lib/inc/download.php', '_blank');
	},
	
	_clickGalleryImage : function(p_event, p_element)
	{
		var mode = 'remove';
		if(p_element.get('checked'))
		{
			mode = 'add';
		}
		
		var updateDownloadsRequest = new Request({
			url: conf['dirs']['ajax'] + '/ajax.updateGalleryDownloads.php', 
			method: 'get', 
			onSuccess: function(response) {
		    	//log if response equals string "false"
				//console.log(response);
			}
		});
		updateDownloadsRequest.send("mode=" + mode + "&id=" + p_element.get('value'));
	}
});
