jQuery.ajaxSetup ({  
	cache: false,
	error:function(xhr,err){
		alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		alert("responseText: "+xhr.responseText);
	}
});
var ajax_load = '<img src="/images/loading.gif" alt="Loading..." />';  
var loadUrl = '/ajax.asp';  

jQuery(document).ready(function() {
// HOVER FOR LOGO
	jQuery('#buttonHome').hover(function() {
		jQuery(this).css({ opacity:1});
	}, function() {
		jQuery(this).css({ opacity:0.7});
	});
// ROLLOVERS WITH PRELOADING
    jQuery('IMG[src*=_off]').each(function(){
		jQuery(this).addClass('rollover');
        jQuery('<IMG/>').appendTo('BODY')
            .css({ display: "none" })
            .attr('src',$(this).attr('src').replace('_off', '_on'));
    });
    jQuery('.rollover').hover(function(){
            t = $(this);
            t.attr('src',t.attr('src').replace('_off', '_on'));
        },function(){ 
            t = $(this);
            t.attr('src',t.attr('src').replace('_on', '_off'));
        }
     );
	 
// SETUP PHONES SLIDER	 
	// SETUP KEYBOARD SHORTCUTS
	jQuery('html').live('keydown', function (e) {
	   if (e.keyCode == 37){ // LEFT
		   jQuery('#carouselButtonLeft').click();
		}else if(e.keyCode == 39){ // RIGHT
		   jQuery('#carouselButtonRight').click();
		}else if(e.keyCode == 27) { // ESC
			jQuery('#carouselThumbnails').stop().animate({left:0},{queue:false,duration:300});
			carouselCurrentIndex = 0;
		}
	});
// SETUP COLORBOX FOR CAROUSEL
	jQuery("a[rel='carouselPhoto']").colorbox({transition:"none"});
//	jQuery("a[rel='carouselPhoto'] IMG").hover(function() {
//		jQuery(this).addClass('hover');
//	}, function() {
//		jQuery(this).removeClass('hover');
//	});
// SETUP CAROUSEL
	var carouselCurrentIndex = 0;
	var carouselViewportWidth = jQuery('#carouselViewport').width();
	var carouselItemPadding = 0;
	var carouselItemWidth = jQuery('#carouselThumbnails IMG').width();
	carouselItemWidth = carouselItemWidth + carouselItemPadding;
	var carouselVisibleCount = Math.floor(parseInt(carouselViewportWidth) / carouselItemWidth);
	var carouselItemCount = jQuery('#carouselThumbnails > LI').length;
	jQuery('#carouselThumbnails > LI').css('width', '200px');
	jQuery('#carouselButtonLeft').hover(
		function() {
			jQuery(this).addClass('hover');
		},
		function() {
			jQuery(this).removeClass('hover');
		}
	).click(
		function() {
		if(carouselCurrentIndex > 0) {
			carouselCurrentIndex--;
			if (jQuery('#carouselThumbnails').position().left < carouselItemWidth){
				jQuery('#carouselThumbnails').stop().animate({left:'+='+carouselItemWidth},{queue:false,duration:300});
			}
		}
	});
	jQuery('#carouselButtonRight').hover(
		function() {
			jQuery(this).addClass('hover');
		},
		function() {
			jQuery(this).removeClass('hover');
		}
	).click(function() {
		if(Math.abs(carouselCurrentIndex) < (carouselItemCount - carouselVisibleCount)) {
			carouselCurrentIndex++;
			jQuery('#carouselThumbnails').stop().animate({left:'-='+carouselItemWidth},{queue:false,duration:300});
		}
	});
	var whenToHideCarouselControls = 4;
	if (jQuery('#carouselThumbnails > LI').size() <= whenToHideCarouselControls) {
		jQuery('#carouselButtonLeft').hide();
		jQuery('#carouselButtonRight').hide();
		jQuery('.usedVehicleHelp').hide();
	}

// SETUP DIALOG DIVS
	jQuery('#dialog').hide();
	jQuery('#dim').hide();
	jQuery('#dim').css('height', jQuery(document).height());
	jQuery('#dim').click(function() {
		jQuery('BODY').css('overflow','auto');
		jQuery('#dim').toggle();
		jQuery('#dialog').hide();
	});

// CLEAR LAST LI BORDER
	jQuery('.lined LI:last-child').css('border','none');

// CAPTURE AJAX BUTTONS - in your code use the format <a href="/ajax/page-name">
	jQuery('a[href*="ajax"]').click(function () {
		var temp = jQuery(this).attr('href');
		temp = temp.split('/');
		var page = temp.pop();
		revealDialog(page);
		return false;
	});
	jQuery('.buttonContact').click(function () {
		revealDialog('contact');
		return false;
	});
	jQuery('.buttonPrivacy').click(function () {
		revealDialog('privacy');
		return false;
	});

// SETUP COLORBOX FOR PHOTO GALLERY
	jQuery("a[rel='photogallery']").colorbox({transition:"none"});
	jQuery("a[rel='photogallery'] IMG").hover(function() {
		jQuery(this).addClass('hover');
	}, function() {
		jQuery(this).removeClass('hover');
	});

// SETUP SHARING
	jQuery('#socialSharing').Sharing();
});

// DIALOG BOX WITH AJAX
function revealDialog(element){
	jQuery('BODY').css('overflow','hidden');
	jQuery('#ajax').load(loadUrl + ' #' + element, null, function(){
		if (element == 'contact') {
			jQuery('#ajax TABLE.zebra tr:even').addClass('zebraEven');
		}

		// VALIDATE FORM THEN AJAX SUBMIT
		var t = jQuery('#formWithCaptcha').captcha();
		var v = jQuery('#formContact').validate({
			submitHandler: function(form){
				jQuery(form).ajaxSubmit({
					target: "#ajax",
					success: function() { 
						jQuery('#dialogWrapper').toggle();
						}  
				});
			}
		});
	});
	jQuery('#close').click(function () {
		jQuery('BODY').css('overflow','auto');
		jQuery('#dim').hide();
		jQuery('#dialog').hide();

	});
	jQuery('#dialog').css('top', (jQuery(window).scrollTop() + 50) + 'px');
	var tempHeight = jQuery(window).height();
	jQuery('#dialog').css('height', tempHeight-100 + 'px');
	jQuery('#dim').toggle();
	jQuery('#dialog').toggle();
}

// RESIZE DIM ON WINDOW CHANGE
jQuery(window).bind('resize', function(){
   jQuery('#dim').css('height', jQuery(window).height());
});

