var selected_images = {
	image_ids: [],
	block_toolbar: false,
	add: function (image_id) {
		this.remove(image_id);
		this.image_ids.push(image_id);
		this.save_to_cookie();
	},
	remove: function (image_id) {
		this.image_ids = $(this.image_ids).reject(function() { return this == image_id })
		this.save_to_cookie();
		if (this.count() == 0) { this.toggle_toolbar(); }
	},
	save_to_cookie: function() {
		$.cookie('selected_images', this.image_ids.join(','), {expires: 365, path: '/'})
	},
	fetch_from_cookie: function() {
		if ($.cookie('selected_images')) {this.image_ids = $.cookie('selected_images').split(',');}
	},
	title_url: function() {
		return this.image_ids.length > 0 ? this.url_for(this.image_ids[0]) : '/images/no_image_60x60.gif';
	},
	url_for: function(image_id, width, aspect) {
		if (width == null) {width = 50;}
		if (aspect == null) {aspect = 1;}
		return '/image/'+image_id+'/'+aspect+'/0/'+width;
	},
	id_for: function(url) {
		return url.replace('/image/', '').replace(/\/\d+\/0\/\d+$/, '')
	},
	image_urls: function() {
		var url_for = this.url_for;
		return $(this.image_ids).collect(function() { return url_for(this) })
	},
	image_list_items: function() {
		var image_urls = this.image_urls;
		var id_for = this.id_for;
		return $(this.image_urls()).collect(function() { return '<li><a href="#" rel="'+id_for(this)+'" class="delete">x</a><img src="'+this+'" /></li>' })
	},
	count: function() {
		return this.image_ids.length;
	},
	update_page: function() {
		$('.selected_images_count').html(this.count());
		$('#selected_images').html(this.image_list_items().join(''));
		$('#selected_images a.delete').click(function() {
			selected_images.remove($(this).attr('rel'));
			selected_images.update_page();
			return false;
		});
		$('input.gallery_image_entry_ids').val(this.image_ids);
	},
	toggle_toolbar: function() {
		var selected_images = this;
		$('#toolbar').slideToggle('fast', function() {
			$.cookie('toolbar_shown', $('#toolbar').is(':visible'), {expires: 365, path: '/'})
			selected_images.load_form();
		});
	},
	show_if_any: function () {
		if ($.cookie('toolbar_shown') == 'true' && !this.block_toolbar && this.image_ids.length > 0) {
			$('#toolbar').show();
			this.load_form();
		}
	},
	load_form: function() {
		var selected_images = this;
		if ($('#toolbar').is(':visible') && $('#toolbar form').length == 0) {
			$('#add_to_set_form').load('/add_to_set_form_fragment', function() {
				selected_images.initialize_new_gallery_toggle();
				selected_images.update_page();
			});
			$('#add_to_set_form').removeClass('loading_big');
		}
	},
	initialize_new_gallery_toggle: function() {
		$('.new_gallery_toggle').click(function() { 

			$('#gallery_title_field,#upload_gallery_title_field').slideToggle('fast');
			$('#gallery_image_entries_gallery_id_field,#upload_gallery_image_entries_gallery_id_field').slideToggle('fast');

			if ($('#upload_gallery_title_field')) {
				if ($('#upload_gallery_title_field').is(':visible')) {
					$('#upload_gallery_image_entries_gallery_id').focus();
				} else {
					$('#upload_gallery_title').focus();
				}
			} else {
				if ($('#gallery_title_field').is(':visible')) {
					$('#gallery_image_entries_gallery_id').focus();
				} else {
					$('#gallery_title').focus();
				}
			}
			return false;
		});
	}
}

$(document).ready(function(){
	selected_images.fetch_from_cookie();
	selected_images.update_page();
	selected_images.show_if_any();
	selected_images.initialize_new_gallery_toggle();
	
	$('.select_image').click(function() {
		selected_images.add($(this).attr('rel'));
		selected_images.update_page();
		if (!$('#toolbar').is(':visible')) {
			selected_images.toggle_toolbar();
		}
		return false;
	});
	
	$('.tools .toggle,.toolbar_toggle').click(function() {
		 selected_images.toggle_toolbar(); return false;
	});
	
	var supportsFixedPositioning = supportsFixed();

	$(window).scroll(function() {
		if ($(this).scrollTop() > 65) {
			if (supportsFixedPositioning) {
				$('#toolbar').addClass('fixed');
				$('#toolbar').css('top', "0px");
			} else {
				$('#toolbar').css('top', $(this).scrollTop() + "px");
			}
		} else {
			if (supportsFixedPositioning) {
				$('#toolbar').removeClass('fixed');
			} else {
			}
			$('#toolbar').css('top', "65px");
			
		}
	});
});

function supportsFixed() {
	var testDiv = document.createElement("div");
	testDiv.id = "testingPositionFixed";
	testDiv.style.position = "fixed";
	testDiv.style.top = "0px";
	testDiv.style.right = "0px";
	document.body.appendChild(testDiv);
	var offset = 1;
	if (typeof testDiv.offsetTop == "number" && testDiv.offsetTop != null && testDiv.offsetTop != "undefined") {
		offset = parseInt(testDiv.offsetTop);
	}
	if (offset == 0) {
		return true;
	}
	return false;
};