
function closePopup(){
	$.prettyPhoto.close();
}


function initsidebarmenu(menuid){

	//all uls in th top level ul
	$("#"+ menuid + " ul").each(function(){

		//when ul, give parent li's a tag style of "arrow"
		$(this).parent('li').find('a:first').addClass("arrow");

		//get width of parent li and set as "left" in css
		var parentWidth = $(this).parent().width();
		$(this).css("left", parentWidth + "px");

		$(this).parent().hover(
			function(){
				//first ul
				$(this).find("ul:eq(0)").fadeIn("fast");
			},function(){
				$(this).find("ul:eq(0)").fadeOut("fast");
			}
		);
	});

	//cleanup - hide all uls within menu
	$("#"+ menuid + " ul").css("display","none");
}



function initMenu() {
  $('#menu ul').hide();
  $('#menu ul:first').show();
  $('#menu li a').click(
    function() {
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        return false;
        }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('#menu ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
        return false;
        }
      }
    );
  }

function centreGallery(){
	//CENTRE ARTICLE IMAGES (GALLERY TYPE)
	//set widths of each article img gallery
	$('.imgs-gallery').each(function(){
		var width = 0;
		var images = $(this).find('.image');
		var imgNo = images.length;
		
		images.each(function(){
			width += $(this).width();
			
		})
		width += ((imgNo-1)*10);
	
		$(this).width(width).show();
	})
}

//CHECK FOR UNLOADED IMAGES
function checkUnloadedImages(imageJPath,time){
	//console.log("Initialising check: " + imageJPath);
	setTimeout(function(){checkImages(imageJPath,time);},time);
}

/* height check in jpath not working :S*/
function checkImages(imageJPath,time){
	var unloadedImgs = [];
	$(imageJPath).each(function(){
		if($(this).height() < 25){
			unloadedImgs.push($(this));
		}
	})

	//console.log("executing check: "+imageJPath+"\nCount: "+ unloadedImgs.length);

	for(x=0;x < unloadedImgs.length;x++){
		var image = unloadedImgs[x];
		var imgSrc = image.attr("src");
		image.attr("src", imgSrc + "?timestamp=" + new Date().getTime());
		//console.log("image "+x+" fixed: " + imageJPath);
		
		if(image.parent().parent().parent().hasClass('imgs-gallery')){
			setTimeout(centreGallery,1000);
		}
	}
	unloadedImgs = [];
	setTimeout(function(){checkUnloadedImages(imageJPath,time);},time);
}



/*******************************************************************/

//RUN AFTER IMAGES HAVE LOADED
runOnLoad(centreGallery);

$(document).ready(function() {

	//Nav
	initsidebarmenu("menu");
	//initMenu();

	/* lightboxes for images */
	$(".image a").prettyPhoto({theme:'light_rounded'});
	
	/* lightboxes for iframe */
	$("a.popup").prettyPhoto({theme:'light_rounded'});
	
	/* Load gallery (with success check) */
	checkPageGallery();
	
	/* start constant checks for unloaded images */
	checkUnloadedImages('#content img',4000); //Images on every page

	/* Newsletter select dropdown */
	var newsletterSelect = $('#select-newsletter');
	if(typeof(newsletterId) != "undefined"){
		newsletterSelect.val(newsletterId);
	}
	newsletterSelect.change(function(){
		window.location.href = siteRoot + "news/newsletters/" + $(this).val() + "/";
	});
	
	
	//Gallery manager
	var properties = {
		galleries:$('.imgs-multiple'),
		interval:4000
	}
	
	var myGalleryManager = new GalleryManager(properties);
	
	
	//Quote resizing
	var height = $('#quote').height();
	for(size=17;height>90;size--){
		$('#quote').css('font-size',size+'px');
		height = $('#quote').height();
	}
	


});





