// Default Constants values
var DHC_CLOSED = false;
                      

// sub navigation
subnav = {

	init : function(sel, content_sel, autoselect) {		
		// globals
		this.sel = sel || '#sub-nav li a';
		this.content_sel = content_sel || '#main div.subnav-content';
		this.autoselect = autoselect || 1;
				
		// delay 10 ms before initing the whole thing (Safari 3.2.x bug)		
		t = 0;				
		var timer = setInterval(function() {
			t++;
			if (t == 10) {
				clearInterval(timer);

				$(sel).click(function(){
					if($(this).attr('hash')) {
						subnav.trigger(this);
						return false;				
					} else if($(this).hasClass('ajax')) {
						subnav.load_ajax_page(this);
						return false;
					}
				});					

				// activate first menu item and its content
				if($(sel).length > 1 && subnav.autoselect == 1) {
					var first_child = $(subnav.sel + ":first");		
					subnav.trigger(first_child);
				}

			}
		}, 10);		
			
	},
	
	load_ajax_page : function(a) {		
		$.get(a.href, function(data) {
			e = $("#ajax-container");
			$(subnav.content_sel).hide().addClass('hide');	
			subnav.adjust_content_height(e);
		 	e.html(data).show();
			subnav.activate_nav(a);
		});		
	},
	
	trigger : function(a) {
		subnav.display_pane_content(a);
		subnav.activate_nav(a);
	},	
	
	display_pane_content : function(e) {	
		var hash = $(e).attr('hash') || null;
		
		if(hash) {
			// find element with link #hash
			var e = $('body').find($(e).attr('hash'));
			$(subnav.content_sel).hide().addClass('hide');		
			e.show().removeClass('hide');	
			subnav.adjust_content_height(e);
		}
	},
	
	activate_nav : function(e, sel) {
		$(subnav.sel).removeClass('active');
		$(e).addClass('active');
	},
	
	adjust_content_height : function(e) {
		var current_height = $(e).outerHeight(true);
		var new_height = subnav.calculate_content_height($(e).children());
		var vertical_margin = 20;
				
		// do nothing if new height is smaller than min-height
		if(new_height > current_height) {
			$(e).css('height', new_height + vertical_margin);	
		}		
	},
	
	calculate_content_height : function(elements) {
		var height = 0;
		$(elements).each(function() {
			height += $(this).outerHeight(true);
		});		
				
		return height;		
	}

};

students = {

	init : function(sel, education_id, path) {
		
		// 1. attach even on main select (group)
		$(sel).change(function() {
			
			// find group letter
			var letter = $(this).find(":selected").attr('name');
						
			$.post(path, { education_id: education_id, letter: letter }, function(data) {
				$("#students").html(data);

				// init students medias
				students.init_medias();	
				
				// reset container height
				subnav.adjust_content_height($('#description'));			
			});
			
			// reset any opened group and no-students message
			$('.students-group, .no-students-found').hide();
			
			// empty student area
			$('#students').empty();
			
			// will try select element based on "select" return value : #group-a, #group-b, etc
			var group = jQuery.find($(this).val());				
			if(group.length > 0) {				
				$(group).show().trigger('change');
			} else {
				$(".no-students-found").show();
			}						
		});
				
		// 2. attach event on students groups <select>
		$('select.students-group').change(function() {
			// find sibling link and trigger click on it
			var id = $(this).val();			
			var a = $('a#' + id);
			if(a) { $(a).trigger('click'); }
		});
	
		// 3. init media on first page
		students.init_medias();
	},
	
	init_medias : function() {		
		// re init pretty photo
		$("a[rel^='prettyPhoto']").prettyPhoto({
					animationSpeed: 'fast', /* fast/slow/normal */
					padding: 20, /* padding for each side of the picture */
					opacity: 0.85, /* Value betwee 0 and 1 */
					showTitle: false, /* true/false */
					allowresize: true, /* true/false */
					counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
					theme: 'dark_square' /* light_rounded / dark_rounded / light_square / dark_square */
		});
		
		// facebox
		$("a[rel^='facebox']").facebox({
			overlay				: true,
			opacity				: 0.85,
			loadingImage	: '/stylesheets/prettyPhoto/dark_square/loader.gif',
	    closeImage   	: '/images/btnClose.png'
		});
		
		// re init all videos
		$('a.video').each(function() {
			$(this).flash({ 
				swf: '/swf/player.swf',
				width: 520,
				height: 300,
				allowfullscreen: true,
				flashvars: {  
					'file': this.href,
					'autostart': false,
					'skin': '/swf/skins/modieus.swf'
				}		
			});
		});
	}
		
};


// toggle show/hide link for long description
function excerpt_content(e) {	
	var min_childs = 6;
	var excerpt_length = 1;
	
	children = $(e).children().not('a#more');	
	if(children.length > min_childs) {
	  $("a#more").show();
  	children.each(function(i) {
  	  if(i > excerpt_length) {
  	    $(this).hide();
  	  }
  	});	  
	} else {
		$("a#more").hide();
	}
	
	$("a#more").click(function() {
    children.show();
    subnav.adjust_content_height($('#description'));
    $(this).hide();
    return false;
	});
}

// teasers panel on homepage
// depends on jQuery cycle plugin
teasers = {
	
	init : function() {
		$('#home-teasers').cycle(
			{
				fx:      'scrollDown', 
				speed:    350, 
				timeout:  5000,
				width: 		840,
				height: 	360
			}
		);
	}
	
};

function embeded_animated_background() {
	var width = $(window).width();
	var height = $(window).height();
	
	$('#animated-background').flash({ 
		swf: '/swf/DHC_back.swf',
		width: width,
		height: height,
		id: $(this).id,
		params: {  
			wmode: 'transparent'
		}		
	});  	
}

function resize_animated_background() {
	var width = $(window).width();
	var height = $(window).height();
	$('#animated-background').children().attr('width', width);
	$('#animated-background').children().attr('height', height);	
}
         

show_closed_alert = {
		         
	settings : {
		el: "#closed-alert"
	},
		
	init : function() {		
		this.move();
		this.overlay();
		
		var close = $(this.settings.el).find('a');
		$(close).click(function() {
			show_closed_alert.close();
		})
	},    
	              
	move : function() {
		var el = $(this.settings.el);
		var window_height = $(window).height();               
		var top = parseInt((window_height - el.height()) / 2);

    // set top position and show element 
		el.css('top', top).show();
	},
	
	overlay: function() {
		$('body').append('<div id="closed-alert-overlay"></div>');
		$('#closed-alert-overlay').height($(document).height()).css('opacity', 0.5).click(function() { show_closed_alert.close() });
	},
	
	close : function() {
		$(this.settings.el).hide();
		$('#closed-alert-overlay').hide();  
		
		// set cookie                      
		$.cookie('dhc_alert_history', true, { expires: 14 });
		
	}
	
}


// on window resize methods to call
function on_resize_methods() {
	resize_animated_background();
	
	// resize only if DHC closed alert is active
	if(DHC_CLOSED && !$.cookie('dhc_alert_history')) {
		show_closed_alert.move();
	}
}


$(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});


