var menuhovering = false;
var theMenu;
var menudiv = {};

/*
	LOCATION/DIRECTIONS HANDLING FUNCTIONS
*/

function getLocation() {
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
	} else {
		noLocation();
	}
	
}

function foundLocation(position)
{
  var lat = position.coords.latitude;
  var long = position.coords.longitude;
  queryString = "http://maps.google.com/maps?";
	queryString += "q=" + lat + "+" + long + "&saddr=" + lat + "+" + long + "&daddr=3310+W+Vanowen+St,+Burbank,+CA+91505";
	document.location = queryString;
}

function noLocation()
{
	queryString = "http://maps.google.com/maps?";
	queryString += "daddr=3310+W+Vanowen+St,+Burbank,+CA+91505";
	document.location = queryString;
}

/*
	DRAWING FUNCTIONS
*/
var drawingFeatured = true;
var clearedFeatured = false;

function getProductionLI(theProduction) {
	theProductionURL = '/productions/'+theProduction.seoTitle+'/';
	if (theProduction.featured) {
		// beginning to draw the non featured elements
		// need to clear the above elements
		clearDiv = $('<div/>').addClass('clear');
		theListItem = $('<div/>')
						.attr( { id: 'menu'+theProduction.id })
						.addClass('dropmenuitem standardmenuitem')
						.append($('<a/>').attr( { title: 'more information about '+theProduction.title, alt: 'more information about '+theProduction.title, href: theProductionURL })
						.html(theProduction.title))
						.hover(function () {
							$(this).addClass('activemenuitem');
						},function() {
							$(this).removeClass('activemenuitem');
						})
						.data('URL',theProductionURL)
						.click(function(e) {
							hideDropdown();
							document.location = theProductionURL;
						});
		if (!clearedFeatured) {
			clearedFeatured = true;
			theListItem.before(clearDiv);
		}
	} else {
		// draw a featured production in the list
		theListItem = $('<div/>')
						.attr( { id: 'menu'+theProduction.id })
						.addClass('dropmenuitem featuredmenuitem')
						.click(function(e) {
							hideDropdown();
							document.location = '/productions/'+theProduction.seoTitle+'/';
							return false;
						});

		featuredImage = $('<image/>').attr({
			src: theProduction.productionImageURL,
			alt: "an image from our show, " + theProduction.title, 
			width: "260"
		});
		featuredImageDiv = $('<div/>').addClass('featuredimage').append(featuredImage);
		featuredDetailsDiv = $('<div/>').addClass('featureddetails').append($('<h2/>').append($('<a/>').attr( { title: 'more information about '+theProduction.title, alt: 'more information about '+theProduction.title, href: theProductionURL }).html(theProduction.title)));
		//featuredDetailsDiv = $('<div/>').addClass('featureddetails').append($('<h2/>').html(theProduction.title)).append($('<p/>').html(theProduction.description));
		theListItem
			.append(featuredImageDiv)
			.append(featuredDetailsDiv)
			.append($('<div/>')
			.addClass('clear'));
	}
	return theListItem;
}

function drawEFATPopup(e) {
	thetop = e.pageY - 20;
	theleft = e.pageX;
	thepop = $('#efatpop');
	thepop.css('top',thetop+'px').css('left',theleft + 'px');
	thepop.show().css('opacity','1.0');
	thepop.animate( { opacity: 0 }, { duration: 5000, easing: 'easeInExpo', complete: function() { 
			thepop.css('top','0px').css('left','0px');
		}	
	});
}

function drawJobsPopup(e) {
	thetop = e.pageY - 20;
	theleft = e.pageX;
	//jobspop = $('<div/>').addClass('jobspopup').html('Send us a PDF of your resume to evolutionjobs@evolutionusa.com');
	jobspop = $('#jobspopup');
	jobspop.css('top',thetop+'px').css('left',theleft + 'px').html('Send a PDF of your resume to evolutionjobs@evolutionusa.com');
	jobspop.show().css('opacity','1.0');
	jobspop.animate( { opacity: 0 }, { duration: 10000, easing: 'easeInExpo', complete: function() { 
			jobspop.css('top','0px').css('left','0px');
		}	
	});
}

function buildProductionMenu() {
	showsMenu = $('#showsmenu');
	theOffset = showsMenu.offset();
	theHeight = showsMenu.css('height');
	menudiv = $('<div/>').attr('id','showdropdown').css('height','auto');
	theMenuLeft = theOffset.left;
	theMenuTop = theOffset.top;
	theMenuList = $('<div/>').attr('id','themenulist');
	$.each(allproductionsjson,function () {
		theMenuList.append(getProductionLI(this));
	});
	menudiv.css( { position: 'absolute', left: (theMenuLeft-180) + 'px', top: (theMenuTop+30)+'px', display: 'none'}).append(theMenuList);
	$('body').append(menudiv);
	addDropdownEvents();
	return menudiv;
}

function repositionMenu() {
	if (menuVisible) {
		showsMenu = $('#showsmenu');
		theOffset = showsMenu.offset();
		theHeight = showsMenu.css('height');
		theMenuLeft = theOffset.left;
		theMenuTop = theOffset.top;
		menudiv.css( { left: (theMenuLeft-180) + 'px', top: (theMenuTop+30)+'px'});
	}
}

function hideDropdown() {
	menuVisible = false; 
	$('#showdropdown').hide();
}

function clearMenuHighlights() {
	$('.menuitem').removeClass('menuhover');
}

/*
	EVENT FUNCTIONS
*/
function addDropdownEvents() {
	menudiv.hover(function () {
		menuVisible = true;
		overShowMenu = false;
		if (menuTimeout) {
			clearTimeout(menuTimeout);
		}
	},function () {		
		menuTimeout = setTimeout("if (!overShowMenu) { hideDropdown(); clearTimeout(menuTimeout); clearMenuHighlights(); } else {  }",100);
	});
	$('.standardmenuitem').hover(function () {
		$(this).addClass('activemenuitem');
	},function() {
		$(this).removeClass('activemenuitem');
	});
}


/*
	UTILITY FUNCTIONS
*/


function preloadProductionImages() {
	if (allproductionsjson) {
		$.each(allproductionsjson,function () {
			var tempImage = new Image();
			tempImage.src = this.productionImageURL;
		});
		}
}

var menuVisible = false;
var overShowMenu = false;
var menuTimeout;
$('document').unload(function () {
	hideDropdown();
});

$('document').ready(function () {
	//buildProductionMenu();
	//hideDropdown();
	
	/*
	$(window).resize(function() {
		// check if the menu is showing and move it.
		repositionMenu();
	});
	*/
	$('.menuitem').hover(function (event) {
		if ($(this).attr('id') == 'showsmenu') {
			overShowMenu = true;
			if (!menuVisible) {
				// over the shows menu but the menu is not showing
				$('.menuitem').removeClass('menuhover');
				menudiv.hide();
				$(this).animate({ opacity: 0.01 }, {duration: 0}).addClass('menuhover').animate({ opacity: 1.0 }, {duration: 200});
			}
		} else {
			overShowMenu = false;
			menuVisible = false;
			$('.menuitem').removeClass('menuhover');
			menudiv.hide();
			$(this).animate({ opacity: 0.01 }, {duration: 0}).addClass('menuhover').animate({ opacity: 1.0 }, {duration: 200});
		}
	},function () {
		if (!menuVisible) {
			$(this).removeClass('menuhover');
			menudiv.hide();
			overShowMenu = false;
		} else {
			menuTimeout = setTimeout(' if (!menuVisible) { menudiv.hide(); overShowMenu = false; clearTimeout(menuTimeout);}',200);
		}
	});
	
	
	$('.menuitem').live('click', function (e) {
		$(this).addClass('menuhover');
		if ($(this).attr('id') == 'showsmenu') {
			// clicked the shows menu
			theMenuItem = $(this);
			theOffset = theMenuItem.offset();
			theHeight = theMenuItem.css('height');
			theMenuLeft = theOffset.left;
			theMenuTop = theOffset.top;
			if ($(menudiv).css('display') == 'none') {
				//menu div is not showing
				// the menu is not showing
				menudiv.css( { left: (theMenuLeft) + 'px', top: (theMenuTop+30)+'px'}).append(theMenuList);
				menudiv.slideDown(100);
				menuVisible = true;
				return false;
			} else {
    			$('.menuitem').removeClass('menuhover');
    			menudiv.hide();
				menuVisible = false;
    			return false;
			}
			
		} else {
			document.location = $(e.target).children('a').attr('href');
		}
	});

	

	$('#finduslink').click(getLocation);
	
	$('#efatpopup').live('click',function (event) {
		drawEFATPopup(event);
	});
	
	$('#jobslink').click(function (event) {
	   drawJobsPopup(event);
	});
	//preloadProductionImages();

});
