
// render create lightbox dialog
function renderCreateLightboxDialog(){

	var filename = '/' + labels.lang + '/lightboxes.html';

	var send = {
		'ajax': "2",
		'action': "render-create-lightbox-dialog"
	}

	var encoded = $.toJSON(send);
    var params = $.evalJSON(encoded);

	$.post(filename, params, function (response){

		//create content
		var decoded = $('<div></div>').html(response);
		var content = $('<div id="create-lightbox-dialog"></div>');
		content.html(decoded);

		// create dialog
		var dialog = content.dialog({
            title: 'Create new lightbox',
            dialogClass: 'ngl_dialog_container',
            autoOpen: false,
            position: 'center',
            height: 'auto',
            width: 'auto',
            modal: true,
            draggable: false,
            resizable: false
		});

		// open dialog
		if( !$('#create-lightbox-dialog').dialog("isOpen") ){
			dialog.dialog('open');			
		}
		
		// destroy dialog on close	
		$("#create-lightbox-dialog").bind("dialogclose", function(){
        	$("#create-lightbox-dialog").dialog('destroy');
            $("#create-lightbox-dialog").remove();
		});
	});
}

// create lightbox
function createLightbox( obj ){

	var process = true;

	var form		= obj.parents("#createLightboxForm");

	var lb_title	= $("input[name='lb_title']", form).val();
	var lb_descr	= $("textarea[name='lb_descr']", form).val();

	if( lb_title == '' ){
		$("input[name='lb_title']", form).addClass('error');
		process = false;
	}	

	if( process ){

		var filename = '/' + labels.lang + '/lightboxes.html';

		var send = {
			'ajax': "2",
			'action': "create-lightbox",
		}

		send.lb_title = lb_title;
		send.lb_descr = lb_descr;

		var encoded = $.toJSON(send);
		var params = $.evalJSON(encoded);

		$.ajax({
			"url": filename, 
			"data": params,
			"dataType": "json",
			"type": "POST",
			"async": false,
			"success": function( json_response ){
				if( typeof json_response.error && json_response.error ){
					console.log( json_response.error );
				}
				else {
					document.location.href = filename;
				}		
				return false;
			}
		});
	}
}

// render edit lightbox dialog
function renderEditLightboxDialog( obj ){

	var process = true;

	var lb_id = obj.attr('data-lightbox-id');

	if( lb_id == '' || lb_id == 0 ){
		process = false;
	}

	if( process ){

		var filename = '/' + labels.lang + '/lightboxes.html';

		var send = {
			'ajax': "2",
			'action': "render-edit-lightbox-dialog"
		}

		send.lb_id = lb_id;

		var encoded = $.toJSON(send);
		var params = $.evalJSON(encoded);

		$.post(filename, params, function (response){

			//create content
			var decoded = $('<div></div>').html(response);
			var content = $('<div id="edit-lightbox-dialog"></div>');
			content.html(decoded);

			// create dialog
			var dialog = content.dialog({
				title: 'Edit lightbox',
				dialogClass: 'ngl_dialog_container',
				autoOpen: false,
				position: 'center',
				height: 'auto',
				width: 'auto',
				modal: true,
				draggable: false,
				resizable: false
			});

			// open dialog
			if( !$('#edit-lightbox-dialog').dialog("isOpen") ){
				dialog.dialog('open');			
			}
			
			// destroy dialog on close	
			$("#edit-lightbox-dialog").bind("dialogclose", function(){
				$("#edit-lightbox-dialog").dialog('destroy');
				$("#edit-lightbox-dialog").remove();
			});
		});
	}
}

// edit lightbox
function editLightbox( obj ){

	var process = true;

	var form		= obj.parents("#editLightboxForm");

	var lb_title	= $("input[name='lb_title']", form).val();
	var lb_descr	= $("textarea[name='lb_descr']", form).val();
	var lid			= $("input[name='lid']", form).val();

	if( lb_title == '' ){
		$("input[name='lb_title']", form).addClass('error');
		process = false;
	}	

	if( process ){

		var filename = '/' + labels.lang + '/lightboxes.html';

		var send = {
			'ajax': "2",
			'action': "edit-lightbox",
		}

		send.lb_title = lb_title;
		send.lb_descr = lb_descr;
		send.lid = lid;

		var encoded = $.toJSON(send);
		var params = $.evalJSON(encoded);

		$.ajax({
			"url": filename, 
			"data": params,
			"dataType": "json",
			"type": "POST",
			"async": false,
			"success": function( json_response ){
				if( typeof json_response.error && json_response.error ){
					console.log( json_response.error );
				}
				else {
					document.location.href = filename;
				}		
				return false;
			}
		});
	}
}

// delete lightbox
function deleteLightbox( obj ){

	var process = true;

	var lid = obj.attr('data-lightbox-id');

	if( lid == '' || lid == 0 ){
		process = false;
	}

	if( process ){

		var filename = '/' + labels.lang + '/lightboxes.html';

		var send = {
			'ajax': "2",
			'action': "delete-lightbox",
		}

		send.lid = lid;

		var encoded = $.toJSON(send);
		var params = $.evalJSON(encoded);

		$.ajax({
			"url": filename, 
			"data": params,
			"dataType": "json",
			"type": "POST",
			"async": false,
			"success": function( json_response ){
				if( typeof json_response.error && json_response.error ){
					
					//create content
					var decoded = $('<div></div>').html( 'This lightbox can\'t be deleted, it\'s assigned to an event!' );
					var content = $('<div id="status-dialog"></div>');
					content.html(decoded);
					
					// create dialog
					var dialog = content.dialog({
						title: 'Status',
						dialogClass: 'ngl_dialog_container',
						autoOpen: false,
						position: 'center',
						height: 'auto',
						width: 'auto',
						modal: true,
						draggable: false,
						resizable: false
					});

					// open dialog
					if( !$('#status-dialog').dialog("isOpen") ){
						dialog.dialog('open');			
					}
					
					// destroy dialog on close	
					$("#status-dialog").bind("dialogclose", function(){
						$("#status-dialog").dialog('destroy');
						$("#status-dialog").remove();
					});
				}
				else {
					document.location.href = filename;
				}		
				return false;
			}
		});
	}
}

// TODO - Delete images from lightbox page
function deleteImageFromLightbox(obj){
	var filename = String($(obj).attr('href').split("?",1));
    var myparams = getparams($(obj).attr('href'));
	myparams.pop();
	var paramsStr = '';
    $(myparams).each(function(i){
        paramsStr += myparams[i]+',';
    });
    paramsStr += 'ajax:2';
    var params = $.evalJSON('{'+paramsStr+'}');
	//debuglog(params);
	$.ajax({
		"type":"GET",
		"async":false,
		"url":filename,
		"data":params,
		"success":function(json){
			var respond = $.evalJSON(json);
			if( typeof respond.err != 'undefined' && respond.err.length !== 0){
				show_stack_bottomleft(true, respond.err);	
			}
			
			if(typeof respond.msg != 'undefined' && respond.msg.length !== 0){
				show_stack_bottomleft(false, respond.msg);	
				$(obj).parents("div.ImageLBBigBlock").fadeOut(800, function(){
					$(this).remove();
				});	
			}
		}
	});
	
	//$(obj).parents(".ImageLBBigBlock").find("[name='image_id'][value='"+params.image_id+"']");
}

// on load methods
$(window).load( function(){

	// create lightbox dialog trigger
	$(".render-create-lightbox-button").live('click', function(e){
		e.preventDefault();
		renderCreateLightboxDialog();	
	});	

	// create lightbox trigger
	$(".create-lightbox-button").live('click', function(e){
		e.preventDefault();
		createLightbox( $(this) );	
	});

	// edit lightbox dialog trigger
	$(".render-edit-lightbox-button").live('click', function(e){
		e.preventDefault();
		renderEditLightboxDialog( $(this) );	
	});	

	// edit lightbox trigger
	$(".edit-lightbox-button").live('click', function(e){
		e.preventDefault();
		editLightbox( $(this) );	
	});	

	// delete lightbox trigger
	$(".delete-lightbox-button").live('click',function(){	
		var functionToCall = deleteLightbox;
		deleteNGLConfirmation( functionToCall, $(this) );
	});
	
} );
