function showGallery()
{
    $("#cat_title").empty().html('Galerie Foto');
    $("#center_container").empty();
    var ajax_load = "<center><img src='images/ajax-loader.gif' alt='loading...' /></center>";
    var loadUrl = 'foto.ajax.php';
    var data = {
                    op:'show_gallery'
    };
    var callbackFunction = function(){
        window.location = '#top';
        initPaginationGallery();
    };
    $("#center_container").html(ajax_load).load(loadUrl,data,callbackFunction);
}

function pageselectCallbackGallery(page_index, jq){
    var max_elem = Math.min((page_index+1) * items_per_page_gallery, $('#copil_gallery_hidden div.result_gallery').length);
    var newcontent = '';
    for(var i=page_index*items_per_page_gallery;i<max_elem;i++)
    {
        newcontent += $('#copil_gallery_hidden div.result_gallery:eq('+i+')').html();
    }
    $('#copil_gallery').empty().append(newcontent);
}
function initPaginationGallery() {
    var num_entries = $('#copil_gallery_hidden div.result_gallery"').length;
    if (num_entries>0)
    {
        // Create pagination element
        $("#Pagination-Gallery").pagination(num_entries, {
            items_per_page:items_per_page_gallery,
            num_display_entries:num_display_entries,
            num_edge_entries: num_edge_entries,
            callback: pageselectCallbackGallery
        });
    }
    else
    {
        $("#Pagination-Gallery").empty();
        $('#copil_gallery').empty();
        $('#copil_gallery').html('<div align="center">Această categorie nu conţine anunţuri</div>');
    }
}
function only_show_gallery(id,userId){
	var ajax_load = "<center><img src='images/ajax-loader.gif' alt='loading...' /></center>";
	var loadUrl = 'copii_mei.ajax.php';
	var data = {
		action : 'only_show_gallery',
		id:id,
                view_gallery:1
	};
	var callbackFunction = function() {
		show_images(id,userId);
                
	};
	$("#center_container").html(ajax_load).load(loadUrl, data, callbackFunction);
}
function galleryVote(vote)
{
    var child = $('#copil_id').html();
    var pic_name = $('#poza_nume_galerie').html();
    var parent = $('#parent_id_galerie').html();
    $.ajax({
      url: "foto.ajax.php?op=vote&stars="+vote+"&child="+child+"&parent="+parent+"&pic_name="+pic_name,
      cache: false,
      success: function(html){
          $("#votare").empty().html(html);
      }
    });
}
function show_images(id,userId)
{
	$.ajax({
		url: "copii_mei.ajax.php",
		method: 'POST',
		data: ({action: 'get_image_gallery',id:id,view_gallery:1,userId:userId}),
		complete: function(msg)
		{
			var r = msg.responseText.replace(/^\s+|\s+$/g,"");

			var len = r.length;
			if(r.substr(len - 2, 2) == "ok")
			{
				var arr = r.split("|");
				$('#container-galerie').html(arr[0]);
				var idx = arr[1];

				if(arr[0].length > 0)
				{
					var galleries = $('.ad-gallery').adGallery({
					  loader_image: 'loader.gif',
					  width: 525, // Width of the image, set to false and it will read the CSS width
					  height: 375, // Height of the image, set to false and it will read the CSS height
					  thumb_opacity: 0.5, // Opacity that the thumbs fades to/from, (1 removes fade effect)
										  // Note that this effect combined with other effects might be resource intensive
										  // and make animations lag
					  start_at_index: idx, // Which image should be displayed at first? 0 is the first image
					  animate_first_image: false, // Should first image just be displayed, or animated in?
					  animation_speed: 400, // Which ever effect is used to switch images, how long should it take?
					  display_next_and_prev: true, // Can you navigate by clicking on the left/right on the image?
					  display_back_and_forward: true, // Are you allowed to scroll the thumb list?
					  scroll_jump: 0, // If 0, it jumps the width of the container
					  slideshow: {
						enable: false,
						autostart: false
					  },
					  effect: 'fade', // or 'slide-vert', 'resize', 'fade', 'none' or false
					  enable_keyboard_move: true, // Move to next/previous image with keyboard arrows?
					  cycle: true, // If set to false, you can't go from the last image to the first, and vice versa
					  // All callbacks has the AdGallery objects as 'this' reference
					  callbacks: {
						// Executes right after the internal init, can be used to choose which images
						// you want to preload
						init: function() {
						  // preloadAll uses recursion to preload each image right after one another
						  this.preloadAll();
						},
						// This gets fired right after the new_image is fully visible
						afterImageVisible: function() {
							var image = this.images[this.current_index];
							var imgNameArray = image.image.split('/');
                                                        $('#poza_nume_galerie').html(imgNameArray[4]);
                                                        $('#parent_id_galerie').html(imgNameArray[2]);
                                                        makeRating(imgNameArray[2], imgNameArray[4])
						},
						// This gets fired right before old_image is about to go away, and new_image
						// is about to come in
						beforeImageVisible: function(new_image, old_image) {
						  // Do something wild!
						}
					  }
					});
				}
			} else {
				if(r.length > 0) show_error(r,'error');
			}
			$('.ad-nav').width(($('.ad-gallery').width()));
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			show_error('Am intalnit o eroare. Incercati mai tarziu.','error');
			return false;
		}
	});
}

function makeRating(parent,pic_name)
{
    var child = $("#child_id").html();
    var ajax_load = "<img src='images/ajax-loader.gif' alt='loading...' />";
    var loadUrl = 'foto.ajax.php';
    var data = {
            op: 'make_rating',
            child:child,
            parent:parent,
            pic_name:pic_name
    };
    var callbackFunction = function() {
    };
    $("#votare").html(ajax_load).load(loadUrl, data, callbackFunction);
}