var insideGallery;

function insideGalleryClass()
{
	this.init = function()
	{
		if ($('galleryLGItem1'))
		{
			this.latest = new insideGalleryLatest();
		}
		
		if ($('galleryNewComments'))
		{
			this.comments = new insideGalleryComments();
		}
	};
}

function insideGalleryLatest()
{
	this.setRegion = function(iRegionId)
	{
		this.iRegionId = iRegionId;
		$('galleryLGLoad').setStyle('visibility', 'visible');
		var oJsonRequest = new Json.Remote("/xhr/gallerylatestevents",
		{
			onComplete: function(xhrresponse)
			{
				insideGallery.latest.galleries =  xhrresponse.galleries;
				insideGallery.latest.offset = 0;
				insideGallery.latest.total = xhrresponse.galleries.length;
				insideGallery.latest.redraw();
				$('galleryLGLoad').setStyle('visibility', 'hidden');
			}
		});
		oJsonRequest.send({'region':iRegionId});	
	};
	
	this.redraw = function()
	{
		if (this.total < 3)
		{
			$('galleryLGVBox').setStyle('display', 'none');
			$('galleryLGXBox').setStyle('display', 'block');
			
			return;
		}
		else
		{
			$('galleryLGVBox').setStyle('display', 'block');
			$('galleryLGXBox').setStyle('display', 'none');
			
		}
		
		iSlot1Offset = this.offset;
		
		if (this.offset == (this.total-1))
		{
			var iSlot2Offset = 0;
		}
		else
		{
			var iSlot2Offset = this.offset + 1;
		}
		
		if (this.offset >= (this.total-2))
		{
			var iSlot3Offset = this.offset - (this.total-2);
		}
		else
		{
			var iSlot3Offset = this.offset + 2;
		}		
		
		this.fillSlotWithOffset(1, iSlot1Offset);
		this.fillSlotWithOffset(2, iSlot2Offset);
		this.fillSlotWithOffset(3, iSlot3Offset);
	};
	
	this.next = function()
	{
		this.offset++;
		if (this.offset > (this.total-1))
		{
			this.offset = 0;
		}
		this.redraw();
	}
	
	this.prev = function()
	{
		this.offset--;
		if (this.offset == -1)
		{
			this.offset = (this.total-1);
		}
		this.redraw();
	}
	
	this.fillSlotWithOffset = function(iSlotId, iSlotOffset)
	{
		var aSlotData = this.galleries[iSlotOffset];
		this['slot'+iSlotId].setGalleryId(aSlotData.id);
		this['slot'+iSlotId].setEventId(aSlotData.eventId);
		this['slot'+iSlotId].setEventDate(aSlotData.eventDate);
		this['slot'+iSlotId].setEventLocation(aSlotData.eventLocation);
		this['slot'+iSlotId].setEventName(aSlotData.eventName);
		this['slot'+iSlotId].setEventPreview(aSlotData.id, aSlotData.preview);
		this['slot'+iSlotId].setEventFlyer(aSlotData.eventFlyer);
		this['slot'+iSlotId].setGalleryUrl(aSlotData.eventUrl);
	}

	$('galleryORegionSelect').addEvent('change',function()
	{
		insideGallery.latest.setRegion($('galleryORegionSelect').value);
	});

	this.slot1 = new insideGalleryItemSlot(1);
	this.slot2 = new insideGalleryItemSlot(2);
	this.slot3 = new insideGalleryItemSlot(3);
	this.setRegion(0);
}


function insideGalleryItemSlot(iSlotId)
{
	this.element = $('galleryLGItem'+iSlotId);
	
	this.getEl = function(sClassName)
	{
		return this.element.getElementsByClassName(sClassName)[0];
	}
	
	this.setGalleryId = function(iGalleryId)
	{
		this.iGalleryId = iGalleryId
	}
	
	this.setEventId = function(iEventId)
	{
		this.iEventId = iEventId;
	}
	
	this.setEventName = function(sEventName)
	{
		this.getEl('eventName').setText(sEventName);
	}
	
	this.setEventDate = function(sEventDate)
	{
		this.getEl('eventDate').setText(sEventDate);
		// this.element.getElementsByClassName('eventDate')[0].innerHTML = sEventDate;
	}

	this.setEventLocation = function(sEventLocation)
	{
		this.getEl('eventLocation').setText(sEventLocation);
	}
	
	this.setEventPreview = function(iGalleryId, sThumbHash)
	{
		var sPreviewUrl = MEDIAHOST + '/gallery/folder_'+iGalleryId+'/t_'+sThumbHash+'.jpg';
		this.getEl('galleryPreview').getElementsByTagName('img')[0].src = sPreviewUrl;
	}
	
	this.setEventFlyer = function(bFlyer)
	{
		if (bFlyer == 'Y')
		{
			var sFlyerUrl = MEDIAHOST + '/m/e/' + this.iEventId.substr(0,1) + '/' + this.iEventId.substr(1,1) + '/' + this.iEventId + '/185x75.jpg';
		}
		else
		{
			var sFlyerUrl = MEDIAHOST + '/m/layout/events/noflyer.jpg';
		}
		this.getEl('eventFlyer').setStyle('background-image', 'url('+sFlyerUrl+')');	
	}
	
	this.setGalleryUrl = function(sEventUrl)
	{
		var sGalleryUrl = '/gallery/'+this.iGalleryId+'/'+sEventUrl.toLowerCase()+'/1';
		var aGalleryUrlElements = this.element.getElementsByClassName('galleryUrl');
		for(i=0; i < aGalleryUrlElements.length; i++)
		{
			aGalleryUrlElements[i].setProperty('href', sGalleryUrl);
			
		}
	}
	
}

function insideGalleryComments()
{
	this.oAncors = $('galleryNewComments').getElements('a').addEvents({
		'mouseenter': function () {
			var oGalleryItemData = this.id.substr(14).split('x');
			var iGalleryFolderId = oGalleryItemData[1];
			var sGalleryItemHash = oGalleryItemData[2];
					
			var oGalleryCommentPos = this.getPosition();
			var oGalleryItemPreviewImg = document.createElement('img');
			oGalleryItemPreviewImg.src = MEDIAHOST + '/gallery/folder_' + iGalleryFolderId + '/t_' + sGalleryItemHash + '.jpg';

			var oGalleryItemPreview = document.createElement('div');
			oGalleryItemPreview.appendChild(oGalleryItemPreviewImg);
			oGalleryItemPreview.id = 'galleryCommentPreview';
			oGalleryItemPreview
			.setStyle('background', '#ffffff')
			.setStyle('width', 110)
			.setStyle('height', 82)
			.setStyle('position', 'absolute')
			.setStyle('top', oGalleryCommentPos.y)
			.setStyle('left', oGalleryCommentPos.x-115)
			.setStyle('border', '2px solid #fff');
			$('pibody').appendChild(oGalleryItemPreview);			
		},
		
		'mouseleave': function () {
			$('galleryCommentPreview').remove();
		}	
	});
}

var insideGalleryAdmin = {
	"delComment" : function (iCommentId, oCommentNode)
	{
		if (confirm('Kommentar wirklich löschen?'))
		{
			var oJsonRequest = new Json.Remote("/xhr/gallerydelcomment",
			{
				onComplete: function(xhrresponse)
				{
					oCommentNode.remove();
				}
			});
			oJsonRequest.send({"id":iCommentId});
		}	
	}
};

window.addEvent('domready', function()
{
	insideGallery = new insideGalleryClass();
	insideGallery.init();
});