$(function() {
	$("a[rel^=Lightbox]").lightBox();
});

$(document).ready(function(){
	// Read More link for Featured Employee
	if($('#TextInner').height() > $('#TextOuter').height()) {
		//alert('test');
		$('#ReadMoreLink').css('display', 'block');
	}

	// Site Search
	$("#Query").focus(function(){
		if($(this).attr("value") == "Site Search") $(this).attr("value", "");
	});
	$("#Query").blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", "Site Search");
	});
	
	// Menu Bar
	$(".MainMenu").each(function(index) {
		var ParentWidth = $(this).outerWidth() / 2;
		var ChildWidth = $(this).children(".SubmenuContainer").outerWidth() / 2;
		$(this).children(".SubmenuContainer").css('left', (ParentWidth - ChildWidth) + 'px');
	});
	$(".MainMenu").hover(function() {
		$(this).css("background", "transparent url('http://www.preferredautogroup.com/Templates/Default/gfx/HeaderBar_HoverBackground.png') left bottom repeat-x scroll");
		$(this).children(".SubmenuContainer").css('display', 'block');
	}, function() {
		$(this).css("background-image", "none");
		$(this).children(".SubmenuContainer").css('display', 'none');
	});
	/*
	$(".MainMenu").mouseover(function() {
		//$(this).css("background-color", "#000000");
		$(this).css("background", "transparent url('/Templates/Default/gfx/HeaderBar_HoverBackground.png') left bottom repeat-x scroll");
		$(this).children(".SubmenuContainer").css('display', 'block');
	});
	$(".MainMenu").mouseout(function() {
		$(this).css("background-image", "none");
		$(this).children(".SubmenuContainer").css('display', 'none');
	});
	*/
	$(".SubmenuDealer").mouseover(function(){
		$(this).css("background-color", "#3b3b3b");
	});
	$(".SubmenuDealer").mouseout(function(){
		$(this).css("background-color", "transparent");
	});
	
	$(".SubmenuItem").mouseover(function(){
		$(this).css("background-color", "#3b3b3b");
	});
	$(".SubmenuItem").mouseout(function(){
		$(this).css("background-color", "transparent");
	});
	
	// Auto Search
	$("#ZipCode").focus(function(){
		if($(this).attr("value") == "Your ZIP") $(this).attr("value", "");
	});
	$("#ZipCode").blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", "Your ZIP");
	});
	
	$("#InputCertified").toggle(function(){
		$(this).addClass("InputCheckboxChecked");
		$("#Certified").attr("value", 1);
	}, function(){
		$(this).removeClass("InputCheckboxChecked");
		$("#Certified").attr("value", 0);
	});
	
	$("#Body").focus(function(){
		if($(this).attr("value") == "Body") $(this).attr("value", "");
	});
	$("#Body").blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", "Body");
	});
	
	$("#Engine").focus(function(){
		if($(this).attr("value") == "Engine") $(this).attr("value", "");
	});
	$("#Engine").blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", "Engine");
	});
	
	$("#Transmission").focus(function(){
		if($(this).attr("value") == "Transmission") $(this).attr("value", "");
	});
	$("#Transmission").blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", "Transmission");
	});
	
	$("#DriveTrain").focus(function(){
		if($(this).attr("value") == "Drive Train") $(this).attr("value", "");
	});
	$("#DriveTrain").blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", "Drive Train");
	});
	
	$("#StockID").focus(function(){
		if($(this).attr("value") == "Stock ID") $(this).attr("value", "");
	});
	$("#StockID").blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", "Stock ID");
	});
	
	$(".InputSelect").toggle(function(){
		$(this).children(".OptionList").slideDown();
	}, function(){
		$(this).children(".OptionList").slideUp();
	});
	
	$(".Option").hover(function(){
		$(this).css("background-color", "#eeeeee");
	}, function(){
		$(this).css("background-color", "transparent");
	});
	
	$(".Option").click(function(event){
		$(this).parent(".OptionList").parent(".InputSelect").children(".Chosen").html($(this).html());
		if($(this).parent(".OptionList").css("display") == "block") $(this).parent(".OptionList").slideUp();
		if($(this).parent(".OptionList").parent(".InputSelect").attr("id") == "InputMake") {
			if($(this).html() == "Vehicle Make") {
				$("#" + str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id'))).attr("value", "");
				$("#InputModel").html("<div class='Chosen'>Vehicle Model</div><div class='OptionList' style='z-index:490;'><div class='Option' id='Model'>Vehicle Model</div></div>");
				$("#Model").attr("value", "");
			} else {
				$("#" + str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id'))).attr("value", $(this).html());
			}
		} else if($(this).parent(".OptionList").parent(".InputSelect").attr("id") == "InputDealer") {
			if($(this).html() == "Dealer Location") {
				$("#" + str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id'))).attr("value", "");
				$("#InputDealer").html("<div class='Chosen'>Dealer Location</div><div class='OptionList' style='z-index:520;'><div class='Option' id='Dealer'>Dealer Location</div></div>");
				$("#Dealer").attr("value", "");
			} else {
				$("#" + str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id'))).attr("value", $(this).html());
			}
		} else if($(this).parent(".OptionList").parent(".InputSelect").attr("id") == "InputModel") {
			if($(this).html() == "Vehicle Model") {
				$("#" + str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id'))).attr("value", "");
			} else {
				$("#" + str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id'))).attr("value", str_replace(str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id')), "", str_replace("_", " ", $(this).attr("id"))));
			}
		} else {
			if($(this).html() == "Category") {
				$("#" + str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id'))).attr("value", "");
			} else {
				$("#" + str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id'))).attr("value", str_replace(str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id')), "", str_replace("_", " ", $(this).attr("id"))));
			}
		}
		
		if($(this).parent(".OptionList").parent(".InputSelect").attr("id") == "InputMake") {
			// Retrieve Model list for selected Make
			var Parms = {};
			Parms['ID']			= str_replace("Make", "", $(this).attr("id"));
			Parms['Category']	= str_replace("Category", "", $('#InputCategory > .Chosen').html());
			CAJAX.Add("Makes", "Module", "GetModels", Parms, function(Code, Content){
				if(Code == 0) {
					//alert(Content);
				} else {
					var Models = JSON.parse(Content);
					var ModelList = "<div class='Chosen'>Vehicle Model</div><div class='OptionList' style='z-index:490;'><div class='Option' id='Model'>Vehicle Model</div>";
					for (var ID in Models) {
						ModelList += "<div class='Option' id='Model" + str_replace(" ", "_", Models[ID]) + "'>" + Models[ID] + "</div>";
					}
					ModelList += "</div>";
					$("#InputModel").html(ModelList);
					$("#InputModel").toggle(function(){
						$(this).children(".OptionList").slideDown();
					}, function(){
						$(this).children(".OptionList").slideUp();
					});
					$("#InputModel").children(".OptionList").children(".Option").hover(function(){
						$(this).css("background-color", "#eeeeee");
					}, function(){
						$(this).css("background-color", "transparent");
					});
					$("#InputModel").children(".OptionList").children(".Option").click(function(event){
						$(this).parent(".OptionList").parent(".InputSelect").children(".Chosen").html($(this).html());
						$(this).parent(".OptionList").slideUp();
						if($(this).html() == "Vehicle Model") {
							$("#" + str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id'))).attr("value", "");
						} else {
							$("#" + str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id'))).attr("value", str_replace(str_replace('Input', '', $(this).parent('.OptionList').parent('.InputSelect').attr('id')), "", str_replace("_", " ", $(this).attr("id"))));
						}
						event.stopPropagation();
					});
				}
			});
		}
		
		event.stopPropagation();
	});
	
	$('#AutoSearch').submit(function(){
		if($('#ZipCode').attr('value') == "Your ZIP") $('#ZipCode').attr('value', '');
		if($('#Body').attr('value') == "Body") $('#Body').attr('value', '');
		if($('#Engine').attr('value') == "Engine") $('#Engine').attr('value', '');
		if($('#Transmission').attr('value') == "Transmission") $('#Transmission').attr('value', '');
		if($('#DriveTrain').attr('value') == "Drive Train") $('#DriveTrain').attr('value', '');
		if($('#StockID').attr('value') == "Stock ID") $('#StockID').attr('value', '');
		return true;
	});
	
	
	
	// Vehicle Page
	$('#LetsTalk .Textarea').focus(function(){
		if($(this).val() == "Ask a Question or Enter Your Message Here") $(this).val("");
	});
	$('#LetsTalk .Textarea').blur(function(){
		if($(this).val() == "") $(this).val("Ask a Question or Enter Your Message Here");
	});
	
	$('#LetsTalk div.Checkbox').toggle(function(event){
		$(this).addClass('CheckboxChecked');
		$(this).children('.InputCheckbox').attr('checked', 'checked');
		event.stopPropagation();
	}, function(event){
		$(this).removeClass('CheckboxChecked');
		$(this).children('.InputCheckbox').removeAttr('checked', '');
		event.stopPropagation();
	});
	
	
	// Stay In Touch form
	$("#StayInTouch div.Checkbox").click(function(event){
		if($(this).children("input").attr("checked") == true) {
			$(this).removeClass("CheckboxChecked");
			$(this).children("input").attr("checked", false);
		} else {
			$(this).addClass("CheckboxChecked");
			$(this).children("input").attr("checked", true);
		}
		event.stopPropagation();
	});
	$('#StayInTouch').submit(function(){
		if($('#StayInTouch #phETacRETrazewaC').attr("value") == '') {
			alert("Please enter an Email Address");
			$('#StayInTouch #phETacRETrazewaC').focus();
			return false;
		}
		return true;
	});
	
	// Let's Talk form
	$('#LetsTalk').submit(function(){
		if($('#LetsTalk #FirstName').attr("value") == '') {
			alert("Please enter a First Name");
			return false;
		}
		if($('#LetsTalk #LastName').attr("value") == '') {
			alert("Please enter a Last Name");
			return false;
		}
		if($('#LetsTalk #EmailAddress').attr("value") == '') {
			alert("Please enter an Email Address");
			return false;
		}
		/*
		if($('#LetsTalk #PhoneDay').attr("checked") == '' && $('#LetsTalk #PhoneEvening').attr("checked") == '') {
			alert("Please select Day or Evening");
			return false;
		}
		if($('#LetsTalk #Phone1').attr("value") == '' || $('#LetsTalk #Phone2').attr("value") == '' || $('#LetsTalk #Phone3').attr("value") == '') {
			alert("Please enter a Phone Number");
			return false;
		}
		*/
		if($('#LetsTalk #ZipCode').attr("value") == '') {
			alert("Please enter a Zip Code");
			return false;
		}
		return true;
	});
	
});

SelectThumbnail = function(Image, Title, Summary, URL) {
	$("#ThumbnailSelected").html("<img src='" + Image + "' style='margin:0px; padding:0px; border:1px solid #999999;'>");
	if(URL != "") {
		Title = "<a href='" + URL + "' style='color:#666666;'>" + Title + "</a>";
	}
	$("#ThumbnailTitle").html(Title);
	$("#ThumbnailSummary").html(Summary);
}

VerifyAppType = function() {
	for (var i = 0; i < document.FinanceApp.AppType.length; i++) {
		if (document.FinanceApp.AppType[i].checked) {
			return true;
		}
	}
	alert("Please select an Application Type");
	return false;
}

VerifyAppRequired = function() {
	if($('#FirstName').val() == "") {
		alert("Please enter your First Name");
		return false;
	} else if($('#LastName').val() == "") {
		alert("Please enter your Last Name");
		return false;
	} else if($('#Email').val() == "") {
		alert("Please enter your Email Address");
		return false;
	} else if($('#HomePhone').val() == "") {
		alert("Please enter your Home Phone");
		return false;
	} else if($('#DateOfBirth').val() == "") {
		alert("Please enter your Date Of Birth");
		return false;
	} else if($('#SocialSecurityNo').val() == "") {
		alert("Please enter your Social Security Number");
		return false;
	} else if($('#DriversLicenseNo').val() == "") {
		alert("Please enter your Drivers License Number");
		return false;
	} else if($('#DriversLicenseState').val() == "") {
		alert("Please select your Driver License State");
		return false;
	} else if($('#Address').val() == "") {
		alert("Please enter your Address");
		return false;
	} else if($('#City').val() == "") {
		alert("Please enter your City");
		return false;
	} else if($('#County').val() == "") {
		alert("Please enter your County");
		return false;
	} else if($('#State').val() == "") {
		alert("Please select your State");
		return false;
	} else if($('#ZipCode').val() == "") {
		alert("Please enter your Zip Code");
		return false;
	} else if($('#MortgageCoLandlord').val() == "") {
		alert("Please enter your Mortgage Company / Landlord");
		return false;
	} else if($('#HomeType').val() == "") {
		alert("Please select your Home Type");
		return false;
	} else if($('#RentMortgagePayment').val() == "") {
		alert("Please enter your Rent / Mortgage Payment");
		return false;
	} else if($('#YearsAtRes').val() == "") {
		alert("Please enter your Years At Res");
		return false;
	} else if($('#MonthsAtRes').val() == "") {
		alert("Please enter your Months At Res");
		return false;
	} else if($('#CurrentEmployer').val() == "") {
		alert("Please enter your Current Employer");
		return false;
	} else if($('#Occupation').val() == "") {
		alert("Please enter your Occupation");
		return false;
	} else if($('#BusinessPhone').val() == "") {
		alert("Please enter your Business Phone");
		return false;
	} else if($('#YearsOnJob').val() == "") {
		alert("Please enter your Years On Job");
		return false;
	} else if($('#MonthsOnJob').val() == "") {
		alert("Please enter your Months On Job");
		return false;
	} else if($('#MonthlyIncome').val() == "") {
		alert("Please enter your Monthly Income");
		return false;
	} else if($('#MothersMaidenName').val() == "") {
		alert("Please enter your Mother's Maiden Name");
		return false;
	}
	
	return true;
}

VerifyServiceApptRequired = function() {
	if($('#FirstName').val() == "") {
		alert("Please enter your First Name");
		return false;
	} else if($('#LastName').val() == "") {
		alert("Please enter your Last Name");
		return false;
	} else if($('#Email').val() == "") {
		alert("Please enter your Email Address");
		return false;
	} else if($('#Date').val() == "") {
		alert("Please enter a requested Date");
		return false;
	} else if($('#Time').val() == "") {
		alert("Please enter a requested Time");
		return false;
	} else if($('#Year').val() == "") {
		alert("Please enter the vehicle Year");
		return false;
	} else if($('#Make').val() == "") {
		alert("Please enter the vehicle Make");
		return false;
	} else if($('#Model').val() == "") {
		alert("Please enter the vehicle Model");
		return false;
	}
	
	return true;

}

// Auto-scroll thumbnails on Vehicle page
AutoScroll = function(i) {
	if($('#Vehicle' + i).length == 0) {
		i = 0;
	}
	
	$('#Vehicle' + i).click();
	
	setTimeout('AutoScroll(' + (i + 1) + ')', 6000);
}

// Auto-scroll thumbnails on all other pages
AutoScrollThumbnails = function(i) {
	if($('#Thumbnail' + i).length == 0) {
		i = 0;
	}
	
	$('#Thumbnail' + i).click();
	
	setTimeout('AutoScrollThumbnails(' + (i + 1) + ')', 6000);
}
