// Class definition
PhotoBrowser = function() {
	this.init();
}
PhotoBrowser.prototype.init = function() {
	this.id_prefix = "photo_browser";
	
	this._collection = [];
	this._image_size = "banner";
	this.current_key = 0;
	this.current_image_url = "";
	this.lightbox_add_url = "/basket/photo/add/";
	this.lightbox_remove_url = "/basket/photo/remove/";
	this.default_slideshow_delay = 15000;
	this.slideshow_interval = false;
}

	PhotoBrowser.prototype.register_starting_image = function(id) {
		this.current_key = this.id_to_key(id);
	}
	PhotoBrowser.prototype.register_collection = function(a_collection) {
		this._collection = a_collection;
	}
	PhotoBrowser.prototype.set_image_size = function(s_size) {
		this._image_size = s_size;
	}
	PhotoBrowser.prototype.total_photos = function() {
		return this._collection.length;
	}
	PhotoBrowser.prototype.max_key = function() {
		return (this.total_photos() == 0)? 0 : this.total_photos()-1;
	}
	
	PhotoBrowser.prototype.switch_orientation = function(orient) {
		(orient=="portrait") ? Element.addClassName('photo_browser','orient_portrait') : Element.removeClassName('photo_browser','orient_portrait');
	}
	
	PhotoBrowser.prototype.view_current_photo = function() {
		document.location = this.current_image_url;
	}
	PhotoBrowser.prototype.buy_current_photo = function() {
		new Ajax.Updater(this.id_prefix+"_lightbox_confirm",
						 this.lightbox_add_url+this.key_to_id(this.current_key),
						{asynchronous:true, evalScripts:true, 
						  onComplete: 	function(request){
										Effect.Appear(photo_browser.id_prefix+"_lightbox_confirm"); }
										});
	}
	PhotoBrowser.prototype.unbuy_current_photo = function() {
		new Ajax.Updater(this.id_prefix+"_lightbox_confirm",
						 this.lightbox_remove_url+this.key_to_id(this.current_key),
						{asynchronous:true, evalScripts:true, 
						  onComplete: 	function(request){
										Effect.Fade(photo_browser.id_prefix+"_lightbox_confirm", {delay: 1.0}); }
										});
	}
	
	PhotoBrowser.prototype.next_photo = function(keepInterval) {
		this.load_photo_by_key(((this.current_key >= this.max_key())? 0 : this.current_key+1));
		if(!keepInterval) this.stop_slideshow();
	}
	PhotoBrowser.prototype.previous_photo = function(keepInterval) {
		this.load_photo_by_key(((this.current_key <= 0)? this.max_key() : this.current_key-1));
		if(!keepInterval) this.stop_slideshow();
	}
	
	
	PhotoBrowser.prototype.load_photo_by_key = function(key) {
		this.load_photo_by_id(this._collection[key]);
	}
	PhotoBrowser.prototype.load_photo_by_id = function(id) {
		this.current_key = this.id_to_key(id);
		new Ajax.Updater(this.id_prefix+'_snippet_receiver',
						 '/photos/serve_browser_snippet/'+id+'?options_id='+this.id_prefix+'&image_size='+this._image_size,
						 {asynchronous:true, evalScripts:true, 
						  onLoading: 	function(request){ 	new Effect.Fade(photo_browser.id_prefix+'_image',				{duration: 0.5});
						 								   	new Effect.Fade(photo_browser.id_prefix+"_information",		{duration: 0.5}); }});
	}
	PhotoBrowser.prototype.id_to_key = function(find_id) {
		found_at_key = 0;
		this._collection.each(function(id,key) {
			if(id==find_id) return found_at_key = key;
		});
		return found_at_key;
	}
	PhotoBrowser.prototype.key_to_id = function(find_key) {
		found_id = 0;
		this._collection.each(function(id,key) {
			if(key==find_key) return found_id = id;
		});
		return found_id;
	}
	
	PhotoBrowser.prototype.start_idling_fields = function() {
		var fields = $A(['postcard_from_email','postcard_from_name','postcard_to_name','postcard_to_email']);
		var vals = $A(["Your email address","Your name","Your friend's name","Your friend's email address"]);
		fields.each(function(id,key) {
			$(id).value = "";
			window.setInterval("photo_browser.auto_idle_field('"+id+"',\""+vals[key]+"\");",2000);
		});
	}
	
	PhotoBrowser.prototype.auto_idle_field = function(fieldid, value) {		
			elem = $(fieldid);
			if(elem.value == value) {
				Element.addClassName(fieldid,'idle');
		  	} else if(elem.value == "" || elem.value == " ") {
				Element.addClassName(fieldid,'idle');
				elem.value = value;
		  	} else {
				Element.removeClassName(fieldid,'idle');
			}
	}
	
	PhotoBrowser.prototype.start_slideshow = function(delay) {
		var d = (delay)? delay : this.default_slideshow_delay;
		this.slideshow_interval = window.setInterval(function() { 
		  photo_browser.next_photo(true);
		},d);
		return true;
	}
	PhotoBrowser.prototype.stop_slideshow = function() {
		window.clearInterval(this.slideshow_interval);
		return true;
	}
	
	
var photo_browser;
photo_browser = false;
photo_browser = new PhotoBrowser();