
/*-------------------------------------------------------
---- BEHAVIOUR (Primary Javascript file) ----------------
--------------------------------------------------------*/

// Run in No conflict mode to avoid issues when using Sitecore preview functionality
// Switch all $ references to jQuery
jQuery.noConflict();

//Clear and reset input values
function clearField(){
    jQuery('.frmGeneric input[type="text"], .frmGeneric textarea, .frmGeneric input[type="password"]').each(function(){
        this.defaultValue = this.value;
        jQuery(this).click(function(){
            if(this.value == this.defaultValue){jQuery(this).val("");}
            return false;
        });
        jQuery(this).blur(function(){
            if(this.value == ""){jQuery(this).val(this.defaultValue);}
        });
    });
}

jQuery(document).ready( function() { 																	//When the document is ready

//Activate styles for javascript
var body = jQuery('body');
body.addClass('js');
	
//Open external links in new windows
		
		var external = jQuery('a[rel="external"]');														//Get all links with rel="external" and place them into the 'external'
	
		external.attr("title", external.attr("title") + " - Link opens in a new window..");			//Take the links current title and append it with the external link message
			
		external.click( function() {																//When an external link is clicked
			
		window.open( jQuery(this).attr('href') );														//Open its href in a new window
        
		return false;																				//Cancel the links default behaviour
		
		});																							//End function



    
//Site Select -----------------------

		jQuery('.frmSiteSelect .frmRowSubmit').hide();


//Uniform styled select -----------------------

		jQuery('select').not('#filter select').uniform();

//Global select -----------------------

	jQuery('.globalSelect ul').hide();

	jQuery('.globalSelect a').not('.globalSelect ul li a').click( function(){
		
		if (jQuery('.globalSelect ul').is(":hidden")) {
			jQuery('.globalSelect ul').slideDown();
		} else {
			jQuery('.globalSelect ul').slideUp();				
		}
		
		return false;
		
	})
	
	jQuery('body').click( function(){
		jQuery('.globalSelect ul').slideUp();
		//console.log("body click");
	})
	
    jQuery(".globalSelect").click(function(e){
        e.stopPropagation();
		//console.log("stop propagation");
    });


//Homepage Primary Navigation click enhancement --------------------

	jQuery('.home .primaryNavigation li img').hover( function(){
		
		jQuery(this).css('cursor','pointer');
		
	});

	jQuery('.home .primaryNavigation li img').click( function(){
		
		window.location = jQuery(this).siblings('h3').children('a').attr('href');
		
	})				


//Tabbed Panes function ------------------------------------------

	if('.tabbedContent'){

		jQuery('.tabbedContent').each( function(){
			
			jQuery(this).addClass('panes');
			
			jQuery(this).before("<ul class='tabs'></ul>");
		
			jQuery(this).children('div.pane').each( function(i){
				
				
				jQuery(this).addClass('pane' + i);
				
				jQuery(this).parent('.tabbedContent').siblings('ul.tabs').append("<li id='" + i + "'><a href='#'>" + jQuery(this).find('.panelHeading').text() + "</a></li>");
				
				jQuery(this).find('.panelHeading').addClass('throwOff');
				
			});
		
			if(jQuery(this).siblings('ul.tabs').children().length != 1){
				jQuery(this).siblings('ul.tabs').children('li#0').addClass('active');	
			}
			
			jQuery(this).children('div.pane').hide();
			jQuery(this).children('div:first').show();
			
			if (jQuery(this).siblings('ul.tabs').children().length == 1) {
				if(jQuery(this).parents('ul').find('li').size() > 1)
				{
					jQuery('ul.tabs li a').hover(function(){
					
						jQuery(this).css({'background-color': '#9A928F','cursor': 'default'});
						
					})
				} else {
					jQuery(this).siblings('.tabs').find('#0 a').css({'background-color': '#9b4c10','cursor':'default'});
				}
			}
	
			jQuery(this).siblings('.tabs').children('li').children('a').click( function(){
			
				jQuery(this).parent('li').parent('ul').children('li').removeClass('active');
				
				if (jQuery(this).parent('li').parent('ul').children().length != 1) {
						jQuery(this).parent('li').addClass('active');
				}
				
				var pane = jQuery(this).parent('li').attr('id');
				
				jQuery(this).parent('li').parent('ul').siblings('.tabbedContent').children('.pane').hide();
				jQuery(this).parent('li').parent('ul').siblings('.tabbedContent').children('.pane' + pane).show();
				
				return false;
				
			});
			
		});
		
	}
  


//Tabbed Panes function (automated, works on hover) ------------------------------------------

	if('.automatedContent'){

		jQuery('.automatedContent').each( function(){
			
			jQuery(this).addClass('panes');
			
			jQuery(this).before("<ul class='tabs'></ul>");
		
			jQuery(this).children('div.pane').each( function(i){
				
        //console.log(i);
				
				jQuery(this).addClass('pane' + i);
        
        if(jQuery(this).children('h3').length){
        
          var theHREF = jQuery(this).children('h3').children('a').attr('href');
          
          //console.log('here');
				
        } else {
        
          var theHREF = jQuery(this).children('a').attr('href');
          
          //console.log('here 2');
        
        }
        
        
        
				jQuery(this).parent('.automatedContent').siblings('ul.tabs').append("<li id='" + i + "'><a href='" + theHREF + "'>" + jQuery(this).find('.panelHeading').text() + "</a></li>");
				
				jQuery(this).find('.panelHeading').addClass('throwOff');
				
			});
		
			if(jQuery(this).siblings('ul.tabs').children().length != 1){
				jQuery(this).siblings('ul.tabs').children('li#0').addClass('active');	
			}
			
			jQuery(this).children('div.pane').hide();
			jQuery(this).children('div:first').show();
			
			if (jQuery(this).siblings('ul.tabs').children().length == 1) {
				if(jQuery(this).parents('ul').find('li').size() > 1)
				{
					jQuery('ul.tabs li a').hover(function(){
					
						jQuery(this).css({'background-color': '#9A928F','cursor': 'default'});
						
					})
				} else {
					jQuery(this).siblings('.tabs').find('#0 a').css({'background-color': '#9b4c10','cursor':'default'});
				}
			}
	
			jQuery(this).siblings('.tabs').children('li').children('a').hover( function(){
      
        clearInterval(timer);
			
				jQuery(this).parent('li').parent('ul').children('li').removeClass('active');
				
				if (jQuery(this).parent('li').parent('ul').children().length != 1) {
						jQuery(this).parent('li').addClass('active');
				}
				
				var pane = jQuery(this).parent('li').attr('id');
				
				jQuery(this).parent('li').parent('ul').siblings('.automatedContent').children('.pane').hide();
				jQuery(this).parent('li').parent('ul').siblings('.automatedContent').children('.pane' + pane).show();
				
				return false;
				
			});
 
      
      var current = 1;
      var total = jQuery('.tabbedFeatureWrapper ul.tabs li').length;
      
      function automate (){
      
        if(current === total){
        
          current = 1;       
        
        } else {
        
          current++;
        
        }
      
          jQuery('.tabbedFeatureWrapper ul.tabs li').removeClass('active');
          jQuery('.tabbedFeatureWrapper ul.tabs li:nth-child(' + current + ')').addClass('active');
          jQuery('.automatedContent').children('.pane').hide();
          jQuery('.automatedContent .pane:nth-child(' + current + ')').fadeIn();          
        
      }
      
      var timer = setInterval(function(){automate()},5000);      
        
      });
      
		
	}

//validate ------------------------------------------
//not working currently - possibly come back to
/*jQuery('.frmContact').validate(
{
	rules: {
			fldEmailAddress: {
				required: true,
				email: true
			}
		},
		messages: {
			fldEmailAddress: {
				required: "Please enter a valid email address"
			}
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function() {
			alert("submitted!");
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
});*/

//Overlay function ------------------------------------------

	
	  jQuery('body').append('<div id="overlay"><div id="stage"><a href="#" class="btnClose"><span>Close</span></a></div></div>');
	  jQuery('#overlay').height(jQuery('body').innerHeight());
	  jQuery('#overlay').css({'position':'absolute', 'top':'0px', 'left':'0px'});

	    jQuery('.overlayed').click( function(){
	        jQuery('#overlay').fadeIn();
			jQuery(window).scrollTop(0);
	      return false;
	    })


	      jQuery('#overlay, .btnClose').click( function(){
	        jQuery('#overlay').fadeOut();
	        jQuery("#stage div").hide();
	      });

	 
		jQuery("#overlay #stage").click(function(e) {
	        e.stopPropagation();
	       });

		
	      var KEYCODE_ESC = 27;
	      
	      jQuery(document).keyup(function(e) {
	        if (e.keyCode == KEYCODE_ESC) { 

	        jQuery('#overlay').fadeOut();
	        return false;  
	        }
	      });
	
	
//Restyle labels from forms	

	jQuery('.frmContact .frmRow').each( function(){
		
		var theLabel = jQuery(this).children('label').text();
		jQuery(this).children('label').css({'position':'absolute','left':'-9999px'});
		jQuery(this).children('input[type="text"],input[type="password"]').attr('value',theLabel).css({'width':'475px'});
		jQuery(this).children('textarea').text(theLabel).css({'width':'475px'});
		jQuery(this).children('select').css({'width':'100%'});

		
	})
    
//Clear and reset input values
clearField();
	
	//CLIPPINGS
	if(jQuery('.clippings').length)
	{
		jQuery('.clippings').click(function()
		{
			jQuery(this).attr('href');
			jQuery(this).after('<div id="clipBox"><div id="clipBoxInner"><a href="/africa/myclippings.php">View My Clippings</a> <a href="#" class="close">Close</a></div></div>');
			jQuery('.close').click(function()
			{
				jQuery(this).parents('#clipBox').remove();
				return false;
			});
			jQuery('#clipBoxInner a:first-child').click(function()
			{
				window.location = jQuery(this).attr('href');
			});
			return false;
		});
	}
	
	if(jQuery('#clippings').length)
	{
		if(jQuery(this).find('ul').length)
		{
			jQuery('#clippings .intro').hide();
			jQuery('.remove, .print').show();
		}
		jQuery('#clippings li .delete').click(function()
		{
			jQuery(this).parents('li').remove();
			return false;
		});
		jQuery('#clippings .remove').click(function()
		{
			jQuery(this).parents('#clippings').find('ul').remove();
			jQuery('#clippings .intro').show();
			jQuery('.remove, .print').hide();
			return false;
		});
	}
	
    
    
    
    
	//slider nav for Carousel
	if(jQuery('.slider-nav').length)
	{
		var sliderNav = jQuery('.slider-nav');
		sliderNav.append('<a href="#" class="back"></a><a href="#" class="next"></a>');
		var widthLI = sliderNav.find('li').size();
		widthLI = (widthLI*107);
		sliderNav.find('ul').css('width',widthLI+'px');
		//click handlers for next and back btns
		sliderNav.find('.back').click(function()
		{
			if(sliderNav.find('ul').css('left') != '0px')
			{
				sliderNav.find('ul').not(':animated').animate({left:'+=107'});
				//sliderNav.find('.selected').removeClass('selected').prev().addClass('selected');
			}
			return false;
		});
		//alert(-(widthLI-(107*6))+'px');
		sliderNav.find('.next').click(function()
		{
			if(sliderNav.find('ul').css('left') != -(widthLI-(107*6))+'px')
			{
				sliderNav.find('ul').not(':animated').animate({left:'-=107'});
			}
			return false;
		});
		sliderNav.find('li a').click(function()
		{
			var cur = jQuery(this).parents('li').index();
			jQuery('.slider-wrapper li').animate({'opacity':'0','z-index':'10'});
			jQuery('.slider-wrapper li:eq('+cur+')').animate({'opacity':'1','z-index':'100'});
			sliderNav.find('.selected').removeClass('selected');
			jQuery(this).parent().addClass('selected');
		});
	}
	

// Hide show show filters --
jQuery('ul.filterList').each(function () {
    if (jQuery('li', this).length > 5) {
        jQuery(this).after("<a href='#' class='showMore'>Show more</a>");

        var overspill = "";

        jQuery('li', this).each(function () {
            if (jQuery(this).index() > 4) {

                overspill = overspill + "<li>" + jQuery(this).html() + "</li>";

                jQuery(this).remove();

            }
        })

        jQuery(this).append('<div class="overflow expanded">' + overspill + '</div>');

        jQuery(this).siblings('.showMore').click(function () {

            var excessArea = jQuery(this).parent('.filterBlock').find('.overflow');

            if (jQuery(excessArea).hasClass('expanded')) {

                jQuery(excessArea).removeClass('expanded');
                jQuery(excessArea).slideUp();
                jQuery(this).text("Show more");

            } else {

                jQuery(excessArea).addClass('expanded');
                jQuery(excessArea).slideDown();
                jQuery(this).text("Show less");

            }



            return false;
        })
        jQuery(this).siblings('.showMore').click();


    }
})

















	
    
    //Carouslide
    jQuery('.tabbedContent .CarouSlide').CarouSlide({
        animInfinity:true,
        showSlideNav:true,
        autoAnim:false,
        showPauseButton:false,
        showBackNext:true			
    });
    
    if(jQuery('#aside .CarouSlide .slider-holder li').length > 1){
    jQuery('#aside .CarouSlide').CarouSlide({
        animTime: 500,
        animInfinity:true,
        showSlideNav:true,
        autoAnim:false,
        showPauseButton:false,
        showBackNext:false			
    });
  }
    
    



    
    
//Show and hide of Starting year selector,
//dependent on a PhD category being selected

jQuery('.PhDyear').hide();//hide the year selector

jQuery("#fldCat option:selected").each(function() {
  if (jQuery(this).attr('class') === "PhDcat") {//if a phd option is selected
    jQuery('.PhDyear').show();//show the year selector
    }else{
      jQuery('.PhDyear').hide();
    }
})

jQuery("#fldCat").change(function() {//on change of category selector
  if (jQuery('option:selected', this).attr('class') === "PhDcat") {//if a phd option is selected
    jQuery('.PhDyear').show();//show the year selector
    }else{
      jQuery('.PhDyear').hide();
    }
});

//print page
jQuery('.standardContent .pageUtility').prepend('<ul><li class="print"><a href="#print">Print</a></li></ul>');
jQuery('.standardContent .pageUtility ul li.print a').click(function() {
  window.print();
  return false;
});
    
}); 																								//End document.ready




