// remap jQuery to $
(function($){})(window.jQuery);


/* trigger when page is ready */
$(document).ready(function (){

// trigger the image resize on load
ResizeImages();

// store how wide img containers are on load (should be an ID, but... (to work on!)
var loadWidth = $( "div.content-img" ).width();

// when the window is resized, check the width of the same img containers.  If its not the same as it was on load then we've hit a MQ breakpoint
$(window).resize(function () {
            if ($('div.content-img').width() != loadWidth) {
                //If the media query has been triggered, store the new width
                loadWidth = $('div.content-img').width();
				// and reload/resize the images
                ResizeImages();
            }
        });





$(".gallery_preview").click( 

	function() {

		var maxWidth = $("div.content-img").width();
		maxWidth = maxWidth - 10;
		var imgSrc = $(this).find("img").attr('rel');
	
		if (imgSrc.search("flickr.com") != -1) {
			$("div.content-img").find("img").attr('src', imgSrc);	
		} else {
			$("div.content-img").find("img").attr('src', "/thumbnail.php?gd=2&src="+imgSrc+"&maxw="+Math.ceil(maxWidth));
		}
	
});

});

function ResizeImages() {

$( "div.content-img" ).each(
 
	function(){
 
		var maxWidth = $(this).width(); // get the width of the img container div.
		var maxHeight = 600; // I don't want my images to be bigger than this
		maxWidth = maxWidth - 10; // knock off 10px for the border
		maxHeight = maxHeight - 10;
		var imgSrc = $(this).find("img").attr('rel'); // img paths are stored in rel - theres probably a better way of doing this
	
		if (imgSrc.search("flickr.com") != -1) { // catch any old images being pulled from Flickr - this is only really needed on my blog
			$(this).find("img").attr('src', imgSrc);	// if its a Flickr img, just show it
		} else {
			$(this).find("img").attr('src', "/thumb.php?file="+imgSrc+"&sizex="+Math.ceil(maxWidth)+"&sizey="+Math.ceil(maxHeight)); // set the img src to the path to our funky PHP script and pass it the calculated width and height - I'm still amazed that the browser pulls the image even though technically its delivered after load, but hey ho.
		}

	});

}


/* optional triggers

$(window).load(function() {
	
});

$(window).resize(function() {
	
});

*/
