/*****************************************************************************************
	* jQuery plug-in
	* Flickr Photo Gallery
	* Developed by J.P. Given (http://johnpatrickgiven.com)
	* Useage: anyone so long as credit is left alone...oh and get your own API key ;)
******************************************************************************************/
//Globals
var obj = null; // Container object
var x = 0; // Object X
var y = 0; // Object Y
var c = 0; // Object center point
var ct = 0; // Object center point from top
var mX = 0; // Mouse X
var mY = 0;  // Mouse Y
var imgArray = new Array(); // Array to hold urls to flickr images
var titleArray = new Array(); // Array to hold image titles if they exist
var descArray = new Array(); //
var currentIndex = 0; // Default image index
var fImg = null; // For checking if the image object is loaded.
var mycont = 0;


// Callback for Flickr - Simply set array
function setFlickrData(flickrData) {
	var length = flickrData.photoset.photo.length;
	var thumbHTML = '';
	
	for (i=0; i<length; i++) {
		mycont ++;
		var photoURL = 'farm' + flickrData.photoset.photo[i].farm + '.' + 'static.flickr.com/' + flickrData.photoset.photo[i].server + '/' + flickrData.photoset.photo[i].id + '_' + flickrData.photoset.photo[i].secret +'.jpg'
		var thumbURL = 'http://farm' + flickrData.photoset.photo[i].farm + '.' + 'static.flickr.com/' + flickrData.photoset.photo[i].server + '/' + flickrData.photoset.photo[i].id + '_' + flickrData.photoset.photo[i].secret + '_s.jpg'
		thumbHTML += '<a href="util/pickimg.cfm?img=' +photoURL+'" class="lightbox" rel="group"><img src=' + thumbURL + ' width="50" height="50"></a>';
		

	
	}
	
	//thumbHTML += '</ul>';
	
	$("#flickr_thumbs").html(thumbHTML);
	
	//$("#flickr_thumbs").slideUp("slow");
	
	
$("a.lightbox").fancybox();
	
	
}




// Simple function to check if the image is fully loaded
function checkImageLoad() {
	if (fImg.attr( "complete" ) == false) {
		setTimeout("checkImageLoad()",1000);
	} else {
		$("#flickr_loader").fadeOut();
		
		var current_count = currentIndex + 1;
		$("#flickr_count").html("");
		if (titleArray[currentIndex] != "") {
			$("#flickr_count").append(titleArray[currentIndex]);
		
		}
		
		
		if (descArray[currentIndex] != "") {
			$("#flickr_count").append(" <br> " + descArray[currentIndex]);
		
		}
		
	}
}



/*

// Sort of like an init() but re-positions dynamic elements if browser resized.
$(window).resize(function() {
	// Get the position of the element Flickr obj will be loaded into
	x = obj.offset().left;
	y = obj.offset().top;
	c = x + (obj.outerWidth(true) / 2);
	ct = y + (obj.outerHeight(true) / 2);
	
	//$("#flickr_loader").css("background-color","#fff"); // Set background color of loader to the background-color of container
	$("#flickr_loader").css("width",obj.width() + "px");
	$("#flickr_loader").css("height",obj.height() + "px");

	//$("#flickr_thumbs").css("background-color",obj.css("background-color"));
	//$("#flickr_thumbs").css("width",obj.width() + "px");
//	$("#flickr_thumbs").css("left",x + "px");
//	$("#flickr_thumbs").css("top",y + "px");
});


*/

// Plug-In
(function($) {
	
	// plugin definition
	$.fn.flickrGallery = function(setID,apiKEY) {		
		// Set Obj to the Object
		obj = this;
		
		// Assure images are centered
		this.css("text-align","center")
		
		// init the image loader and set values
	//	$("body").append('<div id="flickr_loader"></div>');
		//$("#flickr_loader").css("background-color","#fff"); // Set background color of loader to the background-color of container
	//	$("#flickr_loader").css("width", "40px");
	//	$("#flickr_loader").css("height","40px");
		
		// CSS object overflow for aspect ratio
		obj.css("overflow","hidden");
		
		// Get the Flickr Set :)
		$.getScript("http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=" + setID + "&api_key=" + apiKEY + "&extras=description&jsoncallback=setFlickrData", function(data){
			// When data is set, load first image.
			//navImg(0);
		});
		
		
	
		
	
		
		
	};
	
	

	
	// private function for debugging
	function debug(msg) {
		window.console.log(msg);
	};
})(jQuery);	


