$(document).ready(function(){//maps

	$('.pageheader').divMap('#bubblemap');
	
	//gutter fixing
	
	$.gutterCheck = function(){
		if ($(window).width() < 1077) {
			$('.gutter').width($(window).width());
			var widthDiff = (1077 - $(window).width()) / 2;
			$('.gutter').css({
				'background-position': '-' + widthDiff + 'px 0px',
				'margin-left': '-' + ($(window).width() / 2) + 'px'
			});
		}
		else{
			$('.gutter').width(1077);
			$('.gutter').css({
				'background-position': '0px 0px',
				'margin-left': '-538px'
			});
		};
	};
	
//	$.gutterCheck();
	
	$(window).bind('resize',function(){
//		$.gutterCheck();
	});
	
	//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)) {
		
		$('.results .output').css({
			'min-height':'50px',
			'height': 'auto !important',
			'height': '50px'
		});
		$('.drop').css({'background':'#24292d url("") top left no-repeat','border':'1px solid #4f5c65'});
	    
		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;' : '';
	        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:relative;white-space:pre-line;display:inline-block;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).hide();
	        jQuery(this).after(strNewHTML);
	        
	    });
	    
	    
	    $('body').find("*").each(function(){
	        var bgIMG = $(this).css('background-image');
	        if (bgIMG.indexOf(".png") != -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='crop')",
					"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');
		});
	}
	
	//active nav
	
	var pageName = $.url('all');
	
	var tabs = new Array();
	
	$('.navbar .link').each(function(){
	    var linkIndex = $('.navbar .link').index(this);    
	    	
		if (pageName.indexOf('index.php') == -1 && pageName.indexOf('mycars.php') == -1 && pageName.indexOf('specials.php') == -1 && pageName.indexOf('inventory.php') == -1) {
	            var theLink = $(this).find('a:eq(0)').attr('href');
	            if (theLink.indexOf(pageName) != -1) {
	                tabs.push(linkIndex);
	            }
	    }
		else if (pageName.indexOf('mycars.php') != -1){
			tabs.push('5');
		}
		else if (pageName.indexOf('specials.php?t=New') != -1){
			tabs.push('3');
		}
		else if (pageName.indexOf('specials.php?t=Used') != -1){
			tabs.push('4');
		}
		else if (pageName.indexOf('compare') != -1){
			tabs.push('7');
		}
		else if (pageName.indexOf('inventory.php') != -1){
			var carType = $('.invis .ident').text();
			if(carType == 'New'){
				tabs.push('1');
			}
			else{
				tabs.push('2');
			}		
		}
	    else {
	        tabs.push('0');
	    }
	});

	var lastEdit = tabs.pop();
	
	$('.navbar .link:eq(' + lastEdit + ')').css('color', '#ffc066');

	//logo panel
	$('.logo_panel .logo').hover(function(){
			var toplogos = 'buickto-sscad-lotusto-saabto-hummerto-jagto-landto';
			var tPos = (toplogos.indexOf( $(this).attr('id') ) !== -1)?9:99;
			$(this).css('backgroundPosition', '-'+$(this).css('left')+' -'+(tPos + 251)+'px');
		},function(){
			var toplogos = 'buickto-sscad-lotusto-saabto-hummerto-jagto-landto';
			var tPos = (toplogos.indexOf( $(this).attr('id') ) !== -1)?'-9px':'-99px';
			$(this).css('backgroundPosition', '-'+$(this).css('left')+' '+tPos );
		}
	);
	//logo panel revised
	$('.logo_panel2 .logo').hover(function(){
			var toplogos = 'buickto-sscad-lotusto-lexusto-jagto-landto';
			var tPos = (toplogos.indexOf( $(this).attr('id') ) !== -1)?9:99;
			$(this).css('backgroundPosition', '-'+$(this).css('left')+' -'+(tPos + 251)+'px');
		},function(){
			var toplogos = 'buickto-sscad-lotusto-lexusto-jagto-landto';
			var tPos = (toplogos.indexOf( $(this).attr('id') ) !== -1)?'-9px':'-99px';
			$(this).css('backgroundPosition', '-'+$(this).css('left')+' '+tPos );
		}
	);

	
	//nav
	
	$('.link:has(.drop)').hover(function(){
		menu = $(this).find('.drop');
		$(menu).show();
	},function(){
		menu = $(this).find('.drop');
		$(menu).hide()
	});
	
	$('.noFollow').bind('click',function(){
		return false;
	});
	
	$('input[type=text],textarea').data('selected','false');
	
	$.selectDefine = function(){
		$('input[type=text]:data(selected=false),textarea:data(selected=false)').bind('focus',function(){
			$(this).css('border-color', '#dd7e00');
		});
		
		$('input[type=text]:data(selected=false),textarea:data(selected=false)').bind('blur',function(){
			$(this).css('border-color', '#d3d2d7');
		});
	};
	
	$.selectDefine();
	
	$('input[type=text],textarea').bind('click',function(){
		$('input[type=text]:data(selected=true),textarea:data(selected=true)').data('selected','false').css('border-color','#d3d2d7');
		$(this).data('selected','true').css('border-color','#dd7e00');
		$(this).unbind('mouseleave').unbind('mouseenter');
		$.selectDefine();
	});

});