/*
	[[[ Flickr2Highslide addition to Highslide JS (http://highslide.com/) and JQuery.Flickr (http://github.com/rpheath/jquery-flickr)
	[[[ Author: Dennis Kosse (http://www.denniskosse.com)
*/

// The configuration starts here...

	//You can use %title and %href in the custom Caption
	var flickr2HighslideCaption = '%title<a href="%href" target="_blank"><img src="scripts/flickr_logo.gif" title="View on Flickr" align="right"></a>'; 

	var flickrConfig = {
	//Your own API key needs to be inserted here
	api_key: 'bb250910fc3b72de8cdfc4a282bd63b0',

	//The size configuration is only applied when calculateLargeImgUrl is set to False;
	link_to_size: 'm',

	//If you set this value to true, the link_to_size configuration is ignored, and a large image URL will be calculated from smaller URL's
	calculateLargeImgUrl: true,

	//Configured which class should be defined on the A link (leave this for Highslide usage)
	hrefClass: 'highslide',

	//Use this setting to change settings on Highslide;
	//You can use %class which will be replace with the Classname of the DIV that the gallery is in;
	hrefOnClick: 'return hs.expand(this,{ slideshowGroup:  \'%class\', captionText: \'' + flickr2HighslideCaption + '\' })'
	}

// The configuration stops here...
// ========================================================================================================================
// ========================================================================================================================
// ========================================================================================================================
// ========================================================================================================================
// The code starts here...


		//This replaces the existing 'linkTag' method, so that the creation of the <A> tags for Gallery creation is customized
		$.flickr.linkTag = function(text, photo, href) {
				  var classHTML = '';
				  var onClickHTML = '';

				  //Use the class attribute when this is defined in the configuration
				  if ($.flickr.settings.hrefClass.length > 0)
					classHTML = ' class="' + $.flickr.settings.hrefClass + '"';

					//Use the OnClick attribute when this is defined in the configuration
					if ($.flickr.settings.hrefOnClick.length > 0)
					{
						//Replace needed characters and values
						var CurrentHrefOnClick = $.flickr.settings.hrefOnClick;
						CurrentHrefOnClick = CurrentHrefOnClick.replace (/"/g, "\\'");
						onClickHTML = ' onclick="' + CurrentHrefOnClick + '"';
						onClickHTML = onClickHTML.replace ('%href', 'http://flickr.com/photo.gne?id=' + photo.id);
						var photoTitleSafe = photo.title.replace("'", "&amp;#39;");
						onClickHTML = onClickHTML.replace ('%title', photoTitleSafe);
						onClickHTML = onClickHTML.replace ('%class', $.flickr.self.attr("class"));
					}

					//Setup the image URL, and calculate it to Large when configured
				  if (href === undefined) href = ['http://www.flickr.com/photos', photo.owner, photo.id].join('/')      
				  if ($.flickr.settings.calculateLargeImgUrl == true) href = calculateFlickrUrl (href, 'b');

				  //Return the calculated <A> element
				  return '<a href="' + href + '" title="' + photo.title + '"' + classHTML + onClickHTML + '>' + text + '</a>'			
		};

		//This function calculates a Flickr image size URL based on an existing URL
	  function calculateFlickrUrl (url, size)
		{
				var href_largeimg = url;
				href_largeimg = href_largeimg.replace ("_s.jpg", ".jpg");
				href_largeimg = href_largeimg.replace ("_t.jpg", ".jpg");
				href_largeimg = href_largeimg.replace ("_m.jpg", ".jpg");
				href_largeimg = href_largeimg.replace ("_z.jpg", ".jpg");
				href_largeimg = href_largeimg.replace ("_b.jpg", ".jpg");
				href_largeimg = href_largeimg.replace ("_o.jpg", ".jpg");
				href_largeimg = href_largeimg.replace (".jpg", "_" + size + ".jpg");
				return href_largeimg;
		}

		//This function will add the customized caption to the Highslide lightbox
		hs.onSetClickEvent = function ( sender, e ) {
			e.element.onclick = function () {

				var customSlideshowGroup = this.parentNode.className;
				var customCaptionText = flickr2HighslideCaption.replace ('%title', this.title).replace('%href', this.href);
				var calculatedSource = calculateFlickrUrl (this.getElementsByTagName('img')[0].src, 'b');

				return hs.expand(this, { slideshowGroup: customSlideshowGroup, 
										captionText: customCaptionText,  src: calculatedSource });
			}
			// return false to prevent the onclick being set once again
			return false;
		}

		//This code will remove all <A> links that are setup to be a Photoset Gallery
		$(function(){
			//For each flickrset class element
			$('.flickrset').each(function() {   
				
				//Retrieve the settings
				var flickrSetUrl = $(this).attr('href');
				var numberOfImages = $(this).attr('rel');
				if (numberOfImages == '') numberOfImages = 50;

				//Remove trailing slash
				if (flickrSetUrl.charAt(flickrSetUrl.length - 1) == "/") 
				{
					flickrSetUrl = flickrSetUrl.substring(0, flickrSetUrl.length - 1);
				}
				var splitted = flickrSetUrl.split ('/');
				var flickrSetId = splitted[splitted.length-1]; //Take the last element in the array, which is the Set ID

				//Read the Thumbnails and link to Highslide
				$(this).parent().flickr(flickrConfig).photosetsGetPhotos({photoset_id: flickrSetId, per_page: numberOfImages}); 
				//Tweak to get recent photostream instead of a set
				//$(this).parent().flickr(flickrConfig).photosSearch({user_id: 'me', privacy_filter: '1', per_page: numberOfImages});

				//remove the existing <A> Href
				$(this).remove();
			}); 
		 });
