function removeLast(){
    $('.footer-small-text-right a:last').css("border-right","0");
    $('.footer-small-text-right a:last').css("padding-right","0");
	$('.nav-middle a:last').css("border-bottom","0");
    $('.nav-middle a:last').css("padding-bottom","0");	
}

function remEmpty(){
	$('#contentleft span.check:empty').prev('h3').hide();
	$('#contentright span.check:empty').prev('h3').hide();
	$('#contentright p.check:empty').prev('h3').hide();
	$('#thumbHolder a#first:empty').parent().parent().parent().hide();
	if ($('#thumbHolder a#first > *').size() == 0) {
		$('#thumbHolder a#first').remove();	
	}
	if ($('#thumbHolder a#second > *').size() == 0) {
		$('#thumbHolder a#second').remove();	
	}
	if ($('#thumbHolder a#third > *').size() == 0) {
		$('#thumbHolder a#third').remove();	
	}
	if ($('#thumbHolder a#fourth > *').size() == 0) {
		$('#thumbHolder a#fourth').remove();	
	}
	if ($('#thumbHolder a#fifth > *').size() == 0) {
		$('#thumbHolder a#fifth').remove();	
	}
	if ($('#thumbHolder a#sixth > *').size() == 0) {
		$('#thumbHolder a#sixth').remove();	
	}
	$('h2:empty').hide();
	$('#highlightedcopy p:empty').parent().hide();
	$('.venuetext:empty').parent().hide();
	$('.feetext:empty').parent().hide();
}

function rotate2(){
var count = $('#rotator2 li').size();
	var randomNo = Math.floor(Math.random()*count)
	$('#rotator2 li').hide();
	$('#rotator2 li:eq(' + randomNo + ')').show();
}	


function navWidth(){
	
	totalWidth = 960;
	itemWidth = 0;
	$('#topNav > li:first').addClass('first');
	$('#topNav > li:last').addClass('last');
	$('#topNav > li > a').css('padding-left', '0px').css('padding-right', '0px');
	$('#topNav > li').each(function(){
		itemWidth += $(this).width();
	});
	spaceLeft = (totalWidth - itemWidth);
	padding = Math.floor((spaceLeft / $('#topNav > li').size()) / 2);
	$('#topNav > li > a').css('padding-left', padding + 'px').css('padding-right', padding + 'px');
	remainder = (totalWidth - $('#topNav').width());
	$('#topNav > li:last > a').css('padding-right', padding + Math.ceil(remainder/2) + 'px');
	$('#topNav > li:first > a').css('padding-left', padding + Math.floor(remainder/2) + 'px');
}


function twitter(){
	$('#twitter_update_list').html('<span>Loading Twitter feed</span>');
	if($('#twitter_update_list').size() > 0){
		$.getScript("http://twitter.com/javascripts/blogger.js", function(){
			$.getScript("http://twitter.com/statuses/user_timeline/GlassHouseCLD.json?callback=twitterCallback2&count=2");
		});
	}
}

function addLinks(){
		setTimeout(function() {
        	$("#twitter_update_list a[href^='http']").attr('target','_blank');
        }, 4000);
}

function eventTracking(){
	var pageUrl = String(document.location);

	// check if current page is within the Useful Resources section i.e. page URL contains "-resources"
	if (pageUrl.indexOf("-resources") != -1){
		$('a[href$="pdf"]').live('mousedown', function(){
			var linktext = escape($(this).text());
			var section = escape(pageUrl.replace("http://www.theglasshouse.org.uk",""));
			
			// for use with old GA script
			// pageTracker._trackEvent('Useful Resources', section, linktext);

			//if(document.location.href.indexOf('?tester') > -1){
				//alert('_trackEvent' + ', ' + 'Useful Resources' + ', ' + section + ', '  + linktext );
			//}
			// for use with a-synchronous GA script
			_gaq.push(['_trackEvent', 'Useful Resources', section, linktext]);
			
		});
	}
}

/* Remove same date for event */
function eventDatesRemover(){

	$('.trainingeventdate').each(function(){
		var eventdates = $(this).html();
		var date_full = eventdates.split(" - ",2);
		if(date_full[0] == date_full[1]) {
			// remove second date
			$(this).html(date_full[0]);
		} else { // compare years and months	
			var date_part1 = date_full[0].split(" ",3);
			var date_part2 = date_full[1].split(" ",3);
			if(date_part1[2] == date_part2[2]) {
				// compare months
				if(date_part1[1] == date_part2[1]) {
					// remove month
					$(this).html(date_part1[0]+' - '+date_full[1]);
				} else {
					// remove year from the first date
					$(this).html(date_part1[0]+' '+date_part1[1]+' - '+date_full[1]);
				}
			}
			
		}
	});
}

$(document).ready(function(){
    twitter();
	addLinks();
    removeLast();
	remEmpty();
	rotate2();
	navWidth();
	eventTracking();
	eventDatesRemover();
    
    //  Home banner fader/cycle
    $homeBanner = $('#homeBanner');
    
    if ( $homeBanner.length > 0 ) {
      
      	var $bannerBtns 	= $('#bannerBtns');
      
        $homeBanner.cycle({
          fx: 'fade'				//  Transition type
          , pager: $bannerBtns		//  Div containing the slide numbers
          , pause: 0				//  Pause on hover
          , speed: 1000				//  Transition speed
          , timeout: 3000			//  Time between transitions (allowing for the "speed")
        });
      
    }
	
	//  Nav-hovering
	if ( $( 'ul.nav-child-menu' ).length > 0 ) {
	
		$('#topNav > li').hover( function(){

			var $menu = $( this );

			$menu.find('a:first').addClass('on');

			if( $menu.find('ul:first li').size() > 0){
				$(this).find('ul:first').show();
			}

		}, function(){
			$(this).find('ul:first').hide();
			$(this).find('a:first').removeClass('on');
		});

	}
	
	var newsSignup = $("#newsSignupForm");
 	newsSignup.submit(function(){
			var carriedemail;
			carrynewsletter();
			
			document.newsSignup.submit();
		});

	function carrynewsletter() {
		var carriedemail = $('#mod_signup_email').val();
		
		if(carriedemail != ""){
			$.cookie('newsletterSignup', carriedemail, { expires: 2, path: '/' });
		}
		
		//#newsSignupEmail
		
			
	}
	
 }); 

 function autofill() {
			
			$('html,body').animate({scrollTop: $("#newslettersignupform").offset().top},'slow');
			 
			var giveitback = $.cookie('newsletterSignup');
			
			$('#mod_signup_email').val(giveitback);
			
	} 
