// JavaScript Document

$(document).ready(function(){

	//Find browser for drop menu fixes
	var browser=navigator.appName;
	var version=parseFloat(navigator.appVersion);
	var IE6 = (browser == "Microsoft Internet Explorer" && version == "4" && window.XMLHttpRequest == null) ? true : false;
	
	//Landing page menu functionality-------------------------
	
	//if (!/Firefox[\/\s](2\.\d+)/.test(navigator.userAgent)){ //-----------<< removed animation, so check is no longer necessary

		$("#language_menu li ul").css({display:"none"});
		$("#language_menu li").hover(
			function () {
				$(this).find("ul").show();//slideDown(100);
			}, 
			function () {
				$(this).find("ul").hide();//slideUp(80);
			}
		);
	//}
	
	//Landing page flash--------------------------------------
	
	if($("#landing_flash").length > 0){
		var version = "9";
		var useInstall = true;
		
		if(navigator.platform == "MacPPC"){
			var version = "10";
			var useInstall = true;
		}
		
		var so = new SWFObject("LandingGallery.swf", "Landing Gallery", "100%", "100%", version, "#FFFFFF");
		if(useInstall) so.useExpressInstall('scripts/expressinstall.swf');
		so.addParam('wmode','transparent');
		so.write("landing_flash");
		
		if(so.getAttribute('doExpressInstall')){
			$("#landing_flash").css({height:"137px"});
		}
	}
	
	//Search menu functionality-------------------------------
	
	var searchFocus = false;
	var retainFocus = false;
	
	
	$("#head_nav .search").hover(
		function () {
			$(this).find(".scope").stop();
			$(this).find(".scope").css({height:"auto"});
			$(this).find(".scope").slideDown(100);
		}, 
		function () {
			if(searchFocus == false){
				$(this).find(".scope").stop();
				$(this).find(".scope").slideUp(90);
			}
		}
	)
	
	
	$("#google_search").focus(function(){
		searchFocus = true;
	});
	
	
	$("#google_search").blur(function(){
		searchFocus = false;
	});
	
	$("#head_nav .search").focus(function(){
		retainFocus = true;
	});
	
	$("#head_nav .search").blur(function(){
		if(retainFocus == false){
			$("#head_nav .search .scope").stop();
			$("#head_nav .search .scope").slideUp(90);
		}
		retainFocus = false;
	});
	
	//Stylize on click, remove any currently styled selectors
	$("#head_nav .search .scope label").click(function(){
		$("#head_nav .search .scope .selected").removeClass("selected");
		$(this).addClass("selected");
	});
	
	
	//Find initial value and stylize
	$("#head_nav .search .scope input:checked").parent().addClass("selected");
	
	
	//Search Box Functionality-------------------------------
	
	inputToggle($("#google_search"));
	
	//Dealer Locator Functionality-------------------------------
	
	inputToggle($("#dealer_search"));
	
	//Input Toggles---------------------------------------------
	
	inputToggle($(".input_toggle"));
	
	//Main Drop down functionality-------------------------------
	
	var current = 0;
	var total = $("#main_nav li .drop_wrapper").length;
	
	$("#main_nav li .drop_wrapper").each(function(){	
		var css = {}
	
		if(current > total*.5)
			css = {right:0,left:"auto"};
		
		current++;
		
		css.display = "none";
		$(this).css(css);
		$(this).append('<iframe style="width:'+$(this).width()+'px; height:'+$(this).height()+'px; border:none; position:absolute; top:0; left:0; z-index:1;"></iframe>');
	});
	
	$("#main_nav").children("li").hover(
		function () {
			$(this).find(".drop_wrapper").stop();
			$(this).find(".drop_wrapper").css({height:"auto",overflow:"visible"});
			$(this).find(".drop_wrapper").slideDown(100);
			if(IE6){
				$(this).children("a").css({color:"#FFFFFF",background:"#0088CE"});
				$(this).children("a").find("span").css({backgroundPosition:"top right"});
			}
		}, 
		function () {
			$(this).find(".drop_wrapper").stop();
			$(this).find(".drop_wrapper").slideUp(90);
			if(IE6){
				$(this).children("a").css({color:"#0088CE",background:"none"});
				$(this).children("a").find("span").css({backgroundPosition:"top center"});
			}
		}
	);
	
	//InternalDrop down functionality-------------------------------
	
	$("#internal_nav li.drop .drop_wrapper").each(function(){	
		$(this).css({display:"none"});
		$(this).append('<iframe style="width:'+$(this).width()+'px; height:'+$(this).height()+'px; border:none; position:absolute; top:0; left:0; z-index:1;"></iframe>');
	});
	
	$("#internal_nav li.drop .drop_wrapper").parent().hover(
		function () {
			$(this).find(".drop_wrapper").stop();
			$(this).find(".drop_wrapper").css({height:"auto",overflow:"visible"});
			$(this).find(".drop_wrapper").slideDown(100);
			if(IE6){
				$(this).children("a").css({color:"#FFFFFF",background:"#0088CE"});
				$(this).next().css({marginTop:-12});
			}
		}, 
		function () {
			$(this).find(".drop_wrapper").stop();
			if(IE6){
				$(this).find(".drop_wrapper").slideUp(90,function(){
					$(this).parent().children("a").css({color:"#666666",background:"none"});
					$(this).parent().next().css({marginTop:0});
				});
			}else
				$(this).find(".drop_wrapper").slideUp(90);
		}
	);
	
	//Drop shadows on the Drop Down Items-------------------------------
	
	$(".drop_wrapper li").hover(
		function (){
			$(this).next().css({background:"url(/images/home_navigation_drop_shadow_bottom.gif) repeat-x top"});
			$(this).prev().css({background:"url(/images/home_navigation_drop_shadow_top.gif) repeat-x bottom"});
		},
		function (){
			$(this).next().css({background:"none"});
			$(this).prev().css({background:"none"});
		}
	);
	
	//Let's see what we can get from google as far as suggestions...

	// Going to hold on the autosuggest - need to set up an asp.net proxy to serve up the results from google....talk to mike.
	
	/*
	$("#dealer_search").keyup(function(){
		suggestCheck();
	});
	*/
	
	//Set up local scrolls for anchors
	$('#content_wrapper').localScroll({
	   duration:200,
	   easing:'easeInOutQuad'
	});
	
		
	//Set up facebox popups
	$('.facebox').facebox({opacity:.5});
	
	//Create Panel Browsers
	
	if($("#spotlight_panels").length > 0)
		createPanelBrowser($("#spotlight_panels .jquery_panel_selectors"),$("#spotlight_panels .jquery_panel_selectees"));
	if($("#valley_values").length > 0)
		createPanelBrowser($("#valley_values .jquery_panel_selectors"),$("#valley_values .jquery_panel_selectees"));
	
	//News Functionality-------------------------------
	
	if($("#news li").length > 1){
		
		var news = $("#news_flash").html();
		var styles = 'p{color:#666666;}a:link{color:#0088ce;}a:hover{text-decoration:underline;}';
		
		$("#news_flash").mouseover(function(){
			thisMovie("news_flash_object").reset();
		});
		$("#news_flash").mouseout(function(){
			thisMovie("news_flash_object").restart();
		});
		
		var so = new SWFObject("News.swf", "news_flash_object", "100%", "100%", "9", "#FFFFFF");
		//so.useExpressInstall('scripts/expressinstall.swf'); // <--doesn't work so well with a 30px high stage...
		so.addParam('wmode','transparent');
		so.addVariable('news',escape(news));
		so.addVariable('styles',styles);
		so.write("news_flash");
		
	}else if($("#news li").length == 0){
		$("#news").css("display","none");
		$("#main_nav").css("margin","0 0 10px");
	}
	
	//Flash Brochure Category Browser Functionality----------------------------------
	$(".news_list li h4").toggle(
		function(){
			$(this).parent().addClass("open");
			$(this).parent().children(".intro").show("fast");
		},
		function(){
			$(this).parent().removeClass("open");
			$(this).parent().children(".intro").hide("fast");
		}
	);
	
	$("#news_room_brochures_menu a").click(function(){
		var target = $(this).attr("href");
		
		$("#news_room_brochures_menu li.at").removeClass("at").find(".at").fadeOut("fast");
		$("#news_room_brochures li.at").removeClass("at").fadeOut("slow");
		
		$(this).parent().addClass("at").find(".at").fadeIn("fast");
		$(target).addClass("at").fadeIn("slow");
		
		$(this).blur();
		return(false);
	});
	
	$(".flash_brochure_category a").each(function(){
		var id = Number($(this).attr("alt"));
		if(id > 0){
			loadBrochureCategory($(this).parent(),id);
		}
	});
});


//Flash Brochure Category Browser Functionality----------------------------------
		
function loadBrochureCategory(target,id){
	$(target).empty().append('<span class="loading"></span>')
	$.get("/flashBrochure/category.aspx", {cid:id},
		function(data){
			if(!embedFlash(target,data)){
				$(target).empty().append(parseCategoryHtml(data));
				//Set up facebox popups
				$(target).find('.facebox').facebox({opacity:.3});
			}
		}
	);
}

var flashEmbed = 1;
function embedFlash(target,data){
	$(target).empty().append('<div class="flash_brochure_category_flash" id="flash_brochure_category_embed'+flashEmbed+'">');
	var so = new SWFObject("/flashBrochure/FlashBrochureCategory.swf", "Brochure Category Gallery", "100%", "100%", "9", "#FFFFFF");
	so.useExpressInstall('/scripts/expressinstall.swf');
	so.addParam('wmode','transparent');
	so.addVariable("padding","15");
	so.addVariable("margin","20");
	so.addVariable("brochures",data);
	return so.write("flash_brochure_category_embed"+(flashEmbed++));
}

function parseCategoryHtml(data){
	var returnHTML = '<div class="flash_brochure_category_html"><div class="wrap">';
	var brochures = data.split(";");
	for(var brochureInt = 0; brochureInt < brochures.length - 1; brochureInt++){
		var brochure = brochures[brochureInt].split(",")
		returnHTML += '<a href="/flashBrochure/?flashBrochureId='+brochure[1]+'" class="facebox" title="'+brochure[0]+'">';
		returnHTML += '<img src="/flashBrochure/thumbnail.aspx?flashBrochureId='+brochure[1]+'" alt="'+brochure[0]+'"></a>';
	}
	returnHTML += "</div></div>";
	return returnHTML;
}

function openFacebox(url){
	jQuery.facebox({ajax:url});
}

//BEGIN:  used for locations map scroll arrows (Scott G)
$(document).ready(function(){
    var id = "0";
    $(".scrollarrow").mouseup(function(){ 
        id = $(this).attr("id");
        if (id.toString() == "leftarrow"){
            $("#themap").animate({marginLeft: "0px"}, 585);
            $("#leftarrow").fadeOut(585);
            $("#rightarrow").fadeIn(585);                        
        }
        if (id.toString() == "rightarrow"){
            $("#themap").animate({marginLeft: "-536px"}, 585);
            $("#leftarrow").fadeIn(585);
            $("#rightarrow").fadeOut(585);            
        }
    });
    //this function is making locations dissapear in crappy IE 6 and 7    
//    $(".map_location").hover(function(){
//        id = $(this).attr("id");
//        $("#" + id.toString()).fadeTo(100,.7);            
//    },function(){
//        $("#" + id.toString()).fadeTo(100,1.0);
//    });    
})
//END:  used for locations map scroll arrows (Scott G)

function inputToggle(inputs) {
	inputs.parents("form").submit(function(){
		inputs.each(function(){
			if($(this).val() == $(this).attr("alt")) $(this).val("");
		});
	});
	
	inputs.focus(function(){
		if($(this).val() == $(this).attr("alt")) $(this).val("");
	});
	
	inputs.blur(function(){
		if($(this).val() == "") $(this).val($(this).attr("alt"));
	});
	
	inputs.blur();
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function printArticle() {
	if (window.print) {
		window.print();
	}else if (agt.indexOf("mac") != -1) {
		alert("Press 'Cmd+p' on your keyboard to print article.");
	}else {
		alert("Press 'Ctrl+p' on your keyboard to print article.")
	}
}
