// Load jQuery
/* Disabled for offline development
google.load("jquery", "1");

google.setOnLoadCallback(function() 
{
	$('#navbar ul li').hover(function(){
		$(this).children('ul').fadeIn(200);
	}, function(){
		$(this).children('ul').fadeOut(200);
	})
});
*/

$(window).ready(function(){
	
	//Shorten descriptions
	homepage_descriptions();
	
	//Menu drop down
	$('#navbar ul li').hover(function(){
		$(this).children('ul').show();
	}, function(){
		$(this).children('ul').hide();
	})
	
	//Tidy up borders on hover
	$('#navbar ul li ul li').hover(function(){
		$(this).css({'border': 'none'});
	}, function(){
		$(this).css({'border': 'none'});
	})
	
	//Homepage?
	if($('body').attr('id') == 'home'){
	
		initialize_course_finder();
		initialize_study_options();
	}
	
	//Are we showing the map?
	if($('body').attr('id') == 'locations' && $('body').attr('class') == 'map'){
		initialize_map();
	}
	
	//Course builder
	if($('body').attr('id') == 'courses' && $('body').attr('class') == 'builder'){
		initialize_coursebuilder();
	}
	
	$("input[type='file']").change(function () {
		(this.value == "") ? $(this).parent().removeClass("file_selected") : $(this).parent().addClass("file_selected");
	});
	
	$(".form_wrapper form").validate();	

});

//This hides descriptions and adds 'read more' links
function homepage_descriptions()
{
	$('.shorten').each(function(){
		var h;
		h = $(this).css('height');

		$(this).css({'height': '16.5em', 'overflow': 'hidden'}).after('<a href="#" class="more">Read more&#8230;</a>');
		
			$(this).next('a').click(function(){
				$(this).hide();
				$(this).prev('.shorten').css({'height': h});
				return false;
			});
	});
}


// =====================
// = Map functionality =
// =====================
function initialize_map()
{
	//$('#maplist .panel').css({opacity : 0}).removeClass('hide');
	
	//Show the panel when browsing list
	//We are applying classes to fix a z-index issue whereby .panel was hidden but laying over the map markers, makign them unclickable
	$('#maplist a.name').hover(function(){
		$(this).next('.marker').contents('.panel').css({opacity : 0}).removeClass('hide');
		
		$(this).next('.marker').contents('.panel').filter(':not(:animated)').animate({ opacity : '1', left : 38}, 200).addClass('open');
	}, function(){
		$(this).next('.marker').contents('.panel').animate({opacity : '0', left : 58}, 200, function(){ 
																									$(this).removeClass('open');
																									$(this).next('.marker').contents('.panel').addClass('hide');
																								});	
	});
	
	//Show panel when mousing over on marker
	$('#maplist a.marker_image').mouseover(function(){
		var o = this;

		if($(this).siblings('.panel').hasClass('hide'))
		{
			$(this).siblings('.panel').css({opacity : 0}).removeClass('hide');
			
			$('#maplist .panel.open').animate({ opacity : '0', left : 38}, 200, function(){ $(this).removeClass('open')});
			$(this).siblings('.panel').animate({ opacity : '1', left : 38}, 200).addClass('open');
		}else{
			
			$(this).siblings('.panel').animate({ opacity : '0', left : 58}, 200, function(){ 
																							$(this).removeClass('open');
																							$(this).siblings('.panel').addClass('hide');
																						});

		}
	});
	
	$('#maplist a.marker_image').mouseout(function(){
		$('#maplist .panel').animate({opacity : 0, left : 58}, 200, function(){ 
			$(this).removeClass('open')
			$(this).addClass('hide');
		});
	});
}




// ============================
// = Course builder functions =
// ============================
function initialize_coursebuilder()
{
	$('#location_options input').change(function(){
		show_loader();
		
		//This function is in the templates, to allow automatic URL building using
		//EE's path functions
		fetch_data();
	})
	price_loader()
	request_more_info();
	
	//Book online button
	$('a.buy_now').click(function(){
        if(!$(this).hasClass('inactive')){
            document.courseBuilderForm.submit();
        }
        return false;
	});
	
	//Book online button
	$('a.invoice_me').click(function(){
        if(!$(this).hasClass('inactive')){
			$(document.courseBuilderForm).attr('action', '/index.php/contact/invoice');
            document.courseBuilderForm.submit();
        }
        return false;
	});
}

function show_loader()
{
	$('#method_options *:not(#loader)').fadeOut(200, function(){ 
		
		$(this).remove();
		$('#method_options #loader').css({opacity : 0}).removeClass('hide').animate({opacity : 1}, 200);
		$('.price_info').fadeOut(200);
		$('a.buy_now, a.invoice_me').addClass('inactive');
	});
	
}

function hide_loader(cb)
{
	$('#method_options #loader').animate({opacity : 0}, 200, function(){ 
		$(this).addClass('hide')
		cb();
	});
}

function price_loader()
{	
	//This is for manual selection
	$('#method_options input').live('change', function(){
		reset_success_message();
		var price = parseFloat($(this).attr('rel'));
		$('span#value').text(price.toFixed(2));
		$('input#total_price').val(price.toFixed(2) + assessment_fee);
		$('.price_info').fadeIn(200);
		$('a.buy_now, a.invoice_me').removeClass('inactive');
	});
}




// ====================================
// = Homepage course picker functions =
// ====================================

function initialize_course_finder()
{
	$('#course_finder_location').change(function(){
		course_finder_load_subject($(this).attr('value'));
	});
	
	$('#course_finder_subject').change(function(){
		course_finder_load_method($('#course_finder_location option:selected').attr('rel'), $(this).attr('value'));
	});
	
	
	
	//Fix IE7 stupid select width problem by hiding the image sliders
	
	if($.browser.msie){
		$('#course_finder select').change(function(){
			$('#course_finder select').css({'width' : 'auto'});
			$('#banner_image_rotator').animate({'width' : 0}, 400);
			$('#course_finder').animate({'width' : '926px'}, 400);
		})
	}

}

function course_finder_loader(action)
{
	if(action == 'hide')
	{
		$('#course_finder_loader').hide();
	}
	if(action == 'show')
	{
		$('#course_finder_loader').show();
	}
}

function course_finder_button(action)
{
	if(action == 'show')
	{
		$('#course_finder_submit_p').addClass('active');
//		$('#course_finder_submit_p input.submit').attr('disabled', '');
	}
	if(action == 'hide')
	{
		$('#course_finder_submit_p').removeClass('active');
//		$('#course_finder_submit_p input.submit').attr('disabled', 'disabled');
	}
}


// =============================
// = Homepage twitter link fix =
// =============================

function tidy_twitter(username, domposition)
{
	var str = $(domposition).text();
	
	//Get rid of the username
	str = str.replace(username + ": ", "");
	
	//Convert links
	str = linkify(str);
	
	$(domposition).html(str);
	
}

function linkify(text)
{
	if( !text ) return text;

	text = text.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,function(url){
		nice = url;
		/*if( url.match('^https?:\/\/') )
		{
			nice = nice.replace(/^https?:\/\//i,'')
		}
		else
			url = 'http://'+url;
*/

		return '<a target="_blank" rel="nofollow" href="'+ url +'">'+ nice.replace(/^www./i,'') +'</a>';
	});

	return text;
}


// =============================================
// = Homepage study options ajax functionality =
// =============================================
function initialize_study_options () {
	$("div.study_options").find("a.button").click(function(){
		
		$(this).prev().toggleClass("hide").prev().toggleClass("hide");
		
		if($(this).text() == 'View all ↓')
		{
			$(this).text('Hide all ↑');
		}else{
			$(this).text('View all ↓');
		}
		
		return false;
	});
}









