$(document).ready(function() {

//	$('#cycle').cycle({
//		fx: 'fade'
//	});

//	$('figure.gallery-item a').attr('rel','gallery');
//	$('figure.gallery-item a[rel="gallery"]').fancybox();

	$(document).pngFix();
	$('#menu-primary-navigation').supersubs({ 
            minWidth:    12,   // minimum width of sub-menus in em units 
            maxWidth:    27,   // maximum width of sub-menus in em units 
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
        }).superfish();  // call supersubs first, then superfish, so that subs are 
                         // not display:none when measuring. Call before initialising 
                         // containing tabs for same reason. 

});


$(document).ready(function(){

	// TOGGLE SCRIPT
	$(".hide").hide();
	
	$("a.toggle").click(function(event){
	$(this).parent().next('.hide').toggle("normal");
	//$(this).parent().next('.hide').slideToggle("normal");
	// Stop the link click from doing its normal thing
	return false;
	}); // END TOGGLE

}); // END ready function


if (typeof jQuery != 'undefined') {
	jQuery(document).ready(function($) {
		var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3|mp4|m4a|m4v)$/i;
		var baseHref = '';
		if (jQuery('base').attr('href') != undefined)
			baseHref = jQuery('base').attr('href');
		jQuery('a').each(function() {
			var href = jQuery(this).attr('href');
			if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
				jQuery(this).click(function() {
					var extLink = href.replace(/^https?\:\/\//i, '');
					_gaq.push(['_trackEvent', 'External', 'Click', extLink]);
					if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
						setTimeout(function() { location.href = href; }, 200);
						return false;
					}
				});
			}
			else if (href && href.match(/^mailto\:/i)) {
				jQuery(this).click(function() {
					var mailLink = href.replace(/^mailto\:/i, '');
					_gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
				});
			}
			else if (href && href.match(filetypes)) {
				jQuery(this).click(function() {
					var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
					var filePath = href;
					_gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
					if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
						setTimeout(function() { location.href = baseHref + href; }, 200);
						return false;
					}
				});
			}
		});
	});
}

function slideDownIfNotVisible(selector, speed)
{
	speed = (speed == undefined) ? 'fast' : speed;
	if(selector.not(":visible"))
		selector.slideDown(speed);
}

function slideUpIfVisible(selector, speed)
{
	speed = (speed == undefined) ? 'fast' : speed;
	if(selector.is(":visible"))
		selector.slideUp(speed);	
}

jQuery(document).ready(function() {
	var toggle_visibility_links = jQuery("a.toggle_visibility_link");
	
	// on load hide any hidden sections
	jQuery("div.hidden_on_load").hide();
	
	toggle_visibility_links.click(function(){
		//var toggle_visibility_section	= jQuery(this).parent().parent().nextAll(".toggle_visibility_section").get(0);
		var toggle_visibility_section	= jQuery(this).parent().nextAll(".toggle_visibility_section").get(0);
		
		// turn element into a jQuery object
		var jq_toggle_visibility_section= jQuery(toggle_visibility_section);
		if(jq_toggle_visibility_section.is(':visible'))
		{
			jQuery(this).text('show');
			slideUpIfVisible(jq_toggle_visibility_section);
		}else
		{
			jQuery(this).text('hide');
			slideDownIfNotVisible(jq_toggle_visibility_section);
		}	
	});

	jQuery("a.first-level-toggle-visibility-link").click(function(){
		var first_level_section = jQuery(this).parent().next(".first-level-section");
		
		if(first_level_section.is(':visible'))
		{
			slideUpIfVisible(first_level_section);
			jQuery(this).html('Show &raquo;');
		}else{
			slideDownIfNotVisible(first_level_section);
			jQuery(this).html('Hide &laquo;');
		}
	});
	
	jQuery("a.second-level-toggle-visibility-link").click(function(){
		var second_level_section = jQuery(this).next().next(".second-level-section");
		
		if(second_level_section.is(':visible'))
		{
			slideUpIfVisible(second_level_section);
			jQuery(this).html('Show &raquo;');
		}else{
			slideDownIfNotVisible(second_level_section);
			jQuery(this).html('Hide &laquo;');
		}
	});
	
	jQuery("a.third-level-toggle-visibility-link").click(function(){
		var third_level_section = jQuery(this).next().next(".third-level-section");
		
		if(third_level_section.is(':visible'))
		{
			slideUpIfVisible(third_level_section);
			jQuery(this).html('Show &raquo;');
		}else{
			slideDownIfNotVisible(third_level_section);
			jQuery(this).html('Hide &laquo;');
		}
	});
});
