jQuery.fn.gallery = function( options ){
	var This = $(this);
	var strip = $(options.strip);
	var speed = options.speed;
	var mainImg = $(options.mainImg);
	var internal = options.internal;
	var slider = $(options.slider);
	var content = $(options.content);
	var closer = $(options.closer);
	var contSlide = content.parent();
	
	var currMX = 0;
	var iniSize = strip.find('a').size();
	var eachW = strip.find('a').outerWidth(true);
	var eachH = strip.find('a').outerHeight();
	var winW = $(window).width();
	var iniThumbs = Math.floor(winW/eachW);
	var currML = 0;
	var iniLeft = iniSize*eachW;
	
	if (iniSize > iniThumbs){
		strip.append(strip.html());
		strip.append(strip.html());
		var totalW = eachW*(iniSize*3);
	}else{
		var totalW = eachW*iniSize;
	}
	
	if (internal){
		setTimeout(function(){
			var contH = content.outerHeight(true);
			contSlide.parent().height(contH);
			$('body').data('state', 0);
		}, 500);
	}
	
	strip.width(totalW).mousemove(function(e){
		var mX = e.pageX;
		if (currMX == 0){
			currMX = mX;
		}
		if (mX > currMX){
			currML-=speed;
		}else{
			currML+=speed;
		}
		
		//Move strip
		strip.css({marginLeft: currML+'px'});
		
		//Make strip continues
		if (iniSize > iniThumbs){			
			if ((currML*-1) < ((iniLeft*2)+speed) && (currML*-1) > ((iniLeft*2)-speed)){
				strip.css({marginLeft: -(iniLeft)});
				currML = -iniLeft;
			}
			
			if ((currML+speed) >= 0 && (currML-speed) <= 0){
				strip.css({marginLeft: -(eachW*iniSize)});
				currML = -(eachW*iniSize);
			}
		}
		
		currMX = mX;
	});
		
	//Make click funtion
	strip.find('a').click(function(){
		var This = $(this);
		if (internal){
			var contH = content.outerHeight(true);
			switch ($('body').data('state')){
				case 0:
					contSlide.animate({marginTop: -contH}, {queue:false, duration: 'slow', complete: function(){
						var sliderH = slider.outerHeight(true);
						contSlide.parent().animate({'height': sliderH}, {queue:false, complete: function(){
							change_img( This );
							center_align();
						}});
					}});
					$('body').data('state', 1);
				break;
				case 1:
					change_img( This );
				break;
			}
		}else{
			change_img( This );
		}
		return false;
	});	
		
	closer.click(function(){
		if ($('body').data('state') == 1){
			var contH = content.outerHeight(true);
			contSlide.parent().animate({'height': contH}, {queue:false, complete: function(){
				contSlide.animate({marginTop: 0}, {queue:false, duration: 'slow'});
			}});
			$('body').data('state', 0);
		}
	});
	
	$(window).resize(function(){
		if ($('body').data('state') == 1){
			center_align();
		}
	});
		
	function change_img(src){
		var rel = src.attr('href');
		mainImg.animate({'opacity':0}, {queue:false, complete:function(){
			mainImg.attr('src', rel).load(function(){
				mainImg.animate({'opacity':1}, {queue:false});					
			});				
		}});
	}
	
	function center_align(){
		var winW = $(window).width();
		var elemW = $('.internalContentCentre1').outerWidth();
		if (elemW > winW){
			var mL = (winW - elemW)/2;
			$('.internalContentCentre1').css('margin-left', mL);
		}else{
			$('.internalContentCentre1').css('margin', '0 auto');
		}
	}
	
	
}
