/* Effects file for nice car company website */

$(document).ready(function(){
	var mouseX = 0;
	var mouseY = 0;
	
	$(document).mousemove(function(e){
		mouseX = e.pageX;
		mouseY = e.pageY;
	});

	// print features
	$('.printable_view').click(function(){
        	$('#header').hide();
        	var lNav = $('#leftnav').html();
        	$('#leftnav').html("");
        	window.print();
        	$('#header').show();
        	$('#leftnav').html(lNav);
        });
	
	/* Left Nav Filtering Stuff */
	$('.vehicleFilters .f_symbol').click(function(){
			if( $(this).html() == '+' ){
				$(this).html("-").css('padding','0 5px 0 4px');
				$(this).parent('li').children('div').show();
				$(this).parent('li').children('.f_text').css('text-decoration','none');
			} else {
				$(this).html("+").css('padding','0 3px 0 3px');
				$(this).parent('li').children('div').hide();
				$(this).parent('li').children('.f_text').css('text-decoration','underline');
			}
	});
	$('.vehicleFilters .f_text').click(function(){
			if( $(this).css("text-decoration") != 'none' ){
				$(this).parent('li').children('.f_symbol').css('padding','0 5px 0 4px');
				$(this).parent('li').children('.f_symbol').html("-");
				$(this).parent('li').children('.filter_hide').show();
				$(this).parent('li').children('.f_text').css('text-decoration','none');
			} else {
				$(this).parent('li').children('.f_symbol').css('padding','0 3px 0 3px');
				$(this).parent('li').children('.f_symbol').html("+");
				$(this).parent('li').children('.filter_hide').hide();
				$(this).parent('li').children('.f_text').css('text-decoration','underline');
			}
	});
	
	$('.select_filter_results').change(function(){
			var cur_url = document.URL;
			// alert(cur_url);
			// var selected_sort = $(this).children('option:selected').attr('value');
			var selected_sort = $('.select_filter_results option:selected').attr('value');
//			alert($(this).children('option:selected').val());
			var sort_str = selected_sort.split(" ");
			var sortby = "";
			sortby = sort_str[0];
			var sortorder =  "";
			sortorder = sort_str[1];
			// search for sortby and sortorder in the url and replace or apppend and reload
			if( get_pos( cur_url, "sortby", 0 ) === false ){
				// if no ? add it then the filters
				if( get_pos( cur_url, "?", 0 ) === false ){ 
					cur_url = cur_url + "?";
				}
				new_url = cur_url + "&sortby=" + sortby + "&sortorder=" + sortorder;
				window.location = new_url;
			} else {
				// replace all possible with the new that way it will get caught
				cur_url = cur_url.replace(/Year/g,sortby);
				cur_url = cur_url.replace(/Make/g,sortby);
				cur_url = cur_url.replace(/Model/g,sortby);
				cur_url = cur_url.replace(/Mileage/g,sortby);
				cur_url = cur_url.replace(/Price/g,sortby);
				
				cur_url = cur_url.replace(/ASC/g,sortorder);
				cur_url = cur_url.replace(/DESC/g,sortorder);
				window.location = cur_url;
			}
	});
	
	$('.paginate_search').click(function(){
			pos = $(this).position();
			show_search_form(pos);
	});
	$('.nav-buttons').children('img').css("border","none !important");
	$('.nav-buttons').children('img').children('a').css('border','none !important');
	
	$('#adv_query_box').click(function(){
			if( $(this).val() == $(this).attr('def') ){
				$(this).val("");
			}
	});
		
	$('#query').click(function(){
			if( $(this).val() == "Search Anything" ){
				$(this).val("");
			}
	});
	
	$('.paginate_sort').bind("mouseenter",function(){
			$(this).css('background','url("/images/pg-hover.png") no-repeat top left').css('color','#fff');
	}).bind("mouseleave", function(){
		$(this).css('background','transparent').css('color','#555');
	});
	
	
	// first lets bind the bkg img to turn on on mouse enter
	$('.top_hoover').bind("mouseenter",function(){
		// $(this).children('img').attr('width',$(this).width() + 24);
		// $(this).children("img").css("display","inline").css('margin-left','-12px');;
		$(this).children("a").css("color","#debe75");
	// then lets turn it off
	}).bind("mouseleave",function(){
		$(this).children("img").css("display","none");
		$(this).children("a").css("color","#fff");
	});

/* New Header navigation */
	//$('.top_sub_sub').hide();	

	// Brand stop_sub
	$('.top_sub').bind("mouseenter",function(){
		var brand = $(this).attr('brand');
		$('#' + brand + '_models').show();
		$(this).children('img').attr('width',$(this).width() + 24);
		$(this).children("img").css("display","inline").css('margin-left','-12px');;
		$(this).children("a").css("color","#af0000");
	// then lets turn it off
	}).bind("mouseleave",function(){
		var brand = $(this).attr('brand');
		$('#' + brand + '_models').hide();
		$(this).children("img").css("display","none");
		$(this).children("a").css("color","#2e2e2e");
	});
	
	// next lets bind the bottom nav img to the navigation hoover
	$('.bottom_hoover').bind("mouseenter",function(){
		$(this).children('img').attr('width',$(this).width() + 16);
		$(this).children("img").css("display","inline");
		$(this).children("a").css("color","#722b27");
	}).bind("mouseleave",function(){
		$(this).children("img").css("display","none");
		$(this).children("a").css("color","#555");
	});

	$('.left_hoover').bind("mouseenter",function(){
		$(this).children("img").css("display","inline");
		$(this).children("a").css("font-weight","600");
		$(this).children('img').attr("url","/images/left-hover.png");
	// then lets turn it off
	}).bind("mouseleave",function(){
		$(this).children("a").css("font-weight","300");
		$(this).children('img').attr("url","/images/left-hover.png");
	});
	
	// this gives the LI the same navigation as its link
	$('li').click(function(){
		if( $(this).children("a").index() >= 1 ){
			if( $(this).children("a").attr("href") != "https://creditapp.autotrader.com/consumer/creditapp/controller.jsp?action=nav_creditapp&dealer_id=568023&origin_page=DEALERS_DDA_INDEX_APPLY_CREDIT" ){
				window.location = $(this).children("a").attr("href");
			}
		}
	});
		

	$('.autoclear').focus(function(){
		var cur = $(this).attr("value");
		var def = $(this).attr("default");
		if( cur == def ){
			$(this).attr("value","");
			$(this).css("color","#555");
		}
	});
	
	$('#refine_search').click(function(){
		$('#searchbox').load('/forms/search-page.php');
		$('#searchbox').toggle();
	});
	
	$('.sort_button').click(function(){
		sort_val = $(this).attr("id").replace("sort_","");
		$('#h_sort').attr("value",sort_val);
		$('#paginationForm').submit();
	});
	
	$('.ncc').bind("mouseenter",function(){
		var posnow = $(this).position();
		newtop = posnow.top;
		newleft = posnow.left;
		$('#warranty_bubble').css("top",newtop);
		$('#warranty_bubble').css("left",newleft);
		$('#warranty_bubble').show("fast");
	});
	
	$('#car_row ul li').each(function(){
		$(this).bind("mouseenter", function(){
		var cur = $(this).attr('class');
		var also = cur + "a";
		$('.' + also + ' a').css('color','#f61214').css('text-decoration','underline');
		
		}).bind("mouseleave", function(){
			var cur = $(this).attr('class');
			var also = cur + "a";
			$('.' + also + ' a').css('color','#9a0001').css('text-decoration','none');
		});
	});
	
	// New Header Features
	$('#new_vehicles_button').bind("mouseenter",function(){
		$('#used_brands').stop(true,true).hide();
		$('#header_search_form').stop(true,true).hide();
		$('#new_brands').stop(true,true).show();
		$('#nav_logic').animate({
				width: '784px'	
		}, "fast" );
		$('.topCenter').bind("mouseleave",function(){
				$('#new_brands').stop(true,true).hide();
				$('#nav_logic').stop(true,true).animate({
						width: '4px'
				});
		});
	});
	
	// Used Header Features
	$('#used_vehicles_button').bind("mouseenter",function(){
		$('#new_brands').stop(true,true).hide();
		$('#header_search_form').stop(true,true).hide();
		$('#used_brands').stop(true,true).show();
		$('#nav_logic').stop(true,true).animate({
				width: '784px'	
		}, "fast" );
		$('.topCenter').bind("mouseleave",function(){
				$('#used_brands').stop(true,true).hide();
				$('#nav_logic').stop(true,true).animate({
						width: '4px'
				});
		});
	});
	
	// Used Header Features
	$('#vehicle_search_button').bind("mouseenter",function(){
		$('#new_brands').stop(true,true).hide();
		$('#used_brands').stop(true,true).hide();
		$('#header_search_form').stop(true,true).show();
		$('#nav_logic').stop(true,true).animate({
				width: '784px'	
		}, "fast" );
		$('.topCenter').bind("mouseleave",function(){
				$('#header_search_form').stop(true,true).hide();
				$('#nav_logic').stop(true,true).animate({
						width: '4px'
				});
		});
	});
	
	var pushX = $('.topCenter').width();
	var pushY = $('.topCenter').height();
	var offX = pushX - ((174*2)+20);
	var offY = pushY - 63;
	$('#logic_logo').css('margin-top',offY).css('left',offX);
	
	$('#new_brands .brand_heading a').bind("mouseenter",function(){
			$('.brand_heading').css('background','none');
			$('.brand_models').stop(true,true).hide();
			var show = $(this).attr('thisone');
			var Offset = $(this).position();
			if( $('#'+show).height() > 40 ){ 
				$('#'+show).css('left', Offset.left + 32).css('top',32);
			} else {
				$('#'+show).css('left', Offset.left + 32).css('top', ( Offset.top - ( $('#'+show).height() / 2 ) ) );
			}
			$('#'+show).stop(true,true).show();
			$(this).parent('div').css("background","url('/images/brand_heading_bkg.png') no-repeat top left");
			$('.brand_models').bind("mouseleave",function(){
					$('.brand_heading').css('background','none');
					$('#'+show).stop(true,true).hide();
			});
	});
	
	$('#used_brands .brand_heading a').bind("mouseenter",function(){
			$('.brand_heading').css('background','none');
			$('.used_brand_models').stop(true,true).hide();
			var show = $(this).attr('thisone');
			var Offset = $(this).position();
			diff = 32;
			
			if( $('#'+show).height() > 40 ){ 
				$('#'+show).css('left', Offset.left).css('top',32);
			} else {
				$('#'+show).css('left', Offset.left).css('top', ( Offset.top - ( $('#'+show).height() / 2 ) ) );
			}
			
			$('#' + show).stop(true,true).show();
			$(this).parent('div').css("background","url('/images/brand_heading_bkg.png') no-repeat top left");
			$('.used_brand_models').bind("mouseleave",function(){
					$('.brand_heading').css('background','none');
					$('#'+show).stop(true,true).hide();
			});
	});
	
	$('#__used_brands .brand_heading a').bind("mouseenter",function(){
			// the idea will be pop using the bottom of the box if longer than the box
			var show = $(this).attr('thisone');
			var startPOS = $(this).position();
			startPosition = 0;
			thatStartPosition = 0;
			if( startPosition > thatStartPosition ){
				//$('#'+show).css('margin-top', (startPosition.top - ( startPosition - thatStartPosition ) * -1)  ).css('left', thisPOS.left );
				$('#'+show).css('top', thisPOS.top ).css('left', thisPOS.left);
			} else {
				
				$('#'+show).css('top', thisPOS.top ).css('left', thisPOS.left);
			}
			$('#' + show).stop(true,true).show();
			$(this).parent('div').css("background","url('/images/brand_heading_bkg.png') no-repeat top left");
			$('.used_brand_models').bind("mouseleave",function(){
					$('.brand_heading').css('background','none');
					$('#'+show).stop(true,true).hide();
			});
	});
	
	$('.used_brand_models').bind("mouseleave",function(){
			$('.brand_models').stop(true,true).hide();
	});
		
	
	// Brand HOver disabled by adding XX to class
	$('.brand_hoverXX').bind("mouseenter",function(){
		// create new mid content
		var cur_brand = $(this).attr('id');
		switch( cur_brand ){
		case "brand1":
			header_functions("dodge");
			break;
			
		case "brand2":
			header_functions("jeep");
			break;
			
		case "brand3":
			header_functions("chrysler");
			break;
			
		case "brand4":
			header_functions("ram");
			break;
		default:
			header_functions("main");
		}
		
	}).bind("mouseleave",function(){
		// if the mouse is below the header hide the header images
		$(document).mousemove(function(e){
				if( e.pageY > 200 || e.pageY < 40 ){
					header_functions("main");
				}
				else{
					// timeout hide
				}
		}); 

	});
	
	$('.hide_brands').bind("mouseenter", function(){
			$('.header_brand').stop(false,true).slideUp();
			$('#main_header').stop(false,true).slideDown();
	});
	
	
	// make the header brand lis clickable to the same link as the a
	$('.model').bind("mouseenter", function(){
		var cur_model = $(this).attr('id');
		var cur_bkg = $(this).parent('ul').parent('div').attr('id');
		
		switch(cur_model){
			// dodge brands
			case "avenger":
				$('#' + cur_bkg).css("background","url('/images/headers/dodge-avenger-header.jpg') no-repeat bottom center");
				break;
			case "caliber":
				$('#' + cur_bkg).css("background","url('/images/headers/dodge-caliber-header.jpg') no-repeat bottom center");
				break;
			case "challenger":
				$('#' + cur_bkg).css("background","url('/images/headers/dodge-challenger-header.jpg') no-repeat bottom center");
				break;
			case "charger":
				$('#' + cur_bkg).css("background","url('/images/headers/dodge-charger-header.jpg') no-repeat bottom center");
				break;
			case "durango":
				$('#' + cur_bkg).css("background","url('/images/headers/dodge-durango-header.jpg') no-repeat bottom center");
				break;
			case "grand_caravan":
				$('#' + cur_bkg).css("background","url('/images/headers/dodge-grand-caravan-header.jpg') no-repeat bottom center");
				break;
			case "journey":
				$('#' + cur_bkg).css("background","url('/images/headers/dodge-journey-header.jpg') no-repeat bottom center");
				break;
			case "nitro":
				$('#' + cur_bkg).css("background","url('/images/headers/dodge-nitro-header.jpg') no-repeat bottom center");
				break;
			// jeep
			case "compass":
				$('#' + cur_bkg).css("background","url('/images/headers/jeep-compass-header.jpg') no-repeat bottom center");
				break;
			case "grand_cherokee":
				$('#' + cur_bkg).css("background","url('/images/headers/jeep-grand-cherokee-header.jpg') no-repeat bottom center");
				break;
			case "liberty":
				$('#' + cur_bkg).css("background","url('/images/headers/jeep-liberty-header.jpg') no-repeat bottom center");
				break;
			case "patriot":
				$('#' + cur_bkg).css("background","url('/images/headers/jeep-patriot-header.jpg') no-repeat bottom center");
				break;
			case "wrangler":
				$('#' + cur_bkg).css("background","url('/images/headers/jeep-wrangler-header.jpg') no-repeat bottom center");
				break;
			
			// chrysler 
			case "c200":
				$('#' + cur_bkg).css("background","url('/images/headers/chrysler-200-header.jpg') no-repeat bottom center");
				break;
			case "c300":
				$('#' + cur_bkg).css("background","url('/images/headers/chrysler-300-header.jpg') no-repeat bottom center");
				break;
			case "c300c":
				$('#' + cur_bkg).css("background","url('/images/headers/chrysler-300-header.jpg') no-repeat bottom center");
				break;
			case "town_and_country":
				$('#' + cur_bkg).css("background","url('/images/headers/chrysler-town-and-country-header.jpg') no-repeat bottom center");
				break;
			
			// ram
			case "dakota":
				$('#' + cur_bkg).css("background","url('/images/headers/ram-dakota-header.jpg') no-repeat bottom center");
				break;
			case "r1500":
				$('#' + cur_bkg).css("background","url('/images/headers/ram-1500-header.jpg') no-repeat bottom center");
				break;
			case "r2500":
				$('#' + cur_bkg).css("background","url('/images/headers/ram-2500-header.jpg') no-repeat bottom center");
				break;
			case "r3500":
				$('#' + cur_bkg).css("background","url('/images/headers/ram-2500-header.jpg') no-repeat bottom center");
				break;
		}
		
		$('#' + cur_bkg).bind("mouseleave",function(){
			// stop animation build up
			
			if( cur_bkg == 'dodge_header' ){
				$(this).css("background","url('/images/headers/dodge-header.jpg') no-repeat bottom center");
			} else if( cur_bkg == 'jeep_header' ){
				$(this).css("background","url('/images/headers/jeep-header.jpg') no-repeat bottom center");
			} else if( cur_bkg == 'chrysler_header' ){
				$(this).css("background","url('/images/headers/chrysler-header.jpg') no-repeat bottom center");
			} else if( cur_bkg == 'ram_header' ){
				$(this).css("background","url('/images/headers/ram-header.jpg') no-repeat bottom center");
			} else {
				$(this).css("background","url('/images/header-bkg.jpg') no-repeat bottom center");
			}
		});
				
	});
	
});

var last_active = "main";


function get_pos(haystack,needle,offset){
	var i = (haystack+'').indexOf(needle, (offset || 0));
	return i === -1 ? false : i;
}

function header_functions(Brand){
	if( Brand == '' || Brand == 'main' ){ 
		$('.header_brand').stop(false,true).slideUp();
		$('#main_header').stop(false,true).slideDown();
	} 
	
	switch( Brand ){
		case "dodge":
			$('.all_headers').stop(false,true).slideUp();
			$('#dodge_header').stop(false,true).slideDown();
			break;
		case "jeep":
			$('.all_headers').stop(false,true).slideUp();
			$('#jeep_header').stop(false,true).slideDown();
			break;
		case "chrysler":
			$('.all_headers').stop(false,true).slideUp();
			$('#chrysler_header').stop(false,true).slideDown();
			break;
		case "ram":
			$('.all_headers').stop(false,true).slideUp();
			$('#ram_header').stop(false,true).slideDown();
			break;
	}
	
	last_active = Brand;
	
}

function show_search_form(pos){
	// alert(pos);
	
	var h = $('body').height();
	var w = $('body').width();
	
	$('#ultimate_shadow').css("width",w).css("height",h);
	$('#ult_shadow_content').css("width",w).css("height",h).css('margin-top',(pos.top - 200));
	
	$('#ultimate_shadow').show();
	$('#ult_shadow_content').show();
	
	return;
}



