/***********************
* Acciones de composición de Adobe Edge Animate
*
* Editar este archivo con precaución, teniendo cuidado de conservar 
* las firmas de función y los comentarios que comienzan con "Edge" para mantener la 
* capacidad de interactuar con estas acciones en Adobe Edge Animate
*
***********************/
(function($, Edge, compId){
var Composition = Edge.Composition, Symbol = Edge.Symbol; // los alias más comunes para las clases de Edge

   //Edge symbol: 'stage'
   (function(symbolName) {
      
      
      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      Symbol.bindElementAction(compId, symbolName, "document", "compositionReady", function(sym, e) {
         
         $("body").css("user-select","none");
         
         function matrixToArray(matrix) {
             var scaleVars = matrix.substr(7, matrix.length - 8).split(', ');
             return scaleVars[0];
         }
         
             //Make element draggable
             	$(".elementos").draggable({
         	      helper: 'clone',
               	appendTo: "body",
               	containment: '#Stage',        
         			//opacity: 0.5,
         			iframeFix: true,
         			zIndex: 100,	
         			refreshPositions: true,			
         			scroll:false,			
         			revertDuration: 500,
         			helper: function() {
         			var escalaTemp = $('#Stage').css('transform');
         			var escala = matrixToArray(escalaTemp);
               	var helper = $(this).clone(); 
               	helper.css({
             			"transform":"scale("+escala+")",
             			"-o-transform":"scale("+escala+")",
             			"-ms-transform":"scale("+escala+")",
             			"-webkit-transform":"scale("+escala+")",
             			"-moz-transform":"scale("+escala+")",
             			"-o-transform":"scale("+escala+")",
             			"-moz-transform-origin":"left top",
         				"-webkit-transform-origin":"left top",
         				"-o-transform-origin":"left top",
         				"-ms-transform-origin":"left top",
             		});        
               	return helper;
           		}
             });
         
         	margenIzq = 65;
         	counter = 0;
         
              $("#Stage_Patron").on( "drop", function( event, ui ) {
         
                     if ($(ui.helper).attr('id').search(/drag_[0-9]/) != -1) {
                         counter++;   
         
                         if(counter < 5){                            
                         	//$("#Stage_Recipiente1").removeClass("ui-droppable");
         
                         	var element = $(ui.draggable).clone();
                         	element.addClass("tempclass");
                         	$(this).append(element);
                         	$(".tempclass").attr("id", "clonediv" + counter);
                         	$("#clonediv" + counter).removeClass("tempclass");
                         	//Get the dynamically item id
                         	draggedNumber = $(ui.helper).attr('id').search(/drag_([0-9])/)
                         	itemDragged = "dragged" + RegExp.$1                	               
                         	//console.log(itemDragged);
                         	$("#clonediv" + counter).addClass(itemDragged);                 	
         
                         	if(counter <= 4){                            
         	                	$("#clonediv" + counter).css({opacity:"1",top: "20px", left:  margenIzq});       
                         		margenIzq=margenIzq + 100;       
                         	}
                         }
                     }
         
             });
         
         
         	margenIzq2 = 65;
         	 counter2 = 0;
         
             $("#Stage_Secuencia").droppable({
                 drop: function (ev, ui) {
                     if ($(ui.helper).attr('id').search(/drag_[0-9]/) != -1) {
                         counter2++;   
         
                         if(counter2 < 11){                            
                         	//$("#Stage_Recipiente1").removeClass("ui-droppable");
         
                         	var element = $(ui.draggable).clone();
                         	element.addClass("tempclass");
                         	$(this).append(element);
                         	$(".tempclass").attr("id", "clonediv2" + counter2);
                         	$("#clonediv2" + counter2).removeClass("tempclass");
                         	//Get the dynamically item id
                         	draggedNumber = $(ui.helper).attr('id').search(/drag_([0-9])/)
                         	itemDragged = "dragged2" + RegExp.$1                	               
                         	//console.log(itemDragged);
                         	$("#clonediv2" + counter2).addClass(itemDragged);                 	
         
                         	if(counter2==1){                       
         	                	$("#clonediv2" + counter2).css({opacity:"0.8",top: "130px", left:  margenIzq2});       
         	               }
         	               if(counter2==2){                       
         	                	$("#clonediv2" + counter2).css({opacity:"0.8",top: "110px", left:  "260px"});       
         	               }
         	               if(counter2==3){                       
         	                	$("#clonediv2" + counter2).css({opacity:"0.8",top: "115px", left:  "310px"});       
         	               }
         	               if(counter2==4){                       
         	                	$("#clonediv2" + counter2).css({opacity:"0.8",top: "125px", left:  "360px"});       
         	               }
         	               if(counter2==5){                       
         	                	$("#clonediv2" + counter2).css({opacity:"0.8",top: "130px", left:  "410px"});       
         	               }
         	               if(counter2==6){                       
         	                	$("#clonediv2" + counter2).css({opacity:"0.8",top: "135px", left:  "460px"});       
         	               }
         	               if(counter2==7){                       
         	                	$("#clonediv2" + counter2).css({opacity:"0.8",top: "140px", left:  "510px"});       
         	               }
         	               if(counter2==8){                       
         	                	$("#clonediv2" + counter2).css({opacity:"0.8",top: "140px", left:  "560px"});       
         	               }
         	               if(counter2==9){                       
         	                	$("#clonediv2" + counter2).css({opacity:"0.8",top: "33px", left:  "680px"});       
         	               }
         	               if(counter2==10){                       
         	                	$("#clonediv2" + counter2).css({opacity:"0.8",top: "45px", left:  "725px"});       
         	               }
         
                         }
                     }
                 }
         
         
             });
         
         
         
         /*
         // let the gallery items be draggable
         $(".spheres").draggable({
         cancel: "a.ui-icon", // clicking an icon won't initiate dragging
         revert: "invalid", // when not dropped, the item will revert back to its initial position
         //containment: "document",
         helper: "clone",
         cursor: "move",
         
         appendTo: "body",
         	containment: '#Stage', 
         	opacity: 0.4,
         	iframeFix: true,
         	zIndex: 100,	
         	refreshPositions: true,
         	//snap:true,
         	//snapMode: "both",
         	//snapTolerance:20,
         	scroll:false,
         	//helper: "clone",
         	revertDuration: 500,
         
         
         	helper: function() {
         		var escalaTemp = $('#Stage').css('transform');
         		var escala = matrixToArray(escalaTemp);
               var helper = $(this).clone(); 
               helper.css({
             	"transform":"scale("+escala+")",
             	"-o-transform":"scale("+escala+")",
             	"-ms-transform":"scale("+escala+")",
             	"-webkit-transform":"scale("+escala+")",
             	"-moz-transform":"scale("+escala+")",
             	"-o-transform":"scale("+escala+")",
             	"-moz-transform-origin":"left top",
         		"-webkit-transform-origin":"left top",
         		"-o-transform-origin":"left top",
         		"-ms-transform-origin":"left top",
             	});        
               return helper;
            },
         
            stop: function(event, ui) {
                 //$(ui.helper).clone(true).removeClass('spheres').addClass('spheres').appendTo('body');
                 //$(ui.helper).clone(true).removeClass('ui-draggable ui-draggable-dragging').addClass('sphere-clone').appendTo('#Stage');
                 $(ui.helper).clone(true).addClass('sphere-clone').appendTo('#Stage');
                 //$(this).draggable('option','revert','invalid');
            }
         });
         */
         
         function coordenadas (Drop, simbolo) {
                varxdrop = sym.$(Drop).css("left");
                varydrop = sym.$(Drop).css("top");
                varxdropanch = sym.$(Drop).css("width");
                varydropalto = sym.$(Drop).css("height");
                varxdropcentro = parseInt(varxdrop) + parseInt(varxdropanch)/2;
                varydropcentro = parseInt(varydrop) + parseInt(varydropalto)/2;
                varxdraganch = sym.$(simbolo).css("width");
                varydragalto = sym.$(simbolo).css("height");
                varxdragcentro = parseInt(varxdraganch)/2;
                varydragcentro = parseInt(varydragalto)/2;
                sym.$(simbolo).css({top: varydropcentro-varydragcentro});
                sym.$(simbolo).css({left: varxdropcentro-varxdragcentro});
         };
         

      });
      //Edge binding end

      Symbol.bindSymbolAction(compId, symbolName, "creationComplete", function(sym, e) {
         sym.$('.center-wrapper').css("overflow","hidden");
         
         for(var i=10; i<=30; i++){
         	sym.setVariable("left"+i, $("#Stage_drag_"+i).css("left"));
         	sym.setVariable("top"+i, $("#Stage_drag_"+i).css("top"));
         }
         
         	sym.$("#Stage_Patron").droppable({
               	drop: function( event, ui ){ 
                		//coordenadas("#Stage_Patron", ui.draggable.attr('id').slice(6));	
         			}
            });	
         
         
         		 function coordenadas (Drop, simbolo) {
                varxdrop = sym.$(Drop).css("left");
                varydrop = sym.$(Drop).css("top");
                varxdropanch = sym.$(Drop).css("width");
                varydropalto = sym.$(Drop).css("height");
                varxdropcentro = parseInt(varxdrop) + parseInt(varxdropanch)/2;
                varydropcentro = parseInt(varydrop) + parseInt(varydropalto)/2;
                varxdraganch = sym.$(simbolo).css("width");
                varydragalto = sym.$(simbolo).css("height");
                varxdragcentro = parseInt(varxdraganch)/2;
                varydragcentro = parseInt(varydragalto)/2;
                sym.$(simbolo).css({top: varydropcentro-varydragcentro});
                sym.$(simbolo).css({left: varxdropcentro-varxdragcentro});
                };
         
         
         

      });
      //Edge binding end

      Symbol.bindElementAction(compId, symbolName, "${_hotspot_reiniciar}", "click", function(sym, e) {
         // insert code for mouse click here
         $("[class*='dragged']").fadeOut();
         
         setTimeout(borrarDragClone,200);
         
         function borrarDragClone(){
         $("[class*='dragged']").remove();
         margenIzq = 30;
         counter = 0;
         
         margenIzq2 = 65;
         counter2 = 0;
         }
         

      });
      //Edge binding end

   })("stage");
   //Edge symbol end:'stage'

   //=========================================================
   
   //Edge symbol: 'Precargador'
   (function(symbolName) {   
   
   })("Precargador");
   //Edge symbol end:'Precargador'

   //=========================================================
   
   //Edge symbol: 'flecha_bt'
   (function(symbolName) {   
   
      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 0, function(sym, e) {
         // introducir código aquí
         sym.stop();

      });
      //Edge binding end

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 250, function(sym, e) {
         // introducir código aquí
         sym.stop();

      });
      //Edge binding end

   })("flecha_bt");
   //Edge symbol end:'flecha_bt'

   //=========================================================

   //=========================================================

   //=========================================================
   
   //Edge symbol: 'bt_audio'
   (function(symbolName) {   
   
      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 0, function(sym, e) {
         // introducir código aquí
         sym.stop();

      });
      //Edge binding end

      Symbol.bindElementAction(compId, symbolName, "${_ht_spot}", "click", function(sym, e) {
         var soundId = sym.getSymbolElement().attr( "title" );
         var currentAudio=sym.getComposition().getStage().$(soundId)[0].currentTime;
         var duracion= sym.getComposition().getStage().$(soundId).duration;
         console.log("soundId="+soundId);
         if (duracion== currentAudio){
         sym.stop("up");
         }
         
         sym.getComposition().getStage().$(soundId).on('ended', function() {
         sym.stop("up");
            // enable button/link
         });
         
         console.log("currentAudio="+currentAudio);
         console.log("duracion="+duracion);
         // Definir un conmutador para reproducir o pausar una pista de audio 
         if (sym.getComposition().getStage().$(soundId)[0].paused) {
         	sym.getComposition().getStage().$(soundId)[0].play();
         	sym.getComposition().getStage().$(soundId)[0].volume=0.7;
         	sym.stop("down");
         
         } else {
         //activar para pausar en vez de reiniciar
         	sym.getComposition().getStage().$(soundId)[0].pause();
         	sym.stop("up");
         }

      });
      //Edge binding end

      

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 250, function(sym, e) {
         sym.stop();

      });
      //Edge binding end

      

   })("bt_audio");
   //Edge symbol end:'bt_audio'

   //=========================================================
   
   //Edge symbol: 'resaltado'
   (function(symbolName) {   
   
      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 2820, function(sym, e) {
         sym.play("resaltado");

      });
      //Edge binding end

   })("resaltado");
   //Edge symbol end:'resaltado'

   //=========================================================
   
   //Edge symbol: 'bt_reiniciar'
   (function(symbolName) {   
   
      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 0, function(sym, e) {
         // introducir código aquí
         sym.stop();

      });
      //Edge binding end

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 250, function(sym, e) {
         // introducir código aquí
         sym.stop();

      });
      //Edge binding end

   })("bt_reiniciar");
   //Edge symbol end:'bt_reiniciar'

})(jQuery, AdobeEdge, "EDGE-94063035");