$(document).ready(function() {		
	
	//****************************************************************************
	//  DROP DOWN MENU LOADERS
	$('#s').change(function() {
		$.get('PreownedServ',{a: 'updateModels', s: $(this).val(), r: $('#r').val()}, function(data) {
			$('#m').html(data);
			$('#m').resetSS();
		});
	});
	
	$('#s').change(function() {
		$.get('PreownedServ',{a: 'updateYears', s: $(this).val(), r:$('#r').val()}, function(data) {
			$('#y').html(data);
			$('#y').resetSS();
		});
	});
		
	$('#s').change(function() {	
		$.get('PreownedServ',{a: 'updatePrice', s: $(this).val(), r:$('#r').val()}, function(data) {
			$('#p').html(data);
			$('#p').resetSS();
		});
  	});
	
	//****************************************************************************
	//  STYLISH SELECT
	$('#s').sSelect();
	$('#m').sSelect();
	$('#y').sSelect();
	$('#p').sSelect();
	
	//****************************************************************************
	//  PREOWNED SEARCH 	
	$('#preSearchBtn').click(function() {	
		$('#preSearchForm').submit();
  	});
	
	//****************************************************************************
	//  ADD MOUSEOVER TO PREOWNED FEATURE SCOLL RIGHT
	$('#preowned-scroll-right').hover(
		function () {
			$(this).addClass('preowned-scroll-right-on');
			$(this).removeClass('preowned-scroll-right-off');
		},
		function () {
			$(this).addClass('preowned-scroll-right-off');
			$(this).removeClass('preowned-scroll-right-on');
		}
	);
	
	//  ADD onCLICK {next} TO PREOWNED FEATURE SCOLL RIGHT
	
	$('#preowned-scroll-right').click(function() {
			galleryPreownedNext();
	});

	//****************************************************************************
	//  ADD MOUSEOVER TO PREOWNED FEATURE SCOLL LEFT
	$('#preowned-scroll-left').hover(
		function () {
			$(this).addClass('preowned-scroll-left-on');
			$(this).removeClass('preowned-scroll-left-off');
		},
		function () {
			$(this).addClass('preowned-scroll-left-off');
			$(this).removeClass('preowned-scroll-left-on');
		}
	);
	
	//  ADD onCLICK {prev} TO PREOWNED FEATURE SCOLL LEFT
	$('#preowned-scroll-left').click(function() {
				galleryPreownedPrev();
	});
	

	
	
	
	
	
	
	
	
	$('.preFeatureFrame:first').addClass('show');
	
	//$('#preFeature').append('<div class="preFeatureFrame" id="restart"></div>');

	//Set the opacity of all images to 0
	$('.preFeatureFrame').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('.preFeatureFrame:first').css({opacity: 1.0});

	$('#preowned-scroll').css({top:-40,opacity: 1.0});

	//Display the caption
	//$('.preFeatureInfo').css({opacity: 0.7});
	
	//Call the gallery function to run the slideshow	
	var timer3 = setInterval('galleryPreowned()',2000);
		//pause the slideshow on mouse over - show flash scroll
	$('#preFeature').hover(
		function () {
			$('#preowned-scroll').css({top:0});
		}, 	
		function () {
			$('#preowned-scroll').css({top:-40});			
		}
	);
	
	
});


function galleryPreowned() {

	var current = ($('#preFeature div.show') ?  $('#preFeature div.show') : $('.preFeatureFrame:first'));
	var next = ((current.next().length) ? ((current.next().attr('class') == 'restart') ? $('.preFeatureFrame:first') : current.next()) : $('.preFeatureFrame:first'));
		
	changePreownedImage(next, current);

}


function galleryPreownedNext() {
	
	var current = ($('#preFeature div.show') ?  $('#preFeature div.show') : $('.preFeatureFrame:first'));
	var next = ((current.next().length) ? ((current.next().attr('class') == 'restart') ? $('.preFeatureFrame:first') : current.next()) : $('.preFeatureFrame:first'));
	
	changePreownedImage(next, current);
	
}

function galleryPreownedPrev() {
	
	var current = ($('#preFeature div.show') ?  $('#preFeature div.show') : $('.preFeatureFrame:first'));
	var next = ((current.prev().length) ? ((current.prev().attr('class') == 'restart') ? $('.preFeatureFrame:first') : current.prev()) : $('.preFeatureFrame:first'));
	
}

function changePreownedImage(next, current) {
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 300);
	current.animate({opacity: 0.0}, 300).removeClass('show');
}



