(function($) {

	$.adGallery = {
		defaults : {
			fadeSpeed	: 600,
			animation : 300,
			caption		: 1,
			count 		: 1,
			title			: 1
		},
		
		current 					: 0,
		items 						: [],
		total 						: 0,
		constraintWidth  	: 800,
		constraintHeight 	: 600,
		width 						: 0,
		height 						: 0,
		offsetX 					: 0,
		offsetY						: 0,
		animationQuick		: 150,
		
		layoutIds : {
			fader 		: 'adMediaFader',
			holder 		: 'adMediaHolder',
			btnClose 	: 'adMediaClose',
			btnNext 	: 'adMediaNext',
			btnBack 	: 'adMediaBack',
			caption 	: 'adMediaCaption',
			count 		: 'adMediaCount',
			title 		: 'adMediaTitle',
			media			: 'adMedia'			
		}	
		
	};
	
	var layout = '<div id="'+$.adGallery.layoutIds.fader+'"></div>';
	layout += '<div id="'+$.adGallery.layoutIds.holder+'">';
		layout += '<div id="'+$.adGallery.layoutIds.btnClose+'"></div>';
		layout += '<div id="'+$.adGallery.layoutIds.btnNext+'"><div class="adMediaButton"></div></div>';
		layout += '<div id="'+$.adGallery.layoutIds.btnBack+'"><div class="adMediaButton"></div></div>';
		layout += '<div id="'+$.adGallery.layoutIds.caption+'">';
			layout += '<div id="'+$.adGallery.layoutIds.count+'"></div>';
			layout += '<div id="'+$.adGallery.layoutIds.title+'"></div>';
		layout += '</div>';
		layout += '<div id="'+$.adGallery.layoutIds.media+'"></div>';
	layout += '</div><!-- ad media holder -->';
	
	
	/* extend jquery with the plugin */
	$.fn.extend({
		adGallery:function(config) {
			var obj = $(this);
			
			/* use defaults or properties supplied by user */
			if(config) { if(config.animation) $.adGallery.animationQuick = Math.floor(config.animation/2); }
			var config = $.extend($.adGallery.defaults, config);
			
			$.adGallery.init();
			
			obj.click(function(){
				var galleryItem = $(this);
				var rel = galleryItem.attr('rel');
				
				$.adGallery.items = $('a[rel="'+rel+'"]');
				$.adGallery.current = $.adGallery.items.index(galleryItem);
				$.adGallery.total = $.adGallery.items.length-1;
				
				$.adGallery.setup();
				return false;
			});
			
			/* handle the close clicks */
			$('#'+$.adGallery.layoutIds.fader+',#'+$.adGallery.layoutIds.btnClose).click(function(){
				$.adGallery.fade('down');	
			});
			
			/* handles the next clicks */
			$('#'+$.adGallery.layoutIds.btnNext).click(function(){
				$.adGallery.change('next');	
			});
			/* handles the back clicks */
			$('#'+$.adGallery.layoutIds.btnBack).click(function(){
				$.adGallery.change('back');	
			});
					
			/* return the jquery object for chaining */
			return this;
		}
	});
	
	
	/* control functions */
	$(window).resize(function(){ $.adGallery.resetData(); });
	
	$.adGallery.init = function() {
		if($($.adGallery.fader).length < 1) {
			$('body').append(layout);
		}
		$.adGallery.resetData();
	}
	
	$.adGallery.resetData = function(){
		$.adGallery.constraintWidth = $(window).width()-40;
		$.adGallery.constraintHeight = $(window).height()-40;	
	}

	$.adGallery.setup = function(){
		$.adGallery.defaults.caption 	? $('#'+$.adGallery.layoutIds.caption).show() : $('#'+$.adGallery.layoutIds.caption).hide();
		$.adGallery.defaults.count 		? $('#'+$.adGallery.layoutIds.count).show() 	: $('#'+$.adGallery.layoutIds.count).hide();
		$.adGallery.defaults.title 		? $('#'+$.adGallery.layoutIds.title).show() 	: $('#'+$.adGallery.layoutIds.title).hide();
		
		$.adGallery.loadContent();
	}
	
	$.adGallery.fade = function(dir) {
		switch(dir) {
			case 'up':
				$('#'+$.adGallery.layoutIds.fader,'#'+$.adGallery.layoutIds.holder).fadeIn($.adGallery.fadeSpeed);
			break;	
			case 'down':
				$('#'+$.adGallery.layoutIds.btnClose).fadeOut($.adGallery.animationQuick,function(){
					if($.adGallery.defaults.caption) {
						$('#'+$.adGallery.layoutIds.caption).fadeOut($.adGallery.animationQuick,function(){
							$('#'+$.adGallery.layoutIds.fader).fadeOut($.adGallery.fadeSpeed);
							$('#'+$.adGallery.layoutIds.holder).fadeOut($.adGallery.fadeSpeed,function(){ $.adGallery.unloadContent(); });
						});
					} else {
						$('#'+$.adGallery.layoutIds.fader).fadeOut($.adGallery.fadeSpeed);
						$('#'+$.adGallery.layoutIds.holder).fadeOut($.adGallery.fadeSpeed,function(){ $.adGallery.unloadContent(); });
					}
				});
			break;
			case 'load':
				$('#'+$.adGallery.layoutIds.fader).fadeIn($.adGallery.fadeSpeed);
				$('#'+$.adGallery.layoutIds.holder).fadeIn($.adGallery.fadeSpeed,function(){ $.adGallery.resizeBox(); });
			break;
		}
	}

	$.adGallery.loadContent = function() {
		var clickedItem = $.adGallery.items[$.adGallery.current];
		var src = $(clickedItem).attr('href');
		var image = new Image();
		
		$.adGallery.updateCount();
		
		$(image)
			.load(function(){
				var img = $(this);
				img.hide();
				$('#'+$.adGallery.layoutIds.caption).hide();

				$('#'+$.adGallery.layoutIds.media).empty().append(img);
				$.adGallery.width = this.width;
				$.adGallery.height = this.height;
				
				$.adGallery.resizeContent(img);
				
				img.width($.adGallery.width).height($.adGallery.height);
				
				var caption = $(clickedItem).next('.caption');
				if(caption.length) {
					captionText = caption.html();
				} else {
					captionText = '';
				}
				$('#'+$.adGallery.layoutIds.title).empty().append(captionText);
								
				($('#'+$.adGallery.layoutIds.fader).css('display') == 'none') ?	$.adGallery.fade('load') : $.adGallery.resizeBox();
								
			})
			.error(function () {
				alert('error loading image');
				$.adGallery.fade('down');
			})
			.attr('src',src)
		;
	}
	
	$.adGallery.updateCount = function() {
		$('#'+$.adGallery.layoutIds.count).empty().append(($.adGallery.current+1)+'/'+($.adGallery.total+1));	
	}
	
	$.adGallery.resizeBox = function() {
		$.adGallery.offsetX = Math.floor($.adGallery.width / 2);
		$.adGallery.offsetY = Math.floor($.adGallery.height / 2);
		$('#'+$.adGallery.layoutIds.holder).clearQueue().animate({
			width 			: $.adGallery.width,
			marginLeft 	: -$.adGallery.offsetX 	
		},$.adGallery.defaults.animation,function(){
			$(this).animate({
				height		: $.adGallery.height,
				marginTop : -$.adGallery.offsetY	
			}, $.adGallery.defaults.animate,function(){ $.adGallery.showContent(); });	
		});
	}
	
	$.adGallery.showContent = function() {
		$('#'+$.adGallery.layoutIds.media).width($.adGallery.width).height($.adGallery.height);
		$('#'+$.adGallery.layoutIds.media+' img').fadeIn($.adGallery.defaults.animation, function(){
			($.adGallery.current == 0) ? $('#'+$.adGallery.layoutIds.btnBack).hide() : $('#'+$.adGallery.layoutIds.btnBack).show();
			($.adGallery.current == $.adGallery.total) ? $('#'+$.adGallery.layoutIds.btnNext).hide() : $('#'+$.adGallery.layoutIds.btnNext).show();

			if($.adGallery.defaults.caption) {
				$('#'+$.adGallery.layoutIds.caption).fadeIn($.adGallery.animationQuick, function(){
					$('#'+$.adGallery.layoutIds.btnClose).clearQueue().fadeIn($.adGallery.animationQuick);
				});
			} else {
				$('#'+$.adGallery.layoutIds.btnClose).clearQueue().fadeIn($.adGallery.animationQuick);
			}
		});	
	}
	
	$.adGallery.change = function(dir) {
		$.adGallery.debug($.adGallery.defaults);
		switch(dir) {
			case 'next':
			case 'back':
				var direction = dir;
			break;	
			default:
				var direction = false;
			break;
		}	
		if(direction) {
			$('#'+$.adGallery.layoutIds.btnNext).clearQueue().fadeOut($.adGallery.animationQuick);
			$('#'+$.adGallery.layoutIds.btnBack).clearQueue().fadeOut($.adGallery.animationQuick,function(){
				$('#'+$.adGallery.layoutIds.btnClose).clearQueue().fadeOut($.adGallery.animationQuick,function(){
					if($.adGallery.defaults.caption) {
						$('#'+$.adGallery.layoutIds.caption).fadeOut($.adGallery.animationQuick, function(){
							$.adGallery.goDirection(dir);
						})	
					} else {
						$.adGallery.goDirection(dir);
					}
				});
			});
		}
	}
	
	$.adGallery.goDirection = function(dir) {
		switch(dir) {
			case 'next':
			case 'back':
				$('#'+$.adGallery.layoutIds.media+' img').fadeOut($.adGallery.defaults.animation,function(){
					(dir == 'next') ? $.adGallery.current++ : $.adGallery.current--;
					$.adGallery.loadContent();
				});
			break;	
		}	
	}
	
	$.adGallery.unloadContent = function() {
		$('#'+$.adGallery.layoutIds.count+', #'+$.adGallery.layoutIds.title+', #'+$.adGallery.layoutIds.media).empty();
	}
	
	$.adGallery.resizeContent = function(content) {
		var w = content[0].width;
		var h = content[0].height;
		
		if (w > $.adGallery.constraintWidth) {
			h = h * ($.adGallery.constraintWidth / w); 
			w = $.adGallery.constraintWidth; 
			if (h > $.adGallery.constraintHeight)	{ 
				w = w * ($.adGallery.constraintHeight / h); 
				h = $.adGallery.constraintHeight; 
			}
		} else if (h > $.adGallery.constraintHeight) { 
			w = w * ($.adGallery.constraintHeight / h); 
			h = $.adGallery.constraintHeight; 
			if (w > $.adGallery.constraintWidth){ 
				h = h * ($.adGallery.constraintWidth / w); 
				w = $.adGallery.constraintWidth;
			}
		}
		
		w = Math.floor(w);
		h = Math.floor(h);
		
		w < 250 ? $.adGallery.width = 250 : $.adGallery.width = w;
		h < 250 ? $.adGallery.height = 250 : $.adGallery.height = h;
	}
		
	$.adGallery.debug = function(data){
		if(window.console) {
			 console.log(data);
		} else {
			 alert(data);
		}	
	}
	
	/* end control functions */
	
	
})(jQuery);
