var electricity = { outside: 1, initGame: function(objects,spots){ var that=this; objects.each(function(){ $.data($(this)[0],'pos', { top: $(this).css('top') , left: $(this).css('left') }); $(this).draggable({ containment: 'parent', stack: 'img', start: function(){ that.outside=1; }, stop: function(){ if(that.outside==1){ var pos =$(this).data('pos'); $(this).animate($(this).data('pos')); } } }); }); spots.each(function(){ $(this).droppable({ accept: 'img[applicable]', drop: function(ev,ui){ if($(this).attr('id') ==ui.draggable.attr('applicable')){ that.outside=0; var temp = ui.draggable.attr('id'); ui.draggable.animate({ top: $('#'+temp+'-center').css('top'), left: $('#'+temp+'-center').css('left') }); } else that.outside=1; }, out: function(){ that.outside=1; }, }); }); }, }; function appendBackLink(){ var backlink = 'BACK TO PREVIOUS PAGE'; var delay = null; if($(window).width() < 800){ $('body').append(backlink); $('#back-link').css({fontFamily: 'sans-serif' , color: '#0769AD', fontWeight:900, display:'block', marginTop:'15px'}); } clearTimeout(delay); $(window).on('resize',function(){ delay = setTimeout(function(){ if($(this).width()<800){ if($('#back-link').length==0){ $('body').append(backlink); $('#back-link').css({fontFamily: 'sans-serif' , color: '#0769AD', fontWeight:900, display:'block', marginTop:'15px'}); } } else{ if($('#back-link').length!=0) $('#back-link').remove(); } },500) }); } $(function(){ appendBackLink(); electricity.initGame($('#main-container img[applicable]'), $('#main, #both , #battery')); });