/*  jQuery functions for all public pages  */


$(document).ready(function(){

	$('a.message').click(function(evt) {
		$('body').prepend('<div id="greyCover"></div><div id="extra"></div>') ;
		var mess = $(this).attr('title');
		$('#extra').html('<a href="#"><img class="btn_close" src="'+rootUrl+'chrome/close.png"'
			+' style="position:relative ; float:right ; right:-70px ; top:-30px" /></a>');
		$('#extra').append('<p>'+mess+'</p>').show(function() {
			$('#extra, #greyCover, .btn_close').click(function(evt) {
				$('#extra').remove();
				$('#greyCover').remove();
				evt.preventDefault();
			});
		});
		evt.preventDefault();
	});	
	
	// $('.menuList').hide(); // hide sub-menus

	// $('#artistsLink>a').click(function(event) {
	// 	event.preventDefault();
	// });
	var listHite = 400; // start value, reset on load and window resize
	function menuHoverOver() { $('ul', this).css({maxHeight:listHite}).fadeIn() }
	function menuHoverOut() { $('ul', this).fadeOut() }
	var menuHoverConfig = {    
	     sensitivity: 5, // number = sensitivity threshold - no. pixels to move over fewer than (min: 1)    
	     interval: 50, // number = milliseconds for onMouseOver polling interval    
	     over: menuHoverOver, // function = onMouseOver callback (REQUIRED)    
	     timeout: 250, // number = milliseconds delay before onMouseOut    
	     out: menuHoverOut // function = onMouseOut callback (REQUIRED)    
	};
	$('#siteMap li').hoverIntent(menuHoverConfig);




	// things to do when resizing window - resizing other elements
	function resizeAll () {
		// these may be used later in several specific ways to re-calculate bits of various pages
		// so NOT declared with "var" makes them GLOBAL - their scope extends outside this function

		// just figured out, webkit measures things before images have loaded, so be careful what you measure
		winWide = Math.floor($(window).width());
		winHite = Math.floor($(window).height());
		contTop = Math.floor($('#content').offset().top); // offset is absolute, "position" is relative to parent
		// bottomMargin = Math.floor(0.15*winHite); 	// percentage margin to apply at bottom of page
													// used on artist_detail and exhib pages
		bottomMargin = Math.floor( (winHite/2) -240);	// this prevents it getting really tight on small screen
														// it will get negative & resort to scrolling

		listHite = winHite-100; // max-height for menus ... only useful if we have long artist list in a menu
								// 							which we don't anymore...

		// - adjust width of #page on wide screen to prevent excess stretching which leaves space in the middle
		var winRatio = winWide/winHite;
		var winWidStandard = 81; // standard page width 81% ... why 81? ... can't remember
		if ( winRatio>1.65 ) {
			// 1.65 is about the ratio on 768x1024 screen with normal browser toolbars etc.
			// reduce width of #page ()
			var redux = Math.ceil(Math.pow(winRatio,3.5)); // may be a better way but this gives reasonable result 
			$('#page').css({width:(winWidStandard-redux-0)+'%'});
		} else{
			$('#page').css({width:winWidStandard+'%'});
			// width will likely be less than min-width, so ignored anyway
		};
		
		// adjust navigation menu spacing - standard 20px fits all items on small screen, can increase to 35 or so
		// var spaceForMenu = $('#content').width() - $('#head').width();
		var spaceForMenu = $('#content').width() - 190; // head.width not the same on some bodged pages (news,press)
		var menuTextSize = 0;
		$('#siteMap>li>a').each(function(index) {
			menuTextSize += $(this).width();
		});
		var paddingAvailable = Math.floor(((spaceForMenu - menuTextSize) / $('#siteMap>li').size()) / 2);
		$('#siteMap>li').css({padding:'0 '+paddingAvailable+'px'});
		$('#siteMap>li:first-child').css({paddingLeft:'0'});
	}
	resizeAll(); // run on document.ready
	$(window).resize(function() { // and run on resize
		resizeAll();
	});


// fairs & exhibitions
	$('#listExibs li').each(function() {
		// vertically centre text and image in listings
		// Webkit NEEDS to know image sizes before loading! So PHP must do calculations...
		//  ...how many times do I need to be reminded??!!
		var pic = $(this).find('.pic img');
		var tex = $(this).find('.inset');
		if ( pic.height() > tex.height() ) {
			var texHite = ((pic.height()-tex.height())/2);
			$(this).find('.inset').css({ paddingTop:texHite }); // centre the text vertically
		} else {
			var picHite = ((tex.height()-pic.height())/2);
			$(this).find('.pic img').css({ paddingTop:picHite+'px' }); // centre the image vertically
		};
	});
	
});

