var wp_gallery = {
    init: function(opts) {
		//Set options,
		this.handlers = opts.handlers ? opts.handlers : alert('You must provide handlers');
		this.next = opts.next ? opts.next : false;
		this.prev = opts.prev ? opts.prev : false;
		this.handlerAttr = 'rel'; //TO FIX
		this.containers = opts.containers ? {} : alert('You must provide a container');
		this.containers.image = opts.containers.image ? opts.containers.image : false;
		this.containers.title = opts.containers.title ? opts.containers.title : false;
		this.anim = true; //TO FIX
		this.dclick = false; // dclick stands for 'disabled_click',
		this.defaultItem = opts.defaultItem ? opts.defaultItem : false;
		this.current = false;
		this.css_class = {active: 'selected'}; //TO FIX

		//Opts Funcs,
		this.click = opts.click ? opts.click : false;
		this.after_click = opts.after_click ? opts.after_click : false;
		
		//Init the anims,
		this.init_anims();
		//Set current if there's one,
		this.defaultItem ? this.set_current(this.defaultItem) : false;
		return this;
	},
	init_anims: function() {
		var wp_g = this;
		
		//set imgs opacity,
		//this.handlers.children('img').css('opacity', 0.7);
		
		//set handlers click event,
		this.handlers.live('click',function(e) { this.blur(); wp_g.switch_image(this); });
		
		//Set next and previous events,
		if(this.next) {
			this.next.live('click',function() {
				var next = wp_g.current.parent().next().children('a');
				if(next.length == 0) {
					next = wp_g.current.parent().parent().next().children('li').children('a');
					if(next.length == 0) { next = wp_g.handlers.get(0); }
				}
				wp_g.switch_image(next);
			});
		}
		if(this.prev) {
			this.prev.live('click',function() {
				var prev = wp_g.current.parent().prev().children('a');
				if(prev.length == 0) { prev = wp_g.handlers.get(wp_g.handlers.length-1); }
				wp_g.switch_image(prev);
			});
		}	
	},
	switch_image: function(el) {
		var wp_g 	= this;
		var el 		= $(el);
		//Disabled multi-clicks,
		if(wp_g.dclick) { return false; }
		wp_g.dclick = true;
		
		//On click,
		wp_g.click ? wp_g.live('click',this, el, e) : false;
		
		wp_g.set_current(el);
	//	if(wp_g.containers.image.attr('src') !== el.attr('rel')) {
			wp_g.containers.image.parent().fadeOut('slow', function() {
				var src = el.attr("rel");
				var image = wp_g.containers.image.children('img');
				wp_g.containers.image.attr('src', src).load(function() {
					wp_g.containers.image.parent().fadeIn('slow');
					//After click,
					wp_g.click ? wp_g.after_click(wp_g, el, e) : false;
					//Enable click
					wp_g.dclick = false;
				});	
			});
		// } else {
		// 	wp_g.click ? wp_g.after_click(wp_g, el, e) : false;
		// 	wp_g.dclick = false;
		// }
		// 
		
		//If the title contianer is set, change it,
		if(wp_g.containers.title) {
			wp_g.containers.title.fadeOut('slow', function() {
				wp_g.containers.title.html(el.children('img').attr('alt'));
				wp_g.containers.title.fadeIn('slow');
			});
		}
	},
	set_current: function(el) {
		var el = $(el);
		
		if(this.current != "") {
			this.current.removeClass(this.css_class.active);
			//this.current.children('img').css('opacity', 0.7);
			
		}
		
		this.current = el;
		this.current.addClass(this.css_class.active);
		//this.current.children('img').css('opacity', 1);
	},
	current : ""
};
