/**
Сборка приложения
==============================================================================================
(c) Mychko Eduard, 2015, Moscow, Russia, eduard.mychko@gmail.com
==============================================================================================
*/
$( document ).ready(function() {
	objInterface.interfaceBuild(AVG); /*сборка интерфейса*/
	$(".inset:not(.content-measurement)").hide();
	$(".menu-measurement").addClass('menu_hover_class'); /*присваиваем класс выделенного элемента*/
	//$(".inset:not(.content-analyses)").hide();
	//$(".inset:not(.content-modeling)").hide();
	//$(".inset:not(.content-about)").hide();
	$(".tooltip").easyTooltip();
	//window.onresize = objInterface.updateWindow;
	//window.onresize = objMeasurement.resize;
});


//
// Library
//
var gui  = require('nw.gui');

//
// Global
//
global.gui          = gui;
global.mainWindow   = gui.Window.get();
global.jQuery       = jQuery;
global.objTablemeasur       = objTablemeasur;
global.objFilelist       = objFilelist;
global.objHelpers       = objHelpers;
global.AVG       = AVG;
global.localStorage = window.localStorage;
$editorPreview = $('#editor-preview');
$editorInput   = $('#save-box');
    path           = require('path');
    fdialogs       = require('node-webkit-fdialogs');
    notifier       = require('node-notifier');
	fs			   		= require('fs');
    $              = global.jQuery;
//
// Requires
//
//require('../scripts/objTablemeasur');

//require('../scripts/windowEvents');
//require('../scripts/editor');
require('../scripts/folderPictures');
var menu = require('../scripts/menu');


// var $              = jQuery,
// $editorInputs   = $('.tbl_filename_input');
 
// $editorInputs[0].addEventListener('contextmenu', function(ev) {
  // ev.preventDefault();
  // contextMenu.popup(ev.x, ev.y);
// });
 
 $('#open-new-table-file').on('click', function() {
 // $('#app-function-menu').toggle();

  var openDialog = new fdialogs.FDialog({
    type: 'open',
    accept: ['.txt']
  });

  openDialog.readFile(function(err, data, path) {
   // $editorInput.val( data ).trigger('textchange');
   //global.objTablemeasur.testtest(data);
  
    /*если нет ошибки*/
	if(err) throw err;
		var title = path.split('\\'); /*вычленяем название файла без пути*/
		global.objTablemeasur.getTitleFile(title[title.length-1]); /*передаем название*/  
 		var array = data.toString().split("\n");
		for(i in array) {
			array[i] = array[i].toString().split("\t");
			//console.log(array[i]);
		}
	global.objTablemeasur.makeDataPoints(array); /*передаем массив в функцию*/
  
  
  
   // $editorInput.val(path).trigger('textchange');
  });
   
});

//
// Save
//
$('#extract-new-table-file').on('click', function() {
 // $('#app-function-menu').toggle();

  var content = $editorInput.val();
  var contentBuffer = new Buffer(content, 'utf-8');
  var saveDialog = new fdialogs.FDialog({
    type: 'save',
    accept: ['.txt']
  });

  saveDialog.saveFile(contentBuffer, 'utf-8', function(err, filepath) {
    notifier.notify({
      title: 'RAUPOLOGY',
      message: 'File ' + path.basename( filepath ) + ' successfully saved!',
      /*icon: path.join(__dirname, '../images/brand.png')*/
    });
  });
});


//
//DeleteFiles
//
$('#remove-image-file').on('click', function() {
	var count = 0;
	$(".content-toolbar-tabs-images-content-item").each(function(indx){
		var $divImage =  $(this);
		var image = $(this).attr('data');
		var $check = $(this).find('input[type=checkbox]');
		if( $check.prop("checked")){
			//console.log(image);
			fs.unlinkSync(image);
			$(this).remove();
			 objHelpers.popup(AVG.lang[AVG.language]['picture_deleted'],"information");
			count = count +1;
		} 
		
	});
	
	if (count==0){
		objHelpers.popup(AVG.lang[AVG.language]['select_pictures'],"warning"); 
	}
	
});

//
// Scroll
//
$editorInput.on('scroll', function() {
  $editorPreview.scrollTop($( this ).scrollTop());
});
 



menu.initBarMenu();
menu.initContextMenu();

