$(document).ready(function() {
	
	// Delay function
	jQuery.fn.delay = function(time,func){
		this.each(function(){
			setTimeout(func,time);
		});

		return this;
	};
	
// Navigation ------------------------------------------------------------------------------------------
	
	// Kills the navigation JS if in IE6
	var thisbrowser = jQuery.browser;
	var thisbrowser_ver = jQuery.browser.version;
	if(thisbrowser != 'msie' && thisbrowser_ver != '6.0') {
	
// Home ------------------------------------------------------------------------------------------------
		$('.nav1 a').hover(function() {
			if ( $(this).parent().hasClass("selected") ) { } else { //if not selected
				$(this).css({'background-position' : '0 -87px'}); //add the new width and the negitive margin
			}
		},function() {
			if ( $(this).parent().hasClass("selected") ) { } else { // if not selected
				if ($(".nav2").hasClass("selected") ) { 
					$(this).css({'background-position' : '0 -268px'});
				} else {
					$(this).css({'background-position' : '0 0'}); //add the new width and the negitive margin
				}
			}
		});
	 
// Our Wine --------------------------------------------------------------------------------------------
		$('.nav2 a').hover(function() {
			if ( $(this).parent().hasClass("selected")) { } else {
				if ($(".nav1").hasClass("selected") ) { 
					$(this).css({'background-position' : '-146px -87px'});
				} else { //if not selected
					var width = $(this).width(); //get the current width
					var newwidth = width+3 + "px"; //increase the width value
					$(this).css({'margin-left' : '-3px', 'width' : newwidth, 'background-position' : '-143px -87px'}); //add the new width and the negitive margin
					$('.nav1 a').css({'background-position' : '0 -268px'});
				}
			}
		},function() {
			if ( $(this).parent().hasClass("selected")) {} else {
				if($(".nav1").hasClass("selected") ) {
					$(this).css({'background-position' : '-146px 0'});
				} else { // if not selected
					var width = $(this).width();
					var oldwidth = width-3 + "px";
					$(this).css({'margin' : '0px', 'width' : oldwidth, 'background-position' : '-146px 0'}); //add the original width and the zero margin
					if ($(".nav3").hasClass("selected") ) { 
						$(this).css({'background-position' : '-146px -268px'});
					}
					if ($(".nav1").hasClass("selected") ) { } else {
						$('.nav1 a').css({'background-position' : '0 0'});
					}
				}
			}
		});
	
// Stockist --------------------------------------------------------------------------------------------
		$('.nav3 a').hover(function() {
			if ( $(this).parent().hasClass("selected")) { } else {
				if($(".nav2").hasClass("selected") ) {
					$(this).css({'background-position' : '-289px -87px'});
				} else { //if not selected
					var width = $(this).width(); //get the current width
					var newwidth = width+3 + "px"; //increase the width value
					$(this).css({'margin-left' : '-3px', 'width' : newwidth, 'background-position' : '-286px -87px'}); //add the new width and the negitive margin
					$('.nav2 a').css({'background-position' : '-146px -268px'});
				}
			}
		},function() {
			if ( $(this).parent().hasClass("selected")) {} else {
				if($(".nav2").hasClass("selected") ) {
					$(this).css({'background-position' : '-289px 0'});
				} else { // if not selected
					var width = $(this).width();
					var oldwidth = width-3 + "px";
					$(this).css({'margin' : '0px', 'width' : oldwidth, 'background-position' : '-289px 0'}); //add the original width and the zero margin
					if ($(".nav4").hasClass("selected") ) { 
						$(this).css({'background-position' : '-289px -268px'});
					}
					if ($(".nav2").hasClass("selected") ) { } else {
						$('.nav2 a').css({'background-position' : '-146px 0'});
					}
				}
			}
		});
	
// About Us --------------------------------------------------------------------------------------------
		$('.nav4 a').hover(function() {
			if ( $(this).parent().hasClass("selected")) { } else {
				if($(".nav3").hasClass("selected") ) { 
					$(this).css({'background-position' : '-432px -87px'});
				} else { //if not selected
					var width = $(this).width(); //get the current width
					var newwidth = width+3 + "px"; //increase the width value
					$(this).css({'margin-left' : '-3px', 'width' : newwidth, 'background-position' : '-429px -87px'}); //add the new width and the negitive margin
					$('.nav3 a').css({'background-position' : '-289px -268px'});
				}
			}
		},function() {
			if ( $(this).parent().hasClass("selected")) {} else {
				if($(".nav3").hasClass("selected") ) {
					$(this).css({'background-position' : '-432px 0'});
				} else { // if not selected
					var width = $(this).width();
					var oldwidth = width-3 + "px";
					$(this).css({'margin' : '0px', 'width' : oldwidth, 'background-position' : '-432px 0'}); //add the original width and the zero margin
					if ($(".nav5").hasClass("selected") ) { 
						$(this).css({'background-position' : '-432px -268px'});
					}
					if ($(".nav3").hasClass("selected") ) { } else {
						$('.nav3 a').css({'background-position' : '-289px 0'});
					}
				}
			}
		});
	
// Contact Us ------------------------------------------------------------------------------------------
		$('.nav5 a').hover(function() {
			if ( $(this).parent().hasClass("selected")) {} else {
				if($(".nav4").hasClass("selected") ) { } else { //if not selected
					var width = $(this).width(); //get the current width
					var newwidth = width+3 + "px"; //increase the width value
					$(this).css({'margin-left' : '-3px', 'width' : newwidth}); //add the new width and the negitive margin
					$('.nav4 a').css({'background-position' : '-432px -268px'});
				}
			}
		},function() {
			if ( $(this).parent().hasClass("selected")) {} else {
				if($(".nav4").hasClass("selected") ) { } else { // if not selected
					var width = $(this).width();
					var oldwidth = width-3 + "px";
					$(this).css({'margin' : '0px', 'width' : oldwidth}); //add the original width and the zero margin
					if ($(".nav4").hasClass("selected") ) { } else {
						$('.nav4 a').css({'background-position' : '-432px 0'});
					}
				}
			}
		});
	
	}
	
	
// Contact Page Validation -----------------------------------------------------------------------------
	
	//setting variables
	var message_shown = false;
	var message_shown2 = false;
	var message_shown3 = false;
	
	// colours for the javascript to use
	var error_border = "#7d282d";
	var error_background = "#ffffff";
	
	var default_border = "#c8c0b3";
	var default_background = "#ffffff";
	
	$('#submit-enquiry').click(function(event) {
		//event.preventDefault();
		var to_fullname = $('#contact-name').val();
		var to_email = $('#contact-email').val();
		var to_message = $('#contact-message').val();
		
		//checks the value of the required fields
		
		if(to_fullname) { //checks full name
			$('#contact-name').css({'background-color' : default_background, 'border-color' : default_border});
			var name_present = true;
		} else {
			$('#contact-name').css({'background-color' : error_background, 'border-color' : error_border});
			var name_present = false;
		}
		
		if(to_email) { //checks email
			if(to_email != 0) {
				if(isValidEmailAddress(to_email)) { //validates email format
					$('#contact-email').css({'background-color' : default_background, 'border-color' : default_border});
					var email_present = true;
				} else {
					$('#contact-email').css({'background-color' : error_background, 'border-color' : error_border});
					var email_present = false;
				}
			}
		} else {
			$('#contact-email').css({'background-color' : error_background, 'border-color' : error_border});
		}

		if(to_message) { //checks the message
			$('#contact-message').css({'background-color' : default_background, 'border-color' : default_border});
			var message_present = true;
		} else {
			$('#contact-message').css({'background-color' : error_background, 'border-color' : error_border});
			var message_present = false;
		}
		
		if(name_present && email_present && message_present) {
			//send email
			$("#errormessage p").fadeOut('slow');
			$(this).delay(400,function(){
				$("#errormessage").slideUp('slow');
			});
			return this;
		} else {
			//return an error message
			if(message_shown==false) {
				message_shown = true;
				$("#contact-form").prepend("<div id='errormessage'><p style='display:none;'>Please fill in all required fields marked with an asterix (*), and make sure your email address is valid.</p></div>");
				$("#errormessage").slideDown('slow');
				$(this).delay(400,function(){
					$("#errormessage p").fadeIn('slow');
				});
			}
			return false;
		}
	});
	
	//validates email address
	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
		return pattern.test(emailAddress);
	}
	
	
// Read More -------------------------------------------------------------------------------------------

	// this is overly complex because it is displaying inline
	$('.read-more').click(function(event) {
		// As changing the class isn't always registering as a new 
		if($(this).hasClass("read-more")) {
			event.preventDefault();
			// adds the parent class, just as a tempory selector
			$(this).parent().parent().addClass("parent");
			// clears the height
			$('.parent p').css({'height' : ''});
			// gets the original height of the paragraph
			var hiddenheight = $('.parent p').height();
			// gets the height of the read more paragraph when closed
			var firstheight1 = $('.parent').height();
			// adds 'px' to the height value
			var firstheight = firstheight1 + 'px';
			// hides the parent, while allowing for the full height to be measured
			$('.parent').css({'height' : firstheight, 'overflow' : 'hidden', 'opacity' : '0.01'});
			// shows the expanded text
			$('.parent span').show();
			// measures the full height of expanded content
			var shownheight = $('.parent p').height();
			// hides the expanded text
			$('.parent span').hide();
			// kills the styles that allowed for the height to be measured
			$('.parent').css({'height' : '', 'overflow' : '', 'opacity' : ''});
			// toggle the class
			$(this).toggleClass("read-more");
			$(this).toggleClass("read-less");
			// change the button text
			$(this).text('read less');
			// adds the original height value as a class
			if($('.parent p').hasClass("" + hiddenheight)) {} else {
				$('.parent p').addClass("" + hiddenheight);
			}
			// expands the container
			$('.parent p').animate({ 
		        height: shownheight
		      }, 400 );
			// fades in the extra text
			$('.parent span').fadeIn();
			// removes the selector from the container
			$('.parent').removeClass('parent');
		} else {
			event.preventDefault();
			// toggle the class
			$(this).toggleClass("read-less");
			$(this).toggleClass("read-more");
			// change the button text
			$(this).text('read more');
			// gets the original height from the container's class
			var oldheight = $(this).parent().attr('class');
			// fades out the expanded text
			$(this).siblings().fadeOut();
			// contracts the container
			$(this).parent().animate({ 
		        height: oldheight
		      }, 400 );
		}
	});
	
	$('.read-less').click(function(event) {
		event.preventDefault();
		// toggle the class
		$(this).toggleClass("read-less");
		$(this).toggleClass("read-more");
		// change the button text
		$(this).text('read more');
		// gets the original height from the container's class
		var oldheight = $(this).parent().attr('class');
		// fades out the expanded text
		$(this).siblings().fadeOut();
		// contracts the container
		$(this).parent().animate({ 
	        height: oldheight
	      }, 400 );
	});

// Back to Top -----------------------------------------------------------------------------------------
	$('#back-to-top').click(function(event) {
		event.preventDefault();
        $('html,body').animate({scrollTop: 0}, 800);
	});
	
	$("a.fancy").fancybox();

});


