// JavaScript Document

//Replace any images missed by the CMS with generic image
//This may cause performance issues on older browsers
$(document).ready(function(){
$('img').each(function(index){
	if($(this).attr('src')=='') {
		$(this).attr('src','http://dealerassets.nmsdigital.co.uk/no-image-found.png')
	}
});
});

$(document).ready(function(){	 
	 var filter = new Filter();
	 filter.init(typeof qMinPrice != 'undefined' ? qMinPrice : "", typeof qMaxPrice != 'undefined' ? qMaxPrice : "", typeof qMinMileage != 'undefined' ? qMinMileage : "", typeof qMaxMileage != 'undefined' ? qMaxMileage : ""); 
	 
	 setupNav();
	 
     // swap background
	 var docClass = $('body').attr("class");	
	 document.getElementsByTagName('html')[0].className = docClass; 
	 
		 
	
	/* accordion behaviour */	
	$('.accordion .head').click(function() {								 
			if ($(this).next().is(':visible')){
				$(this).next().fadeOut(500);
			} else {
				$(this).toggleClass('active');
				$(this).next().fadeIn(1000).show();	
			}
											 
			return false;
	}).next().hide();
	
	
	
	// this has been added - triggers accordion click behaviour on following parent hidden div
	
	
	$('.tcTog').click(function(){		
			$(this).parent().parent().next().find('.head').trigger('click');		
			return false;
	});
	
	
	
	if( $('#mod1').length > 0 ){	
		$('#mod1').cycle({ 
			fx: 'scrollDown' ,
			next:   '#next1', 
			prev:   '#prev1',
			timeout: 0			
		});
		
		$('#mod2').cycle({ 
			fx: 'scrollDown' ,
			next:   '#next2', 
			prev:   '#prev2',
			timeout: 0			
		});
	
		$('#mod3').cycle({ 
			fx: 'scrollDown' ,
			next:   '#next3', 
			prev:   '#prev3',
			timeout: 0			
		});
		$('.module .arrows A').click(function(){	
			return false;
		});
			
	}



	/* Append Params  From generic.js*/
    appendToTestDrive();


/*  Begin: Shawn's Testing Section */

// leaf_enquiry validator
if ($('#leaf_enquiry').length > 0) {
	
    var container = $('div#errorCont');
    // Override default error message
    jQuery.validator.messages.required = "";
    container.hide();
    $(".excMark").hide();
    var validator = $("#leaf_enquiry").validate({

        errorContainer: container,
        errorLabelContainer: $("ol", container),
        wrapper: 'li',
        rules: {
            "first_name": {
                required: true                    
            },
            "last_name": {
                required: true
            },
            "email": {
                required: true,
				email:true
            },
            "telephone": {
                required: true

            },
            "email_subject": {
                required: true
            },
	        "email_message": {
	            required: true
	        }
	
        },
        meta: "validate",
        messages: {
            "first_name": "Please enter first your name",
			"last_name": "Please enter last your name",
			"email": "Please enter a valid email address",
            "telephone": "Please enter your phone number",
            "email_subject": "Please enter the email subject",
            "email_message": "Please enter your email message"
        },
        errorPlacement: function (error, element) {
            errorDiv = $('div#errorCont');
            error.appendTo(errorDiv);
        },
        debug: false,
        unhighlight: function (element, errorClass) {
            $(element).next('.excMark').hide();
            if (validator.numberOfInvalids() == 0) {
                $("#errorCont").hide();
            }
        },
        highlight: function (element, errorClass) {
            $(element).next('.excMark').show();
            $("#errorCont").show();
        }
    });
}


/*  End:  Shawn's Testing Section */


	/* form validation */
	
	if ( $('.formValidate').length > 0 ) {		
		$('.formValidate').validate();		
	}
	
			
	var toolTip = new Tooltip();	
	toolTip.init();	
	
		
	/* side links hover */	

   if ( $('a.btn-over').length > 0 ) {
	$('a.btn-over').hover(function(e){								 
								  
								  $(this).parent().stop().animate({backgroundPosition:"(0 -45px)"},{duration:500});								
								  },  function(){									  
								 $(this).parent().stop().animate({backgroundPosition:"(0 0)"},{duration:500});	  
	 });
	
   }


    //open external links in new window
	$('.footerLogo a[href*=http]').click(function() {
        
		window.open(this.href);
		
		return false;		
		
    });

	//SM - align the height of the modules on vehicle information pages
	if ( $('.gradeMod P').length > 0 ) {
		var maxHeight = 0;
		$('.gradeMod P:not(.hilite2)').each(function(){
			if (maxHeight < $(this).height()) {maxHeight = $(this).height()}
		});
		$('.gradeMod P:not(.hilite2)').each(function(){
			$(this).height(maxHeight);
		});
	}





	

});

			
			
function accordion(event) {
	$(this) = event.target.id;	
	/*
		if ($(this).next().is(':visible')){
			$(this).next().fadeOut(500);
			$(this).find('.imagetoggle').attr("src","http://dealerassets.nmsdigital.co.uk/images/modules/title-more-details.png");	
		} else {
    		$(this).toggleClass('active');
			$(this).next().fadeIn(1000).show();			
			$(this).find('.imagetoggle').attr("src","http://dealerassets.nmsdigital.co.uk/images/modules/title-less-details.png");			
		}										 
		return false;
}).next().hide();
*/

};

  
  
  
// sort out submit button - this is specifically for IE6 - which cannot show PNG transparency. Creates a surrounding div, and attaches the PNG fix to that.



					
function SetDisplayButton(flag){				
							
					if(!flag){
						//$("#btnSearch").css("display", "none");
					   $(".ie6bckg").css("display", "none");
						
					} else {
						//$("#btnSearch").css("display", "block");
					$(".ie6bckg").css("display", "block");
					
					}
}


function toggleBkgd(flag){
	
		var submitButtonImg = $('input.submitSearch').attr("src");
		$('input.submitSearch').css({
							background:'none',
							position:'absolute',
							width:'140px',
							height:'18px',
							margin:'0'
						});
	
		$('input.submitSearch').attr({
							src:'http://dealerassets.nmsdigital.co.uk/images/graphics-common/spacer.gif'					
						});
		
		$('<img class="pgfx swapOut"  src="' + submitButtonImg + '" style="position:absolute; width:140px; height:18px;" />').insertBefore('input.submitSearch');
		$('.swapOut').wrapAll(' <div class="ie6bckg"  style="float:right; display:inline; position:relative; width:140px; height:18px; margin-top:20px" > ');
	
}	

  
  

















  
  
  


