/**
 * Methods
 */

var addthis_config = {
     ui_language: 'nl'
}

/*
addthis.addEventListener('addthis.menu.close', eventHandler);

function eventHandler(evt) {

	console.log('jep');

}
*/

$(document).ready(function() {
	
	/*
	 * Datepicker
	 */
	
	$('.datepicker').datepicker({
		dateFormat: 'dd-mm-yy'
	});
	
	//#########################################################################################################
	
	/*
	 * Application - Upload
	 */
	
	$('#application-upload').MultiFile({
		max: 1,
	});
	
	//#########################################################################################################
	
	/*
	 * Submit click
	 */
	
	var clicked_submit = '';
	var clicked_submit_element = '';
	
	$('input[type="submit"]').live('click', function() {
		
		// Set clicked submit
		clicked_submit = $(this).attr('name');
		clicked_submit_element = $(this);
		
	});
	
	/*
	 * Validation
	 */
	
	jQuery.ajaxSubmitLoad = function(form) {
	
		// Set form
		var this_form = $(form);
		
		// Validate
		$(this_form).validate({
 			invalidHandler: function(form, validator) {
 			
 				if (!$(clicked_submit_element).hasClass('cancel')) {
 			
	 				// Remove old
	 				$('label', this_form).removeClass('field-error');
	 				$('input', this_form).removeClass('field-error');
	 			
	 				// Errors
	 				var errors = validator.invalid;
	 			
	 				for (var field_name in errors) {
	 				
	 					$('label[for="'+field_name+'"]').addClass('field-error');
	 					$('input[name="'+field_name+'"]').addClass('field-error');
	 				
	 				}
 				
 				}
 			
 			},
 			errorPlacement: function(error, element) {
 			
 			},
		});
		
		// OnSubmit
		$(this_form).submit( function() {
		
			$(clicked_submit_element).attr('disabled', 'disabled');
		
			if ($(this_form).hasClass('vacancy')) {
			
				$('.vacancy-results').html('').addClass('loading');
			
			}
		
		    // submit the form 
		    $(this).ajaxSubmit({
		    	dataType: 'json',
		    	data: {'frm_submit': clicked_submit},
		    	success: function(json) {
		    		
		    		//console.log('goed');
		    		//console.log(json);
		    		
		    		// Success
		    		if (json.success != undefined) {
		    			
		    			// Redirect
		    			if (json.redirect != undefined) {
		    				
		    				// Redirect
		    				document.location = json.redirect;
		    				
		    			}
		    			
		    			// Close overlay
		    			else if (json.action == 'close-overlay') {
		    			
		    				// Hide
							$('.ui-overlay').hide();
		    			
		    			}
		    			
		    			// Ajax
		    			else if (json.ajax != undefined) {
		    				
							$.ajax({
								type: 'POST',
								url: json.ajax,
								success: function(output) {
								
									// Vacancy results
									if ($('.vacancy-results').length > 0) {
								
										$('.vacancy-results').html(output).removeClass('loading');
										
										$('.vacancy-scrollbar').jScrollPane({
											showArrows: true,
											horizontalGutter: 30,
											verticalGutter: 30
										});
									
									}
									
									else {
									
										$('.overlay-content').html(output);
									
									}
									
									// Shadow fix
						    		if ($('.ui-overlay').length > 0) {
							
										// Shadow fix
										var overlay_height = $('.ui-overlay .ui-widget-content').height();
										$('.ui-overlay .ui-widget-shadow').height(overlay_height+60);
									
									}
								
								}
							});
		    			
		    			}
		    			
		    		}
		    		
		    		$(clicked_submit_element).removeAttr('disabled');
		    		
		    	},
		    	error: function (xhr, ajaxOptions, thrownError) {
		    		
		    		//console.log('fout');
		    		//console.log(xhr);
		    		
		    		// Show error
		    		//$('.response-text:first', this_form).show();
		    		//$('.response-text:first', this_form).html(xhr.responseText);
		    		
		    		$(clicked_submit_element).removeAttr('disabled');
		    		
		    		// Shadow fix
		    		if ($('.ui-overlay').length > 0) {
			
						// Shadow fix
						var overlay_height = $('.ui-overlay .ui-widget-content').height();
						$('.ui-overlay .ui-widget-shadow').height(overlay_height+60);
					
					}
		    		
		    	}
		    });
		    
		    return false;
		    
		});
	
	};
	
	//$('.form').each( function() {
	$('.form').livequery( function() {
	
		$.ajaxSubmitLoad(this);
		
		// Vacancy search submit
		if ($('.vacancy-scrollbar').length > 0) {
		
			$('form.vacancy').submit();
		
		}
	
	});
	
	//#########################################################################################################
	
	/*
	 * Maps
	 */
	
	$('.map').each( function() {
		
		if ($(this).attr('lat') != "" || $(this).attr('lng') != "") {
		
		    var latlng = new google.maps.LatLng($(this).attr('lat'), $(this).attr('lng'));
		    
		    var myOptions = {
		      zoom: 15,
		      center: latlng,
		      mapTypeId: google.maps.MapTypeId.ROADMAP
		    };
		    
		    var map = new google.maps.Map(document.getElementById($(this).attr('id')), myOptions);
		    
		    var marker = new google.maps.Marker({
		        position: latlng, 
		        map: map
		    });
		    
		}
	
	})
	
	//#########################################################################################################
	
	/*
	 * Dropdownable
	 */
	 
	// Default load
	$('.dropdownable').each( function() {
	
		// Get value
		var value = $('input', this).val();
		
		// Not empty
		if (value != "") {
			
			if (value.length > 15) {
				value = value.substr(0,15)+'…';
			}
			
			// Title
			$('span', this).html(value);
			
		}
	
	});
	
	$('.dropdownable').click( function() {
	
		if ($(this).hasClass('active')) {
		
			$(this).removeClass('active');
		
		}
		
		else {
		
			// Remove all
			$('.dropdownable.active').each( function() {
			
				$(this).removeClass('active');
			
			});
			
		
			// Add selected
			$(this).addClass('active');
		
		}
	
	});
	
	$('.dropdownable li').click( function() {
	
		if ($(this).hasClass('all')) {
		
			var value = $(this).attr('label');
			
			// Input
			$(this).parent().parent().find('input').val('');
		
		}
		
		else {
	
			var value = $(this).html();
			
			// Input
			$(this).parent().parent().find('input').val(value);
			
		}
		
		if (value.length > 15) {
			value = value.substr(0,15)+'…';
		}
		
		// Title
		$(this).parent().parent().find('span').html(value);
		
		
		
	});
	
	/*
	 * Tooltip
	 */
		
	// Positie van hover object bepalen
	$('#list-wrapper .vacancy-results li').live('mouseover mouseout', function(event) {
	
		if (event.type == 'mouseover') {
	
			var content = $(this).find('p').html();
			var pos = $(this).position();
			var scroll = $('.jspPane').position();
			
			$("#tooltip p").html(content);
			$("#tooltip").css({
				display: 'block',
				top: pos.top+scroll.top+'px'
			});
		
		}
		
		else {
		
			$("#tooltip").css({
				display: 'none'
			});
			
		}
	
	});
	
	$("#tooltip").live('mouseover mouseout', function(event) {
	
		if (event.type == 'mouseover') {
	
			$(this).css({display: 'block'});
	
		}
		
		else {
	
			$(this).css({display: 'none'});
		
		}
	
	});
	
	/*
	 * Scrollbar
	 */
	
	$('.scrollbar').jScrollPane({
		showArrows: true,
		horizontalGutter: 30,
		verticalGutter: 30
	});
	
	//#########################################################################################################
	
	/*
	 * Vacancy
	 */
	 
	$('form.vacancy input[name="srh_value"], form.vacancy input[name="srh_zipcode"]').click( function() {
	
		if ($(this).val() == $(this).attr('default')) {
		
			$(this).val('');
		
		}
	
	});
	
	$('form.vacancy input[name="srh_value"], form.vacancy input[name="srh_zipcode"]').focusout( function() {
	
		if ($(this).val() == '') {
	
			$(this).val($(this).attr('default'));
		
		}
	
	});
	
});
