$(document).ready(function(){
	 // cool slideshow for everyone else
	$('.front-slideshow').nivoSlider({effect:'random', controlNav:false, pauseTime:4000, pauseOnHover:true});
	
	$('.split_list').easyListSplitter({ colNumber: 2, direction: 'vertical'  });
	$('.split_list2').easyListSplitter({ colNumber: 4, direction: 'vertical'  });
	$('.split_list3').easyListSplitter({ colNumber: 2, direction: 'vertical'  });
	$('.split_list4').easyListSplitter({ colNumber: 3, direction: 'vertical'  });
	
	$("#floatingBoxCloseBtn").live("click", function() {   $('#floatingBox').fadeOut('slow'); });
	$('#seachsite').hint();
	$('span.mailme').mailme();
	
	// ----------------------------------------------------------------------------------------
	// Detect a change of uri
	// ----------------------------------------------------------------------------------------
	$.address.change(function(event) {  
    	var addrVal = event.value;
    	if(addrVal != '/') {
    		var arrAdd = addrVal.split("/");
    		if(arrAdd[1] == 'query') searchSite( arrAdd );
    		if(arrAdd[1] == 'sect') doTab( arrAdd[2] );
    	}  
	});
	
	// ----------------------------------------------------------------------------------------
	// Search Request
	// ----------------------------------------------------------------------------------------
	function searchSite(arrAdd){
		$('#loadingSearch').show();
		
		if( arrAdd[2]!='' && arrAdd[2]!='Search Site' ) {
			var mystart= '';
			if(arrAdd[4]!=null)
				mystart = arrAdd[4];
			
			$.post(url,{ sect:'search', method:'ajax', query:arrAdd[2], start:mystart }, function(content) { 
				$('#search_results_content').html(content) ;
			});
			
			// move back to the top of the page	
		  	var destination = $('#header').offset().top;
		   	$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
		   	$('#loadingSearch').animate({opacity: 1.0}, 1000).fadeOut('fast');
		   	return false;	
		}
	}
	
	// Button
	// ----------------------------------------------------------------------------------------
	$("#seachsiteBtn").live("click", function() {
		window.location = '/search/#/query/'+$('#seachsite').val();
	});
	
	// Button
	// ----------------------------------------------------------------------------------------
	$("#topBtn").click(function() {               
		var destination = $('#header').offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
		   	
	
	});
	
	// ----------------------------------------------------------------------------------------
	// Contact Form
	// ----------------------------------------------------------------------------------------
	$("#contactSendBtn").click(function() {               
		$('#errors').fadeOut('fast');
		errs ='';
		if($('#name').val() == '') errs += 'Please enter your name<br />';
		if($('#email').val() == '') errs += 'Please enter your email address<br />';
		if($('#message').val() == '') errs += 'Please enter a message<br />';
		
		if(errs =='') {
			$.post(url,{ sect:'contact', p:'index', method:'ajax', submit:'', name:$('#name').val(), email:$('#email').val(), 
						 message: $('#message').val() }, function(content) {
				if(content.search('error')<0/*  && content !='' */) {
					$('#name').val('');
					$('#email').val('');
					$('#message').val('');
					$('#AjaxSuccess').html('Your message has been sent').centerInClient().show().animate({opacity: 1.0}, 1000).fadeOut('slow')  ;
				}
				else{
					// server side errors
					content = content.replace("error - ", "");
					$('#errors').html(content).fadeIn('slow');
				}
				return false;
			});
		}
		else{
			$('#errors').html(errs).fadeIn('slow');
		}
	});
	
	
	// ----------------------------------------------------------------------------------------
	$('.enquiry-box').hide();
	$('#enquiry-box-general').show();
	
	$("#enquiry-select").live("click", function() {                 
		$('.enquiry-box').hide();
		var val = $(this).val(); 
		$('#enquiry-box-'+val).fadeIn('slow');
	});
	
	// GOALS
	// ----------------------------------------------------------------------------------------
	$('.goal').hide();
	$('.goal:first').show();
	
	var g = 0;
	var gl = $('.goal').size()-1;
	$("#goalsBtn").live("click", function() {
		g++;
		if(g > gl) g = 0;
		$('.goal').hide();
		$('.goal').eq( g ).fadeIn('slow');
	});
	
	// PUBLICATIONS
	// ----------------------------------------------------------------------------------------
	$('.pub_box').hide();
	$('.pub_box:first').show();
	
	$('#year_select').change(function () {
		var year = $('#year_select option:selected').val();
		$('.pub_box').hide();
		$('#pubs_'+year+'').show();
	});
	
	// ----------------------------------------------------------------------------------------
	// TABS
	// ----------------------------------------------------------------------------------------
	$('.tab').hide();
	$('.tab:first').show();
	
	$("#tab-nav li").live("click", function() {
		var id = $(this).attr("title");
		$.address.value('sect/'+id);
	});
	
	function doTab(tab){
		var content = '';
		$('.tab').hide();	
		$('#tab-nav li').removeClass( 'active' );
		
		$('#tab_'+tab).fadeIn('slow'); 
		$('#tab-nav_'+tab).addClass( 'active' );
		
		if(tab == 'on-campus'){
			content = $('#mod_AQUPF2').html();
			$('.modDecContent').html(content);
		}
		if(tab == 'online'){
			content = $('#mod_AQUP2F').html();
			$('.modDecContent').html(content);
		}
		if(tab == 'cals'){
			//$('#mod_cal-oncampus').show();
			//content = $('#mod_cal-oncampus').html();
			//$('.modDecContent').html(content);
		}
		if(tab == '1'){
			content = $('#ug_mod_blueplanet').html();
			$('.modDecContent').html(content);
		}
		if(tab == '2'){
			content = $('#ug_mod_envir').html();
			$('.modDecContent').html(content);
		}
		if(tab == '3'){
			content = $('#ug_mod_aquatic_resources').html();
			$('.modDecContent').html(content);
		}
		if(tab == '4'){
			content = $('#ug_mod_aquaculture_nutrition').html();
			$('.modDecContent').html(content);
		}
		if(tab == '4b'){
			content = $('#ug_mod_systtaxonomy').html();
			$('.modDecContent').html(content);
		}
	}
	
	// COURSES
	// ----------------------------------------------------------------------------------------
	var content = $('.ModDesc:first').html();
	$('.modDecContent').html(content);
		
	$(".ugModBtn").live("click", function() {                 
		var id = $(this).attr("name");
		var content = $('#ug_mod_'+id).html();
		$('.modDecContent').html(content);
	});
	
	$(".mastersModBtn").live("click", function() { 
		var id = $(this).attr("name");
		var content = $('#mod_'+id).html();
		$('.modDecContent').html(content);
	});
	
	$(".cpdModBtn").live("click", function() {
		var id = $(this).attr("name");
		var content = $('#mod_'+id).html();
		$('.modDecContent').html(content);
	});
	
	$(".cpdCalBtn").live("click", function() {                 
		var id = $(this).attr("id").substr(10);
		var content = $('#mod_'+id).html();
		$('.modDecContent').html(content);
		
	});
	
	// FAQS 
	// ----------------------------------------------------------------------------------------
	$('.faqAns').hide();
	$(".faqBtn").live("click", function() {                 
		var id = $(this).attr("id").substr(7);
	
		if($('#faqAns_'+id).is(":visible"))
			$('#faqAns_'+id).slideUp('fast');
		else{
			$('.faqAns').slideUp('fast');
			$('#faqAns_'+id).slideDown('fast');
		}
	});
	
	$('#masters-course-option').change(function () {              
		var opt = $(this).val(); 
		window.location = 'http://'+window.location.hostname+'/training/masters/'+opt+'/';	
	});
	
	$('#nav-option').change(function () {
		var opt = $(this).val(); 
		window.location = $('#urlroot').val()+opt+'/';
	});
	
	
	$('.hide').hide();
	$('.hide:first').show();
	$('#myselect').change(function () {              
		 $('.hide').hide();
		 var id = $(this).val();
		 $('#sect_'+id).show();
	});
	
	// SLIDES
	// ---------------------------------------------------------------------------------------- 
	$('.slides img').hide();
	$('.slides img:first').show();
	var s = 0;
	var st = $('.slides img').size()-1;
	$("#slide-next-btn").live("click", function() {
		s++;
		if(s > st) s = 0;
		$('.slides img').hide();
		$('.slides img').eq( s ).fadeIn('slow');
	});
	
	$("#slide-prev-btn").live("click", function() {
		s--;
		if(s < 0) s = $('.slides img').size()-1;
		$('.slides img').hide();
		$('.slides img').eq( s ).fadeIn('slow');
	});
	
	// SLIDES 2
	// ---------------------------------------------------------------------------------------- 
	$('.slides2 img').hide();
	$('.slides2 img:first').show();
	var s2 = 0;
	var st2 = $('.slides2 img').size()-1;
	$("#slide2-next-btn").live("click", function() {
		s2++;
		if(s2 > st2) s2 = 0;
		$('.slides2 img').hide();
		$('.slides2 img').eq( s2 ).fadeIn('slow');
	});
	
	$("#slide2-prev-btn").live("click", function() {
		s2--;
		if(s2 < 0) s2 = $('.slides2 img').size()-1;
		$('.slides2 img').hide();
		$('.slides2 img').eq( s2 ).fadeIn('slow');
	});
	
	// SLIDES 2
	// ---------------------------------------------------------------------------------------- 
	$('.slides3 img').hide();
	$('.slides3 img:first').show();
	var s3 = 0;
	var st3 = $('.slides3 img').size()-1;
	$("#slide3-next-btn").live("click", function() {
		s3++;
		if(s3 > st3) s3 = 0;
		$('.slides3 img').hide();
		$('.slides3 img').eq( s3 ).fadeIn('slow');
	});
	
	$("#slide3-prev-btn").live("click", function() {
		s3--;
		if(s3 < 0) s3 = $('.slides3 img').size()-1;
		$('.slides3 img').hide();
		$('.slides3 img').eq( s3 ).fadeIn('slow');
	});
	
	// ----------------------------------------------------------------------------------------
	$('#view-protected-btn').live("click", function() {                 
	
		$.post(url,{ sect:'protected', p:$('#page').val(), method:'ajax', submit:'', subuser: $('#protected-user').val(), subsect: $('#subsect').val(), 
						subpass: $('#protected-pass').val() }, function(content) {
				if(content.search('error')<0/*  && content !='' */) {
					$('#password-form').slideUp('fast');
					$('#hidden-content').html(content);
				}
				else{
					// server side errors
					$('#AjaxWarning').html('Wrong Username / Password').centerInClient().show().animate({opacity: 1.0}, 1000).fadeOut('slow')  ;
				}
				return false;
		});
	});
	// ----------------------------------------------------------------------------------------
	
	$('#pepe').hide();
	// ----------------------------------------------------------------------------------------
	$(".openWinBtn").live("click", function() {                 
		var content = $(this).attr("title");
		var content2 = $('#'+content).html();
		 
		$('#floatingBoxContent').html(content2); 
		$('#floatingBox').centerInClient().fadeIn('slow');
	});
	// ----------------------------------------------------------------------------------------
	
	
	$('#icsr_outcome').hide();
	var icsrtot = 0;
	// ----------------------------------------------------------------------------------------
	$(".icsr_hotel").live("click", function() {                 
		doIcsrTot();
	});
	// ----------------------------------------------------------------------------------------
	
	// ----------------------------------------------------------------------------------------
	$(".icsr_extra").live("click", function() {                 
		doIcsrTot();
	});
	// ----------------------------------------------------------------------------------------
	
	$('#icsr_reg_fee').change(function () {              
		doIcsrTot();
	});
	
	$('#icsr_days').live("click", function() {                 
		doIcsrTot();
	});
	
	
	// ----------------------------------------------------------------------------------------
	function doIcsrTot(){
		var extrastot = 0;
		var accomtot = 0;
		
		regfeetot = parseInt($('#icsr_reg_fee').val());
		regfeetot = regfeetot*125;
		
		if(regfeetot>350) regfeetot=350;
		
		if( $('.icsr_hotel:checked').attr("title") == 'Chalet Pendreich Way')
			accomtot = parseInt($('.icsr_hotel:checked').val());
		else
			accomtot = parseInt($('.icsr_hotel:checked').val()) * parseInt($('#icsr_days').val());
		
		icsrtot = regfeetot + accomtot;
		
		if( $('#icsr_boat').is(':checked') ){
  			extrastot = extrastot + parseInt($('#icsr_boat').val())
		}
		
		if( $('#icsr_tattoo').is(':checked') ){
  			extrastot = extrastot + parseInt($('#icsr_tattoo').val())
		}
		
		icsrtot =  extrastot + accomtot + regfeetot;
	
		$('#icsr_regfeetot').html(regfeetot);
		
		$('#icsr_extratot').html(extrastot);
		$('#icsr_hoteltot').html(accomtot);
		$('#icsr_total').html(icsrtot);
	}
	// ----------------------------------------------------------------------------------------
	
	// ----------------------------------------------------------------------------------------
	$("#icsr_submitbtn").live("click", function() {                 
		
		//
		if($('#icsr_name').val()=='' || $('#icsr_org').val()=='' || $('#icsr_email').val()==''|| $('#icsr_phone').val()=='' ){
		
			
			if($('#icsr_name').val()==''){
				alert('Please provide your full name.');
				// move back to the top of the page	
				var destination = $('#icsr_regform').offset().top;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
				return;
			}
			if($('#icsr_org').val()==''){
				alert('Please provide your organisation');
				// move back to the top of the page	
				var destination = $('#icsr_regform').offset().top;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
				return;
			}
				
			if($('#icsr_email').val()==''){
				alert('Please provide your email address');
				// move back to the top of the page	
				var destination = $('#icsr_regform').offset().top;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
				return;
			}
				
			if($('#icsr_phone').val()==''){
				alert('Please provide your telephone number');
				// move back to the top of the page	
				var destination = $('#icsr_regform').offset().top;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
				return;
			}
		}
		else{
	
			doIcsrTot();
			
			var icsrhotel = $('.icsr_hotel:checked').attr("title");
			var icsrhoteltot = $('#icsr_hoteltot').html();
			var icsrextras = '';
			var icsrextrastot = $('#icsr_extratot').html();
			var icsrregfeetot = $('#icsr_regfeetot').html();
			
			
			if( $('#icsr_boat').is(':checked') ){
	  			icsrextras = icsrextras + 'Field Trip, ';
			}
			
			if( $('#icsr_tattoo').is(':checked') ){
	  			icsrextras = icsrextras + 'Edinburgh Military Tattoo';
			}
			
			// 
			$.post(url,{ sect:'ICSR14', p:'ICSR14', method:'ajax', submit:'', name: $('#icsr_name').val(), org:$('#icsr_org').val(),
						email: $('#icsr_email').val(), address: $('#icsr_address').val(), job: $('#icsr_job').val(), 
						phone: $('#icsr_phone').val(), dietary: $('#icsr_dietary').val() , tot: icsrtot, hotel: icsrhotel, hoteltot:icsrhoteltot, 
						regfee:icsrregfeetot,  extras:icsrextras, extrastot:icsrextrastot, cap: $('#icsr_cap').val(), 
						reg_fee_days: $('#icsr_reg_fee').val(), hotelarrivdate: $('#icsr_date').val(), hoteldays: $('#icsr_days').val() 
						 }, function(content) {
			 
				if(content.search('error')<0/*  && content !='' */) {
						
							$('#icsr_confirm_ref').html(content);
							$('#icsr_confirm_email').html($('#icsr_email').val());
							
							if(icsrtot == 0) icsrtot = parseInt($('#icsr_regfee').val()) ;
							
							$('#icsr_confirm_amount').html(icsrtot);
							// move back to the top of the page	
							//var destination = $('#header').offset().top;
							//$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
							   	
							$('#icsr_regform').hide();
							$('#icsr_outcome').fadeIn('slow');
				}
				else{
					// server side errors
					$('#AjaxWarning').html(content).centerInClient().show().animate({opacity: 1.0}, 1000).fadeOut('slow')  ; 
				}
				;
				return false;
				
			});
		}
	});
	// ----------------------------------------------------------------------------------------
	
	
});
