$(window).load(function(){
	var myimg = $('#content img');
	myimg.each(function(){
		var imgHeight = $(this).height();
		var imgWidth = $(this).width();
		$(this).attr('height',imgHeight);
		$(this).attr('width',imgWidth);
	});
	$('div.thumb img').each(function(){ // fixes for home thumbs which were nulled by the above code.
		$(this).attr('height','80');
		$(this).attr('width','160');
	});
	$('#image_promo_container div.image img').each(function(){ // fixes for home thumbs which were nulled by the above code.
		$(this).attr('height','110');
		$(this).attr('width','374');
	});
	$('a.video-close img').each(function(){ // fixes for video close button which was nulled by the above code.
		$(this).attr('height','16');
		$(this).attr('width','16');
	});
});
//_________ end image attr. setter _________\\
$(document).ready(function(){
	$('#gallery_thumbs div.thumb').css('opacity',0);
	$('#vehicle_spec_buttons div.spec-flyout').css('opacity',0.95);
	$('#service_parts ul.video-sub-nav').css('opacity',0.95);
	$('#gallery_nav').css('opacity',0.85);
//_________ end opacity settings _________\\
	//maybe I should make this into a plugin
	$('#location_trigger').click(function(){//in place of toggle
		if($('#location_table').is(':visible')){//add/remove class open/closed depending on visible state
			$(this).next().removeClass('open').addClass('closed').slideUp(400);
		}
		else{
			$(this).next().removeClass('closed').addClass('open').slideDown(400);
		}
	});
	$('#location_table span.close').click(function(){//same function, but with the 'X' button instead
		if($('#location_table').is(':visible')){
			$(this).parent().removeClass('open').addClass('closed').slideUp(400);
		}
		else{
			$(this).parent().removeClass('closed').addClass('open').slideDown(400);
		}
	});
//_________ end location flyout _________\\
	if ($.browser.msie) {
		if(jQuery.browser.version.substr(0,3) == '6.0'){ //problematic for ie 6...freezes...

		}
		else if(jQuery.browser.version.substr(0,3) >= '7.0') {
			$('#main_nav li').hover(function(){//add hoverIntent later
				$(this).find('ul.sub-nav').stop().fadeTo(500,0.9).css('display','block');
			},
			function(){
				$(this).find('ul.sub-nav').stop().fadeTo(500,0, function(){ $(this).css('display','none')});
			});
		}
	}
	else if (!$.browser.msie) {//for some reason ie6 freezes otherwise
		$('#main_nav li').hover(function(){//add hoverIntent later
			$(this).find('ul.sub-nav').stop().css('display','block').fadeTo(500,0.9);
		},
		function(){
			$(this).find('ul.sub-nav').stop().fadeTo(500,0, function(){ $(this).css('display','none')});
		});
	}
//_________ end sub nav _________\\
	$('#main_nav li a:eq(1)').attr('id','why_video_button');
	var navText = $('a#why_video_button').text();
	$('a#why_video_button').html(navText + "<img class='why-video' src='accelsite/themes/temp999/images/nav_video.png' title='' alt='' border='0' />");
	$('#main_nav li a:last').attr('id','video_button').addClass('closed');
//_________ end main nav video buttons _________\\
	$('#main_nav li.divider:last').hide();
//_________ end contact line _________\\
	$('#vehicle_search input.search-box').addClass("idleField");
	$('#vehicle_search input.search-box').focus(function() {
		$(this).css('background-image','url(accelsite/themes/temp999/images/search_field_bg_on.png)');
		$(this).removeClass("idleField").addClass("focusField");
		if (this.value == this.defaultValue){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('#vehicle_search input.search-box').blur(function() {
		$(this).css('background-image','url(accelsite/themes/temp999/images/search_field_bg_on.png)');
		if ($.browser.msie) {
				if(jQuery.browser.version.substr(0,3) == '6.0'){
					$(this).removeClass("focusField").addClass("idleField").css({'padding-left':'3px', 'width':'132px'});
				}
			}
		else {
			$(this).removeClass("focusField").addClass("idleField").css({'padding-left':'3px', 'width':'140px'});
		}
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
//_________ end search box _________\\
	$('#vehicle_spec_buttons.inner div.spec-box:last').addClass('last');
//_________ end spec box fix for ie 7 ________\\
	$(".vehicle-new-used a").click(function(){ // new used classes
		var myType = $(this).text().toLowerCase();

		$(".vehicle-new-used a").removeClass('on off');
		$(this).addClass('on');
		$(".vehicle-new-used a").not($(this)).addClass('off');

		if($("div.spec-flyout:visible")){ // close flyouts
			var openFlyout = $("div.spec-flyout:visible");
			openFlyout.hide(500, fetchVehicleNav(myType));
			
		} else {
			fetchVehicleNav(myType);
		}
	});
//_________ end NEW/USED Buttons _________\\
	jsReload();
	$('table#vehicle_search_results tr.found-vehicle').hover(function(){
		$(this).children('td').addClass('hover');
	},
	function(){
		$(this).children('td').removeClass('hover');
	});
//_________ end found-vehicle td hover __________\\
	$('table td', '#vehicle_spec_buttons').hover(function(){
		$(this).addClass('hover');
	},
	function(){
		$(this).removeClass('hover');
	});
//_________ end found-vehicle td hover __________\\
	$('form[name=tradeValueForm]').children('table').attr('id','employment_form');
	$('#employment_form input[type=radio]').addClass('radio');
	$('#employment_form input[type=checkbox]').addClass('radio');
//_________ end employment form input mod _________\\
	$('#member_links a:last').addClass('last');
//_________ end member links _________\\
	$("a[href*='unitytools']").attr('rel','prettyPhoto[iframes]');
	$('a[rel="prettyPhoto[iframes]"]').each(function(){
		$(this).attr('href', $(this).attr('href') + '?iframe=true&width=774&height=492');
	});
	$("a[rel^='prettyPhoto[iframes]']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.5, /* Value betwee 0 and 1 */
		default_width: 771,
		default_height: 488,
		showTitle: false, /* true/false */
		allowresize: false, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
		hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
		modal: true /* If set to true, only the close button will close the window */
	});
//_________ end Pretty Photo Code _________\\
	// $('#pp_full_res').children().css('overflow','hidden');
	if ($.browser.msie) {
		$('a.pp_close').click(function(){
			$('#pp_full_res').empty();
			$(this).parents('div.pp_pic_holder').hide();
			$('div.pp_overlay').hide();
		});
	}
	$('#content_main_inner p:contains("unitytools")').hide().parent().addClass('video');
	var videotext = $('#content_main_inner p:contains("unitytools")').text();
	videotext.replace('&amp;','&');
	//console.log(videotext);
	if($('#content_main_inner').hasClass('video')){
		var videoPage = $('#content_main_inner');
		videoPage.parent().css('width','772px');
		videoPage.css({'padding-left':'0', 'padding-right':'0'});
		videoPage.html('<iframe width="771" height="500" frameborder="0" scrolling="no" src="'+ videotext +'" />');
	}
//__________ end inner video pages code converter __________\\
	$('#vehicle_nav_inner div.specs').addClass(($('#vehicle_nav_inner').hasClass('nav-closed') ? 'closed' : 'open'));//makes the open/close links work.
//_________ end spec open/close mod _________\\
	$('#content_main_inner input').each(function(){
		var inputItem = $(this);
		var inputType = $(this).attr('type');
		inputItem.attr('class', inputType);
	});
//_________ end input classes _________\\
	$('a.photoCropperLink').click(function(){
		$(document).ajaxComplete(function(){  
			$('div:not([class],[id])').not('#container div').addClass('cropper').css('z-index',150);
		});	
	});
//_________ end cropper div _________\\
	$(window).load(function(){ // waits for images to load fully before calling Cycle 
		if($('#detail_gallery img').length == 1){
			var singleImage = $('#detail_gallery img').height();
			$('#detail_gallery img').css('display','block');
		}
		$('#detail_gallery').cycle({ 
		    fx:     'fade', 
		    speed:  'slow', 
		    timeout: 3000,
		    pause:         1, 
		    pauseOnPagerHover: 1,
			prev: 'a#prev',
			next: 'a#next',
		    pager:  '#detail_thumbs ul',
			after: onAfter,
		    pagerAnchorBuilder: function(idx, slide) { 
		        return '<li><a href="#"><img src="' + slide.src + '" width="69" height="52" border=\'0\' /></a></li>'; 
		    } 
		}); // end Cycle
	}, 
		function(){ // displays images once they have loaded
			onAfter();
		}
	);
	var vehicleCount = $('#detail_gallery img').length;
	if(vehicleCount <= 9){
		$('a#more_images').hide();
	}
//_________ end vehicle gallery _________\\
	$('.video_box').removeClass('open').addClass('closed').hide();
	$('a.play-video', this).click(function(){
		$(this).next('.video_box').removeClass('closed').addClass('open').slideToggle();
			return false;
	});
	$('a.video-close', this).click(function(){
		$(this).parent('.video_box').slideUp('fast',
			function(){
				$(this).children('.video').empty();
		});
		return false;
	});
	$('a[rel=prettyPhoto][iframes]').click(function(){
		$('.video_box').slideUp('fast',
			function(){
				$('.video').empty();
		});
		return false;
	});
	$('a#detail_photo_button').click(function(){
		if($('div.video_box').hasClass('open')){
			$('.video_box').slideUp('fast',
				function(){
					$(this).children('.video').empty();
			});
		}
		return false;
	});
//_________ end photo button video closer _________\\
	$("#view_pricing_details").click(function() {
		$('#pricing_deals').slideToggle(400);
		return false;
	});
//_________ end price detail pop-up slider _________\\
	$("#tabbed_features a.tab").click(function () {
		// switch all tabs off
		$("#tabbed_features a.active").removeClass("active");

		// switch this tab on
		$(this).addClass("active");

		// slide all content up
		$("#tab_container div.content-container").slideUp(400);

		// slide this content up
		var content_show = $(this).attr("title");
		$("#"+content_show).slideDown(600);
		return false;
	});
//_________ end features tab _________\\
	$('div#service_nav ul li:gt(4)').addClass('bottom');
//_________ end service nav _________\\
	//$('div.faq-answer').hide();
	$('h3.faq-question').click(function(){
		$(this).next('div.faq-answer').slideToggle(500, function(){
			$(this).children('table').show(1);
			//console.log($(this).children('table:visible'));
			//alert($(this).children('table:visible'));
		});
		$(this).children('table').show(1);
	});
//_________ end FAQ toggle _________\\
	$("#service_appt li").hover(function(){
		$(this).addClass('active');
	},
	function(){
		if($(this).children('a').hasClass('.active')){
			
		}
		else {
			$(this).removeClass('active');
		}
	});
//_________ end active li _________\\
	$('#service_appt li a').each(function(){
		 var liChar = $(this).text().length;
		 var liWidth = liChar*6+19;
		 $(this).css('width', liWidth + 'px');
	});
//_________ end tab width _________\\
	$("#service_appt a.tab:first").parent('li').addClass('active');
	$("#service_appt a.tab:first").addClass('active');
	$("#service_appt a.tab").click(function () {
		// switch all tabs off
		$("#service_appt a.active").parent('li').removeClass("active");
		$("#service_appt a.active").removeClass("active");

		// switch this tab on
		$(this).parent('li').addClass("active");
		$(this).addClass("active");

		// slide all content up
		$("#service_tabs div.content-container").slideUp(300);

		// slide this content up
		var content_show = $(this).attr("title");
		$("#"+content_show).slideDown(500);
		
		return false;
	});
//_________ end service appt tab _________\\
	$("#service_tabs tr").each( function(){
		var counter = 1;
		$(this).children("td,th").each( function() {
			$(this).addClass("col-" + counter);
			counter++;
		});
		$(this).children("th:contains(Y)").each( function() {
			$(this).addClass("yes");
		});
	});
	$('div.content-container').each(function(){
		var myTable = $(this).find('table');
		$(this).find('th.yes').each(function(){
			$(this).removeClass('yes');
			myTable.find('td.' + $(this).attr('class')).addClass('yes');
			$(this).addClass('yes');
		});
	})
//_________ end table column styling _________\\
});
//_________ end Document Ready function _________\\
	function onAfter(){
		var currentSlideUrl = $('#detail_thumbs a.activeSlide img').attr('src');
		var selectedImage = '';
		if(currentSlideUrl){
			var pattern = /vehicle[^.|]+\.jpg/
			selectedImage = currentSlideUrl.match(pattern);
		}
		$('a#enlarge').attr('href','index.php?ado=vehiclePhotoGallery&ajax=true&layout=empty&vehicleID='+$('a#enlarge').attr('vin')+'&image='+selectedImage+'&iframe=true&width=510&height=525');
		$('a#more_images').attr('href','index.php?ado=vehiclePhotoGallery&ajax=true&layout=empty&vehicleID='+$('a#enlarge').attr('vin')+'&image='+selectedImage+'&iframe=true&width=510&height=525');
	}
//_________ end vehicle detail gallery onAfter _________\\
	function fetchVehicleNav(myType){
		$.ajax({
			url: "?ado=saturnVehicleNav&ajax=true&newUsed=" + myType,
			success: function(html){
				$("#vehicle_spec_buttons").after(html).remove();
				jsReload();
			}
		});
	}
//_________ end fetchVehicleNav function _________\\
	function jsReload(){
		$('div.spec-flyout').addClass('closed');
		$('#vehicle_spec_buttons a.spec').addClass('off');
	
		$('#vehicle_spec_buttons a.spec').click(function(){//in place of toggle
			if($(this).attr('id') == 'goto_saturn') return true; // new cars 'make' button goes to new cars page
		
			$("div.spec-flyout:visible").not($(this).next('div')).removeClass('open').addClass('closed').hide(500);
			$("a.spec").not($(this)).removeClass('on').addClass('off');
		
			if($($(this).next('div')).is('.open')){//add/remove class open/closed depending on visible state
				$(this).removeClass('on').addClass('off');
				$(this).next('div').removeClass('open').addClass('closed').hide(500);
			}
			else{
				$(this).removeClass('off').addClass('on');
				$(this).next('div').removeClass('closed').addClass('open').show(500);
			}
			return false;
		});
		$('div.spec-flyout span.close').click(function(){//same function, but with the 'X' button instead
			if($(this).parent().is('.open')){
				$(this).parent().prev().removeClass('on').addClass('off');
				$(this).parent().removeClass('open').addClass('closed').hide(500);
			}
			else{
				$(this).parent().prev().removeClass('off').addClass('on');
				$(this).parent().removeClass('closed').addClass('open').show(500);
			}
			return false;
		});
	}
//_________ end jsReload function _________\\