// render collect image dialog
function renderZoomImage( filename, levels ){

	// create content
	var content = $('<div id="zoom-image-dialog" style="width:512px;height:512px;margin:0;padding:0;"><iframe style="width:512px;height:512px;margin:0;padding:0;" width="512px" height="512px" src="/'+labels.lang+'/zoom.html?ajax=2&image='+filename+'&levels='+levels+'" frameborder="0" marginheight="0" marginwidth="0"></iframe></div>');	
	
	// create dialog
	var dialog = content.dialog({
        title: 'Zoom image',
        dialogClass: 'ngl_dialog_container',
        autoOpen: false,
        position: 'center',
        height: 'auto',
        width: 'auto',
        modal: true,
        draggable: false,
        resizable: false
	});

	// open dialog
	if( !$('#zoom-image-dialog').dialog("isOpen") ){
		dialog.dialog('open');			
	}
	
	// destroy dialog on close	
	$("#zoom-image-dialog").bind("dialogclose", function(){
    	$("#zoom-image-dialog").dialog('destroy');
        $("#zoom-image-dialog").remove();
	});

}

// delete pending image
function deletePendingImage( $element ) {
	
	var image_id = $element.attr('data-image-id');
	var filename = '/'+labels.lang+'/uploads.html';
	
	var data = {
		action: "delete-image",
		ajax: 2,
		image_id: image_id    		
	};
	
	$.ajax({
		"url": filename, 
		"data": data,
		"dataType": "json",
		"type": "POST",
		"async": false,
		"success": function( json_response ){
 	
			// check error
			if ( typeof(json_response.error) == 'undefined' ) {
				return false;
			}
		
			if( !json_response.error ) {
				$element.parents('tr').remove();
			}			
					
			return false;
		}
	});
    
}

// render collect image dialog
function renderCollectImageDialog( obj ){

	var filename = '/' + labels.lang + '/lightboxes.html';

	var send = {
		'ajax': "2",
		'action': "render-collect-image-dialog"
	}

	var img_id = obj.attr('data-image-id');
	send.img_id = img_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="collect-image-dialog"></div>');
		content.html(decoded);

		// create dialog
		var dialog = content.dialog({
            title: 'Add image to lightbox',
            dialogClass: 'ngl_dialog_container',
            autoOpen: false,
            position: 'center',
            height: 'auto',
            width: 'auto',
            modal: true,
            draggable: false,
            resizable: false
		});

		// open dialog
		if( !$('#collect-image-dialog').dialog("isOpen") ){
			dialog.dialog('open');			
		}
		
		// destroy dialog on close	
		$("#collect-image-dialog").bind("dialogclose", function(){
        	$("#collect-image-dialog").dialog('destroy');
            $("#collect-image-dialog").remove();
		});
	});
}

// collect image
function collectImageToLightbox( obj ){

	var process = true;

	var form = obj.parents("#collectToLightboxForm");

	var lb_id = $("select[name='lb_id']", form).val();
	var lb_title = $("input[name='lb_title']", form).val();
	var img_id = $("input[name='image_id']", form).val();

	$("select[name='lb_id']", form).change(function(){
		$("input[name='lb_title']", form).removeClass('error');
		$("select[name='lb_id']", form).removeClass('error');
	});

	if ( lb_id == 0 ) {
		$("select[name='lb_id']", form).addClass('error');
		process = false;

		if ( lb_title == "" ) {
			$("input[name='lb_title']", form).addClass('error');
			process = false;
		}
		else {
			$("input[name='lb_title']", form).removeClass('error');

			process = true;
		}
	}
	else {
		$("select[name='lb_id']", form).removeClass('error');
	}

	if( process ){
		var filename = '/' + labels.lang + '/lightboxes.html';

		var send = {
			'ajax': "2",
			'action': "collect-image-to-lightbox"
		}

		send.lb_id = lb_id;
		send.lb_title = lb_title;
		send.img_id = img_id;

		var encoded = $.toJSON(send);
		var params = $.evalJSON(encoded);

		$.post(filename, params, function (response){
			
			var json_response = $.evalJSON(response);
			
			// open dialog
			if( $('#collect-image-dialog').dialog("isOpen") ){
				
				var message = '';

				if( json_response.msg.length > 0 ){
					message = json_response.msg[0];
				}

				if( json_response.err.length > 0 ){
					message = json_response.err[0];
				}

				console.log(message);
				
				var decoded = $('<div></div>').html(message);
				$('#collect-image-dialog').html(decoded);

				setTimeout(function(){
					// close	
					$("#collect-image-dialog").dialog('close');
				}, 2000 ); 
			}
	//		//create content
	//		var decoded = $('<div></div>').html(response);
	//		var content = $('<div id="collect-image-dialog"></div>');
	//		content.html(decoded);
	//
	//		// create dialog
	//		var dialog = content.dialog({
	//            title: 'Add image to lightbox',
	//            dialogClass: 'ngl_dialog_container',
	//            autoOpen: false,
	//            position: 'center',
	//            height: 'auto',
	//            width: 'auto',
	//            modal: true,
	//            draggable: false,
	//            resizable: false
	//		});

			
			// destroy dialog on close	
			$("#collect-image-dialog").bind("dialogclose", function(){
				$("#collect-image-dialog").dialog('destroy');
				$("#collect-image-dialog").remove();
			});
		});
	}
}

// render share to user dialog TODO
function renderShareToUserDialog( obj ){
}

// render share by e-mail dialog
function renderShareByEMailDialog( obj ){

	var filename = '/' + labels.lang + '/lightboxes.html';

	var send = {
		'ajax': "2",
		'action': "render-share-by-email-dialog"
	}

	var img_id = obj.attr('data-image-id');
	send.img_id = img_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="share-by-email-dialog"></div>');
		content.html(decoded);

		// create dialog
		var dialog = content.dialog({
            title: 'Share image by email',
            dialogClass: 'ngl_dialog_container',
            autoOpen: false,
            position: 'center',
            height: 'auto',
            width: 'auto',
            modal: true,
            draggable: false,
            resizable: false
		});

		// open dialog
		if( !$('#share-by-email-dialog').dialog("isOpen") ){
			dialog.dialog('open');			
		}
		
		// destroy dialog on close	
		$("#share-by-email-dialog").bind("dialogclose", function(){
        	$("#share-by-email-dialog").dialog('destroy');
            $("#share-by-email-dialog").remove();
		});
	});
}

// share by e-mail
function shareByEMail( obj ){

	var process = true;

	var form			= obj.parents("#emailLightboxForm");

	var email			= $("input[name='email']", form).val();
	var subject			= $("input[name='subject']", form).val();
	var message			= $("textarea[name='message']", form).val();
	var shared_url		= $("input[name='shared_url']", form).val();
	var uid				= $("input[name='uid']", form).val();

	if( email == "" ){
		$("input[name='email']", form).addClass("error");
		process = false;
	}else{
		$("input[name='email']", form).removeClass("error");
	}

	if( subject == "" ){
		$("input[name='subject']", form).addClass("error");
		process = false;
	}else{
		$("input[name='subject']", form).removeClass("error");
	}

	if( shared_url == "" ){
		$("input[name='shared_url']", form).addClass("error");
		process = false;
	}else{
		$("input[name='shared_url']", form).removeClass("error");
	}

	if( process ){

		var filename = '/' + labels.lang + '/lightboxes.html';

		var send = {
			'ajax': "2",
			'action': "share-by-email"
		}
	}
}	

// on load methods
$(window).load( function(){
	
	// delete image trigger (only for pending images)
	$(".delete-pending-image").livequery( 'click', function( e ) {
		e.preventDefault();
		
		var functionToCall = deletePendingImage;
		deleteNGLConfirmation( functionToCall, $(this) );
	});		
		
	// share image trigger
	$('.share-image').live('click', function(){
		var $obj = $(this);
		//debuglog($obj);	
		shareImage($obj);
		return false;
	});

	// send image trigger
	$(".send-email-image").live('click', function(e){
		e.preventDefault();
		renderShareByEMailDialog( $(this) );	
	});

	// share by email trigger
	$(".share-image-by-email-button").live('click', function(e){
		e.preventDefault();
		shareByEMail( $(this) );	
	});

	// render collect image dialog trigger
	$(".collect-image").live('click', function(e){
		e.preventDefault();
		renderCollectImageDialog( $(this) );	
	});

	// collect image trigger
	$(".collect-image-to-lightbox-button").live('click', function(e){
		e.preventDefault();
		collectImageToLightbox( $(this) );	
	});
	
} );

