var current_gallery = '';

$(document).ready(function() {

	/* * Custom Galleryscript
		 * Click on Image in list open Fullscale-Image on the Top
		 * Add Next/Prev-Buttons,
		 * Click on Image in list scroll the list to this Image
		 * */
	$('.gallery ul li a').live('click', function(e){
		e.preventDefault();

		//Set Current Opened-Gallery
		current_gallery = $(this).closest('.gallery').attr('id');
		$('.gallery_imagefull:not(.hidden)').each(function(){
			if ($(this).parent().find('.gallery').attr('id') != current_gallery) {
				var opengallery = this;
				$(opengallery).addClass('hidden');
				$(opengallery).animate({
					height : 0
				}, 500, function(){
					$(opengallery).attr('style', '');
				});
			}
		});
		var link = this;
		var imagefull = $(link).closest('.gallery').parent().find('.gallery_imagefull img');

		//Add data to Fullscale-Image
		$(imagefull).attr('src', $(link).attr('href'));
		$(imagefull).attr('rel', $(link).attr('rel'));

		//Scroll list to clicked Image
		$(link).closest('.gallery').parent().find('#controls li[rel='+$(link).attr('rel')+']').trigger('click');

		var image_container = $(imagefull).parent();

		//If Fullscale-Image is hidden show it and Add Buttons
		if ($(image_container).hasClass('hidden')) {

			if ( $(image_container).find('.prevBtn').length == 0 ) {
				//Copy Prev-Button
				var button = $(link).closest('.gallery').parent().find('#controls .prevBtn').clone(true);

				//Add Click-Event so the Fullscale-Image will be switched too on click
				$(button).click(function(){
					var thisbutton = this;
					var imagenr = $(thisbutton).parent().find('img').attr('rel');
					imagenr--;
					var newsrc;
					if (imagenr < 1) {
						newsrc =  $(thisbutton).parent().parent().find('.gallery ul li a:nth-child(1)').attr('href');
						imagenr = $(thisbutton).parent().parent().find('.gallery ul li a:nth-child(1)').attr('rel');
					}else newsrc =  $(thisbutton).parent().parent().find('.gallery ul li a[rel='+imagenr+']').attr('href');
					$(thisbutton).parent().find('img').attr('src', newsrc);
					$(thisbutton).parent().find('img').attr('rel', imagenr);
				});

				$(image_container).append(button);

				//Copy Next-Button
				button = $(link).closest('.gallery').parent().find('#controls .nextBtn').clone(true);

				//Add Click-Event so the Fullscale-Image will be switched too on click
				$(button).click(function(){
					var thisbutton = this;
					var imagenr = $(thisbutton).parent().find('img').attr('rel');
					imagenr++;
					var newsrc =  $(thisbutton).parent().parent().find('.gallery ul li a[rel='+imagenr+']').attr('href');
					if (newsrc == undefined) {
						imagenr = 1;
						newsrc =  $(thisbutton).parent().parent().find('.gallery ul li a[rel='+imagenr+']').attr('href');
					}
					$(thisbutton).parent().find('img').attr('src', newsrc);
					$(thisbutton).parent().find('img').attr('rel', imagenr);
				});
				$(image_container).append(button);
			}

			//Remove Hidden-Class so Fullscale-Image is visble, show Fullscale-Image
			$(image_container).removeClass('hidden');
			var containerheight = 400;
			//$(image_container).height(0);
			$(image_container).animate({
				display : 'block',
				height : containerheight
			}, 500, function(){
				//$(image_container).height(auto);
				});

		}
	});

	$('.content_block table.sortable').tablesorter();

	$(".placeholder").placeholder();

	Date.format = 'yyyy-mm-dd';
	$('.date-pick').datePicker();

});

