(function($) {

	$.fn.imgFit = function(){
		var obj = $(this);
		var sliderw = 0;
		if($(obj).hasClass('album')){
			var windowh = $(window).height();
			$('li',obj).each(function(){
				$(this).height(windowh);
				var ratio = $(this).attr('data-height')/$(this).attr('data-width');
				var slidew = $('.border img',this).height()/ratio + 20 + 40;
				sliderw += slidew;
				$(this).width(slidew);
			});
		}else{
			var windowh = $(window).height();
			var windoww = $(window).width();
			windowwh = windoww/windowh;
			$('li',obj).height(windowh);
			$('li',obj).width(windoww);	
			sliderw = windoww*$('li',obj).length;
			$('li',obj).each(function(){
				var ratio = $(this).attr('data-width')/$(this).attr('data-height');
				if(ratio < windowwh){
					var newheight = windoww/ratio;
					$('img',this).width(windoww);
					$('img',this).height(newheight);
					$('img',this).css({'margin':'0'});
					$('img',this).css({'margin-top':'-'+(newheight-windowh)/2+'px'});
				}else{
					var newwidth = windowh*ratio;
					$('img',this).width(newwidth);
					$('img',this).height(windowh);
					$('img',this).css({'margin':'0'});
					$('img',this).css({'margin-right':'-'+(newwidth-windoww)/2+'px'});
				}
			});
		}
		$('ul',this).width(sliderw+'px');
	};

	$.fn.imgLoader = function(options){

		// default configuration properties
		var defaults = {
			speed:	'250',
			delay:	'250'
		}; 

		var options = $.extend(defaults, options);

		var imgcount = 0;

		$('li img',this).each(function(){
			$(this).attr('data',$(this).attr('src'));
			$(this).attr('src','');
		});

		$(this).each(function(){
			var obj = $(this);

			$("li img",obj).css({ opacity: 0 });

			function loadimg(){
				var oimg = $("li img:eq("+imgcount+")",obj);
				if($(oimg).length > 0){
					$(oimg).css({ opacity: 0 });
					$(oimg).attr('src',$(oimg).attr('data'));
					$(oimg).load(function(){ $(oimg).delay(options.delay).animate({opacity: 1}, options.speed,function(){ imgcount++; loadimg(); }); });
				}
			};

			loadimg();
			loadimg();

		});
	};


	$.fn.imgSlider = function(){

		var obj = $(this);
		var current = -1;
		var total = $('li',this).length;
		var action = false;
		var lastloaded = false;
		var timeout;
		if(!$(obj).hasClass('album')) var autoplay = true;
		else var autoplay = false;

		if($('li',obj).length > 1){
			$('body').append('<div id="slidernext"></div><div id="sliderprev"></div>');
		}

		$(".border",obj).css({opacity:0});

		function transition(dir,id){

			if(dir != 'center'){
				action = true;	
				if(id){
					current = id-1;
				}else{
					if(dir == 'prev'){ current--; if(current < 0){ current++; if(lastloaded == false){ action = false; return false; } current = total-1; }
					}else{ current++; if(current == total){ current--; current = 0; } }
				}
				if(dir || id){ clearTimeout(timeout); autoplay = false; }else if(!$(obj).hasClass('album')){ timeout=setTimeout(function(){ if(autoplay) transition() },5000); }
			}
			
			$('li:eq('+(total-1)+') img',obj).load(function(){ lastloaded = true; });
			
			if(current == 0 && lastloaded == false){
				$('#sliderprev').animate({opacity: 0},300);
			}else{
				$('#sliderprev').animate({opacity: 1},300);
			}

			var slider = $('ul',obj);
			var slide = $("li:eq("+current+")",obj);
			var offset = 0;
			var windoww = $(window).width();

			$("li:lt("+current+")",obj).each(function(){ offset += $(this).width(); });
			offset = -1*(offset) + (windoww-$(slide).width())/2;

			if(dir != 'center'){
				$("li:not(':eq("+current+")') .border",obj).animate({opacity:0.25},300,"linear");
				$(".border",slide).animate({opacity:1},300,"linear");
				$(slider).animate({'margin-right':offset+'px'},500,"linear",function(){ action = false; });
			}else{
				$(slider).css({'margin-right':offset+'px'});
			}

		}

		transition();
		
		if($('li',obj).length > 1){
			$(document).keydown(function(e){ if (e.keyCode == 37){ if(!action){ transition("next"); } return false; } });
			$(document).keydown(function(e){ if (e.keyCode == 39){ if(!action){ transition("prev"); } return false; } });

			$('#slidernext').click(function(){ if(!action){ transition("next"); } });
			$('#sliderprev').click(function(){ if(!action){ transition("prev"); } });

			if($(obj).hasClass('album')){
				$('li',obj).click(function(){ if(!action){ transition('',Number($('li',obj).index($(this))+1)); } });
			}
		}

		$(window).resize(function(){ transition('center') });

	};

	})(jQuery);

	$(document).ready(function(){
		// $(".idusSlider").imgLoader();
		$(".idusSlider").css({opacity:0});
		$(".idusSlider").imgFit();
		$(".idusSlider").animate({opacity:1},500);
		$(".idusSlider").imgSlider();
		$(window).resize(function(){ $(".idusSlider").imgFit(); });
	});
