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

		var first_nav_el = $(subnav.sel + ":first");		
		var first_content_el = $(this.content_sel + ":first");
		
		// jQuery Address event
		$.address.change(function(event) {  						
			if(event.pathNames[0]) {
				params = event.pathNames;
				path = event.path				
				
				// ajax store URL #hash
				// $.get('/store_hashed_location?hashed_location=' + path);
								
				$(subnav.sel).each(function() {
					href = $(this).attr('href');
					clean_hash = href.replace(/^#/, '');					
					if ($.inArray(clean_hash, params) != -1 || $(this).attr('href') == path) {
						subnav.click_event(this);
					}
					
					// ugly hack for calendar
					if(params[0] == 'calendar') {
						$('a.calendar').trigger('click');
					}								
				});
			// no path in hash, loading first panel or triggering first link in selection
			} else {
				$(subnav.sel).removeClass('active');
				if (subnav.autoselect == 1) {
					subnav.trigger(first_nav_el);	
				} else {
					subnav.toggle_panes(first_content_el);
				}
			}
		});	
			
	},
	
	click_event : function(a) {
		if($(a).attr('hash')) {
			subnav.trigger(a);
			return false;				
		} else if($(a).hasClass('ajax')) {
			subnav.load_ajax_page(a);
			return false;
		}
	},
	
	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().removeClass('hide');
			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.toggle_panes(e);
		}
	},
	
	toggle_panes : function(e) {
		$(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();
	}
}

    
register_form = {
	
	init : function() {
		register_form.validates();
		
		$("form.register-form input.date").dateinput({ 
      lang: "fr",
      trigger: true,
      min: -1
    });				
	},
	
	validates : function() {
		$("form.register-form").validate({
			highlight: function(element, errorClass, validClass) {
			 	$(element).addClass(errorClass).removeClass(validClass).parent().addClass("with-errors");
			},
			unhighlight: function(element, errorClass, validClass) {
			 	$(element).removeClass(errorClass).addClass(validClass).parent().removeClass("with-errors");
			},
			submitHandler: function(form) {
				register_form.submit_form(form);
				return false;
			},                              
			// messages: REGISTER_FORM_MESSAGES
		}); 
	},
	
	submit_form : function(form) {
		var url = $(form).attr('action');
		var parent = $(form).parent();
		var params = $(form).serialize();
	
		block_ui(dhc_messages.wait);	
		$.ajax({
		  url: url,
		  dataType: 'json',
		  data: params,
			type: "POST",
		  success: function(response) {
				$.unblockUI();
				if(response.success) {
					$(form).slideUp();
					$(parent).append('<h4>'+dhc_messages.thanks+'</h4>');
				} else {
					$(form).slideDown();
				}
			},
			error : function() {
				$.unblockUI();				
			}
		});
	}	  
}

function block_ui(message) {
	jQuery.blockUI({
		css: { 
	    border: 'none', 
	    padding: '25px', 
			width: "200px",
			'font-size': '18px',
			left: "37%",
	    backgroundColor: '#E31B23',
	    color: '#fff'
		},
		message: message,
		baseZ: 100000
	});
}


$(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);
});



