$(document).ready(function(){

	$('<div id="tholder" style="display:none;"></div>').appendTo('body');
	
	if($(".pg_wrap").length){
		var numgal = $(".pg_wrap").length;
		
		for(r=0; r <= numgal - 1; r++)
		{
			$("#gallery_holder").hide();
			$('<div class="gpi_pg_gallery"></div>'+"\n").appendTo("#gallery_holder");
		}
		
		/* Populate galleries without titles */
		$('.pg_wrap').each(function(){
			var wg = $(this).index();
			
			populate_gallery(wg);
		});
		
		
		/* Reorder photos on mouseover */
		$(".image_link").mouseover(function(){
			var linkindex = $(this).index(".image_link");
			var cGal = $('.gpi_pg_gallery').eq(linkindex).html();
			
			reorder_gallery(linkindex,cGal);
		});
		
		$(".title_link").mouseover(function(){
			var linkindex = $(this).index(".title_link");
			var cGal = $('.gpi_pg_gallery').eq(linkindex).html();
			
			reorder_gallery(linkindex,cGal);
		});
		
		/* Open gallery in lightbox */
		$('.image_link').click(function(event){
			var linkindex = $(this).index(".image_link");
			open_lightbox(linkindex);
			event.preventDefault();
		});
		
		$('.title_link').click(function(event){
			var linkindex = $(this).index(".title_link");
			open_lightbox(linkindex);
			event.preventDefault();
		});	
		
	} //end if pg_wrap

});

function open_lightbox(linkindex)
{
	$(".gpi_pg_gallery:eq("+linkindex+") a:first").triggerHandler('click'); //trigger('click');
	$(".gpi_pg_gallery:eq("+linkindex+") a").lightBox();
}

function get_image(pig,pgin,tgurl)
{
	for(z=0; z <= pig -1;)
	{
		var vbm = z+1;
				
		$('#tholder').load(tgurl+'&Params=itemnr='+vbm+' .finfoto',function(){
			var lighty = $('#tholder').html();
			var mygal = $(".gpi_pg_gallery").eq(pgin);
			
			$(lighty).appendTo(mygal);
			$('#tholder').empty();
		});
		z++;		
	}
	
}

function populate_gallery(pgin)
{
	var pig = $(".photo_count").eq(pgin).text();
		pig = pig.replace(" photos.","");
		
	var gallery_url = $(".image_link").eq(pgin).attr("rel");
	
	var	tgurl = 'http://'+window.location.hostname+gallery_url;
			
		gallery_url = gallery_url.replace("apps/pbcs.dll/gallery?","apps/pbcsi.dll/bilde?NewTbl=1&");
			
	var actual_url = window.location.hostname;
		actual_url = "http://"+actual_url;
	
	var image_url = actual_url+gallery_url+"&Item=";
			
	get_image(pig,pgin,tgurl);
}


function reorder_gallery(pgin,gContents)
{
	var beerme = new Array();
	
	$(gContents+' a').each(function(){
		var purl = $(this).attr('href');
			//purl = purl.replace('&amp;','&');
		var ptitle = $(this).attr('title');
		var phnum = purl.split('Item=',2);
			phnum = phnum[1] - 1;
		
		beerme[phnum] = purl+'~'+ptitle; //'<a class="finfoto" href="'+purl+ '" title="'+ptitle+'"></a>';
	});
	
	/*
	var cPGal = $('.gpi_pg_gallery').eq(pgin);
	var fst = $('.gpi_pg_gallery').eq(pgin).html();
	
	$(fst+' a').each(function(){
		fu = $(this).html();
		alert(fu);
		//$(this).detach();
	});
	*/
	
	//$(cPGal+' a').detach(); //empty();
	for(aq=0; aq <= beerme.length; aq++)
	{
		mystringy = String(beerme[aq]);//.toString;
		var fs = mystringy.split('~',2);
		var ft = fs[1];
		var ff = fs[0];
		
		$('.gpi_pg_gallery:eq('+pgin+') a').eq(aq).attr('href',ff);
		$('.gpi_pg_gallery:eq('+pgin+') a').eq(aq).attr('title',ft);
		//$(beerme[aq]).appendTo(cPGal);
	}
	
	var clicky = true;
	
	return clicky;
	
	//launch_light(pgin);
}

function launch_light(pgin)
{
	alert('In Launch Light Function');
	//var mypgal = $('.gpi_pg_gallery').eq(pgin).html();
	//var imurl = $(".gpi_pg_gallery:eq("+pgin+") a:first").attr('href');
	//alert(imurl);
	//alert(mypgal);
	$(".gpi_pg_gallery:eq("+pgin+") a:first").trigger('click');
	$(".gpi_pg_gallery:eq("+pgin+") a").lightBox();
	//$(mypgal+" a:first").trigger('click');
	//$(mypgal+" a").lightBox();
	//return false;
}

