	$(function(){

		// Accordion
		$("#accordion").accordion({ header: "h3" });

		// Tabs
		$('#tabs').tabs();

		var $tabs = $('#tabs').tabs(); // first tab selected

		// Tabs
		$('#tabs2').tabs();

		var $tabs = $('#tabs2').tabs(); // first tab selected

		// Tabs
		$('#tabs3').tabs();

		var $tabs = $('#tabs3').tabs(); // first tab selected

										
		// Dialog			
		$('#dialog').dialog({
			autoOpen: false,
			width: 600,
			buttons: {
				"Ok": function() { 
					$(this).dialog("close"); 
				}, 
				"Cancel": function() { 
					$(this).dialog("close"); 
				} 
			}
		});
		
		// Dialog Link
		$('#dialog_link').click(function(){
			$('#dialog').dialog('open');
			return false;
		});

		// Datepicker
		$(function() {
			$("#datepicker").datepicker();
			$('#datepicker').datepicker('option', {dateFormat: 'DD, MM d, yy'});
		});
		// Slider
		$('#slider').slider({
			range: true,
			values: [17, 67]
		});
		
		// Progressbar
		$("#progressbar").progressbar({
			value: 20 
		});
		
		//hover states on the static widgets
		$('#dialog_link, ul#icons li').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);
		
	});

	$(function () {
		var $alert = $('#alert');		

		$('#ActionPlanButton').click(function(){
		
			var xmlHttp;
			try
			  {
				  // Firefox, Opera 8.0 , Safari
				  xmlHttp=new XMLHttpRequest();
			  }
			catch (e)
			{
				// Internet Explorer
				try
				{
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e)
				{
					try
					{
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e)
					{
						alert("Your browser does not support AJAX!");
						return false;
					}
				}
			}
			xmlHttp.onreadystatechange=function()
			{
				if(xmlHttp.readyState==4)
				{
					document.getElementById('alert').innerHTML = xmlHttp.responseText;
					if(xmlHttp.responseText == 'Great News <span style="color:#1cb368;">We have received your action plan request.</span>')
					{
						document.getElementById('ActionPlanButton').innerHTML = 'SENT';
						document.getElementById('nameC').value = '';
						document.getElementById('emailaddressC').value = '';
						document.getElementById('phoneC').value = '';
						document.getElementById('commentsC').value = '';
					}
					else
						document.getElementById('ActionPlanButton').innerHTML = 'CHECK FORM';							
					var alerttimer = window.setTimeout(function () {
						$alert.trigger('click');						
						document.getElementById('ActionPlanButton').innerHTML = 'ENQUIRE';
					}, 10000);
					$alert.animate({height: $alert.css('line-height') || '250px'}, 150)
					.click(function () {
						window.clearTimeout(alerttimer);
						$alert.animate({height: '0'}, 150);
					});	
				
					//if successful then clear the form
				}
				else
				{
					document.getElementById('ActionPlanButton').innerHTML = 'SENDING';
				}
			}
			xmlHttp.open("GET","../SendActionPlanEnquiry.php?Name="+escape(document.getElementById('nameC').value) 
			+"&EmailAddress="+escape(document.getElementById('emailaddressC').value) 
			+"&Phone="+escape(document.getElementById('phoneC').value) 
			+"&Stock="+escape(document.getElementById('stockC').value) 
			+"&Vehicle="+escape(document.getElementById('vehicleC').value) 
			+"&Comment="+escape(document.getElementById('commentsC').value),true);
			xmlHttp.send(null);
		});	
	});

			
	$(function () {
		var $alert = $('#alert');		

		$('#ContactButton').click(function(){
		
			var xmlHttp;
			try
			  {
				  // Firefox, Opera 8.0 , Safari
				  xmlHttp=new XMLHttpRequest();
			  }
			catch (e)
			{
				// Internet Explorer
				try
				{
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e)
				{
					try
					{
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e)
					{
						alert("Your browser does not support AJAX!");
						return false;
					}
				}
			}
			xmlHttp.onreadystatechange=function()
			{
				if(xmlHttp.readyState==4)
				{
					document.getElementById('alert').innerHTML = xmlHttp.responseText;
					if(xmlHttp.responseText == 'Great News <span style="color:#1cb368;">We have received your contact enquiry.</span>')
					{
						document.getElementById('ContactButton').innerHTML = 'SENT';
						document.getElementById('nameC').value = '';
						document.getElementById('emailaddressC').value = '';
						document.getElementById('phoneC').value = '';
						document.getElementById('commentsC').value = '';
					}
					else
						document.getElementById('ContactButton').innerHTML = 'CHECK FORM';							
					var alerttimer = window.setTimeout(function () {
						$alert.trigger('click');						
						document.getElementById('ContactButton').innerHTML = 'ENQUIRE';
					}, 10000);
					$alert.animate({height: $alert.css('line-height') || '250px'}, 150)
					.click(function () {
						window.clearTimeout(alerttimer);
						$alert.animate({height: '0'}, 150);
					});	
				
					//if successful then clear the form
				}
				else
				{
					document.getElementById('ContactButton').innerHTML = 'SENDING';
				}
			}
			xmlHttp.open("GET","../SendContactEnquiry.php?Name="+escape(document.getElementById('nameC').value) 
			+"&EmailAddress="+escape(document.getElementById('emailaddressC').value) 
			+"&Phone="+escape(document.getElementById('phoneC').value) 
			+"&Stock="+escape(document.getElementById('stockC').value) 
			+"&Vehicle="+escape(document.getElementById('vehicleC').value) 
			+"&Comment="+escape(document.getElementById('commentsC').value),true);
			xmlHttp.send(null);
		});	
	});

	$(function () {
		var $alert = $('#alert');		

		$('#NewsletterButton').click(function(){
		
			var xmlHttp;
			try
			  {
				  // Firefox, Opera 8.0 , Safari
				  xmlHttp=new XMLHttpRequest();
			  }
			catch (e)
			{
				// Internet Explorer
				try
				{
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e)
				{
					try
					{
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e)
					{
						alert("Your browser does not support AJAX!");
						return false;
					}
				}
			}
			xmlHttp.onreadystatechange=function()
			{
				if(xmlHttp.readyState==4)
				{
					document.getElementById('alert').innerHTML = xmlHttp.responseText;
					if(xmlHttp.responseText == 'Great News <span style="color:#1cb368;">You have been added to our newsletter database.</span>')
					{
						document.getElementById('NewsletterButton').innerHTML = 'ADDED';
						document.getElementById('nameN').value = '';
						document.getElementById('emailN').value = '';

					}
					else
						document.getElementById('NewsletterButton').innerHTML = 'CHECK FORM';							
					var alerttimer = window.setTimeout(function () {
						$alert.trigger('click');						
						document.getElementById('NewsletterButton').innerHTML = 'SIGN UP';
					}, 7000);
					$alert.animate({height: $alert.css('line-height') || '50px'}, 200)
					.click(function () {
						window.clearTimeout(alerttimer);
						$alert.animate({height: '0'}, 200);
					});	
				
					//if successful then clear the form
				}
				else
				{
					document.getElementById('NewsletterButton').innerHTML = 'ADDING';
				}
			}
			xmlHttp.open("GET","../SendNewsletter.php?Name="+escape(document.getElementById('nameN').value) 
			+"&EmailAddress="+escape(document.getElementById('emailN').value),true);
			xmlHttp.send(null);
		});	
	});

	$(function () {
		var $alert = $('#alert');		

		$('#BrochureButton').click(function(){
		
			var xmlHttp;
			try
			  {
				  // Firefox, Opera 8.0 , Safari
				  xmlHttp=new XMLHttpRequest();
			  }
			catch (e)
			{
				// Internet Explorer
				try
				{
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e)
				{
					try
					{
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e)
					{
						alert("Your browser does not support AJAX!");
						return false;
					}
				}
			}
			xmlHttp.onreadystatechange=function()
			{
				if(xmlHttp.readyState==4)
				{
					document.getElementById('alert').innerHTML = xmlHttp.responseText;
					if(xmlHttp.responseText == 'Great News <span style="color:#1cb368;">We have received your request, and we will send you a brochure.</span>')
					{
						document.getElementById('BrochureButton').innerHTML = 'ADDED';
						document.getElementById('nameN').value = '';
						document.getElementById('emailN').value = '';

					}
					else
						document.getElementById('BrochureButton').innerHTML = 'CHECK FORM';							
					var alerttimer = window.setTimeout(function () {
						$alert.trigger('click');						
						document.getElementById('BrochureButton').innerHTML = 'SIGN UP';
					}, 7000);
					$alert.animate({height: $alert.css('line-height') || '50px'}, 200)
					.click(function () {
						window.clearTimeout(alerttimer);
						$alert.animate({height: '0'}, 200);
					});	
				
					//if successful then clear the form
				}
				else
				{
					document.getElementById('BrochureButton').innerHTML = 'ADDING';
				}
			}
			xmlHttp.open("GET","../SendBrochure.php?Name="+escape(document.getElementById('nameB').value) 
			+"&EmailAddress="+escape(document.getElementById('emailaddressB').value),true);
			xmlHttp.send(null);
		});	
	});
	
	$(function() {
		var $alert = $('#alert');
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$("#dialog").dialog("destroy");
		
		var name = $("#name"),
			email = $("#email"),
			fname = $("#fname"),
			femail = $("#femail"),
			allFields = $([]).add(name).add(email).add(femail),
			tips = $(".validateTips");

		function updateTips(t) {
			tips
				.text(t)
				.addClass('ui-state-highlight');
			setTimeout(function() {
				tips.removeClass('ui-state-highlight', 1500);
			}, 500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Length of "+n+" must be between "+min+" and "+max+".");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		$("#dialog-form").dialog({
			autoOpen: false,
			height: 420,
			width: 350,
			modal: true,
			buttons: {
				'Tell A Friend': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(name,"username",3,80);
					bValid = bValid && checkLength(email,"email",6,80);
					


					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp(email,/^((([a-z]|d|[!#$%&'* -/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]) (.([a-z]|d|[!#$%&'* -/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]) )*)|((x22)((((x20|x09)*(x0dx0a))?(x20|x09) )?(([x01-x08x0bx0cx0e-x1fx7f]|x21|[x23-x5b]|[x5d-x7e]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([x01-x09x0bx0cx0d-x7f]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))))*(((x20|x09)*(x0dx0a))?(x20|x09) )?(x22)))@((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).) (([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?$/i,"eg. ui@jquery.com");
					
					if (bValid) {
						var xmlHttp;
						try
						  {
							  // Firefox, Opera 8.0 , Safari
							  xmlHttp=new XMLHttpRequest();
						  }
						catch (e)
						{
							// Internet Explorer
							try
							{
								xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
							}
							catch (e)
							{
								try
								{
									xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
								}
								catch (e)
								{
									alert("Your browser does not support AJAX!");
									return false;
								}
							}
						}
						xmlHttp.onreadystatechange=function()
						{
							if(xmlHttp.readyState==4)
							{
						
								//if successful then clear the form
							}
						}
						xmlHttp.open("GET","SendCarToFriend.php?Name="+escape(document.sending.name.value) 
						+"&EmailAddress="+escape(document.sending.email.value) 
						+"&FName="+escape(document.sending.fname.value) 
						+"&FEmailAddress="+escape(document.sending.femail.value),true);
						xmlHttp.send(null);
						$(this).dialog('close');
					}
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		$('#TellAFriend').click(function() {
			$('#dialog-form').dialog('open');
				return false;
			});

	});

	$(function () {
		var $alert = $('#alert');
		if(document.getElementById('alert').innerHTML != '')
		{
			var alerttimer = window.setTimeout(function () {
				$alert.trigger('click');
			}, 7000);
			$alert.animate({height: $alert.css('line-height') || '50px'}, 200)
			.click(function () {
				window.clearTimeout(alerttimer);
				$alert.animate({height: '0'}, 200);
			});
		}
	});
      var headline_count;
      var headline_interval;
      var old_headline = 0;
      var current_headline = 0;
	  
      $(document).ready(function(){
        headline_count = $("div.headline").size();

        $("div.headline:eq("+current_headline+")").css('top', '5px');
       
        headline_interval = setInterval(headline_rotate,10000);
        $('#scrollup').hover(function() {
          clearInterval(headline_interval);
        }, function() {
          headline_interval = setInterval(headline_rotate,10000);
          headline_rotate();
        });
      });

      function headline_rotate() {
        current_headline = (old_headline + 1) % headline_count;
        $("div.headline:eq(" + old_headline + ")")
          .animate({top: -32},"slow", function() {
            $(this).css('top', '210px');
          });
        $("div.headline:eq(" + current_headline + ")")
          .animate({top: 5},"slow");  
        old_headline = current_headline;
      }	
