
$.fn.image = function(src, title, prev_lnk, next_lnk, f){
  return this.each(function(){
	var div = jQuery("<div class=\"image_title\"/>");
	div.text(title);
	div.appendTo(this);
    $("<img />").appendTo(this).each(function(){
            this.src = src;
            this.onload = f;
    });
	//set the next image
	$("#prev").html("<a class=\"album\" href=\"" + prev_lnk +"\">Image précédente</a>");
	$("#next").html("<a class=\"album\" href=\"" + next_lnk +"\">Image suivante</a>");
  });
}

$(document).ready(function(){
	
	//size the slider...
	$('.preview_slider').each(function(){
		$(this).height($(".gp_mask").size()*101);
	});
	
	//cursor the images
	$('.gp_thumb').each(function(){
		$(this).css({cursor: "pointer"});
	});
	
	//set the albums click actions...
	$('div.cadrethumb_large').each(function(){
		$(this).css({cursor: "pointer"});
		$(this).bind('click', function(){
			//alert($(this).find('div.cadrethumb_large').attr('id'));
			window.location = $(this).attr('id');
		});
	});
	
	$('.gp_mask').each(function(){
		$(this).css({opacity: 0.35});
		$(this).mouseover(function(){
			$(this).fadeTo("fast",0);
		});
		$(this).mouseout(function(){
			$(this).fadeTo("fast",0.35);
		});
		$(this).click(function(){
			var thing = $(this);
			var imgdata=thing.attr('id').split("|");			
			$('#main_image').fadeOut("fast", function(){
				$(this).empty();
				$(this).image(imgdata[0], thing.next('img').attr('alt'),imgdata[1],imgdata[2],function(){
					// set the title accordingly...
					$(document).attr("title","Vertical Aventure | " + thing.next('img').attr('title') + " | " + thing.next('img').attr('alt'));
					$('#main_image').fadeIn("slow");
					pageTracker._trackPageview(document.location+"/"+thing.next('img').attr('alt'));
				})
			});
		});
	});	
	
	
	$('.p_slider#left').each(function(){
		$(this).mouseover(function(){
			
			//alert($('.preview_slider').css("top"));
			var top = Number($('.preview_slider').css("top").substring(0,$('.preview_slider').css("top").length -2));
			if(top < -3){
				var slider = $(this);
				$('.preview_slider').animate({"top": "+=20px"}, 10, function(){
					slider.trigger('mouseover');
				});
			}
		});
		$(this).mouseout(function(){
			$('.preview_slider').stop();
		})
	});
	
	$('.p_slider#right').each(function(){
		$(this).mouseover(function(){
			var top = Number($('.preview_slider').css("top").substring(0,$('.preview_slider').css("top").length -2));
			var height = Number($('.preview_slider').height());
			//alert("top " + top + " height " + height + " sum " + (top + height));
			if((top+height) > 600){
				var slider = $(this);
				$('.preview_slider').animate({"top": "-=20px"}, 10, function(){
					slider.trigger('mouseover');
				});
			}
		});
		$(this).mouseout(function(){
			$('.preview_slider').stop();
		})
	});
	initGalerie();
});


function createAlbumPagination(){
	/* Album page pagination */	
	var number_of_album = Number($(".album").size());
	var albs_per_page = 6;
	var pages = number_of_album/albs_per_page;
	
	//alert(number_of_album);
	$("#pagination").pagination(number_of_album, {
		items_per_page:albs_per_page,
		callback: viewalbumpage
	});
}

function viewalbumpage(page_id, jq){
	$('.gallery').fadeOut("fast", function(){
		//alert($(this).height());
		var offset = Number($(this).height())*Number(page_id)*(-1);
		$('.albumpage').css({ top: offset});
		$(this).fadeIn("slow");
		pageTracker._trackPageview("/galerie/page/"+page_id);
	});
}

function initGalerie(){
	createAlbumPagination();
	
	//cursor the images
	$('.gp_thumb').each(function(){
		$(this).css({cursor: "pointer"});
	});
	
	//set the albums click actions...
	$('div.cadrethumb_large').each(function(){
		$(this).css({cursor: "pointer"});
		$(this).bind('click', function(){
			//alert($(this).find('div.cadrethumb_large').attr('id'));
			window.location = $(this).attr('id');
		});
	});
}