$(document).ready(function()
{
	if($('ul#galeryselect'))
	{
		// add the next and back buttons to the gallery holder
		var title = $('<div class="gal_caption" />').appendTo('div#galleryimg');
		var controls = $('<div class="gal_controls" />').appendTo('div#galleryimg');
		var share = $('<a class="gal_share">share image</a>').appendTo(controls);
		var previous = $('<a class="gal_prev">previous</a>').appendTo(controls);
		var next = $('<a class="gal_next">next</a>').appendTo(controls);
		controls.append('<div class="clear">&nbsp;</div>');
		
		function sharesend(hash)
		{
			var imagename = $('div.galimg img').attr('src').split('/');
			imagename = imagename[imagename.length-1];
			
			// assemble the variables
			var senders = {};
			senders.name = $('input#stf_yourname').attr('value');
			senders.email = $('input#stf_youremail').attr('value');
			senders['rnames[]'] = [];
			senders['remails[]'] = [];
			senders.imglink = location.href;
			senders.imgurl = 'http://dev.gvf.com.au/galleryimage/'+imagename
			senders.conf = 'riamu';
			$('input.stf_friendsname').each(function(){senders['rnames[]'].push($(this).attr('value'))});
			$('input.stf_friendsemail').each(function(){senders['remails[]'].push($(this).attr('value'))});
	
			$('#shareform').jqmHide();
			$.post('/share/',senders);
			
			hash.w.show();
		}
		
		function shareshow(hash)
		{
			var galurl = location.href;
			var pageurl = window.location.href.split('gvf.com.au')[1];
			var imagesrc = $('div.galimg img').attr('src').split('/');
			imagesrc = imagesrc[imagesrc.length-1];
			var embedlink = '<a href="'+galurl+'" target="_blank" title="GVF GALLERY!"><img src="http://www.gvf.com.au/galleryimage/'+imagesrc+'" /></a>';
			$('input#embedlink').attr('value',embedlink);
			
			// myspace link
			$('input#myspace_cnt').attr('value',embedlink);
			$('input#myspace_url').attr('value',galurl);
			
			// facebook link
			$('a#gallink_facebook').attr('href',"http://www.facebook.com/share.php?u=http://www.gvf.com.au/galleryimage/"+imagesrc);
//			$('a#gallink_facebook').attr('href',"http://www.facebook.com/share.php?u=http://www.gvf.com.au"+pageurl);
			
			hash.w.show();
		}
		$('input#embedlink').click(function(){this.focus(); this.select();$(this).addClass('selected');});
		$('input#embedlink').blur(function(){$(this).removeClass('selected')});
		
		$('#shareform').jqm({trigger: 'a.gal_share', onShow: shareshow});
		$('#sharethanks').jqm({trigger: 'a#sendform', onShow: sharesend});
		
		// add the next and previous listeners
		previous.click(function()
		{
			var p = $('ul#galeryselect li a.sel').parent().prev();
			if(!p.length)
			{
				var p = $('ul#galeryselect li:last');
			}
			var e = {target:p.find('a img')[0]};
			galclick(e);
	//		p.find('a img').trigger('click');
		});
		next.click(function()
		{
			var p = $('ul#galeryselect li a.sel').parent().next();
			if(!p.length)
			{
				var p = $('ul#galeryselect li:first');
			}
			var e = {target:p.find('a img')[0]};
			galclick(e);
		});
		
		$('ul#galeryselect').click(function(e)
		{
			var e = e;
			galclick(e);
			return false;
		});
		
		galclick = function(e)
		{
			if(e.target.parentNode.nodeName.toLowerCase() != 'a')
			{
				return;
			}
		
			// variables
			var a = $(e.target.parentNode);
			var imgurl = a.attr('href');
			var hr = $('div#galleryimg img');
			var container = $('div#galleryimg div.galimg');
			var imagelist = $('ul#galeryselect li a');
			var caption = a.attr('title');
			var imgid = a.attr('className').split('_')[1];
			
			window.scrollTo(0,500);
			
			// add the image ID to the hash
			window.location.hash='img'+imgid;
			
			// remove the select state from the list and add it to the selected anchor
			imagelist.removeClass('sel');
			a.addClass('sel');
			
			// blur the link
			a.blur();
			
			// set the height of the currently displayed image (for future operability)
			container.css('height',hr.height());
			
			// fade out
			hr.fadeOut('slow',function()
			{
				// add the new caption
				$('div.gal_caption').html(caption);
				
				// create the new image
				var img = $('<img />');
				// on image load
				img.bind('load',function()
				{
					// image load variables
					var op = $(this);
					
					setTimeout(
						function()
						{
							// animate the container
							var height = op.height();
							container.animate({height:height},500);
							op.fadeIn(1000);
						}, 10);
					
					
				});
				// set the image SRC
				img.attr('src',imgurl);
				// hide the image
				img.hide();
				// remove old images
				$('div#galleryimg img').remove();
				// append the new image
				container.append(img);
			});
			
			return false;
		};
		
		var h = window.location.hash;
		if(h)
		{
			var id=h.split('img')[1];
			
			var e = {target:$('ul#galeryselect li a.id_'+id+' img')[0]};
			galclick(e);
		}
	}
});