// Default Constants values
var DHC_CLOSED = false;
                      

// sub navigation
subnav = {

	init : function(sel, content_sel) {		
		// globals
		this.sel = sel || '#sub-nav li a';
		this.content_sel = content_sel || '#main div.content';
		
		// 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;				
					}
				});					

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

			}
		}, 10);		
			
	},
	
	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) {
		
		// 1. attach even on main select (group)
		$(sel).change(function() {
			
			// reset any opened group and no-students message
			$('.students-group, .no-students-found').hide();
			
			// empty student area
			$('#student-details').html('');
			
			// 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');
				// $(group).find('select')
			} else {
				$(".no-students-found").show();
			}
		});
		
		// 2. attach ajax event on students groups <select>
		$('select.students-group').change(function() {

			// empty student area
			$('#students-details').html('');
			
			var url = "/fr/student/" + $(this).val();			
			$.get(url, function(data){
			  // console.log("Data Loaded: " + data);
				$('#student-details').html(data);
				students.init_medias();
			});
		});
	
		// 3. then trigger main SELECT element
		$(sel).trigger('change');							
	},
	
	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 */
		});
		
		// 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'
				}		
			});
		});
	}
		
};

// 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);
});


