//Used on Results Classified, and all Profile pages
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function ShowShadowBox(id) {
		var isIE6 = !$.support.opacity && !window.XMLHttpRequest	
		
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});		
		//hides select boxes not contained inside of pop up so they don't show through pop up
		if (isIE6) {
				$('select:not(#popupBoxes select)').filter(function() {
					return this.style.visibility !== 'hidden';
				}).css({'visibility':'hidden'});
				// fixes opacity on drop shadows
				$('.popShadow').fixPNG();
			}
			
		//transition effect	mask
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
		//transition effect wrapper
		$(id).fadeIn(2000);	
		
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
		var idH = $(id).height()
		var idW = $(id).width()
		var idH2 = $(id).innerHeight()
		var idW2 = $(id).innerWidth()
		//width/height of inner div
		var innerH = $('#popShadowIframe').height();		
		var innerW = $('#popShadowIframe').width();
		//and position of inner div
		var position = $('#popShadowIframe').position();
		var innerTopH = $('.popupInnerTop', id).innerHeight();
		
		var innerBH = innerH - innerTopH - (innerTopH-$('.popupInnerTop', id).height());
		/*now determine difference between width/height of inner div and wrap, 
		add position*2 to give equal margin all around inner div
		*/
		var idnewW = (innerW-idW)+idW;
		var idnewH = (innerH-idH)+idH+(position.top*2)-innerTopH+3;
		
		//Set the popup window to center and give new height/width
		//$('.popupInnerBottom', id).css({'height':innerBH});
		$('.popupInner', id).css({'width':innerW,'height':innerH});
		$(id).css({'width':idnewW,'height':idnewH});	
		if($.browser.safari){ bodyelem = $("body") } else{ bodyelem = $("html") } 
		if($(bodyelem).scrollTop() > 0) {
			$(id).css({'top': $(bodyelem).scrollTop()+(winH/2-idnewH/2),'left': winW/2-idnewW/2});
		} else {
			$(id).css({'top': winH/2-idnewH/2,'left': winW/2-idnewW/2});
		}
}

$(document).ready(function () {
    //Character Counter for price area of Results Block-Classified and Profile pages
    $('.Price1').each(function () {
        // get current number of characters
        var length = $(this).text().length;
        //determine if on Block or Classified-Profile
        if ($(".Price1Title", this).length != 0) {
            ptitleLength = $(".Price1Title", this).text().length;
        } else {
            ptitleLength = $(".PriceTitle", this).text().length
        }
        // get current number of characters     
        var totalLength = length - ptitleLength;
        // add special class if characters are too long
        if (totalLength > 9) {
            $(this).addClass("priceoverflow");
        }
    });
	
	//initiates the tipster
	$(".tipster").tooltip({
		track: true,
		delay: 0,
		showURL: false,
		fade: 250,
		left: -5,
		top: 20
	});	
	
	//If Comparison Queue empty, hide it
	if ($('#ComparisonQueue li').length == 0)
		$('#ComparisonQueue').hide();	
	
	//removes elements from Comparison Queue
	$('#ComparisonQueue li a').click(function() {
		$(this).parents('li:eq(0)').hide('slow',  function () {
	       $(this).remove();

		   //If there are no more items, hide the comaparison block
			if ($('#ComparisonQueue li').length == 0)
				$('#ComparisonQueue').hide(300);					
		 });
	});

	//Fixes IE6 PNG opacity
	var isIE6 = !$.support.opacity && !window.XMLHttpRequest	
	$.fn.fixPNG = function() {
		return this.each(function () {
			var image = $(this).css('backgroundImage');

			if (image.match(/^url\(["']?(.*\.png)["']?\)$/i)) {
				image = RegExp.$1;
				$(this).css({
					'backgroundImage': 'none',
					'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=" + ($(this).css('backgroundRepeat') == 'no-repeat' ? 'crop' : 'scale') + ", src='" + image + "')"
				}).each(function () {
					var position = $(this).css('position');
					if (position != 'absolute' && position != 'relative')
						$(this).css('position', 'relative');
				}).css('zoom', 1);
			}
		});
	};
	
	// Show/Hide Side results search elements
	$("dl span.showHide").click(function () {	
		// test if it is already expanded
		if($(this).hasClass("Minus")){
			// if it's already expanded, turn minus into plus
			// hide the content
			$(this).removeClass("Minus");	
			$("~ dd", this).addClass("Hidden");
		} else {
			// if it's not expanded, turn plus into minus
			// and expand the content
			$(this).addClass("Minus");
			$("~ dd", this).removeClass("Hidden");
		}
		return false;	  
	});
	
	// for pop-up "Photos" tab and Profile-Media, organizes smaller photos
	$.fn.sliderWrap = function() {
		//return this.each(function () {
			var slider = $(this);
			var ulWidth = slider.find("ul").innerWidth();
			var ulCount = slider.find("ul").size();
			var ulContainerWidth = ulWidth*ulCount;	
			slider.find("div").css({'width':ulContainerWidth});
			// Surround the collection of uls with a container div (wide enough for all ul to be lined up end-to-end)
			//$('ul', sliderWrap).wrapAll('<div class="ul-container"></div>');
			// Specify the width of the container div (wide enough for all ul to be lined up end-to-end)
			//$(".ul-container", sliderWrap).css({ width: ulContainerWidth });
		//});
	};		
	
	//Launch Modal Window
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Get the A tag
		var id = $(this).attr('href');		
		
		//Cancel the link behavior
		e.preventDefault();		
				
		ShowShadowBox(id);
		
		//organizes smaller photos on Media-Photos
		$(".invResultsMediaCarRight", id).sliderWrap();
		
	});	
	
	
	// When a tab in pop-up is clicked, switches between active tabs and shows/hides content
	$(".invResultsMediaTabs li").click(function () {		
		// switch all tabs off
		$(".active").removeClass("active");		
		// switch this tab on
		$(this).addClass("active");		
		// slide all elements with the class 'content' up
		$(".popupInnerBottomContent").hide('fast');		
		// Now figure out what the 'title' attribute value is and find the element with that id.  Then slide that down.
		var content_show = $(this).attr("title");
		$("#"+content_show).show('fast');	
		$(".invResultsMediaCarRight").sliderWrap();	
		return false;	  
	});
	
	//if close button is clicked
	$('.popupWrap .closePopup').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.popupWrap').hide();
		
		//show select boxes again
		if (isIE6) {
				$('select:not(#popupBoxes select)').filter(function() {
					return this.style.visibility !== 'visible';
				}).css({'visibility':'visible'});
			}
			
		//Clear Pop Shadow Iframe			
		window.frames["popShadowIframe"].location.href = 'about:blank';			
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.popupWrap').hide();
		//show select boxes again
		if (isIE6) {
				$('select:not(#popupBoxes select)').filter(function() {
					return this.style.visibility !== 'visible';
				}).css({'visibility':'visible'});
			}
			
		//Clear Pop Shadow Iframe			
		window.frames["popShadowIframe"].location.href = 'about:blank';			
	});			
	
});

function getPrintBrochureURL(p_sVIN, p_sDBName){
	var width = screen.availWidth
	var height = screen.availHeight
	var screenX
	var screenY
	var sRate;
	var sMonthNum
	var sPrice;
	var sCash;
	var sMonthPay;
	var sCalcParams;

	if (document.getElementById("monthPay"))
		sMonthPay = document.getElementById("monthPay").innerHTML;
	else
		sMonthPay = '';
	if 	(document.getElementById("carPrice"))				
		sPrice = $("#carPrice").val();
	else
		sPrice = '';
	if (document.getElementById("down_payment"))
		sCash  = $("#down_payment").val();
	else
		sCash = '';
	if (document.getElementById("Rate"))	
		sRate = $("#Rate").val();
	else
		sRate = ''
	if (document.getElementById("term"))
		sMonthNum = $("#term").val();
	else
		sMonthNum = '';

	sCalcParams = "&mPayment=" + sMonthPay + "&price=" + sPrice + "&cash=" + sCash  + "&month=" + sMonthNum  + "&rate=" + sRate
	screenX = width / 2 - 400
	screenY = height / 2 - 350
       
	if (screenX < 0){
		screenX = 0
	}
    if (screenY < 0) {
		screenY = 0
	}
       
	return "/inventory/includes/printprofile.asp?vin=" + p_sVIN + sCalcParams + "&db=" + p_sDBName;
}

