//Add bind method
if(!Function.prototype.bind) {
	Function.prototype.bind = function(context) {
		var __method = this;
		var __arguments = jQuery.makeArray(arguments).slice(arguments.callee.length);
		return function() {
			var args = __arguments.concat(jQuery.makeArray(arguments));
			return __method.apply(context, args);
		};
	};
}

jQuery.fn.extend({
	imgHover: function(on_url) {
		this.each(function() {
			var image = jQuery(this);
			var new_on_url = on_url;
			if(jQuery.isFunction(on_url)) {
				new_on_url = on_url.call(this, image);
			}
			if(!new_on_url) {
				return;
			}
			image.data('on_url', new_on_url);
			image.data('off_url', image.attr('src'));
			image.hover(function() {
				image.attr('src', image.data('on_url'));
			}, function() {
				image.attr('src', image.data('off_url'));
			});
			
			//Preload the image
			document.createElement('img').setAttribute('src', new_on_url);
		});
	}
});

//Image hover
jQuery(document).ready(function() {
	var main_title = jQuery('#main_title img');
	if(main_title.length > 0) {
		main_title.imgHover(main_title.attr('src').replace(/_i\./, '_a.'));
	}
	
	jQuery('#images img').imgHover(function() {
		return this.previousSibling.nodeType === Node.COMMENT_NODE && this.previousSibling.nodeValue;
	});
});

//Overview navigation link description hover
jQuery(document).ready(function() {
	var hover_description = jQuery('#hover_description');
	if(hover_description.length === 0) {
		return;
	}
	
	jQuery('a[title]').live('mouseenter', function() {
		hover_description.text(this.title);
	}).live('mouseleave', function() {
		hover_description.text('');
	});
});

//Stills slider
jQuery(window).bind('load', function() {
	var cont = jQuery('div.stills');
	if(!cont.length) {
		return;
	}
	
	var aspect = 5/4;
	var thumbnail_width = 150;
	var thumbnail_height = thumbnail_width * (1/aspect);
	var still_width = 640;
	var still_height = still_width * (1/aspect);
	
	var disclaimer = cont.next('.disclaimer');
	var timeout = null;

	var slides = cont.children('ul.slides');

	var images = slides.find('img');
	
	images.each(function(i) {
		var image = jQuery(this);
		var slide = image.closest('li');

		if(image.data('imageWidth') > 1000) {
			var download_info = jQuery('<div/>', {'class': 'download-info'});
			var download_button = jQuery('<button/>', {text: 'download ↧'}).click(function() {
				var license_image = null;
				disclaimer.text(image.data('disclaimer'));
				if(image.data('licenseImage')) {
					license_image = jQuery('<img/>', {src: image.data('licenseImage')});
					var license_link = null;
					if(image.data('licenseUrl')) {
						license_link = jQuery('<a/>', {href: image.data('licenseUrl')}).append(license_image);
					}
					disclaimer.append(license_link || license_image);
				}
				disclaimer.slideDown('slow');
				var load_dl = function() {
					var dl_url = image.prop('src').replace(/\?max_width=\d+$/, '?download=true');
					window.location.href = dl_url;
				};
				if(!license_image || license_image.prop('complete')) {
					load_dl();
				} else {
					license_image.bind('load', load_dl);
				}
			}).appendTo(download_info);
			slide.append(download_info);
		}
	});

	cont.flexslider({
		animation: "slide",
		slideshow: false
	}).find('ul.flex-direction-nav li a').each(function() {
		var a = jQuery(this);
		if(a.is('.prev')) {
			a.text('«');
		} else {
			a.text('»');
		}
	}).end().show();

});

