(function($){
 profile_gallery = function(venueid,album_num,source,piclimit,gallerytype) {

	//var gall_dir = "events/"+venueid+"/gallery/"+album_num+"/";
	var gall_dir = "events/"+venueid+"/gallery/1/";

//how many photos to display SHOW PER PAGE
	/* **************************** */
	//var limit = 20;
	var limit = piclimit;
	 if (limit == null) { limit = 20};
	/* **************************** */
				
	//set the value of our hidden input fields
	$('#current_page').val(1);
	$('#show_per_page').val(limit);

	//TOTAL number of photos; 
	var numphotos;
	//NUMBER OF PAGES DISPLAYED
	var number_of_pages;

		 $.ajax({
                 type: "GET",
                 url: gall_dir+album_num,
                 cache: false,
                 success: function(xml) {
                 $("#gallerybox").empty();
                      
             numphotos = $(xml).find("photo").length;
		number_of_pages = Math.ceil(numphotos/limit);  
             
                	 var gname = $(xml).find('name').text();

  		//function to display pictures
		function callingpage(start,end) {
                     $(xml).find('photo').slice(start,end).each(function(){
                         var name_text = $(this).find('pixname').text();
                          var caption_text = $(this).find('caption').text();                        
                          var venuecap = gname;                       
                         if (caption_text !="") {venuecap=gname+" : "+caption_text};                     
                         $('<div class="gall_imglink"><\/div>')
                          .html('<a rel="example_group" href="' + gall_dir +  'large/'+name_text+'" title="'+venuecap+'" ><img src="' + gall_dir + 'small/'+name_text + '"  \/><\/a>')                            
                             .appendTo('#gallerybox');
                     }); //close each
                     }; 
                      
                      
 
	function go_to_page(page_num){
	//get the number of items shown per page
	var show_per_page = parseInt($('#show_per_page').val());

	//get the element number where to start the slice from
	var start_from = (page_num -1)* show_per_page;

	//get the element number where to end the slice
	var end_on = start_from + show_per_page;

	callingpage(start_from,end_on);

	$('.page_link[longdesc=' + page_num +']').addClass('current').siblings('.current').removeClass('current');

	//update the current page input field
	$('#current_page').val(page_num);
	};

	function previous(){
	new_page = parseInt($('#current_page').val()) - 1;
	//if there is an item before the current active link run the function
	if($('.current').prev('.page_link').length==true){
		go_to_page(new_page);
	} else {
		go_to_page(new_page+1);
	};
	};
	function next(){
	new_page = parseInt($('#current_page').val()) + 1;
	//if there is an item after the current active link run the function
	if($('.current').next('.page_link').length==true){
		go_to_page(new_page);
	} else {
		go_to_page(new_page-1);
	};
	};    

//initialise - no number pagination display yet so no live needed
go_to_page(1);

// display the number pagination navigation display
var navigation_html = '<a class="back" href="#null"><span>Prev<\/span><\/a>';
var current_link = 1;
	while(number_of_pages >= current_link){
		navigation_html += '<a class="page_link" href="#null" longdesc="' + current_link +'"><span>'+ (current_link) +'<\/span><\/a>';
		current_link++;
	}
	navigation_html += '<a class="next" href="#null" ><span>Next<\/span><\/a>';
	$('.buttons').html(navigation_html);
//add current class to the first page link
	$('.buttons .page_link:first').addClass('current');

$(".back").click(function(){
	$("#gallerybox").empty();
	previous();
});

$(".next").click(function(e){
	$("#gallerybox").empty();
	next();
	
});

$(".page_link").click(function(){
	$("#gallerybox").empty();
	go_to_page($(this).attr('longdesc'));
}); 
 
     
		var greview = $(xml).find('review').text();
 		var gdate = $(xml).find('date').text();
 		var gname = $(xml).find('name').text();

		//gdate = "Gallery event review on "+ gdate;
		//is page 'recent gallery' page or 'profile' gallery
		if (source != 'profile'){
                     //greview=greview+'<div style="font-size:14px"><a href="index.php?p=profile&id='+venueid+'">Go to '+gname+' profile</a></div>'; 
                     
                    var galleryhref = "index.php?p=profile&type="+gallerytype+"&id="+venueid;
                    $("#gallerytop li.rightmenu a").attr("href",galleryhref);                     
                     
                  }  

            		            
		if(greview==""){
		$('#galleryinfo').css("display", "none");
		} else {
		$('#gallerydate').html(gdate);
		$('#galleryreview').html(greview);   
		//$('.jScrollPaneContainer').jScrollPane();
           	}
           	
           
                 } //success              
             }); //ajax

		$("a[rel=example_group]").livequery(function(){		
		$(this).fancybox({
		 		'titleShow'     : true,
		 		'titlePosition'  : 'inside',
		 		'overlayOpacity': 0.5,
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'titleFormat' : function(){ return '<span style="color:#555;font-size:14px;font-weight:bold">'+this.title+'<\/span>';}		
		});		
		});	
return false;

          }           
})(jQuery);


    
    



