$(document).ready(function(){
	if($('#image-list').length){
		var ig = $('#image-list').gallery({
			prevBtn:$('#prev-image'),
			nextBtn:$('#next-image'),
			caller:$('#image-list')
		});
	}

	$("#quick_contact_form").validate({
			debug:false,
			rules: {
		     	"email_name": "required",
		     	"email_email": {
		       		required: true,
		       		email: true
		     	},
				"email_message": {
					required:true,
					minlength:10
				}
		   	},
			messages:{
				"email_name":"Please provide a name!",
				"email_email":"Need an email to reply to.",
				"email_message":"You'll probably want to send a message."
			},
		
			submitHandler: function(form) {
				form.submit();
			}
		});	
	
});

$.fn.gallery = function(opts){
	var caller = this;
	var opts = opts;
	var g = new Gallery(caller, opts);
	return g;
};

var Gallery = function(caller, opts){
	var gal = this;
	var caller = opts.caller;
	var nextBtn = opts.nextBtn;
	var prevBtn = opts.prevBtn;
	this.imageList = new Array();
	this.opts = opts;
	this.current = 0;
	this.max = 0;

	
	if(prevBtn){
		$(prevBtn).click(function(el){
			el.preventDefault();
			if(!$(this).hasClass('disabled')){
				gal.shift(gal.current - 1);
			}
		}).addClass('disabled');
	}
	if(nextBtn){
		$(nextBtn).click(function(el){
			el.preventDefault();
			if(!$(this).hasClass('disabled')){
				gal.shift(gal.current + 1);
			}
		}).addClass('disabled');
	}

	this.displayImgNum = function(){
		$('#img-num').text((gal.current+1) + '/' + (gal.max+1));
	}
	this.displayImgCap = function(){
		$('#img-title').text($(gal.imageList[gal.current].contain).attr('title'));
	}
	
	this.preload = function(){
		var img;
		var imgLoaded = function(which){
			gal.imageList[which].img = gal.imageList[which].imgLoader;
			$(gal.imageList[which].img).attr('alt',
				$(gal.imageList[which].contain).html(gal.imageList[which].img).addClass('has-img').attr('title')
			);
			var newwidth = $(caller).width() + $(gal.imageList[which].img).width();
			$(caller).css({width:newwidth});
			gal.max++;
			gal.displayImgNum();
			if(gal.current < gal.max){
				$(nextBtn).removeClass('disabled');
			}
			gal.preload();
		}

		for(img in gal.imageList){
			if(!gal.imageList[img].loaded){
				gal.imageList[img].loaded = true;
				gal.imageList[img].imgLoader = new Image();
				$(gal.imageList[img].imgLoader).load(function(){
					imgLoaded(img);
				});
				$(gal.imageList[img].imgLoader).attr('src',gal.imageList[img].url);
				break;
			}
		}
	}

	this.shift = function(pos){
		pos = Math.max((pos%(gal.max+1)),0);
		gal.current = pos;
		if(pos==0){
			$(prevBtn).addClass('disabled');
		} else {
			$(prevBtn).removeClass('disabled');
		}
		if(pos==gal.max){
			$(nextBtn).addClass('disabled');
		} else {
			$(nextBtn).removeClass('disabled');
		}
		gal.displayImgNum();
		gal.displayImgCap();
		var newleft = $(gal.imageList[pos].img).position().left;

		$(caller).stop().animate({
			left:-1*newleft
		});
	}
	
	$('li', caller).each(function(i){
		var url;
		var caption;
		var loaded;
		var img;
		if($(this).hasClass('has-img')){
			url = $('img', this).attr('href');
			caption = $('img', this).attr('title');
			loaded = true;
			img = $('img', this);
		} else {
			url = $(this).text();
			caption = $(this).attr('title');
			loaded = false;
			img = false;
		}
		gal.imageList[i] = {
			loaded:loaded,
			url:url,
			contain:this,
			img:img
		} 	
	});
	gal.preload();
	gal.displayImgNum();
	return gal;
}