$(document).ready(function(){

	$('#slides').cycle({  
		fx: 'scrollHorz',
		speed: 1500,
		timeout: 6800,
		prev: '#slideprev',
		next: '#slidenext'
  	});
  	
  	if ($.browser.msie) {
		$('ul#nav li').hover(function() {
			$(this).addClass('hover')
		}, function() {
			$(this).removeClass('hover');
		});
		$(':nth-child-3n').addClass('nth-child-three');
	}

	//Image Thumb Slider
	
	//start on the first lot of visibile images (image 1-6, screen 1)
	var output = 1;
	
	//total number of images 
	var totalItems = $('#gallerythumbs li').size();
	
	//upper limit is total number of images / 6 (visible) always rounded up 
	var upperLimit = Math.ceil(totalItems/6);
	
	//so we know what's going on:
	//$('p.output').text(output + ' of ' + upperLimit + ' ('+totalItems+')');
	
	//when the previous button is clicked
	$('#thumbprev').click(function() {
		//if currently animating, ignore it
		if ($(':animated').length) {
			return false
		} else {
			//if we're on the first screen
			if (output === 1) {
				//do nothing
				//$('p.output').text('nothing before '+ output + ' of ' + upperLimit + ' ('+totalItems+')');
				return false;
			} else {
				//otherwise slide the thumbs left to show the previous 6 images
				$('#gallerythumbs').animate({
				    left: '+=940'
				  }, 1200, function() {
				  	//update the screen number (minus 1)
				    output = output-1;
					//$('p.output').text(output + ' of ' + upperLimit + ' ('+totalItems+')');
					return false;
				  });
			}
		}
	});
	
	//when the next button is clicked
	$('#thumbnext').click(function() {
		//if currently animating, ignore it
		if ($(':animated').length) {
			return false
		} else {
			//if we're on the last screen
			if (output === upperLimit) {
				//do nothing
				//$('p.output').text('nothing after '+ output + ' of ' + upperLimit + ' ('+totalItems+')');
				return false;
			} else {
				//otherwise slide the thumbs right to show the next 6 images
				$('#gallerythumbs').animate({
				    left: '-=940'
				  }, 1200, function() {
				  	//and update the screen number (plus 1)
				    output = output+1;
					//$('p.output').text(output + ' of ' + upperLimit + ' ('+totalItems+')');
					return false;
				  });
			}
		}
	});
	
	$('#gallerythumbs li a').click(function() {
		$('#gallerythumbs li a.current').removeClass('current');
		$('#gallerymaincaption').html($(this).children('span').html());
		$('#gallerymainimage').children('img').attr('src',$(this).children('img').attr('src').replace('image-gallery-thumb','image-gallery'));
		$(this).addClass('current');
		return false;
	});
	
	
	//current page highlighter in nav and topnav 
	thisURL = window.location.pathname.substring(window.location.pathname.lastIndexOf("/")+1);
	$('#nav li a[href="'+thisURL+'"], #topnav li a[href="'+thisURL+'"], #footer ul li a[href="'+thisURL+'"]').addClass('current');
    $('#nav li a[href="'+thisURL+'"]').each(function() {
		$(this).parent().parent().parent().children('a').addClass('current');
                
	});
        	//Form Validation!
	$('#registersubmit').click(function() {


		//required fields checkermajigger

		//reset the error count
		errorcount = 0;
		$('p.error').remove();
		$('.error').removeClass('error');


		//for textboxes, textareas and selects
		$('label:contains("*")').each(function() {
			//if a label has a * in it, and the next form element is empty
			if ($(this).next('input, select, textarea').val() == "" || $(this).next('fieldset').children('input, select, textarea').val() == "") {
				//add .error to the label
				$(this).addClass('error');

				//OFFSET (IF IT'S REQUIRED)
				//get the label's position (relative to the top)
				//var offset = $(this).position();
				//var offset_correction = -80;
				//var offset_correction = $('#offset_correction').html();
				//$(this).text(offset.top);

				//add .error to the field as well
				$(this).next('input, select, textarea').addClass('error');
				$(this).next('fieldset').children('input, select, textarea').addClass('error');

				//WITH OFFSET show an error message with <label> + "is required" (hidden by default)
				//$(this).next('input, select, textarea').after('<p id="'+$(this).attr('for')+'_error" class="error" style="top:'+(offset.top-offset_correction)+'px;">'+$(this).text().replace('*','')+'  is required</p>');
				//$(this).next('fieldset').children('input, select, textarea').after('<p class="error" style="top:'+(offset.top-offset_correction)+'px;">'+$(this).text().replace('*','')+'  is required</p>');

				//WITHOUT OFFSET
				$(this).next('input, select, textarea').after('<p id="'+$(this).attr('for')+'_error" class="error">'+$(this).text().replace('*','').replace(':','')+'  is required</p>');

				//add one to the error count

				errorcount = errorcount + 1;

				//show all the error messages
				$('p.error').slideDown('slow');
			}
		});
                
		//check the error count
		if (errorcount === 0) {
			//alert(errorcount);
			//all good - submit the form and carry on
			//$(this).text('huzzah!');
			//var form = $(this).parents('form:first');
			//var form=$('.reviewform');
			//alert(form.attr('name'));
			//form.submit();
            $.ajax({
				url: "mailto_db.php",
				type: "POST",
				data: $('#contact_form').serialize()                      
	        });
			$('#contentright p').text('Your message has been sent to Mark McEwan at Label Concepts. Cheers!');
			$('#contact_form').hide();
		} else if (errorcount === 1) {
			//something's wrong.
			$('body').animate({scrollTop:0}, 'slow');
			$('p.intro').after('<p class="intro error">There is an error with the form. Please check that all the fields have been entered correctly.</p>');
		}	else if (errorcount > 1) {
			$('body').animate({scrollTop:0}, 'slow');
			$('p.intro').after('<p class="intro error">There are '+errorcount+' errors with the form. Please check that all the fields have been entered correctly.</p>');
		}
		return false;
	});

	//clear errors if the form field is corrected
	$('input.error, select.error, textarea.error').live('blur', function() {
	//$('input, select, textarea').blur(function(){
		//if ($(this).hasClass('error') == false) {
		//	return;
		//} else {
			if (!$(this).val()) {
				return;
			} else {
				this_id = $(this).attr('id');
				$('label[for="'+this_id+'"]').removeClass('error');
				$(this).removeClass('error');
				$('p#'+this_id+'_error.error').fadeOut(300);
		//	}
		}
	});
});


