// JavaScript Document

function resizeMainImage(type) {
	if (type=="sub") {
		// Check if it is already small and animate only if necessary
		if ( $("#headline").css("top") != "20px" ) { 
			$("#headline").animate({ "top": "20px" }, 500, 'swing');
			$("#mainImageBottom").animate({ "top": "90px" },500, 'swing');
		}
	} else {
		// Check if headline is already big and animate only if necessary
		if ( $("#headline").css("top") != "80px" ) { 
			$("#headline").animate({ "top": "80px" }, 500, 'swing');
			$("#mainImageBottom").animate({ "top": "255px" }, 500, 'swing');
		}
	}
}

function resetContent(id) {
	$(".mainContent").each(function() {
		
			if ( id=="estate" || id=="estate2" || id=="trust" || id=="probate" ) {
				
				if ( $("text_services").css("display") == "none" ) {
					$("#text_services").css({ "display":"none" });	
				}
				
			} else {
				$(this).css({ "display":"none" });
			}
	});
}
function resetSubContent() {
	$(".subContent").each(function() {
		$(this).css({ "display":"none" });						   
	});
}
function changeContent(id) {
		mainNav = "";
		subNav = "";
		
		if (id=="/") { id="home" }; // set root to home content
		
		i = id.indexOf("/"); //ie does wierd things and the links have "/" in them so strip it out
		if ( i>-1 ) { 
			t = id.substring( i+1,id.length); 
			id=t; 
		};
		
		// Set navigation links to active
		if (  $("#nav_"+id).attr("class") != "link" ) {
			$( "#nav_"+id ).attr("class","active");	
		}
		
		// if subnav set main nav and subnav items
		if ( id=="estate" || id=="estate2" || id=="trust" || id=="probate" ) {
			mainNav = "services";
			subNav = id;
		} else {
			mainNav = id;	
		}
	
		generateNav("#nav", "li", mainNav);
		generateNav("#services_subnav", "li", subNav);		
		
		resetContent(id);
		resetSubContent();
		
//		alert("mainNav="+mainNav+" and subNav="+subNav);
		
		showContent( mainNav, subNav );
}
    
function showContent(id, subID) {
//	if (window.devicePixelRatio) { safari=true };
//	if ( id=="estate" || id=="estate2" || id=="trust" || id=="probate" ) {
//		subID=id;
//		id="services";
//	} 
//	resetContent();
	switch (id) {
		case "home" :
		    resizeMainImage('home');
			$("#wrapper").stop().animate({ "height": "700px" }, 250, 'swing', function() { 
				$("#text_home").show("slow");
				$("#home_headline").show("slow");
			 });
			document.title = "Hiskey Law Firm - Home Page";
			break;
			
		case "firm" :
		    resizeMainImage('sub');
			$("#wrapper").stop().animate({ "height": "850px" }, 250, 'swing', function() {
				$("#text_firm").show("slow");
				$("#home_headline").hide("fast");
			});			
			document.title = "Hiskey Law Firm - About the Firm";
			break;
		case "services" :
			resizeMainImage('sub');
//			resetSubContent();
			if ( $("#text_services").css("display")=="none" ) {
				$("#text_services").show("slow");
				$("#home_headline").hide("fast");
			}
			if ( subID=="" ) { // set default subID
				subID="estate";
			} 
			switch (subID) {
				case "estate" :
					generateNav("#services_subnav", "li", "estate");
					document.title = "Hiskey Law Firm - Estate Planning";
					$("#wrapper").stop().animate({ "height": "1000px" }, 250, 'swing', function() {
						$("#text_estate").show("slow");
					})					
					break;
				case "estate2" :
					generateNav("#services_subnav", "li", "estate");
					document.title = "Hiskey Law Firm - Estate Planning";
					$("#wrapper").stop().animate({ "height": "1200px" }, 250, 'swing', function() {
						$("#text_estate2").show("slow");
					})					
					break;
				case "trust" :
					generateNav("#services_subnav", "li", "trust");
					document.title = "Hiskey Law Firm - Trust";
					$("#wrapper").stop().animate({ "height": "550px" }, 250, 'swing', function() {
						$("#text_trust").show("slow");
					})					
					break;
				case "probate" :
					generateNav("#services_subnav", "li", "probate");
					document.title = "Hiskey Law Firm - Probate";
					$("#wrapper").stop().animate({ "height": "550px" }, 250, 'swing', function() {
						$("#text_probate").show("slow");
					})					
					break;
			}

			break;
			// SERVICES SUB NAV END
		
		case "contact" :
            resizeMainImage('sub');
			$("#wrapper").stop().animate({ "height": "700px" }, 250, 'swing', function() {
				$("#text_contact").show("slow");
				$("#home_headline").hide("fast");
			})
			document.title = "Hiskey Law Firm - Contact Us";
			
		    if ( $("#map1").html().length < 1 ) {	
				
				//console.log("Creating Map");
				// Create html for the map info window. Driving directions link to google maps
				mapInfoHTML = "<div class=\"mapInfo\"><strong>Hiskey Law Firm</strong><br/>414 N. Placentia Ave.<br/>Placentia, CA 92870";
				mapInfoHTML += "<p>Get Directions To Here:<br/><form action=\"http://maps.google.com/maps\" method=\"get\" target=\"_blank\">";
				mapInfoHTML += "<label for=\"saddr\">Start Address</label><input type=\"text\" name=\"saddr\" value=\"\" />";
				mapInfoHTML += "<input type=\"hidden\" name=\"daddr\" value=\"414z+North+Placentia+Avenue+Placentia+CA+92870\" />";
				mapInfoHTML += "<input type=\"hidden\" name=\"hl\" value=\"en\" /><input type=\"submit\" value=\"Go\" class=\"goButton\" />";
				mapInfoHTML += "</form></p></div>";
			
				$.getScript("scripts/jquery.jmap-r72.js",function() {
					$("#map1").jmap( 'init', { 
						'mapType':'hybrid',
						'mapCenter':[33.87809, -117.877445], 
						'mapZoom':13,
						'mapShowjMapsIcon':true
					} )
					.jmap("AddMarker", {
						'pointLatLng':[33.87809, -117.877445],
						'pointHTML':mapInfoHTML
					} );					
				});
			} else {
//				console.log("Map Already Created");	
			}
			
			
			break;

        case "privacy" :
            resizeMainImage('sub');
			$("#wrapper").stop().animate({ "height": "630px" }, 250, 'swing', function() {
				$("#text_pp").show("slow");
				$("#home_headline").hide("fast");
			})    
			document.title = "Hiskey Law Firm - Privacy Policy";
            break;

        case "careers" :
            resizeMainImage('sub');
			$("#wrapper").stop().animate({ "height": "400px" }, 250, 'swing', function() {
				$("#text_career").show("slow");
				$("#home_headline").hide("fast");
			})
			document.title = "Hiskey Law Firm - Careers";
            break;
    }
}

function attachNavEvents(parent, childType, itemObject) {		
	$(itemObject)
 		.mouseover(function() {
			$(itemObject).stop().animate({ "color": "#042E6F" }, { duration: 300, queue: true });
		})
		.mouseout(function() {
			$(itemObject).stop().animate({ "color": "#8196b7" }, { duration: 300, queue: true });
		});
}
function removeEvents(itemID) {
	$(itemID)
		.unbind('mouseover')
		.unbind('mouseout')
		.unbind('click');
}
function generateNav(parent, childType, activeItemID) {
	// Set active element's ID
	var activeID = "";
	if ( typeof(activeItemID) == "undefined" ) {
	    activeID = "undefined";
	} else {
		activeID = activeItemID;
	} // end if
	
	// Loop through each parents's childType elements 
	$(parent).children(childType).each(
		function() {
			a = $(this).attr("id");
			i = a.substring( a.indexOf("_")+1, a.length );
			t = "#" + i + "_link";
			s = "#nav_" + i;
	
			if ( a == "nav_" + activeID ) {
				// Set the active element's class
			    $(this).attr("class", "active");
			    $( t ).css({ "color":"#FFFFFF" });				
				removeEvents( t );
				removeEvents( s );
			} else {
				// Remove any previous active class
				$(this).removeAttr("class");
				
				// Force browser to redraw elements
				$( t ).css({ "color": "#8196b7" });				
				// Add item events
				attachNavEvents( parent, childType, t );
			} // end if
		} // end function
	
	) // end .each

} // end generateNav function
function initNav(parent, childType) {
	$(parent).children(childType).each(function() {
			a = $(this).attr("id");
			i = a.substring( a.indexOf("_")+1, a.length );
			t = i + "_link";
			$(this).wrapInner('<a id="' + t + '" href="#' + i + '"></a>') 
	});
}


function generateLinks() {
	$(".link").each( function() {
		$(this)
			.mouseover(function() {
				$(this).stop().animate({ "color": "#042E6F" }, 300 );
			})
			.mouseout(function() {
				$(this).stop().animate({ "color": "#8196b7" }, 300 );
			})			
	});
}



