$(document).ready(function(){
$('.body').prepend('<div class="bodygutter"></div>');

//png fixing
var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

if (jQuery.browser.msie && (ie55 || ie6)) {
	
	$('.topInfo .fuel').css({
   	'background-image':'url("common/phpthumb/phpThumb.php?src=http://ww3.ldvis.com/audi_livermore/img/trim/fuel_logo.png&w=90&f=gif")'
   });
   
   $('.trim_container .factsbox').css({
   	'background-image':'url("common/phpthumb/phpThumb.php?src=http://ww3.ldvis.com/audi_livermore/img/trim/right_bg.png&w=8&f=gif")'
   });
	
	$('.results .output').css({
		'min-height':'50px',
		'height': 'auto !important',
		'height': '50px'
	});
	$('.drop').css({'background':'#d8dce7 url("") top left no-repeat','border':'1px solid #609abf'});
    
	jQuery(this).find("img[src$=.png]").each(function(){
    
        jQuery(this).attr('width', jQuery(this).width());
        jQuery(this).attr('height', jQuery(this).height());
        
        var prevStyle = '';
        var strNewHTML = '';
        var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
        var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
        var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
        var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
        var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
        var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
		var imgPos = $(this).css('position');
		var imgView = ($(this).css('display') === 'none') ? 'none' : 'inline-block';
        if (this.style.border) {
            prevStyle += 'border:' + this.style.border + ';';
            this.style.border = '';
        }
        if (this.style.padding) {
            prevStyle += 'padding:' + this.style.padding + ';';
            this.style.padding = '';
        }
        if (this.style.margin) {
            prevStyle += 'margin:' + this.style.margin + ';';
            this.style.margin = '';
        }
        var imgStyle = (this.style.cssText);
        
        strNewHTML += '<span ' + imgId + imgClass + imgTitle + imgAlt;
        strNewHTML += 'style="position:'+imgPos+';white-space:pre-line;display:'+imgView+';background:transparent;' + imgAlign + imgHand;
        strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
        strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
        strNewHTML += imgStyle + '"></span>';
        if (prevStyle != '') {
            strNewHTML = '<span style="position:relative;display:inline-block;' + prevStyle + imgHand + 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;' + '">' + strNewHTML + '</span>';
        }

        jQuery(this).after(strNewHTML);
		jQuery(this).remove();
    });
    
    $('body').find("*").each(function(){
        var bgIMG = $(this).css('background-image');
        if (bgIMG.indexOf(".png") != -1 && bgIMG.indexOf("&f=gif") == -1) {
            var iebg = bgIMG.split('url("')[1].split('")')[0];
			var content = $(this).html();
			var width = $(this).outerWidth();
			var height = $(this).outerHeight();
            $(this).css({
				'background-image': 'none',
				'background-position':'0px 0px'
			});
			$(this).prepend('<div class="bgcopy"></div>');
			$(this).find('.bgcopy').css({
				"position":"absolute",
				"width":width+'px',
				"height":height+'px',
				"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')",
				"top":"0px",
				"left":"0px",
				"z-index":"0"
			});
        }
    });

	$('body').find("input[src$=.png]").each(function() {
		var bgIMG = jQuery(this).attr('src');
		jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
   		jQuery(this).attr('src', 'img/blank.gif');
	});
}

	var pageName = $.url('all');

	var thisPage = $.url('fileExt');
	if($('.navbar a[href*="'+pageName+'"]').length!==0){
		var navLink = $('.navbar a[href*="'+pageName+'"]:first').parents('.link');
	}else if(pageName.indexOf('compare.php') != -1 || pageName.indexOf('trim.php') != -1 || pageName.indexOf('model.php') != -1){
		var navLink = $('.navbar a[href*="inventory.php?t=N"]:first').parents('.link');
	}else if($('.navbar a[href^="'+thisPage+'"]').length!==0){
		var navLink = $('.navbar a[href*="'+thisPage+'"]:first').parents('.link');
	}
	if(navLink && $(navLink).length !== 0){
		xPos = $(navLink).css('backgroundPosition').split(' ')[0];
		$(navLink).attr('id','active_tab').css('backgroundPosition',xPos+' -39px');
	}

	$('.navbar .link:not(#active_tab)').mouseenter(function(){
			theX = $(this).css('backgroundPosition').split(' ')[0];
			$(this).css('backgroundPosition',theX + ' -39px');
			$(this).find('.dropmenu:hidden').show(400);
        }).mouseleave(function(){
			$(this).stop(true,true);
			theX = $(this).css('backgroundPosition').split(' ')[0];
			$(this).css('backgroundPosition',theX + ' 0px');
			$(this).find('.dropmenu:visible').stop(false,true).hide(800);
    	});

	$('#active_tab').stop().mouseenter(function(){
			$(this).find('.dropmenu:hidden').show(400);
    	}).mouseleave(function(){
			$(this).find('.dropmenu:visible').stop(false,true).hide(800);
    	});

	$('.noFollow').bind('click',function(){
		return false;
	});

	if($('.sidequote:visible').length == 1){
		var bar = $('.sidequote');
		var barOffset = bar.offset();
		var upperLimit = 0;
		var lowerLimit = ($('.subbody').outerHeight() - bar.outerHeight() - upperLimit);
		
		if(lowerLimit < 0){
			lowerLimit = bar.outerHeight();
		}
	
		$(window).scroll(function(){
			var theScroll = $(this).scrollTop();
			var scrollStart = barOffset.top;
			
			if((theScroll > scrollStart) && (theScroll < (scrollStart + lowerLimit))){
				bar.stop().animate({
					marginTop: (upperLimit + (theScroll - scrollStart))+'px'
				},300);
			}
			else if(theScroll >= (scrollStart + lowerLimit)){
				bar.stop().animate({
					marginTop: (lowerLimit + upperLimit) +'px'
				},300);
			}
			else{
				bar.stop().animate({
					marginTop: upperLimit + 'px'
				},300);
			}
		});
	};
	var formDefaults = {'qfn':'* First Name','qln':'* Last Name','qp':'* Phone Number','qea':'* Email Address'};
	$('.lhpane input.text').focus(function(){
			if($(this).attr('value') == formDefaults[$(this).attr('id')]) $(this).attr('value','');
		}).blur(function(){
			if($(this).attr('value') == '') $(this).attr('value',formDefaults[$(this).attr('id')]);
		});
	$('.lhpane textarea').focus(function(){
		$(this).text('');
	});
	
	$('.lhpane #contact').submit(function(){
		$('.lhpane input.text').each(function(){
			if($(this).attr('value') == formDefaults[$(this).attr('id')]) $(this).attr('value','');
		});
	});

});
