$(document).ready(function(){
	$('#info_form input').change(function() {
		if($(this).val()!= '') $(this).removeClass('redInput');
	})
	$('#info_form').submit(function() {
		$('#info_form input').each(function() {
			if($(this).val()=='' && !$(this).hasClass('button') && !$(this).hasClass('noConfirm'))
			{
				$(this).addClass('redInput');
				return false;
			}
		});
		if(!$('#termsCheck').is(':checked')){
			$('.checkbox.terms').animate({
				color: "#a00225",
				paddingLeft: "5px"
			}, 300,function(){
				$('.checkbox.terms').animate({
				color: "#a00225",
				paddingLeft: "-5px",
				}, 300)
			});
			return false;
		}
		dataToSend = $('#info_form').serialize();
		$.post(baseUrl+"/index/submit-request", 
				dataToSend, 
				function(json){
					if(json.error == 1)
					{
						alert('An error has occured!');
					}
					
					else
					{
						$('.darkenBackground').fadeOut(1000);
						$('.request_info_form').html('<h2>Your request has been recieved successfully!</h2>').fadeOut(1000);
					};
				
		},'json');

		return false;
	});
	$('.prev_button').click(function() {
		kids = $('.gallery_thumbs li a > img').size();
		thisNum = $('.gallery_thumbs a.active').attr('id').replace('image-','');
		if(thisNum == 1)
		{
			activateGalleryImage('#image-'+kids);
		}
		else
		{
			activateGalleryImage('#image-'+(parseFloat(thisNum)-1));
		}
	});
	$('.next_button').click(function() {
		kids = $('.gallery_thumbs li a > img').size();
		thisNum = $('.gallery_thumbs a.active').attr('id').replace('image-','');
		if(thisNum == kids)
		{
			activateGalleryImage('#image-1');
		}
		else
		{
			activateGalleryImage('#image-'+(parseFloat(thisNum)+1));
		}
	});
	$('.gallery_thumbs li a').click(function(){
		activateGalleryImage('#'+$(this).attr('id'));
	});
	function activateGalleryImage(thisid)
	{
		$('.gallery_thumbs li a.active').removeClass('active');
		$(thisid).addClass('active');
		$('#large_image').attr('src',$(thisid+' img').attr('src'));
		$('#large_image_description').html($(thisid+' img').attr('title'));
	}
	$('.request.openForm').click(function(){
		$('#pop_up_box_gallery').fadeOut('slow');
		$('#pop_up_box, .darkenBackground').fadeIn('slow')
	});
	$('.request_info_form .close_button,close_request').click(function(){
		$('#pop_up_box, .darkenBackground').fadeOut('slow');
	});
	$('.photo_img_box, .open_slideshow').click(function(){
		$('#pop_up_box').fadeOut('slow');
		$('#pop_up_box_gallery, .darkenBackground').fadeIn('slow')
	});
	$('.gallery_box .close_button').click(function(){
		$('#pop_up_box_gallery, .darkenBackground').fadeOut('slow');
	});
	var countdown = {
        init: function() {
            countdown.remaining = countdown.max - $(countdown.obj).val().length;
            if (countdown.remaining > countdown.max) {
                $(countdown.obj).val($(countdown.obj).val().substring(0,countdown.max));
            }
            $(".remaining").html(countdown.remaining + " characters remaining.");
        },
        max: null,
        remaining: null,
        obj: null
    };
    $("#request_comment").maxLength(250);  
    $(".countdown").each(function() {
        $(this).focus(function() {
            var c = $(this).attr("class");
            countdown.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
            countdown.obj = this;
            iCount = setInterval(countdown.init,500);
        }).blur(function() {
            countdown.init();
            clearInterval(iCount);
        });
    });
});

