function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver > 6.0 ) 
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = 1;
  }
  return msg;
}

function bgRotator() {

	jQuery('#background img').css({display:'none'});
	jQuery('#background img.first').css({display:'block'});
	jQuery('.bg-texts img.first').css({display: 'block'});
	setInterval('rotateBg()',8000);
	var ie = checkVersion();
	if(ie != 1) setInterval('rotateBgText()',8000);

}

function rotateBg() {
	
	var current = (jQuery('#background img').hasClass('show') ? jQuery('#background img.show') : jQuery('#background img.first'));
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? jQuery('#background img.first') : current.next()) : jQuery('#background img.first'));	
	
	next.css({display: 'none'})
	.addClass('show')
	.fadeIn('slow');

	current.fadeOut('slow')
	.removeClass('show');
		
};

function rotateBgText() {
	
	var current = (jQuery('.bg-texts img').hasClass('show') ? jQuery('.bg-texts img.show') : jQuery('.bg-texts img.first'));
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? jQuery('.bg-texts img.first') : current.next()) : jQuery('.bg-texts img.first'));	
	
	next.css({display: 'none'})
	.addClass('show').fadeIn();

	current.fadeOut('slow')
	.removeClass('show');
		
};

function newsRotator() {

	jQuery('.news-carousel > .slide:first').css({top: '0'});
	setInterval('rotateNews()',5000);
	
}

function rotateNews() {	

	var current = (jQuery('.news-carousel > div').hasClass('show') ? jQuery('.news-carousel div.show') : jQuery('.news-carousel > .slide:first'));

	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? jQuery('.news-carousel > .slide:first') :current.next()) : jQuery('.news-carousel > .slide:first'));	
	
	next.css({top:'34px'})
	.addClass('show')
	.animate({top:'0'}, 1500);

	current.animate({top:'-34px'}, 1500, function() { jQuery(this).css({top:'34px'}); })
	.removeClass('show');
		
};

function picBox() {
	
	jQuery('.column-right .first').css({display:'block'});
	
}

function emailValidator(emailAddress) {
	
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
	
}

jQuery(document).ready(function() {		
	
	picBox();

	var k = 1;
	
	jQuery('.article').each(function() {
		jQuery(this).attr('aID',k);
		k++;
	});
    
    jQuery('.article h2').click(function(){
		var i = jQuery(this).parent('.article').attr('aID');
		if(jQuery(this).hasClass("open")) {
			jQuery(this).removeClass("open");
			jQuery('.article div').slideUp();
			jQuery('.image-'+i).fadeOut('slow').removeClass('first');
		}
		else {
			jQuery('.article h2').removeClass("open");
			jQuery(this).addClass("open");
			jQuery('.article div').slideUp();
			jQuery('.column-right > div').each(function() { jQuery(this).fadeOut('slow').removeClass('first'); });
			jQuery('.image-'+i).fadeIn('slow').addClass('first');
			jQuery(this).siblings("div").slideDown();
		}
	});
	
	jQuery('.main-navigation > li.hasSub').hover(function() {
		jQuery(this).addClass('current');
		jQuery(this).children('ul').fadeIn();	
	}, function() {
		jQuery(this).addClass('current');
		jQuery(this).children('ul').fadeOut();	
	});
	
	jQuery('#test-drive-form').submit(function (){
		
		var valid = 1;
			
		if(jQuery('select').val() == '' || jQuery('input#email').val() == '' || emailValidator(jQuery('input#email').val()) == false) { jQuery('#test-drive-content').addClass('error').html(jQuery('.error-input').html()); valid = 0;	}
			
		if(valid == 1) { 
			jQuery('#test-drive-content').hide();
			jQuery(this).hide();
			jQuery('.thankyou').show();
		}
		
		return false;
		
	});
	
    if(jQuery('#background > img').length > 1) {
		newsRotator();
		bgRotator();
		var bodyHeight = jQuery('body').height();
		if(bodyHeight <= 768) {
			jQuery('#footer').css({bottom:'-250px'});	
		}
	}
	else {
		var bodyHeight = jQuery('#wrapper').height();
		bodyHeight = (bodyHeight-172) + 'px';
		jQuery('#wrapper').css({ height: bodyHeight });
	}
			
});
