jQuery.fn.gallery = function(options){
	var This = $(this);
	var sliderC = options.sliderC;
	var imageW = options.imageW;
	var thumbsO = $(options.thumbsO);
	var thumbs = $(options.thumbs);
	var outer = $(options.actives);
	var refCont = $(options.ref);
	var arrowT = $(options.arrowTop);
	var arrowB = $(options.arrowBottom);
	var arrowL = $(options.arrorLeft);
	var arrowR = $(options.arrorRight);	
	var contentO = $(options.contentO);	
	var contentC = options.contentC;
	var totalProj = This.find(sliderC).size();
	var opacity = options.opacity;
	var currPos = 0;
	var currSPos = 0;
	var currProj = 0;
	thumbs.append('<img class="thumbs-hover" src="/images/thumbs-hover.png" alt="" />').css('position', 'relative');
	var $hover = $('.thumbs-hover');
	$hover.hide();
	
	//Set thumbs position
	var thumbSize = thumbs.find('a').size();
	var thumbsW = thumbs.outerWidth(true);
	var thumbsH = thumbs.outerHeight();
	var thumbW = thumbs.find('a').outerWidth(true);
	var thumbH = thumbs.find('a').outerHeight(true);
	var refOff = refCont.offset();
	var refL = refOff.left;
	thumbsO.css({'left': refL+'px', 'top': 0});
	
	//Set slider
	var size = new Array();
	var ThisiniL = refL+thumbsW;
	This.find(sliderC).each(function(){
		var thisS = $(this).find('img').size();
		size[size.length] = thisS;
		$(this).css({'width': (thisS*imageW)+'px', 'margin-left': ThisiniL+'px'});		
	});
	
	thumbs.css({'height': thumbSize*thumbH});
	
	//Set initial project
	load_project(0, 0);
	set_thumbs(0);
	currProj = 0;
	
	//Define thumbs action
	thumbs.find('img:not(.thumbs-hover)').click(function(){
		var obj = $(this);
		var ind = thumbs.find('img').index(obj);
		load_project(ind, 0);
	});
	
	//Define thumbs arrows action
	arrowT.click(function(){
		if (currPos > 0){
			currPos--;
			thumbs_slider(currPos);
		}
		return false;
	});
	
	arrowB.click(function(){
		if (currPos < (thumbSize-4)){
			currPos++;
			thumbs_slider(currPos);
		}
		return false;
	});
	
	//Define slider left and right arrows action
	arrowL.click(function(){
		if (currSPos > 0){
			currSPos--;
			main_slider(currSPos);
		}else{
			if (currProj > 0){
				currProj--;
				var slides = This.find(sliderC+':eq('+currProj+') img').size();
				var next_link = thumbs.find('a:eq('+currProj+')').attr('href');
				window.location.href = next_link;
				load_project(currProj, (slides-1));
				set_thumbs(currProj);
			}
		}
		return false;
	});
	
	arrowR.click(function(){
		if (currSPos < (size[currProj]-1)){
			currSPos++;
			main_slider(currSPos);
		}else{
			if (currProj < (totalProj-1)){
				currProj++;
				var next_link = thumbs.find('a:eq('+currProj+')').attr('href');
				window.location.href = next_link;
				load_project(currProj, 0);
				set_thumbs(currProj);
			}
		}
		return false;
	});
	
	//Click on slider action
	This.find(sliderC).each(function(){
		var obj = $(this);
		obj.find('img').each(function(){
			$(this).css('cursor', 'pointer').click(function(){
				var ind = obj.find('img').index($(this));
				main_slider(ind);
			});
		})
	});
	
	//Set deeplinks
	var url = window.location.href;
	var urlA = url.split('#');
	if (urlA.length > 1){
		var proj = '#'+urlA.pop();
		var currPj = thumbs.find('a[href='+proj+']');
		if (currPj.length > 0){
			var obj = currPj;
			var ind = thumbs.find('a').index(obj);
			load_project(ind, 0);
			set_thumbs(ind, true);
		}
	}
	
	//set window resize
	/*$(window).resize(function(){
		var refOff = refCont.offset();
		var refL = refOff.left;
		thumbsO.css({'left': refL+'px', 'top': 0});
		ThisiniL = refL+thumbsW;
		var mL = imageW*currPos;
		This.find(sliderC+':eq('+currProj+')').css({'margin-left': (ThisiniL-mL)});
	});*/
	
	//load project
	function load_project(ind, pos){
		currProj = ind;	
		This.find(sliderC).hide();
		This.find(sliderC+':eq('+ind+')')
		.css({'margin-left': ThisiniL+'px', 'top': 0})
		.fadeIn();
		contentO.find(contentC).hide();
		contentO.find(contentC+':eq('+ind+')').fadeIn();
		currSPos = pos;
		This.find(sliderC+':eq('+currProj+')').find('img').animate({'opacity': opacity}, {queue:false});
		This.find(sliderC+':eq('+currProj+')').find('img:eq('+currSPos+')').animate({'opacity': 1}, {queue:false});
		
		if (currSPos > 0)
			main_slider(currSPos);
			
			
		//Add hover
		var $obj = thumbs.find('img:eq('+ind+')');
		var currOff = $obj.position();
		var currL = currOff.left;
		var currT = currOff.top;
		$hover.css({position: 'absolute', 'z-index': 10000}).fadeIn();
		$hover.animate({top: currT, left: currL, 'z-index': 10000});
	}
	
	//slider
	function main_slider(ind){
		var mL = imageW*ind;
		This.find(sliderC+':eq('+currProj+')').animate({'margin-left': (ThisiniL-mL)}, {queue:false});
		This.find(sliderC+':eq('+currProj+')').find('img').animate({'opacity': opacity}, {queue:false});
		This.find(sliderC+':eq('+currProj+')').find('img:eq('+ind+')').animate({'opacity': 1}, {queue:false});
	}
	
	//Thumbs slider
	function thumbs_slider(ind){
		var mT = thumbH*ind;
		thumbs.animate({'margin-top': -mT}, {queue:false});
	}
	
	function set_thumbs(ind, loaded){
		if (!loaded){
			if (ind <= (thumbSize-4)){
				var nind = ind;
				currPos = nind;
				thumbs_slider(nind);
			}
		}else{
			//if (ind <= (thumbSize-4)){
				if (nind > 3){
					var nind = ind-3;
				}else{
					var nind = ind;
				}
				currPos = nind;
				thumbs_slider(nind);
			//}
		}
	}
	
}
