Cufon.replace('h1');
Cufon.replace('#header-nav a');
Cufon.replace('#sidebox h2');
Cufon.replace('#sidebox ul a');
Cufon.replace('body.blog .post h2');
Cufon.replace('body.blog .post h3');
Cufon.replace('.homeslides h3');
Cufon.replace('dl.slides dt');

$(document).ready(function() {

	// open external links in a new window. These links must be marked with the
	// rel attribute.

	$("a[rel=external]").click(function() {
		window.open(this.href);
		return false;
	});
	
	// replace email span by real link
	
	$(".email").each(function() {
		var href = $(this).text().replace(" (at) ", "@").replace(" (dot) ", ".");
		$(this).html('<a href="mailto:' + href + '">' + href + '</a>');
	});
	
	$('div.contact-block dt').each(function() {
		var href = $(this).find('span').text().replace(" (at) ", "@").replace(" (dot) ", ".");
		var text = $(this).find('strong').text();
		if (href && text) {
			$(this).html('<a href="mailto:' + href + '">' + text + '</a>');
		}
	});
	
	// lazy load plugin

	$("img").not("dl.slides img").lazyload({ 
		placeholder : TEMPLATE_URL + "/css/img/x.gif",
		effect : "fadeIn"
	});
	
	// Infinite Scroll
	
	$("#posts").infinitescroll({
		debug           : false,
		nextselector    : ".subnav a:first",
		loadingImg      : TEMPLATE_URL + "/css/img/load.gif",
		loadingText     : "<em>Loading more posts...</em>",
		donetext        : "<em>No more posts to display</em>",
		navSelector     : ".subnav",
		contentSelector : "#posts",
		itemSelector    : "#posts div.post"
	});

	// === teknik college ===
	
	// slide show
	
	$('dl.slides').each(function() {
		var slide = $(this);
		slide.find('dt:first-child').addClass('active').next().addClass('active');
		slide.find('dt').click(function() {
			slide.children().removeClass('active');
			$(this).addClass('active').next().addClass('active');
			_gaq.push(['_trackEvent', 'slides', 'click', '']);
		});
	});
	
	$('dl.slides dt').hello_makeFocusable();

	// new start page
	setTimeout("startpage_anim(1)",500);
	
	$('.homeslides>div').css({ 'top': '338px' }).live("mouseover",function() { 
		if(!$(this).hasClass("active") && !$(this).hasClass("nowmoving")) $(this).css({ top: "273px" }); 
	}).live("mouseout",function(){
		if(!$(this).hasClass("active") && !$(this).hasClass("nowmoving")) $(this).css({ top: "278px" });
	}).click(function(){
		var animobj=this;
		$('.active').not(animobj).animate({ top: "278px" },500,'easeOutBack',function() { $(this).removeClass("active"); });
		if($(animobj).hasClass("active")) {
			$(animobj).addClass('nowmoving').animate({ top: "278px" },500,'easeOutBack',function() { $(animobj).removeClass("active").removeClass('nowmoving'); }); 
			_gaq.push(['_trackEvent', 'startpageslides', 'hide', '']);
		} else {
			$(animobj).addClass('nowmoving').animate({ top: "90px" },500,'easeOutBack',function() { $(animobj).addClass("active").removeClass('nowmoving'); }); 
			_gaq.push(['_trackEvent', 'startpageslides', 'show', '']);
		}
	});
	
	// print
	
	$('li.social-print span')
		.css('display', 'block')
		.click(function() {
			window.print();
			return false;
		})
		.hello_makeFocusable()
	;
	
	// dropdowns

	$('#contact-list .dropdown').hello_asDropdown(function(id) {
		if (id) {
			$('.regions li').not('li#r' + id).hide();
			$('li#r' + id).show();
		}
	});
	$('.js_dropdown .dropdown').hello_asDropdown(function(id) {
		if (id) {
			$('#' + id).parents('.js_dropdown').find('.js_show li').not('li#' + id).hide();
			$('.js_show li#' + id).show();
		}
	});
	
	$('#region-select .dropdown').hello_asDropdown(
		function(id) { location.href = id; },
		function(href) { return href; }
	);

	$('#filters .dropdown')
		.hello_asDropdown(function() {})
		.bind('change', function() {
			var alt = false;
			var education = $('.education-filter .dropdown').data('selected-value') || null;
			var region    = $('.region-filter .dropdown').data('selected-value')    || null;
			var city      = $('.city-filter .dropdown').data('selected-value')      || null;
			var niveau    = $('.niveau-filter .dropdown').data('selected-value')    || null;
			if (education || region || city || niveau) {
				$('ul#result-set li').each(function() {
					var that = $(this);
					var cond = (!education || that.is('.e' + education)) &&
					           (!region    || that.is('.r' + region)) &&
					           (!city      || that.is('.c' + city)) &&
					           (!niveau    || that.is('.n' + niveau));
					
					if (cond) {
						that.show().toggleClass('alt', alt);
						alt = !alt;
					} else {
						that.hide();
					}
				});
				$('p.not-found-message').toggle($('ul#result-set li:visible').length == 0);
			} else {
				$('ul#result-set li').hide();
				$('p.not-found-message').hide();
			}
		})
	;
	
	// link on excerpt
	
	$('.post .excerpt').click(function() {
		location.href = $(this).prev().find('a').attr('href');
	});
	
	// track language clicks
	$('#language a').click(function(){
		_gaq.push(['_trackEvent', 'language', 'click '.this.parentNode.className, this.href]);
	});
});

function startpage_anim(no) {
	$('.homeslides .slide-'+no).animate({ top: '278px' },500,'easeOutBack');
	if (no<3) setTimeout('startpage_anim('+(++no)+')',300);
}

// dropdown handling

(function($) {
	
	var SLIDE_UP_TIME = 100;

	$.fn.hello_asDropdown = function(callback, getId) {
		getId = getId || function(href) {
			var parts = href.split('#');
			return parts.length > 1 ? parts.pop() : null;
		};
		
		return this.each(function() {
			var that = $(this);
			var data = that.find('div.data');
			that.find('h4 a').click(function(e) {
				e.preventDefault();
				if (data.is(':hidden')) {
					$('.dropdown div.data:visible').slideUp(SLIDE_UP_TIME);
					_gaq.push(['_trackEvent', 'dropdown', 'hide', 'click dropdown']);
				}
				data.slideToggle(SLIDE_UP_TIME);
			});
			data.find('a').click(function(e) {
				e.preventDefault();
				var id = getId(this.href);
				if (callback(id) !== false) {
					that.find('h4 a').text($(this).text());
					that.data('selected-value', id);
					data.slideUp(SLIDE_UP_TIME);
					that.trigger('change');
					_gaq.push(['_trackEvent', 'dropdown', 'click', '']);
				}
			});
		});
	};

	$(window).keyup(function(e) {
		if (e.keyCode == 27) {
			$('.dropdown div.data:visible').slideUp(SLIDE_UP_TIME);
			_gaq.push(['_trackEvent', 'dropdown', 'hide', 'keyevent 27']);
		}
	});
	
	$(document).click(function(e) {
		if($(e.target).parents('.dropdown').length == 0) {
			$('.dropdown div.data:visible').slideUp(SLIDE_UP_TIME);
			_gaq.push(['_trackEvent', 'dropdown', 'hide', 'click outside']);
		}
	});

})(jQuery);

