$(document).ready(function(){

	var current_project = null;

	random_background();

	if($('#login-form').length){
		$('input[name=username]').focus(function(){
			$('input[name=username]').val('');
		})
		$('input[name=password]').focus(function(){
			$('input[name=password]').val('');
		})
		$('#login-form a').click(function(e){
			e.preventDefault();
			$('#login-form').submit();
		})
	}


	if($('#nav').length){
		var subnav = ('li.media ul');
		$('li.media').mouseenter(function(){
			$(subnav).slideDown('fast');
		})
		$('li.media').mouseleave(function(){
			$(subnav).slideUp('fast');
		})
	}

	if($('#locations').length){
		$('#locations a.area').click(function(e){
			e.preventDefault();

			if(e.target != current_project){
				$(e.target).parent().addClass('open');
				$(e.target).next().slideDown();
			}

			if(current_project){
				$(current_project).parent().removeClass('open');
				$(current_project).next().slideUp();
			}
			current_project = e.target;
		})
	}

	$("#InquiryAddForm").bind('submit', function(e){e.preventDefault()});

	if($('#inquiries').length){

		var validator = $("#InquiryAddForm").validate({
			rules: {
				'data[Inquiry][first_name]': "required",
				'data[Inquiry][last_name]': "required",
				'data[Inquiry][state]': "required",
				'data[Inquiry][zip]': "required",
				'data[Inquiry][email]': {
					required: true,
					email: true
				},
				'data[Inquiry][project_name]': "required",
				'data[Inquiry][phone]': "required"
			},submitHandler: function(form) {
				form.submit();
			}
		});
	}


})

function startSlideshow(){
	var thumbs, pos, width;

	thumbs 	= $('#thumbnails img');
	bigs	= $('#gallery img');
	pos 	= 0;
	width 	= thumbs.length * 50;

	if(width <= 450)
		$('#righty').hide();


	$('#gallery').cycle({
		timeout: 0,
		speed:   300,
		startingSlide: 0
	});


	function toggleSlideArrows(pos){
		if(pos == 0){
			$('#lefty').hide();
		}else{
			$('#lefty').show();
		}
		if(pos == (thumbs.length * -50)  + 450){
			$('#righty').hide();
		}else{
			$('#righty').show();
		}
	}

	$('#thumb_slider').css({'width' : width});

	$.each(thumbs, function(idx, el){
		$(el).bind('mouseenter', {index:idx}, function(e){
			$('#gallery').cycle(e.data.index);
		})
	});

	$('#righty').click(function(e){
		e.preventDefault();
		pos = pos - 50;
		toggleSlideArrows(pos);
		$('#thumb_slider').animate({
			left: pos
		}, 200)
	})

	$('#lefty').click(function(e){
		e.preventDefault();
		pos = pos + 50;
		toggleSlideArrows(pos);
		$('#thumb_slider').animate({
			left: pos
		}, 200)
	})
}

function preloadImages(){
	for(var x=1; x<9; x++){
		$('#preload').attr('src', ('/img/backgrounds/back' + x + '.jpg'));
	}
}

function random_background(){
	var bg_string = '#000 url(/img/backgrounds/back' + Math.floor(Math.random()*8+1) + '.jpg) no-repeat top center';
	$('body').css({'background':bg_string});
}
