var Nav = Nav || {};
var Banner = Banner || {};

$(document).ready( function() {
	var pageName = Nav.parseUrl();

	// Show and highlight navigation links
	Nav.popup = $(".subNav");
	Nav.popup.hide();
	Nav.highlightTopLevel( pageName );
	Nav.handleSubNav( pageName );

	// Set the page name
	pageName = pageName.replace( "-", " " );
	pageName = pageName.replace( /^\w/, function($0) { return $0.toUpperCase(); } );
	$( "#pageName" ).html( pageName );
});

Banner.setRandomImage = function() {
	$.get("/php/image_list.php", function(data) {
		if( data && data.length ) {
			$(".banner").css("background-image", "url(" + data + ")");
		}
	});
}

Banner.setImage = function( imageName ) {
	imageName = "/assets/banner_images/" + imageName;
	$(".banner").css("background-image", "url( '" + imageName + "' )");
}

Nav.parseUrl = function() {
	// Strip path to just page name
	var pageName = "home";
	if( window.location.pathname.length > 1 ) {
		pageName = window.location.pathname.substr( window.location.pathname.lastIndexOf("/") + 1);

		// Remove the .php
		if( pageName.lastIndexOf('.') > -1 ) {
			pageName = pageName.substr( 0, pageName.lastIndexOf('.') );
		}
	}

	return pageName;
}

Nav.highlightTopLevel = function( pageName ) {
	switch( pageName ) {
		// Home
		case "home":				$( "#home" ).addClass( "current" );			break;
		// About
		case "company":				$( "#about" ).addClass( "current" );		break;
		case "partners":			$( "#about" ).addClass( "current" );		break;
		// Capabilities
		case "cad":					$( "#capabilities" ).addClass( "current" );	break;
		case "industrial-sewing":	$( "#capabilities" ).addClass( "current" );	break;
		case "cnc":					$( "#capabilities" ).addClass( "current" );	break;
		case "automated":			$( "#capabilities" ).addClass( "current" );	break;
		case "custom-graphics":		$( "#capabilities" ).addClass( "current" );	break;
		// Products
		case "heat-shields":		$( "#products" ).addClass( "current" );		break;
		case "racing-products":		$( "#products" ).addClass( "current" );		break;
		case "custom-covers":		$( "#products" ).addClass( "current" );		break;
		// Contact
		case "contact":				$( "#contact" ).addClass( "current" );		break;
	}
}

Nav.popup = null;
Nav.curAnchor = null;
Nav.subNav = function( anchor ) {
	if( this.curAnchor != anchor.attr("id") || !this.popup.is(":visible") ) {
		this.curAnchor = anchor.attr("id");
		var topPos = $(".nav").css("height");
		var leftPos = anchor.attr("offsetLeft") + 400;
		topPos = parseInt( topPos.substr(0, topPos.indexOf('p')) ) - 82;

		this.popup.hide();

		Nav.handleSubNav( anchor.attr("id") );
		this.popup.css( "left", leftPos );
		this.popup.css( "top", topPos );

		this.show();
	}
}

Nav.hideFunction = null;
Nav.startHide = function() {
	this.hideFunction = setTimeout( function() {
		Nav.hide();
	}, 300);
}

Nav.stopHide = function() {
	clearTimeout(this.hideFunction);
}

Nav.show = function() {
//	this.popup.show(100);
	this.popup.slideDown(120);
}
Nav.hide = function() {
//	this.popup.hide(100);
	this.popup.slideUp(100);
}

Nav.handleSubNav = function( pageName ) {
	this.popup.children().hide();
	$( "." + pageName + "_subnav" ).show();
}
