// JavaScript Document

var imageGalleryIndex = new Array();

$(document).ready(function(){

	//TODO: imagegallery matched altijd????? Uitzoeken en oplossen.
	if($('a[href^="imagegallery:"]')){
		$('a[href^=imagegallery]').each(function(index){
			imageGalleryIndex[index] = this;
			var array = new Array();
			array = $(this).attr('href').split(':');
			if(array[1] != ''){
				getImageGallery(array[1], index);
			}
		});
	}
	
});//END document ready
	
function getImageGallery(galleryname, index){

	var data = {'action':'getGallery', 'galleryname':galleryname, 'index':index};

	$.ajax({
		url: "/remote/imagegallery.php",
		type: 'POST',
		dataType: 'json', 
		data: data,
		success: function(data){
			if(data.status){
				var imagetags = '';
				for(var i =0; i < data.data.totalrecords; i++){
					if(i == 0){
					//set attr for the image on the screen
						$(imageGalleryIndex[index]).attr('href', '/assets/images/gallery/large/'+data.data.records[i].gi_filename).attr('rel', 'prettyPhoto[gallery2]').attr('title', data.data.records[i].gi_caption);
					}else{
						//add empty a tags that make up the gallery
						imagetags += '<a href="/assets/images/gallery/large/'+data.data.records[i].gi_filename+'" rel="prettyPhoto[gallery2]" title="'+data.data.records[i].gi_caption+'"></a>';
					}
				}//END for

				//wrap it in a div and add the rest of the images next to to visible image
				$(imageGalleryIndex[index]).wrap('<div class="gallery'+index+'">').after(imagetags);

				//Active prettyPhoto
				$(".gallery"+index+":first a[rel^='prettyPhoto']").prettyPhoto({animation_speed:'normal',theme:'light_square',slideshow:3000, autoplay_slideshow: false});
				
			}else{
				alert('error');				
			}
		},
		error: function(){
			alert('Unknown error');
		}
	});
	
}
