$(function(){
	if ( !($.browser.msie && /msie 6.0/i.test(navigator.userAgent)) ) {
		$.fn.bgIframe = $.fn.bgiframe = function(s) {};
	}
	
	if ($.browser.msie) { // if IE
		/* we have trouble detecting onmouseout on flash when the mouse leaves the stage, 
		 * so we need to set an onmouseout listener with javascript to the flash container 
		 * and have it notify flash that the mouse has left
		 */
		$('#top-nav-flashcontent').bind('mouseout', topNavStageMouseOut);
	}
	
	
	// SUBNAV
	$('.subnav li').hover(
			function(){
				return navlink_over_out(this, true);
			}, 
			function() { 
				return navlink_over_out(this, false); 
			} 
	);
});

//
var topnavouttimer;
function topNavStageMouseOut() {
	var topnavflash;
	if ((topnavflash = getFlashMovieObject('topnav')) && topnavflash.onStageMouseOut) {
		topnavflash.onStageMouseOut();
	} else {
		clearTimeout(topnavouttimer);
		topnavouttimer = setTimeout(topNavStageMouseOut, 100);
	}
}

// update the left nav flash
function setLeftNavFlashActiveItem(label) {
	var leftNavFlash = getFlashMovieObject('leftnav');
	if (!leftNavFlash) {
		//alert('no flash');
		setTimeout(function() { setLeftNavFlashActiveItem(label); }, 100);
		return;
	}
	if (!leftNavFlash.leftNavSetActiveItem) {
		//alert('no flash callback');
		setTimeout(function() { setLeftNavFlashActiveItem(label); }, 100);
		return;
	}
	
	leftNavFlash.leftNavSetActiveItem(label);
	//alert('called func with "' + label + '"');
}

// ******************
//		thumbs
// ******************
var thumbRollDelay = 0; // the time to wait before switching to a new thumb
var selectThumbOffDelay = 0; // the time to wait before turning off the select thumb effect
var selectThumbOffTimer = null;
var thumbRollTimer = null;

// this gets called when the entire thumb area is moused-over or moused-out
function do_select_thumb(year, mode) {
	if (mode) {
		// put thumbs area into 'select-one' mode
		clearTimeout(selectThumbOffTimer);
		$('#model-thumbs-'+year+'>div').addClass("select");
	}else{
		// selectThumbOffDelay is 0 by default, however it can be overridden by other pages, such as in index.js
		// this results in a delay onmouseout of the thumbs area
		if (selectThumbOffDelay) {
			selectThumbOffTimer = setTimeout(function(){ $("#model-thumbs-"+year+">div").removeClass("select"); }, selectThumbOffDelay);
		} else {
			// don't wait
			$('#model-thumbs-'+year+'>div').removeClass('select');
		}
	}
}

// this is called onmouseover for each thumb.
function thumbRollModel(model) {
	if(model) {
		$('.thumb-specs a').removeClass("over");
		$('.thumb-specs .'+model).addClass("over");
	}
}

// Only allow numbers, return, and backspace in a text field.  Usage: onKeyPress="return only_numbers(event);"
function only_numbers(event) {
	var key;
	if (window.event) {
		key = window.event.keyCode;
	} else if (event) {
		key = event.which;
	} else {
		return true;
	}
	if(key >= 48 && key <= 57 || key == 8 || key == 13) {
		return true;
	} else {
		return false;
	}
}

// begin flash variable passer
function flashPassVar(varName, varValue, movieName) {
	var flashMovie = getFlashMovieObject(movieName)
	if (flashMovie)
		flashMovie.SetVariable(varName, varValue);
}
	
function flashGetVar(varName, movieName) {
	var flashMovie = getFlashMovieObject(movieName);
	return flashMovie.GetVariable(varName);
}

function getFlashMovieObject(movieName) {
	if (window.document[movieName]) {
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1) {
		if (document.embeds && document.embeds[movieName]) {
			return document.embeds[movieName]; 
		}
	} else { // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
		return document.getElementById(movieName);
	}
}
// end flash variable passer

// interior pages title header
function do_title(title, elem, align, html) {
	elem = elem || "page-title"; // default value for elem?
	var so = new SWFObject(ROOT_DIR+"_m/flash/pagetitle.swf", "flash-page-title-"+elem.replace(/^[^0-9]*/,''), "690", "22", "9", "#cccccc");
	so.addVariable("page_title", title);
	so.addVariable("root_dir", ROOT_DIR);
	so.addVariable("fonts", "_m/flash/all.swf");
	so.addVariable("align", align);
	so.addVariable("htmlParser", html ? 1 : 0);
	so.addParam("wmode", "transparent");
	so.addParam("quality", "high");
	so.addParam("allowScriptAccess", "always");
	so.write(elem);
}

// brochures
function popBrochure(model, year) {
	window.open('http://static.dealertown.com/dashboard/dealerSites/defaultSuzukiDealer/auto/_m/sr_' + year + '/' + model + '/brochures/' + year + '_' + model + '.pdf', 'brochure', 'height=500px, width=630px, resizable=1');
}

// specials picture popup
function popupWin(page, width, height) {
	winprops = "toolbar=0,status=1,scrollbars=1,resizable=1,width=" + width + ", height=" + height;
	popup = window.open( page, "popup", winprops);
	popup.focus();
}

// subnav hovering for models feature gallery and specials
function navlink_over_out(element, over) {
	var classId = element.className.split(" ")[0]; // assume first part of the class name is the part we care about "interior", "exterior"
	var subnavSpan = $("."+classId+" span");
	if (!subnavSpan.hasClass('cur_clicked')) {
		if (over) {
			subnavSpan.addClass('cur');
		} else {
			subnavSpan.removeClass('cur');
		}
	}
}

function showSpecial(section) {
	sections = ['new_vehicle_specials', 'used_vehicle_specials', 'service_specials', 'parts_specials'];
	
	for (i = 0; i < sections.length; i++) {
		if (section != 'all') {
			if (sections[i] == section) $('#' + sections[i]).show();
			else $('#' + sections[i]).hide();
		} else {
			$('#' + sections[i]).show();
		}
	}
	
	$('#specials li span.cur_clicked').removeClass('cur_clicked');
	$('#specials li span.cur').removeClass('cur');
	$('#specials li span.' + section).addClass('cur_clicked');
}

function resizeMovie(width, height) {
	$('#leftnav').width(width);
	$('#leftnav').height(height);
}

function closeNote() {
	$('#special-note').hide();
}

function linkaway(theLink) {
	var msg = "";
	msg += "You are now leaving American Suzuki Motor Corporation's Website, and entering an independent dealer site.\n";
	msg += "\n";
	msg += "American Suzuki is not responsible for the content presented by any independent Suzuki dealer, including advertising claims, special offers, illustrations, names or endorsements.\n";
	msg += "\n";
	msg += "Thank you for visiting our website. Please visit again soon.\n";
	var decision = confirm(msg);
	if (decision == true){document.location = theLink;}
}

function setSubmitValues(type){
	if((type=='parts')||(type=='loanApp')){
		var currentYear=document.getElementById("modelYear").value;
		document.getElementById("modelName").value=document.getElementById("modelName_"+currentYear).value;
	} else if (type=='qq'){
		document.getElementById("suzm").value= qqData[document.getElementById("suzm").value].id;
		document.getElementById("suzv").value= currentTrim;
		document.getElementById("selectedColorName").value=document.getElementById("thumbName").innerHTML;
	} else if(type=='service'){
		get_check_value(document.getElementById("serviceForm"));
		var currentYear=document.getElementById("modelYear").value;
		var currentDay=document.getElementById("preferredDay").value;
		var currentAltDay=document.getElementById("alternateDay").value;
		
		if (currentDay != '' && currentAltDay != '') {
			currentDay = changeDateToDay(currentDay);
			currentAltDay = changeDateToDay(currentAltDay);
		
			document.getElementById("preferredTime").value=document.getElementById("preferredTime_"+currentDay).value;
			document.getElementById("alternateTime").value=document.getElementById("alternateTime_"+currentAltDay).value;
			document.getElementById("modelName").value=document.getElementById("modelName_"+currentYear).value;
		}
	}
}

function displayMap(dealerId) {
	window.location = "http://www.dealertown.com/displayMaps.page?id=" + dealerId + "&pageName=map";
}