$(document).ready(function() {



// acronym
	
	// find the acronyms extracts the title and store it in var acronymTITLE.
	$('acronym').each( function (){
		var acronymTITLE = $(this).attr('title');
		// then insert a span inside the acronym tag and write the value of var acronymTITLE inside the span.
		$(this).append('<span>'+acronymTITLE+'</span>');
	});

	// when your mouse is on an acronym fade in the span.
	$('acronym').mouseenter(function(){
		$(this).children('span').fadeIn(300);
	});
	
	// when your mouse comes off an acronym fade out the span.
	$('acronym').mouseleave(function(){
		$(this).children('span').fadeOut(300);
	});

	// clear the acronym titles.
	$('acronym').attr('title','');



// abbr

	// find the abbr extracts the title and store it in var abbrTITLE.
	$('abbr').each( function (){
		var abbrTITLE = $(this).attr('title');
		// then insert a span inside the abbr tag and write the value of var abbrTITLE inside the span.
		$(this).append('<span>'+abbrTITLE+'</span>');
	});

	// when your mouse is on an abbr fade in the span.
	$('abbr').mouseenter(function(){
		$(this).children('span').fadeIn(300);
	});
	
	// when your mouse comes off an abbr fade out the span.
	$('abbr').mouseleave(function(){
		$(this).children('span').fadeOut(300);
	});

	// clear the abbr titles.
	$('abbr').attr('title','');



// table

		// find the tables and resets cellspacing and cellpadding. Also adds the formatted tr classes to alternating rows.
		$('table').each( function (){
		$(this).attr('cellspacing','0');
		$(this).attr('cellpadding','0');
		$(this).find('tr:nth-child(even)').addClass('tr_even');
		$(this).find('tr:nth-child(odd)').addClass('tr_odd');
	});
	// this removes the above mod, add any classes or id's you want to ignore the mod.
	$('table.no_format').each( function (){
		$(this).find('tr:nth-child(even)').removeClass('tr_even');
		$(this).find('tr:nth-child(odd)').removeClass('tr_odd');
	});



// ordered list

	// Find each list
	$('ol').each( function (){
		// Find the li's and add level 1 class then insert a level 1 div for the bullet.
		$(this).children('li').each( function(){
			// This auto counts the # of level 1 li's and inserts the # as the bullet.
			$(this).addClass('li_lv1');
			var lv1COUNT = $(this).parent('ol').children('.li_lv1').size();
			// Insert the div as the top element inside the li. This prevent the level 1 bullet from displaying below an internal list. 
			$('<div class="div_lv1">'+lv1COUNT+'</div>').prependTo(this);								  

		});

		// Find the internal lists li's and add level 2 class then insert a level 2 div for the bullet.
		$(this).children('li').children('ol').children('li').each( function (){
			// This removes the level 1 classes from any level 2 li's and add a level 2 class.
			$(this).removeClass('li_lv1');
			$(this).addClass('li_lv2');
			// This auto counts the # of level 2 li's and inserts the # as the bullet after the decimal.
			var lv2COUNT = $(this).parent('ol').children('.li_lv2').size();
			// This finds the # of the parent bullet and inserts inserts # as the bullet before the decimal.
			var lv2ANCHOR = $(this).parent('ol').parent('li').children('.div_lv1').text();
			// Insert the div as the top element inside the li. 
			$('<div class="div_lv2">'+lv2ANCHOR+'.'+lv2COUNT+'</div>').prependTo(this);								  
		});

		// This removes the level 1 li's from any level 2 li's.
		$('.li_lv2').children('.div_lv1').remove();	
	});

	// this removes the above mod, add any classes or id's you want to ignore the mod.
	$('ol.no_format').each( function (){
		$(this).find('.div_lv1').remove();
		$(this).find('.div_lv2').remove();
		$(this).find('li').removeClass('li_lv1');
		$(this).find('li').removeClass('li_lv2');
	});

	

// unordered list

		// Find each list
		$('ul').each( function (){
		
		// Find the li's and add level 1 class then insert a level 1 div for the bullet.
		$(this).children('li').each( function(){
			$(this).addClass('li_lv1');
			// Insert the div as the top element inside the li. This prevent the level 1 bullet from displaying below an internal list. 
			$('<div class="div_lv1"></div>').prependTo(this);								  
		});

		// Find the internal lists li's and add level 2 class then insert a level 2 div for the bullet.
		$(this).children('li').children('ul').children('li').each( function (){
			// This removes the level 1 classes form any level 2 li's and add a level 2 class.
			$(this).removeClass('li_lv1');
			$(this).addClass('li_lv2');
			// Insert the div as the top element inside the li. 
			$('<div class="div_lv2"></div>').prependTo(this);								  
		});

		// This removes the level 1 li's from any level 2 li's.
		$('.li_lv2').children('.div_lv1').remove();	
	});

	// this removes the above mod, add any classes or id's you want to ignore the mod.
	$('ul.h_nav, ul.v_nav, ul.no_format, .remove_bullets, ul#breadcrumbs, ul.v_drop, ul.h_drop').each( function (){
		$(this).find('.div_lv1').remove();
		$(this).find('.div_lv2').remove();
		$(this).find('li').removeClass('li_lv1');
		$(this).find('li').removeClass('li_lv2');
	});



// hr
	//replace all hr with a custom div.
	$('hr').replaceWith('<div class="hr"></div>');

// text buttons
	$('a.txt_btn').wrapInner('<div class="tb_text"></div>');

// deal_of_week
	var winW = $(window).width();  
	var siteW = 980
	var boxW = 1080
	var faded = 0.90
	var full = 1
	var calcwrapPOS = (siteW-winW)/2
	var calcboxPOS = (winW-boxW)/2
	$('#deal_of_week_lg_car_wrap, #deal_of_week_sm_cars_wrap, #int_header_img_wrap').css('width',winW);
	$('#deal_of_week_lg_car_wrap, #deal_of_week_sm_cars_wrap, #int_header_img_wrap').css('left', calcwrapPOS);
	$('#deal_of_week_lg_car, #deal_of_week_sm_cars, #int_header_img').css('left',calcboxPOS);
	
	$('#deal_of_week_lg_car, #deal_of_week_sm_cars').children('a').children('img').css('opacity',faded);
	$('#deal_of_week_lg_car, #deal_of_week_sm_cars').children('a').mouseenter(function() {
		$(this).children('img').fadeTo('slow', full);
	});
	$('#deal_of_week_lg_car, #deal_of_week_sm_cars').children('a').mouseleave(function() {
		$(this).children('img').fadeTo('slow', faded);
	});

// car_cycler
	$('table.cars, table#deal_of_week, div.inventory').each( function (){
		$(this).find('img').css('opacity',faded);
		$(this).find('a').mouseenter(function() {
			$(this).children('img').fadeTo('slow', full)
		});
		$(this).find('a').mouseleave(function() {
			$(this).children('img').fadeTo('slow', faded);
		});
	});
	
// detect window size and if this is the home page then position btn_comm

	// function if #header is detected
	$.fn.foundHome = function (){
		// check the window size and then do one of these
		if(winW < 1280) {
				$('#btn_comm').css({top:'-380px', right:'0px'});
			}
			else {
				$('#btn_comm').css({top:'430px', right:'0px'});
			}
	}
	
	// function if #header is not detected
	$.fn.foundInt = function (){
		// check the window size and then do one of these
		if(winW < 1280) {
				$('#btn_comm').css({top:'-210px', right:'0px'});
			}
			else {
				$('#btn_comm').css({top:'430px', right:'0px'});
			}
	}

	// if #header is detected do function
	if ($('#header').length){
		$().foundHome();
		}
	// if #header is not detected do other function
	else {
		$().foundInt();
		}

	$('.scroll_url').click(function(e) {
		e.preventDefault();
		var target = $(this).attr('href');
		var top_margin = 20
		var target_pos = $(target).offset().top - top_margin
		var scroll_time = 2000
		$('html, body').animate({scrollTop: target_pos}, scroll_time);
	});


$.fn.scroll_to_show = function ()
	{
	var hidden_elem = $(this)
	var window_height = $(window).height();
	var hidden_elem_height = $(this).height();
	var hidden_elem_pos = (window_height - hidden_elem_height)/2
	var target = $('#page_nav').offset().top;

	$(this).css('bottom', hidden_elem_pos);
	$(this).hide();
	
	$(window).scroll(function (event) {
		var scrolled = $(this).scrollTop();
		if ( scrolled >= target ) 
			{
				hidden_elem.fadeIn(500);
			} 
		else 
			{
				hidden_elem.fadeOut(300);
			}
	});
	}
	
	if ($('#page_nav').length > 0) {
		$('#back_to_top').scroll_to_show();
	}
	

$.fn.scroll_to_stick = function ()
	{
	var sticky_elem = $(this)
	var window_height = $(window).height();
	var window_width = $(window).width();
	var sticky_elem_height = $(this).height();
	var content_width = $('#content_area').width();
	var sticky_elem_x_pos = (window_width - content_width)/2
	var target = $('#sticky_column').offset().top;
	var target_end = $('#footer').offset().top;
	var target_end_pos = target_end - sticky_elem_height

	$(window).scroll(function (event) {
		var scrolled = $(this).scrollTop();
		if ( scrolled >= target_end_pos ) 
			{
				sticky_elem.css
					({
					position: 'absolute',
					right: '0px',
					top: '',
					bottom: '0px'
					});
			}
		else if ( scrolled >= target ) 
			{
				sticky_elem.css
					({
					position: 'fixed',
					top: '20px',
					right: sticky_elem_x_pos
					});
			} 
		else 
			{
				sticky_elem.css
					({
					position: '',
					right: '',
					top: ''
					});
			}
	});
	}
	
	$('#sticky_column').scroll_to_stick();

});//doc ready brackets


