largeName = $('#large-img').attr('name');
page = getPage("p");
curImage = '';
loc = getPage("loc");



// FUNCTION USED FOR NEXT AND PREV BUTTONS
function f_slideshow( n ) {
	var numImages = $('#right-gallery img').length;
	largeImg = $('#large-img');

	curImage = parseInt(largeImg.attr('name'));
	curImage = curImage + n;
	
		if (curImage > numImages)
			{ curImage = 1 ; }
		if (curImage == 0)
			{ curImage = numImages ; }	
		
			largeImg.fadeOut(500, function() {
										   
				var newSrc = $('#right-gallery img:eq('+(curImage-1)+')').parent().attr('href');
				var target = $('#right-gallery img:eq('+(curImage-1)+')').parent();
				largeImg.attr({'name': String(curImage), 'src': newSrc});
				if (this.complete) $(this).fadeIn(500);
				if (target.attr("rel") != '' && target.attr("rev") != '')
					{
					changeCaption(target.attr("title") +  '<br /><a href="http://' + target.attr("rel") + '" target="_blank">' + target.attr("rel") + '</a>' + '<br />' + target.attr("rev"));
					}
				else if (target.attr("rel") == '' && target.attr("rev") != '')
					{
					changeCaption(target.attr("title") + '<br />' + target.attr("rev"));
					}
				else if (target.attr("rel") != '' && target.attr("rev") == '')
					{
					changeCaption(target.attr("title") +  '<br /><a href="http://' + target.attr("rel") + '" target="_blank">' + target.attr("rel") + '</a>');
					}
				else 
					{
						changeCaption(target.attr("title"));
					}
					showHidePrev();
					showHideNext();	
				});
} 

// $GETing VAR (a) FROM URL - FOR USE IN PREV AND NEXT BUTTONS FUNCTIONALITY- RETURNS "page" - to navigate to next page when clicking prev or next buttons at beginning or end of thumbnail list - if VAR (a) is undefined "page" = 1
function getPage(a) 
	{
		var GET = {};
		document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
			function decode(s) {
				return decodeURIComponent(s.split("+").join(" "));
			}			
			GET[decode(arguments[1])] = decode(arguments[2]);
		});	
			if (GET[a])
			{
				page = (GET[a]);
			}
			else 
			{
				page = 1;
			}
			
			return page;
}

function changeAtt(v){
$("#large-img").attr("name", v);
}

function changeCaption(v){
$("#caption").html(v);
	if (v == '') {
		$("#caption").fadeOut(500);
	}
	else $("#caption").fadeIn(500);
}

// don't display prev or next btn if at first or last image 
function showHidePrev() 
{
	largeName = $('#large-img').attr('name');
	page =getPage("p");	
	if (page == '1' && largeName == '1')
	{
		$('#prev').attr({src :"/images/prev_disabled.png"});
	}
	else
	{
		$('#prev').attr({src :"/images/prev.png"});
	} 
}
			
function showHideNext() 
{
	largeName = parseInt($('#large-img').attr('name'));
	page = getPage("p");
	curImageCount = 10 * (page-1) + largeName;
	total = $('#numImages').val();
	if (curImageCount == total)
	{
		$('#next').attr({src :"/images/next_disabled.png"});
	}
	else
	{
		$('#next').attr({src :"/images/next.png"});
		
	}
	//alert(curImageCount);
	//alert(total);
}

// START DOC READY
$(document).ready(function(){
	
	$('#large-img').fadeIn(500);
	

	showHidePrev();
	showHideNext();
	

	// THUMBNAIL ROLLOVER
	$('#right-gallery a img').hover(
	function(){
		$(this).addClass('over');
												
	},function(){
		$(this).removeClass('over');
	});
	
	//THUMBNAILS CLICK
	$("a.thumb").click(function() {
		var atag = $(this);
		$("#large-img").fadeOut(500, function() {
				$(this).attr({						 
					src: atag.attr('href'),
					name: atag.find("img").attr("name")});
						
				if (this.complete)
				{
					$(this).fadeIn(500);	
					showHidePrev();
					showHideNext();
				}
		 });		
		// CHANGE CAPTION
		// rel is linkLoc field in DB, title is title field in DB
		if ($(this).attr("rel") != '' && $(this).attr("rev") != '')
			{
			changeCaption($(this).attr("title") +  '<br /><a href="http://' + $(this).attr("rel") + '" $(this)="_blank">' + $(this).attr("rel") + '</a>' + '<br />' + $(this).attr("rev"));
			}
		else if ($(this).attr("rel") == '' && $(this).attr("rev") != '')
			{
			changeCaption($(this).attr("title") + '<br />' + $(this).attr("rev"));
			}
		else if ($(this).attr("rel") != '' && $(this).attr("rev") == '')
			{
			changeCaption($(this).attr("title") +  '<br /><a href="http://' + $(this).attr("rel") + '" $(this)="_blank">' + $(this).attr("rel") + '</a>');
			}
		else 
			{
				changeCaption($(this).attr("title"));
			}
	});
	
	//PREV BUTTON CLICK
	$("img#prev").click(function() {
			largeName = parseInt($('#large-img').attr('name'));
			lastpage = parseInt(page) - 1;
			imgSrc = $(this).attr('src');
			loc = getPage("loc");
			page = getPage("p");
			//alert (loc);
				
				if (page == '1' && largeName == '1')
				{
					return;
				}
				else if (largeName == '1' && loc != "1")
				{
					window.location.href = 'index.php?r=true&p=' + lastpage + '&loc=' + loc;
				}
				else if (largeName == '1')
				{
					window.location.href = 'index.php?r=true&p=' + lastpage;
				}
				else 
				{
					f_slideshow(-1);
				}
		 });
	
	// NEXT BUTTON CLICK
	$("img#next").click(function() {
			nextpage = parseInt(page) + 1;
			largeName = parseInt($('#large-img').attr('name'));
			thisPage = getPage("p");
			loc = getPage("loc");
			prevPgs = parseInt(thisPage) - 1;
			if (prevPgs == 0)
			{
				curImageCount = largeName;
			}
			else 
			{
				curImageCount = (prevPgs * 10) + parseInt(largeName);
			}
			
			total = $('#numImages').val();
			if (curImageCount == total) 
			{
				return;
			}
			else if (largeName == '10' && loc  != "1")
			{
				
				window.location.href = 'index.php?p=' + nextpage + '&loc=' + loc;
			}
			else if (largeName == '10')
			{
				window.location.href = 'index.php?p=' + nextpage;
			}
			else 
			{
				f_slideshow(1);
			}
	 });
	
	$('select#property').change(function(){
			prop = $('select#property option:selected').val();
			if (prop) 
			{
			$('#filter_form').attr('action', 'index.php?loc='+prop)
			}		
			$('#filter_form').submit();
	});

}); //DOC READY






