﻿var initsingolodocumento = 0;
	function TogliClassi(div, classe_on, classe_off) {
		jQuery(div).each(function(i) {
			jQuery(this).removeClass(classe_on);
			jQuery(this).addClass(classe_off);
		});
}

 function go(url) {
   location.href = url;
}
 /* apertura - chiusura voci menu a sx */

	var attivo=jQuery.cookie("attivo1");
	var attivoBis=jQuery.cookie("attivo2");
	if(attivo==1){
		jQuery("ul.menusx li.liv0").find("ul").attr("visibility", "visibled").slideDown('fast').addClass('active');
	}
	if(attivoBis==1){
		jQuery("ul.menusx li.liv0_ultimo").find("ul").attr("visibility", "visibled").slideDown('fast').addClass('active');
	}
	jQuery("ul.menusx li.liv0>a").click(function(){
		if(attivo==0){
			jQuery(this).addClass("active");
			jQuery(this).parents("li").find("ul").slideDown('fast').attr("visibility", "visibled");
			attivo=1;
			jQuery.cookie("attivo1", null);
			jQuery.cookie("attivo1", attivo);
		}else {
			jQuery(this).removeClass("active");
			jQuery(this).parents("li").find("ul").slideUp('fast').attr("visibility", "hidden");
			attivo=0;
			jQuery.cookie("attivo1", null);
			jQuery.cookie("attivo1", attivo);
		}
		return false;
});

function ViewCompanyInfo() {
   var container = "#companyinfo";
   var overlayid = "overlay";
   $(overlayid).remove();
   var overlay = "<div class='overlay' id='" + overlayid + "' style='display:none;'></div>";
   $("body").before(overlay);
   //var close = "<div class='button'><input id='close' class='ecommerce-button' type='button' value='ok' onclick=CloseCompanyInfo('" + container + "');></div>";
   //$(container).append(msg);
  // $(container).append(close);
   var maskHeight = jQuery(document).height();
   var maskWidth = jQuery(window).width();

   var winH = jQuery(window).height();
   var winW = jQuery(window).width();

   var containerHeight = jQuery(container).height();
   var containerWidth = jQuery(container).width();
   // calculate the values for center alignment
   var dialogTop = (winH / 2) - (containerHeight / 2);
   var dialogLeft = (winW / 3) - (containerWidth / 2);

   centerPopup();

   jQuery("#" + overlayid).show();
   jQuery(container).show();

   //jQuery(container).css({ top: dialogTop, left: dialogLeft }).animate({ opacity: '+=1' }, 300, function () {
   //});
};

function centerPopup() {
   //request data for centering
   var windowWidth = $(window).width();
   var windowHeight = document.documentElement.clientHeight;
   var popupHeight = $("#companyinfo").height();
   var popupWidth = $("#companyinfo").width();
   //centering
   $("#companyinfo").css({
      "position": "absolute",
      "top": windowHeight / 2 - popupHeight / 2,
      "left": windowWidth / 3 - popupWidth / 2
   });
   //only need force for IE6

   $(".overlay").css({
      "height": windowHeight
   });
   $(window).scrollTop(0);
   //var Startpos = $("#companyinfo").position();
}

function CloseCompanyInfo() {
   jQuery(".overlay").hide();
   jQuery("#companyinfo").hide();
}

function scaleSize(maxW, maxH, currW, currH) {
   var ratio = currH / currW;

   if (currW >= maxW && ratio <= 1) {
      currW = maxW;
      currH = currW * ratio;
   } else if (currH >= maxH) {
      currH = maxH;
      currW = currH / ratio;
   }

   return [currW, currH];
}

function PopDialog(message, tipo, button) {
   // css nel file Validazione.css
   $('#dialog-overlay').remove();
   $(".dialogmessage").remove();
   var container = "<div class=\"dialogmessage\"><div class=\"errore\">{errore}</div><div class=\"button\"><input type=\"button\"  value=\"ok\"></div></div>";
   var dialogoverlay = "<div id='dialog-overlay'></div>";
   var container = container.replace(/{errore}/g, message);
   $(container).insertAfter($("body"));
   $(dialogoverlay).insertAfter($("body"));

   var windowWidth = document.documentElement.clientWidth;
   var windowHeight = document.documentElement.clientHeight;
   var popupHeight = $(".dialogmessage").height();
   var popupWidth = $(".dialogmessage").width();
   var top = windowHeight / 2 - popupHeight / 2;
   var left = windowWidth / 2 - popupWidth / 2;

   $('#dialog-overlay').css({ height: windowHeight, width: windowWidth }).show();
   $('.dialogmessage').css({ top: top, left: left, width: popupWidth }).fadeIn("fast", function () {
   });

   if (tipo == 0) {
      $(".dialogmessage .button input").click(function () {
         $('#dialog-overlay').remove();
         $(".dialogmessage").remove();
      });
   }
   if (tipo == 1) {
      $(".dialogmessage .button input").click(function () {
         $("input ." + button).click();
         $('#dialog-overlay').remove();
         $(".dialogmessage").remove();
      });
   }
}

function UploaderSingleDocument(form, action, folder, nameFile) {

   var uploader = new plupload.Uploader({
      // General settings
      runtimes: 'flash',
      browse_button: 'pickfiles',

      url: '/HandlerUpload.axd?' + $("#" + form).serialize()+'&folder='+folder+'&name='+nameFile,
      max_file_size: '1000mb',
      // Resize images on clientside if we can
      //resize: { width: 320, height: 240, quality: 90 },

      // Specify what files to browse for
      filters: [
			{ title: "Documenti (doc,docx,xls,xlsx,ppt,pptx,pps,ppsx,pdf)", extensions: "doc,docx,xls,xlsx,ppt,pptx,pps,ppsx,pdf" }
		],

      // Flash settings
      flash_swf_url: '/Componenti/scripts/jQuery/Plugins/plupload/js/plupload.flash.swf'

      // Silverlight settings
      //silverlight_xap_url: '/Componenti/scripts/jQuery/PlugIn/plupload/js/plupload.silverlight.xap'
      // Post init events, bound after the internal events
   });

   //    uploader.bind('Init', function (up, params) {
   //        $('#filelist').html("<div>Current runtime: " + params.runtime + "</div>");
   //    });

   if (initsingolodocumento == 0) {
      uploader.init();
      initsingolodocumento = 1;
   }

   uploader.bind('QueueChanged', function (up) {
      if (up.files.length > 0 && uploader.state != 2) {
         uploader.start();
      }
   });
   uploader.bind('FilesAdded', function (up, files) {
      $.each(files, function (i, file) {
         $('#filelist').html(
	                '<div id="' + file.id + '">' +
	                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
	            '</div>');
      });

      up.refresh(); // Reposition Flash/Silverlight
   });

   uploader.bind('UploadProgress', function (up, file) {
      $('#' + file.id + " b").html(file.percent + "%");
   });

   uploader.bind('Error', function (up, err) {
      $('#filelist').append("<div>Error: " + err.code +
	            ", Message: " + err.message +
	            (err.file ? ", File: " + err.file.name : "") +
	            "</div>"
	        );

      up.refresh(); // Reposition Flash/Silverlight
   });

   uploader.bind('FileUploaded', function (up, file) {
      alert(file.id);
      $('#' + file.id + " b").html("100%");
      //action();
      uploader.init();
      //      $.post("/admin/PdfModuleManagement/InDBupload.ashx?name=" + file.name + "&" + $("#" + form).serialize(), function () {
      //         $("#container_pdf").html("il file: " + file.name + " è stato caricato correttamente");
      //      });
   });

   //    $('#uploadfiles').click(function (e) {
   //        uploader.start();
   //        e.preventDefault();
   //    });
};
