var j = jQuery.noConflict();			
j(function() {
	j('#menu > ul').tabs({ fx: { opacity: 'toggle', duration: 'slow', cache: false } });
	j('#header').click(function(){
		document.location.href = 'index.php';
	});
	changeImage();
    
    j('#delete_image').hover(function () {
	    j('#gallery').addClass('selectedOutline');
	  }, 
	  function () {
	    j('#gallery').removeClass('selectedOutline');
	  }
    );
	
	j('#delete_image').click(function() { // function to delete image
		if (confirmSubmit()) { // if the user has clicked 'yes' in the 'are you sure' dialogbox
			var to_del = j('#gallery').attr('alt'); // get the path to the image from the alt tag of the active image
			j.post("photofunctions.php", { file_to_delete: to_del }, function(data){ // send ajax request to photofunctions.php to delete file, and when finished...
				//j.jGrowl(data);
				j("img[src*='"+to_del+"']").fadeTo("fast", .01); // fade any image containing the name of the image to .01% alpha
				j('#image_operations').hide();
				j('#thumbnails').load('thumbnails.php');
				changeImage();
			});
		}
	});
	j('#submitCaption').click(function(){
		var txtfilename = j('#gallery').attr("alt") + '.txt';
		var caption = j("#caption").val();
		j.post("setcaptions.php", { txtfilename: txtfilename, caption: caption }, function(data){
			j.jGrowl (data);
		});
	})
	
	var clicked=false;
	j('#viewfull').css("cursor", "pointer");
	j('#viewfull').click(function(){
		var fullsizeimg = j('#viewfull').attr('rel'); // full size image link is in the "rel" attribute of the medium sized preview image
		var cap = j('#caption').val();
		if (j('#fullview').length > 0){ //if the fullview div exists
			j('#fullsizeimg').attr('src', fullsizeimg); // replace the current image with the fullsize image
			j('#fullsizecaption').html(cap); // set the caption
		} else { // fullview has not yet been created, so create it:
			j('#container').before('<div id="fullview"><img id="fullsizeimg" src="'+fullsizeimg+'"></div>').addClass("animated").css({"position": "relative"}); //insert fullview and prep for animation
			j('#fullview')
				.addClass("animated") // prep fullview for animation
				.append('<p id="fullsizecaption">'+cap+'</p>')  // append caption element
				.click(function(){  // on click function:
					j('#fullview').animate({"top": "-650px"}, "slow");				
					j('#container').animate({"top": "0px"}, "slow");
			});
		}
		j('html,body').animate({scrollTop: 0}, "slow", function(){
			j('#fullview').animate({"top": "0px"}, "slow");	
			j('#container').animate({"top": "650px"}, "slow");
		}); // scroll to the top if not already at the top of the page. this prevents animation weirdness
	});
	
	
})

function changeImage(img) {
	if (img == null) {
		img = j('#thumbnails a:last').attr('rel');
	}
	var txtfile = img + '.txt';
	j('#image_operations').hide("fast");
	j('#gallery').fadeTo("fast",0, function(){
		j('#gallery').attr('src','phpthumb/phpThumb.php?src=/' + img + '&w=300&h=225&fltr[]=ric|15|15').animate({opacity: 0}, 500).fadeTo("fast",1);	
	})
	j('#gallery').attr('alt', img);
	j('#viewfull').attr('rel', 'phpthumb/phpThumb.php?src=/' + img + '&w=800');
	j.ajax({
	  url: txtfile,
	  cache: false,
	  success: function(txt){
	    j('#caption').val(txt);
	  }
	});

	j('#image_operations').show("fast");
	
	var fullsizeimg = j('#viewfull').attr('rel');
	j('#fullsizeimg').attr('src', fullsizeimg);
}

function confirmSubmit()
{
	var agree=confirm("Are you sure you wish to delete selected image?");
	if (agree)
		return true ;
	else
		return false ;
}
