/**
 * Namespace "Base"
 * @class Base
 * @desc Base class of this project
 */
var Base = function() {
	// VARIABLES DEFINED HERE ARE PRIVATE
	var arr_init = []; // Array containing all registered init-functions of subclasses
	
	// FUNCTIONS DEFINED HERE ARE PRIVATE
	
	/**
	 * Adds an rollover alt text to a form row if it has got some extra info
	 */
	function showFormExtraInfo() {
		$('form div.title p').each(function( intIndex ){
			var elem	= $(this); // the current p element
			var parent	= elem.parents('div.title:first'); // the parent container
			var text	= elem.html(); // te extra info text
			
			// give the label element some padding to fit the extra info button
			parent.find('label').addClass("info");
			
			// place the extra info button
			parent.prepend('<span class="info' + (parent.find('label').hasClass('required-for-jobs') ? ' required-for-jobs' : '') + '"></span');
			var info = parent.find('span.info');
			
			// add functionality to the onfo button
			info.mouseover(function(e) { // built and show the info div
				$('body').prepend('<div class="info-hover">' + text + '</div>');
				$('body > div.info-hover').css({
					top: e.pageY+5, left: e.pageX+15
				});
				info.addClass('hover');
			}).mouseout(function() { // remove the info div
				$('body div.info-hover').remove();
				info.removeClass('hover');
			}).mousemove(function(e) { // follow the mouse
				$('body div.info-hover').css({
					top: e.pageY+5, left: e.pageX+15
				});
			});
		});
	}
	
	/**
	  * When a user slides over a persons photo thumbnail the the enlarged image is revealed from behind of it's parent container box 
	  */
	function slideOutLargePhoto() {
		$('img.show-enlarged').each(function( intIndex ){
			var elem	= $(this); // the current img element
			var parent	= elem.parents('.box:first');
			
			// adds the enlarged img container
			$('body').prepend('<div class="img-enlarged"></div>');
			var enlarged = $('body > div.img-enlarged').hide();
			
			elem.clone().removeAttr('class').removeAttr('alt').prependTo(enlarged);

			
			elem.hover(
				function(){
					var pos_parent = parent.offset();
					enlarged.css({ right: $('body').width() - pos_parent.left, top: pos_parent.top + 14 }).animate({width:'show'},350, 'easeOutQuart');
				},
				function() { 
					enlarged.animate({width:'hide'},250, 'easeInQuart');															  
				}
			);
		});
	}


	
	/**
	  * Check and execute registered subclass functions
	  */
	function check_register() {
		for (var i=0; i<arr_init.length; i++) {
			arr_init[i]();
		}
	}
	
	
	return { // Public area
		// VARIABLES DEFINED HERE ARE PUBLIC
		ie6	: ($.browser.msie && ( parseInt($.browser.version)==6 ) ) ? true : false,
		
		// FUNCTIONS DEFINED HERE ARE PUBLIC
		
		/**
		 * Register (initialization) calls from subclasses
		 * @param (obj_function) Function to initialize subclass
		 */
		register: function(obj_function) {
			arr_init.push(obj_function);
		},
		
		/**
		 * Logs a message tot the console or alerts it
		 * @param (message) The message to log
		 */
		log :function(message) {
 			if(window.console) {
    		console.debug(message);
			} else {
				alert(message);
			}
		},
    
		/**
		 * Initialize this class
		 */
		init: function() {
			check_register();
			
			showFormExtraInfo();
			slideOutLargePhoto()
			
			$('.empty-form').bind('click', function(e){
				e.preventDefault();
                if($(this).parents('.register-edu:first').length === 0) {
	    			$(this).siblings(':input').val('');
                }
			});
			
			$('.sync-radio').bind('click', function(e){
				var parent = $(this).parents('.fakepanel-center:first');
				parent.removeClass('sync nosync');
				if ($(this).is(':checked')) {
					parent.addClass('sync');
				} else {
					parent.addClass('nosync');
				}
			});
			
			$('.register-edu .empty-form').bind('click', function(e){
            	var confirmRemove = confirm('Weet je zeker dat je deze gegevens niet naar Myler wilt kopi\353ren?');
                if (confirmRemove){
    				$(this).parents('.clearChildren:first').find(':input').val('');
    				$(this).parents('.clearChildren:first').find(':checked').removeAttr('checked');                
                	$(this).parents('.clearChildren:first').remove();
                }
			});
			
			$('div.add-cb :checkbox').bind('click', function(e){
				var parent = $(this).parents('.education:first');
				parent.removeClass('sync nosync');
				if ($(this).is(':checked')) {
					parent.addClass('sync');
				} else {
					parent.addClass('nosync');
				}
			});
			
			// set the datepicker and it's settings
			$.dpText = {
				TEXT_PREV_YEAR		:	'Vorig jaar',
				TEXT_PREV_MONTH		:	'Vorige maand',
				TEXT_NEXT_YEAR		:	'Volgend jaar',
				TEXT_NEXT_MONTH		:	'Volgende maand',
				TEXT_CLOSE			:	'Sluiten',
				TEXT_CHOOSE_DATE	:	'Selecteer een datum'
			}
			
			
			$('.datepicker').each(function(i) {
				var parent = $(this).parents('.subcolumn:first');
						
				$(this).datePicker({startDate:'01/01/1960'}).
					bind('dateSelected', function(e, selectedDate, $td) {
						var selected = selectedDate.asString().split('/');
					
						parent.find('input[name=day]').val(selected[0]);	
						parent.find('input[name=month]').val(selected[1]);	
						parent.find('input[name=year]').val(selected[2]);
						// for GX the year input onChange is triggered
						parent.find('input[name=year]').trigger('change');
					});
				
				// is er een custom datum geset
				if (parent.find('input[name=selected_date]').length > 0) {
					var val = parent.find('input[name=selected_date]').val();
					if (val == 'today') {
						var currentTime = new Date();
						var month = '0' + (currentTime.getMonth() + 1);
						var day = '0' + currentTime.getDate();
						var year = currentTime.getFullYear();
						
						month = month.substring(month.length-2, month.length); 
						day = day.substring(day.length-2, day.length); 
						
						$(this).dpSetSelected(day + '/' + month + '/' + year);

					} else if (Date.fromString(val)) {
						$(this).dpSetSelected(val);
					}
				}
			});

			$('input.label').innerLabel();
			$('div.slideshow').imageSlideShow();
			
      // Create animated panels
      $('ul.animated li')
        .prepend('<span class="content-toggler"></span>')
        .find('.content-toggler')
          .hover(
            function(){ $(this).addClass('hover'); },
            function(){ $(this).removeClass('hover'); }
          ).end() // Set mouse-over & go back to first selection
        .infoPanel('span.content-toggler', 'div.content');
			
      if ($('div#tabmenu').length > 0) {
				$('div#tabmenu').infoTabs({cancelLinkAction:true});
			}
		}
	}
}();

$(document).ready(function(){
	Base.init();
});
