var Gallery = {

	_current : null,

	_items : new Array(),
	
	_params : null,

	init : function(items, params){
		Gallery._params = params;

		//preload images
		var item = null;
		for(var i=0;i < items.length; i++){
			item = items[i];
			
			if(item != null && typeof item == 'object'){
				Gallery.addItem(item.uri, item.caption);
			}
			else if(item && typeof item == 'string'){
				Gallery.addItem(item);
			}
			else{
				Gallery.addItem('', '');
			}
		}

		if(params.offsetY){
			Gallery._setImage(
				Gallery._getBackground(),
				Gallery._getOverlay().css('backgroundImage')
			);
		}
	},
	
	addItem : function(uri, caption){
		if(!caption) caption = '';
		Gallery._items[Gallery._items.length] = {
			uri		: uri, 
			caption	: caption
		};
		
		if(uri){
			var img = new Image();
			img.src = uri;
		}
	},
	
	next : function(){
		if(Gallery._current == null) Gallery._current = -1;
		Gallery._current++;

		if(Gallery._current > Gallery._items.length-1){
			Gallery._current = 0;
		}
		
		Gallery.show(Gallery._current);
	},

	prev : function(){
		if(Gallery._current == null) Gallery._current = 0;
		Gallery._current--;

		if(Gallery._current < 0){
			Gallery._current = Gallery._items.length-1;
		}
		
		Gallery.show(Gallery._current);
	},	

	show : function(item){
		var items = Gallery._items;
		var offsetY = Gallery._params.offsetY;

		if(Gallery._current != null && Gallery._params.useFade){
			if(offsetY){
				Gallery._getBackground().css(
					'backgroundPosition', 
					'left '+(-offsetY*item)+'px'
				);
				
				var f = function(){
					Gallery._getOverlay().css(
							'backgroundPosition',
							Gallery._getBackground().css('backgroundPosition')
					);
	
					Gallery._getOverlay().show();
				};
			}
			else{
				
				Gallery._setImage(Gallery._getBackground(), items[item].uri);
	
				var f = function(){
					Gallery._setImage(
						Gallery._getOverlay(), 
						Gallery._getBackground().css('backgroundImage')
					);
	
					Gallery._getOverlay().show();
				};
			}

			Gallery._getOverlay().fadeOut("slow", f);
		}
		else{
			if(offsetY){
				Gallery._getOverlay().css(
					'backgroundPosition',
					'left '+(-offsetY*item)+'px'
				);
			}
			else{
				Gallery._setImage(Gallery._getOverlay(), items[item].uri);
			}
		}
		
		if(Gallery._getCaption()){
			if(items[item].caption){
				Gallery._getCaption().html(items[item].caption);
			}
			else{
				Gallery._getCaption().empty();
			}
		}

		Gallery._current = item;
	},

	play : function(interval, item){
		if(!interval){
			interval = 5000;
		}
		
		if(item){
			Gallery.show(item);
		}
		else{
			Gallery.show(0);
		}
		
		if(interval){
			window.setInterval(Gallery.next, interval);
		}
	},
	
	_setImage : function(obj, source){
		
		if(source.substr(0, 4) == 'url('){
			source = source.substr(4, source.length-5);
		}
		
		obj.css('backgroundImage', 'url('+source+')');
		
		if(Gallery._params.useAlpha){
			obj.css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+source+"', sizingMethod='scale'");
		}
	},

	_getBackground : function(){
		return $(Gallery._params.background);
	},

	_getOverlay : function(){
		return $(Gallery._params.overlay);
	},
	
	_getCaption : function(){
		if(Gallery._params.caption){
			return $(Gallery._params.caption);
		}
		else{
			return null;
		}
	}	
};