jQuery(document).ready(function($){
	var flickrGroupID = "1249218@N24"; // set flickr pool group id
	var numImages = 54; // number of images to download
	var poolUri = "http://api.flickr.com/services/rest/?method=flickr.groups.pools.getPhotos&api_key=778202969d3e868e915fa0947d45a966&group_id=" + flickrGroupID + "&extras=url_m%2C+url_sq%2C+date_taken%2C+description&per_page=" + numImages + "&format=json&jsoncallback=?"
	// get flickr pool images from flickr api, add to JQuery Cycle gallery
	$.jsonp({
		url: poolUri,
		type: "GET",
		dataType: "json",
		timeout: 8000,
		success: function(data) {
			$('#gallery-error').remove();
			var html = "<div class='slide'>";
			var items = data.photos.photo;
			var IE = /*@cc_on!@*/false;
			fisherYates(items);
			//if(!IE){
				//items.sort(randOrd); // TO FIX: for some reason, IE is throwing an error if array.sort is called
			//};
			for (var i=0; i<items.length; i++){
				var photoURI = "http://www.flickr.com/photos/" + items[i].owner + "/" + items[i].id + "/sizes/in/pool-" + flickrGroupID;
				var sourceMedium = items[i].url_m;		
				var sourceSquare = items[i].url_sq;
	    		// set html of image
	    		html += '<a title="' + items[i].description._content + '<br /><a href=\'' + photoURI + '\' target=\'_blank\'>Download Image</a>" rel="lightbox-tels" href="' + sourceMedium + '">';
	    		html += '<img src="' + sourceSquare + '" title="' + items[i].description._content + '" alt="' + items[i].title + '"/>';
	    		html += '</a>';
	    		if ((i+1)%9 == 0 || i==items.length-1){ // 8 images per slide
	    			html += "</div>";
	    			//$("#front-gallery").append(html); // add slide to gallery
	    			$("#front-gallery").append(html); // add slide to gallery
	    			html = "<div class='slide'>";
	    		}
			}
	    	
			$('.slide a:first-child').addClass('first');
			$('.slide a:last-child').addClass('last');
			
			// set JQuery Cycle options
	    	$('#front-gallery').cycle({
				fx:     'fade',
				pager:  '#pager',
				timeout: 8000,
				pause:	1,     // true to enable "pause on hover" 
			    pauseOnPagerHover:	1, // true to pause when hovering over pager link
			    next: '#next',
			    prev: '#previous',
			    pagerAnchorBuilder: function(idx, slide) {
		            var src = "/sites/all/themes/tels/pager_overlay.png";
		            return '<a href="#"><img src="' + src + '" alt=""/></a>';
		        } 
			    
			});
	    	
	    	// set slimbox2 options
	    	$("a[rel^='lightbox']").slimbox({
	    		overlayOpacity: 0.7,
	    		loop: 1
	    	}, null, function(el) {
	    		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	    	});
	    	
	    	// pause and play controls for JQuery Cycle gallery
	    	$('#pause').click(function() { 
	    	    $('#front-gallery').cycle('pause');
	    	    $(this).hide();
	    	    $('#play').show();
	    	    $('#front-gallery').cycle('pause');
	    	    return false;
	    	});
	    	
	    	$('#play').click(function() { 
	    	    $('#front-gallery').cycle('resume', true);
	    	    $(this).hide();
	    	    $('#pause').show();
	    	    return false;
	    	});
	    	
	    	$('#pause').show();
	    	$('div.advance').show();
	    	$('#gallery-loading').hide();
		},
		error: function(xhr, textStatus, errorThrown){
            $('#gallery-error').show();
            $('#gallery-loading').hide();
		}
		
		// stop the slideshow when any gallery image link is clicked
    	// cycle pause isn't registering for some reason, so disabling this for now
		/*$("a[rel^='lightbox']").click(function(){
			$('#pause').click();
		});*/

	});
});

function randOrd(){
  return 0.5 - Math.random();
};

// randomizing function from http://sedition.com/perl/javascript-fy.html
function fisherYates ( myArray ) {
	var i = myArray.length;
	if ( i == 0 ) return false;
	while ( --i ) {
	    var j = Math.floor( Math.random() * ( i + 1 ) );
	    var tempi = myArray[i];
	    var tempj = myArray[j];
	    myArray[i] = tempj;
	    myArray[j] = tempi;
   }
}


