/*
	Flick call function
	http://www.flickr.com/services/api/
		
	shadowbox informatie: http://shadowbox-js.com/forum.html#nabble-td1310680
	http://css-tricks.com/build-your-own-social-home/
	http://tantannoodles.com/toolkit/flickr-dhtml-badge/
	http://www.db798.com/pictobrowser/
	
	//slideshows
	http://www.ohlone.edu/org/webteam/proceduresnotes/object-flickrslideshow.html
	http://paulstamatiou.com/how-to-quickie-embedded-flickr-slideshows/
	
	valid
	http://tylrslidr.com/
	get flickr id: http://idgettr.com/	http://get-flickr-id.ubuntu4life.com/
	2010.07.17 (awb; shadowbox setup toegevoegd)
*/
function flickr (settings) {
	this.settings = settings;
	
	this.default_args = {
		api_key: "f28804be7a09c5845676349c7e47d636",
		//api_key: null,
		type: null,                 // [string]    allowed values: 'photoset', 'search', default: 'flickr.photos.getRecent'
		photoset_id: null,          // [string]    required, for type=='photoset'  
		text: null,			            // [string]    for type=='search' free text search
		user_id: null,              // [string]    for type=='search' search by user id
		group_id: null,             // [string]    for type=='search' search by group id
		tags: null,                 // [string]    for type=='search' comma separated list
		tag_mode: 'any',            // [string]    for type=='search' allowed values: 'any' (OR), 'all' (AND)
		sort: 'relevance',    // [string]    for type=='search' allowed values: 'date-posted-asc', 'date-posted-desc', 'date-taken-asc', 'date-taken-desc', 'interestingness-desc', 'interestingness-asc', 'relevance'
		thumb_size: 's',            // [string]    allowed values: 's' (75x75), 't' (100x?), 'm' (240x?)
		size: null,                 // [string]    allowed values: 'm' (240x?), 'b' (1024x?), 'o' (original), default: (500x?)
		per_page: 100,              // [integer]   allowed values: max of 500
		page: 1,                 	// [integer]   see paging notes
		attr: '',                   // [string]    optional, attributes applied to thumbnail <a> tag
		api_url: null,              // [string]    optional, custom url that returns flickr JSON or JSON-P 'photos' or 'photoset'
		params: '',                 // [string]    optional, custom arguments, see http://www.flickr.com/services/api/flickr.photos.search.html
		api_callback: '?',          // [string]    optional, custom callback in flickr JSON-P response
		paging: false,
		frame:	'jquery-flickr'
	}
	
	// zet argumenten weg.
	for(var index in this.default_args) {
		if(typeof this.settings[index] == "undefined") this.settings[index] = this.default_args[index];
	}
	/*
	var frameResult = this.settings['frame'];

	if (frameResult==null){
		this.divTag = document.createElement("div");
		this.divTag.id = this.settings['frame'];
		document.body.appendChild(this.divTag);
	}
	*/
	var that = this;

	this.render = function() {
		this.settings['callback'] = this.refreshPagination;	//roepen we nu pas aan
		$("#" + this.settings.frame).html($("<img/>").attr("src", "/icons/loading.gif").attr("id", "flickr_loader"));
		jQuery("#" + this.settings.frame).flickr(this.settings);
	};
	this.getPhotos = function(page){
		var refreshfunction = this.refreshPagination;
		this.settings['callback'] = refreshfunction;
		this.settings['page'] = page;
		$("#" + this.settings.frame).html($("<img/>").attr("src", "/icons/loading.gif").attr("id", "flickr_loader"));
		jQuery("#" + this.settings.frame).flickr(this.settings);
	}
	this.refreshPagination = function(ele){
		$("#"+that.settings['frame']+" #flickr_loader").remove();	// verwijder laad icoontje

		if (that.settings['paging']){
			var current_page = parseInt($(ele).find("input:eq(0)").val());
			var total_pages = parseInt($(ele).find("input:eq(1)").val());
	
			if (total_pages > 1){
				// taal moet hier uit
				$("<span>").html("Pagina " + current_page + " van " + total_pages).addClass("total").appendTo("#"+that.settings['frame']);
			}
			if (total_pages != 1){
				if (current_page <= 1) {
					$("<span>").html("vorige").addClass("previous").appendTo("#"+that.settings['frame']);
					$("<a>").attr("href", "#").html("volgende").addClass("next").bind("click", function(e) {e.preventDefault();that.getPhotos(current_page + 1);}).appendTo("#"+that.settings['frame']);					
				} else if (current_page >= total_pages) {
					$("<a>").attr("href", "#").html("vorige").addClass("previous").bind("click", function(e) {e.preventDefault();that.getPhotos(current_page - 1);}).appendTo("#"+that.settings['frame']);
					$("<span>").html("volgende").addClass("next").appendTo("#"+that.settings['frame']);
				} else {
					$("<a>").attr("href", "#").html("vorige").addClass("previous").bind("click", function(e) {e.preventDefault();that.getPhotos(current_page - 1);}).appendTo("#"+that.settings['frame']);
					$("<a>").attr("href", "#").html("volgende").addClass("next").bind("click", function(e) {e.preventDefault();that.getPhotos(current_page + 1);}).appendTo("#"+that.settings['frame']);
				}
			}
		}
		// shadowbox herinitialisatie
		that.shadowboxLinks(that.settings['frame']);	// zet rels bij de afbeeldingen
		Shadowbox.setup($('a[rel^=shadowbox]', $(ele)));// vertel shadowbox dat er nieuwe rels zijn
	};
	
	/*maakt alle rel shadowbox aan bij alle nieuwe afbeeldingen */
	this.shadowboxLinks = function(ele) {		
		$('#' + ele + ' a').each(function() {
			$(this).attr('rel','shadowbox[flickr]').addClass("flickr-image");
		});
	}	
}

function callflickr(options){
	// kijk welke pagina we nu zijn
	var current_page = parseInt($('#jquery-flickr ul').find("input:eq(0)").val());
	if (isNaN(current_page)){
		current_page = 1;	// geen pagina index dus pagina 1
	} else {
		current_page += 1;
		var total_pages = parseInt($('#jquery-flickr ul').find("input:eq(1)").val());
		if (current_page > total_pages){
			current_page = 1;	// voor het rouleren zetten we de huidige pagina weer terug.
		}								
	}	

}

