﻿/* This file runs on every page, every time they are loaded. Do not remove it, it's a core file. */
/* Init - runs when document is ready. All functions which require the document to be ready should be called from here */
$(function() {
	Cufon.replace('h1, .latest-news h2, .latest-events h2, .latest-resources h2' );
	Cufon.replace('.static .lead_panel h1', { textShadow: '#333 1px 1px' });
	$(".labeler").labeler();
	//addFirstAndLastClass("addFirstAndLastClass");
	addFirstAndLastClass("afalc");
	initDatePicker();
	createTabs();
	testIfPageContainsColumns();
	moveClassToElement("#content", "body");
	addClassToInputElements();
	removeLoadingClass("body", "pageLoading");
	$(".topMenuAction").slidingPane();
	startSlidingPanel();
	jsp_paging();
	$(".page_tools li a").tooltiper();
	getTwitter();
	
	//subscription form functions
	membershipFilter();  //makes additional field appear only if type of membership A is selected
    sendFilter1();//makes the upload field appear only if upload file is selected
	sendFilter2()//makes the upload field appear only if upload file is selected
  
    $('.slider').anythingSlider({
				easing: "swing",       
				autoPlay: true,                
				delay: 5000,                   
				startStopped: false,            
				animationTime: 600,            
				hashTags: false,                
				buildNavigation: true,         
				pauseOnHover: true,            
				startText: "Go",             
				stopText: "Stop"
			});
		
});

// document load 
$(window).load(function() {
	openExternalLinksInNewWindow();
});




function getTwitter(){	
/*   // Get latest 1 tweet by CMupdates
  $.jTwitter('CMupdates', 1, function(data){
        $('#posts').empty();
        $.each(data, function(i, post){
            $('#posts').append(
                '<div class="post">'
                +' <div class="txt">'
                // See output-demo.js file for details
                +    post.text
                +' </div>'
                +'</div>'
            );
        });
    });		*/
}






function jsp_paging() {
	$('.jsp_paging').each(function(){
								   
		var $pager = $('.jsp_dots',this);
		var $next = $(".next",this);
		var $prev = $(".prev",this);
		var $pages = $('.jsp_page',this);
		var containerHeight = 0;
		
		if($pages.length > 1) {
			
			// If there are more than one pages work out the height and load the paging
			
			if($(this).hasClass('autostart')) {
				var timeout = 4000;
				var speed = 1000;
			} else {
				var timeout = 0;
				var speed = 0;
			}
			
			$pages.each(function(){
				if($(this).height() > containerHeight ) {
					containerHeight = $(this).height();
				}										 
			});
	
			$('.jsp_pages',this).cycle({ 
				speed: speed,
				timeout: timeout,
				pager: $pager,
				next: $next,
				prev: $prev,
				fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
				height: containerHeight
			});
			
		} else {
			
			// If there is just the one page, hide the nav
			
			$('.jsp_nav',this).hide();
			
		}
		

	});
}




function initDatePicker() {
	// Datepickers for date input fields
	$('input.date').datepicker({ dateFormat: 'dd/mm/yy' });
}
 
// Open an AJAX dialog
function openDialog(url,title,width,height) {
	// Width and height
	var w = 500;
	var h= 300;
	if(width) {
		w = width;	 
	}
	if(height) {
		h = height;	 
	}
	 // Reset the dialog
    $("#dialog").html('').dialog('destroy'); 
    // Set up dialog
    $("#dialog").dialog({
		width: w,
		height: h,
		modal: true,
		title: title,
		cache: false
    }); 
    // Fetch content
    $.ajax({
		type: "GET",
		url: url,
		data: "ajax=1",
		success: function(data){
			$("#dialog").html(data);
		}
    });
}

// Replace alrt boxes with a much nicer alternative
function ajaxAlert(msg) {
	if($('#alert').length == 0) {
		$("body").append('<div id="alert"></div>');	
	}
    $('#alert').dialog('destroy').html(msg).dialog({
		dialogClass: 'alert',
		width: 200,
		minHeight: 80,
		modal: true,
		cache: false,
		buttons: { "Ok": function() { $(this).dialog("close"); } }
    }); 
}

/* create tabs function - to allow for tabs to be called by other means */
function createTabs(){
	$(".tabs").tabs(
	{}
	
	);

	
}

// columns script - checks number of div.columns inside of div.row and adds specific class to div.inner.
function testIfPageContainsColumns() {
	// test is .row is in the DOM
	if($(".row").length>=1) {
		// run getNumberOfColumns function
		getNumberOfColumns();
	}
	function getNumberOfColumns() {
		var rowStore = $(".row");
		for(var i=0;i<rowStore.length;i++) {
			var numberOfChildren = new Number($(rowStore[i]).find(".inner .column").length);
			if(numberOfChildren<=2) {
				// do nothing
			}
			else {
				$(rowStore[i]).find(".inner").addClass("numberOfColumns" + numberOfChildren);
			}
		}
	}
}

// removes class from target div and adds it to a given tag
function moveClassToElement(targetID, moveClassNameTo) {
	if($(targetID).length == 1) {
		var className = $(targetID).attr("class");
		$(moveClassNameTo).addClass(className);
		$(targetID).removeAttr("class");
	}
}

// add classes to input elements
function addClassToInputElements() {
	$("input[type=text]").addClass("input_type_text");
	$("input[type=password]").addClass("input_type_password");
	$("input[type=radio]").addClass("input_type_radio");
	$("input[type=checkbox]").addClass("input_type_checkbox");
	$("input[type=submit]").addClass("input_type_submit");
	$("input[type=image]").addClass("input_type_image");
	$("input[type=button]").addClass("input_type_button");
	$("input[type=file]").addClass("input_type_file");
}

// add first and last class to elements which are drawn dynamically 
function addFirstAndLastClass(className) {
	if($("." + className).length>=1) {
		var numberOfFirstAndLastClass = $("." + className);
		for(var i=0;i<numberOfFirstAndLastClass.length;i++) {
			$(numberOfFirstAndLastClass[i]).removeClass(className);
			$(numberOfFirstAndLastClass[i]).children(":first-child").addClass("first-child");
			$(numberOfFirstAndLastClass[i]).children(":last-child").addClass("last-child");
			if($(numberOfFirstAndLastClass[i]).attr("class")==null) {
				$(numberOfFirstAndLastClass[i]).removeAttr("class");
			}
		}
	}
}

function removeLoadingClass(target, pageLoadingClass) {
	$(target).removeClass(pageLoadingClass);
}

// open external links in new window
function openExternalLinksInNewWindow() {
	$("a[href^='http://']" || "a[href^='www.']").click(function(){
		window.open(this.href);
		return false;
	});
}

function startSlidingPanel(){
	
	var animationActive = false;
	$(".primary_zone .tabs .desc").show(); //Show Banner
    $(".primary_zone .tabs .block, .primary_zone .tabs .block2").animate({ opacity: 0.85 }, 1 ); //Set Opacity
    
	$(".tabs_nav li a").click(function(){
    //Set Variables  
	 if(animationActive == false) {
		animationActive = true;
        $(".primary_zone .tabs .block").animate({ opacity: 0, marginBottom: -60 }, 0 , function() { //Pull the block down (negative bottom margin of its own height)
            $(".primary_zone .tabs .block").animate({ opacity: 0.85,  marginBottom: "0" }, 250, function(){animationActive = false;} ); //swap the html of the block, then pull the block container back up  
          });
  
	 } else{
		 
		 return;
		 
	 }	 
    });

   

}




function membershipFilter(){
	
	$('.membershipfilter').change(function(){
	var sel = $(this).val();
	if(sel === '1'){
		$('.category1').slideDown("fast");
		$('.category2').slideDown("fast");
		$('.send11').slideUp("fast");
		$('.send12').slideUp("fast");
		$('.send21').slideUp("fast");
		$('.send22').slideUp("fast");

	} else if(sel === '2'){
		$('.category2').slideDown("fast");
		$('.category1').slideUp("fast");
		$('.send11').slideUp("fast");
		$('.send12').slideUp("fast");
		$('.send21').slideUp("fast");
		$('.send22').slideUp("fast");
} 
	
	
	else {
	///so on...
		$('.category1').slideUp("fast");
		$('.category2').slideUp("fast");
		$('.send11').slideUp("fast");
		$('.send12').slideUp("fast");
		$('.send21').slideUp("fast");
		$('.send22').slideUp("fast");
}
	});
		
}

function sendFilter1(){
	
	$('.sendfilter1').change(function(){
	var sel = $(this).val();
	if(sel === '0'){
	$('.send1').slideDown("fast");
	$('.send11').slideUp("fast");
	$('.send12').slideUp("fast");
    } else if (sel === '1') {
    $('.send11').slideDown("fast");
    $('.send1').slideUp("fast");
    $('.send12').slideUp("fast");
    } else if (sel === '2') {
    $('.send12').slideDown("fast");
    $('.send1').slideUp("fast");
    $('.send11').slideUp("fast");
    } else {
	///so on...
	$('.send1').slideUp("fast");
	$('.send11').slideUp("fast");
	$('.send12').slideUp("fast");
}
	});
		
}

function sendFilter2(){
	
	$('.sendfilter2').change(function(){
	var sel = $(this).val();
	if(sel === '0'){
	$('.send2').slideDown("fast");
	$('.send21').slideUp("fast");
	$('.send22').slideUp("fast");
    } else if (sel === '1') {
    $('.send21').slideDown("fast");
    $('.send2').slideUp("fast");
    $('.send22').slideUp("fast");
    } else if (sel === '2') {
    $('.send22').slideDown("fast");
    $('.send2').slideUp("fast");
    $('.send21').slideUp("fast");
    } else {
	///so on...
	$('.send2').slideUp("fast");
	$('.send21').slideUp("fast");
	$('.send22').slideUp("fast");
}
	});
		
}



