var Gallery = new Class({
	fxGalleryEditBox: null
	,fxGalleryUpdateBox: null
	,fxGalleryAll: null
	,fxAjax: null
	,fxGalleryInfo: null
	,pic_id_before: ""
	,initialize: function() {
		if (null != ($('galleryEditBox'))) {
			$('galleryEditBox').set('slide');
			$('galleryEditBox').slide('hide');
			this.fxGalleryEditBox = $('galleryEditBox').get('slide');
		}
		//this.fxGalleryEditBox = (null != ($('galleryEditBox'))) ? new Fx.Slide('galleryEditBox', {wrapper: this.element}) : null;
		//this.fxGalleryUpdateBox = null != ($('galleryUpdateBox')) ? new Fx.Slide('galleryUpdateBox') : null;
		if (null != ($('galleryUpdateBox'))) {
			$('galleryUpdateBox').set('slide');
			$('galleryUpdateBox').slide('hide');	
			this.fxGalleryUpdateBox = $('galleryUpdateBox').get('slide');
		}
		
		if (null != ($('gallery_all'))) {
			$('gallery_all').set('slide');
			$('gallery_all').slide('hide');
			$('gallery_all').setStyle('display', 'block');
			this.fxGalleryAll = $('gallery_all').get('slide');
			//this.showPics();
		}
		
		if (null != ($('ajaxload'))) {
			$('ajaxload').set('slide');
			this.fxAjax = $('ajaxload').get('slide');
			$('ajaxload').slide('hide');
		}
		
		if (null != ($('galleryInfo'))) {
			$('galleryInfo').set('slide');
			this.fxGalleryInfo = $('galleryInfo').get('slide');
			$('galleryInfo').slide('hide');
		}
		
		if (null != ($('gallery_close'))) {
			$('gallery_close').addEvent('click',function() {
				$('gallery_file').value="";
				$('pic_desc').value = '';
				$('gallery_submit').disabled = false;
				$('gallery_submit').value = 'Hochladen';
				gallery.fxAjax.slideOut();
				gallery.fxGalleryEditBox.slideOut();
			});
		}
	}
	,upload_pic: function() {
		if ($F('gallery_file') == '') {
			$('galleryInfo').slide('in');
			$('a_galleryInfo').focus();
			$('galleryInfo').innerHTML = 'Bitte geben Sie eine JPG-, PNG-Bilddatei oder ein ZIP Archiv an.';
			window.setTimeout('$("galleryInfo").slide("out")',3000);
			return false;
		} else {
			$('ajaxload').style.display = 'block';
			$('gallery_submit').disabled = true;
			$('gallery_submit').value = 'Bitte warten...';
			window.setTimeout("new Effect('ajaxload').Fade();",340000);
			return true;
		}
	}
	,reloadGal: function() {
		this.fxAjax.slideIn();
		new Request.HTML({
			url: 'geco/' + $F('cid') + '/gallery',
			method: 'get',
			evalScripts: false,
			update: $('gallery_table'),
			onComplete: function() {
				window.setTimeout("gallery.fxAjax.slideOut();",1000);
				gallery_tips = new Tips($$('.gallery_tips'),{
					className: 'galleryTips'
				});
				window.setTimeout("gallery.loadInEdit();",1);
			}
		}).send();
	}
	,complete: function(msg) {
		$('galleryInfo').innerHTML = msg;
		$('galleryInfo').slide('in');
		$('a_galleryInfo').focus();
		this.fxAjax.slideOut();
		$('gallery_file').value="";
		$('pic_desc').value = '';
		$('gallery_submit').disabled = false;
		$('gallery_submit').value = 'Hochladen';
		$('galleryEditBox').slide('out');
		//Zeitvergroesserung bei groesseren Fehlermeldungen
		var timeup = 2000;
		if ($('galleryInfo').get('html').length > 50) {
			timeup = 10000;
		}
		window.setTimeout('$("galleryInfo").slide("out")',timeup);
	}
	,notComplete: function(msg) {
		$('galleryInfo').innerHTML = msg;
		$('galleryInfo').slide('in');
		$('a_galleryInfo').focus();
		this.fxAjax.slideOut();
		this.fxGalleryEditBox.slideIn();
		$('gallery_submit').disabled = false;
		$('gallery_submit').value = 'Hochladen';
		window.setTimeout('$("galleryInfo").slide("out")',5000);
	}
	,deletePic: function(link,cid) {
		var mach = window.confirm(unescape("Wollen Sie wirklich das ausgew%E4hlte Bild l%F6schen%3F"));
		if (mach){
			new Request.HTML({
				url: 'geco/'+ cid +'/gallery/delete', 
				method: 'post',
				evalScripts: false,
				update: $('galleryInfo'),
				onComplete: function() {
					//$('galleryInfo').innerHTML = "Bild erfolgreich gelöscht.";
					window.setTimeout("gallery.reloadGal()",200);
					$('galleryInfo').slide('in');
					$('a_galleryInfo').focus();
					setTimeout("$('galleryInfo').slide('out');",3000);
				}
			}).send('link=' + link);
		}
	}
	,showPics: function() {
		this.fxGalleryAll.toggle();
		if (this.fxGalleryAll.wrapper.offsetHeight == 0 || this.fxGalleryAll.wrapper.offsetWidth == 0) {
			$('a_pics').innerHTML = 'Bilder verstecken';
		} else {
			$('a_pics').innerHTML = 'Alle Bilder anzeigen';
		}
	}
	,showBox: function(){
		this.fxGalleryEditBox.toggle();
	}
	,showUpdateBox: function(id, desc) {
		if (this.pic_id_before != '') {
			id = this.pic_id_before;
			this.pic_id_before = '';
		}
		if (this.fxGalleryUpdateBox.wrapper.offsetHeight == 0 ||  this.fxGalleryUpdateBox.wrapper.offsetWidth == 0) {
			this.fxGalleryUpdateBox.slideIn();
			$('pic_desc_u').value = desc;
			$('pic_id').value = id;
			$('img_gallery_update_' + id).src = 'res/button_pencil_cross.gif';
			$('div_gallery_' + id).style.background = '#E6E6FF';
			this.pic_id_before = id;
		} else {
			this.fxGalleryUpdateBox.slideOut();
			$('pic_desc_u').value = "";
			$('pic_id').value = "";
			$('img_gallery_update_' + id).src = 'res/button_pencil.gif';
			$('div_gallery_' + id).style.background = '';
		}
	}
	,updatePic: function() {
		this.fxAjax.slideIn();
		new Request.HTML({
			url: 'geco/' + $('cid').value + '/gallery/update',
			method: 'post',
			evalScripts: false,
			data: $('pic_u'),
			update: $('galleryInfo'),
			onComplete: function (response) {
				gallery.fxAjax.slideOut();
				$('galleryInfo').slide('in');
				$('a_galleryInfo').focus();
				window.setTimeout('gallery.fxGalleryUpdateBox.slideOut();',1000);
				window.setTimeout("gallery.reloadGal();",2000);
				window.setTimeout("$('galleryInfo').slide('out');",2000);
				gallery.showUpdateBox('','');
				$('a_galleryInfo').focus();
			}
		}).send();
	}
	,updateS: function(show) {
		this.fxAjax.slideIn();
		var post = '';
		if (show) {
			post = 'show=true';
		} else {
			post = 'show=false';
		}
		new Request.HTML({
			url: url = 'geco/' + $('cid').value + '/gallery/updateS',
			evalScripts: false,
			data: post,
			update: $('galleryInfo'),
			onComplete: function() {
				gallery.fxAjax.slideOut();
				$('galleryInfo').slide('in');
				$('a_galleryInfo').focus();
				window.setTimeout('gallery.reloadGal();',4000);
				window.setTimeout('$("galleryInfo").slide("out")',5000);
			}
		}).send();
	}
	,loadInEdit: function() {
		$$('.sort_pics').each(function (el) {
			el.addEvent('click', function() {
				this.inlineEdit({
					onComplete: function (element) {
						var gallery = new Gallery();
						gallery.updateOrder(element);
					}
				});
			});
		});
	}
	,updateOrder: function(element){
		var url = 'geco/'+ $('cid').value + '/gallery/updateOrder';
		new Request.JSON({
			url: url,
			onComplete: function(response){
				if (response == null) {
					$('galleryInfo').set('html','Es ist ein unbekannter Fehler aufgetreten');
					$('galleryInfo').slide('in');
					$('a_galleryInfo').focus();
					window.setTimeout('gallery.reloadGal();',4000);
					window.setTimeout('$("galleryInfo").slide("out")',5000);
				} else if (response.status == 'false') {
					$('galleryInfo').set('html', response.msg);
					$('galleryInfo').slide('in');
					$('a_galleryInfo').focus();
					window.setTimeout('gallery.reloadGal();',4000);
					window.setTimeout('$("galleryInfo").slide("out")',5000);
				} else {
					$('galleryInfo').set('html', response.msg);
					$('galleryInfo').slide('in');
					$('a_galleryInfo').focus();
					window.setTimeout('gallery.reloadGal();',20);
					window.setTimeout('$("galleryInfo").slide("out")',5000);
				}
				
			}
		}).send('order=' + element.get('html') + '&pic_id=' + element.get('id') );
	}
	
});

