// JavaScript Document

$(function() {
	
	//If Javascript enabled, add menu function to Sale & Results nav item
	// Replace HREF with a #
	$("a#nav-sales-and-results").attr("href", "#");
	
	//If nav item has active state close, otherwise open it
	$('#nav-sales-and-results').click(function(){
		if($(this).hasClass('menu-open')) {
			$('.sales-menu').slideUp();
			$('#nav-sales-and-results').removeClass('menu-open');	
		} else {
			$('.sales-menu').slideDown("slow");
			$(this).addClass('menu-open');
		}		
	});
	
	//If Javascript enabled, show more options link/hide more options fields in advanced search
	$('.more-options-link').css({"display" : "inline-block"});
	$('.more-option-fields').hide();
	
	$('.more-options-link').toggle(function(){
		$('.more-option-fields').show("fast");
		$(this).html("Less detail");
		$(this).addClass('open');
		}, function () {
		$('.more-option-fields').hide("fast");
		$(this).removeClass('open');
		$(this).html("More detail");
	});
	
	// Create fancybox view when zoom class clicked
	$("a.zoom").fancybox();
		
	// Close link for Sales Menu
	$('.close-menu-link').click(function(){
		$('.sales-menu').slideUp();
		$('#nav-sales-and-results').removeClass('menu-open');
	});
	
	// Show search suggest menu on focus (just indicative of behaviour)
	$('#search-field').focus(function () {
		$('.search ul').css({"display" : "block"});
	});
	
	$('.article-content').click(function () {
		if($('.search ul').css({"display" : "block"})) {
			$('.search ul').css({"display" : "none"});
		}
	});
	
	// Add class to sales list items on hover
	$('.all-sales-list').mouseover(function(){
		$(this).addClass('over');
	});
	
	$('.all-sales-list').mouseout(function(){
		$(this).removeClass('over');
	});
	
	//Hide the FAQ answers to start with		
	$(".answer").hide();
	
	// Toggle FAQ items and change the respective icon
	$(".question a").toggle(
	  function () {
		$(this).parent().next().show();
		$(this).addClass("open");
	  },
	  function () {
		$(this).parent().next().hide();
		$(this).removeClass("open");
	  }
	);
	
	//Tell-a-friend Colorbox popup window
		$(".friend-trigger").colorbox({width:"380px", inline:true, href:"#friend-form-inner"});
	
	// Fix for IE6 & 7 to maintain input focus styling
	if (jQuery.browser.msie === true) {
        jQuery('input')
            .bind('focus', function() {
                  $(this).addClass('ieFocus');
            }).bind('blur', function() {
                  $(this).removeClass('ieFocus');
            });
    }

	// Send newsletter to a friend ajax form submission
	if($('#tell-a-friend-form').length > 0){
		$('#tell-a-friend-form').submit(function(){ // on submit
			if($('#friend_name').val() == ''){
				$('#error_friend_name').slideDown();
				return false;
			} else {
				$('#error_friend_name').slideUp();
			}
			if($('#friend_email').val() == ''){
				$('#error_friend_email').slideDown();
				return false;
			} else {
				$('#error_friend_email').slideUp();
			}
			$.post('/friendform.php', { name: $('#friend_name').val(), email: $('#friend_email').val(), news: "http://tattersalls.fantasmagorical.co.uk/news_article.php?id="+$('#news_id').val(), rand: Math.random() }, function(data){
				$('#tell-a-friend ul').hide();
				$('#friend_success').show();
			});
			return false;
		});
		$('.friend-trigger').click(function(){
			$('#friend_name').val('')
			$('#friend_email').val('')
			$('#tell-a-friend ul').show();
			$('#error_friend_email').hide();
			$('#error_friend_name').hide();
			$('#friend_success').hide();
		})
	}
	
	if($('#nominationsForm').length > 0){
		$('#nominationsForm').submit(function(){
			if($('#contact_name').val() == ''){
				alert('Please enter your name');
				$('#contact_name').focus();
				return false;
			}
			if($('#contact_email').val() == ''){
				alert('Please enter a valid email address');
				$('#contact_email').focus();
				return false;
			}
			if($('#contact_address').val() == ''){
				alert('Please enter your contact address');
				$('#contact_address').focus();
				return false;
			}
			if($('#validate').val() != '7'){
				alert('Validation code is incorrect, please try again');
				$('#validate').focus();
				return false;
			}
		});
	}
});



	




