/**
 * Dialog
 */

$(document).ready(function() {
	
	/*
	 * Create dialog
	 */
	
	$('.dialog-click').live('click', function() {
		
		// Check already exists
		if ($('#dialog').length == 0) {
		
			// Create dialog
			$('body').append('<div id="dialog" title="..."><p>...</p></div><div id="dialog-bg" class="ui-overlay"><div class="ui-widget-overlay"></div></div>');
		
		}
		
		// Vars
		var dialog_id = $('.id', this).html();
		var dialog_width = $('.width', this).html();
		var dialog_type = $('.type', this).html();
		var dialog_title = $('.title', this).html();
		var dialog_content = $('.content', this).html();
		
		// Check if content is url
		if (dialog_content.substr(0, 1) == '/') {
		
			// Dialog content
			$.ajax({
				url: dialog_content,
				type: 'POST',
				data: {'id': dialog_id},
				success: function(html) {
					
					// Add message to dialog
					$('#dialog p').html(html);
					
				}
			});
		
		}
		else {
			
			// Add message to dialog
			$('#dialog p').html(dialog_content);
		
		}
		
		// Buttons
		var buttons = {};
		
		// Delete
		if (dialog_type == 'delete') {
			
			var dialog_url = $(this).attr('href');
			
			buttons = {
			 	'Verwijderen': function() {
			 		
					// Delete ajax
					$.ajax({
						url: dialog_url,
						type: 'POST',
						data: {'id': dialog_id},
						success: function(data) {
							
							// Redirect
		    				document.location.reload();
		    				
						}
					});
					
					// Remove dialog
					$(this).dialog("close");
					
				},
				'Annuleer': function() {
					
					// Remove dialog
					$(this).dialog('close');
					
				}
			}
			
		}

		// Duplicate
		else if (dialog_type == 'duplicate') {
			
			var dialog_url = $(this).attr('href');
			
			buttons = {
			 	'Dupliceren': function() {
			 		
					// Duplicate ajax
					$.ajax({
						url: dialog_url,
						type: 'POST',
						data: {'id': dialog_id},
						success: function(data) {
							
							// Redirect
		    				document.location.reload();
		    				
						}
					});
					
					// Remove dialog
					$(this).dialog("close");
					
				},
				'Annuleer': function() {
					
					// Remove dialog
					$(this).dialog('close');
					
				}
			}
			
		}
		
		// Show dialog
		$('#dialog').dialog({
			title: dialog_title,
			resizable: false,
			draggable: false,
			buttons: buttons,
			open: function(event, ui) {
				
				// Show background
				$('#dialog-bg').show();
				
			},
			beforeClose: function(event, ui) {
				
				// Dialog destroy
				$.dialogAction('destroy');
				
			}
		});
		
		// Check width
		if (dialog_width != null) {
			
			$('#dialog').dialog({
				width: dialog_width
			});
		
		}
		
		// Disable link
		return false;
		
	});
	
	/*
	 * Dialog actions
	 */
	
	jQuery.dialogAction = function(action) {
		
		// Action destroy
		if (action == 'destroy') {
			
			// Remove background
			$('#dialog-bg').remove();
			
			// Destroy dialog
			$('#dialog').dialog('destroy');
			
			// Remove dialog
			$('#dialog').remove();
			
		}
		
	}
	
	//##########################################################################################
	
	/*
	 * Create overlay
	 */
	
	$('.overlay-click').live('click', function() {
		
		// Check already exists
		//if ($('.ui-overlay').length == 0) {
		
		$('body .ui-overlay').remove();
		
			// Create overlay
			$('body').append('<div class="ui-overlay">'+
				'<div class="close"></div>'+
				'<div class="ui-widget-overlay"></div>'+
				'<div class="ui-widget-shadow ui-corner-all"></div>'+
				'<div class="overlay-content ui-widget ui-widget-content ui-corner-all">...</div>'+
			'</div>');
		
		//}
		
		// Vars
		var overlay_id = $('.id', this).html();
		var overlay_type = $('.type', this).html();
		var overlay_content = $('.content', this).html();
		
		var data = {'id': overlay_id};
		
		// Contact
		if (overlay_type == 'contact') {
			
			// Add to data
			$.extend(data, {'vce_id': $('input[name="vce_id"]').val()});
			
		}
		
		// Share
		else if (overlay_type == 'share') {
			
			// Add to data
			$.extend(data, {'vce_id': $('input[name="vce_id"]').val()});
			
		}
		
		// Share
		else if (overlay_type == 'application') {
			
			// Add to data
			$.extend(data, {'vce_id': $('input[name="vce_id"]').val()});
			
		}
		
		// Content
		$.ajax({
			url: overlay_content,
			type: 'POST',
			data: data,
			success: function(html) {
				
				// Add message to overlay
				$('.ui-overlay .ui-widget-content').html(html);
				
				// Show
				$('.ui-overlay').show();
				
				//######################################
				
				// Forms
				$('.form').livequery( function() {
				
					$.ajaxSubmitLoad(this);
				
				});
				
				//######################################
				
				// AddThis toolbox
				$('.addthis_toolbox').livequery( function() {
					
					addthis.toolbox(this);
				
				});
				
				//######################################
				
				// Shadow fix
				var overlay_height = $('.ui-overlay .ui-widget-content').height();
				$('.ui-overlay .ui-widget-shadow').height(overlay_height+60);
							
			}
		});
		
		// Disable link
		return false;
		
	});
	
	/*
	 * Close overlay
	 */
	
	$('.ui-overlay .close').live('click', function() {
		
		// Hide
		$('.ui-overlay').hide();
		
	});
	
});
