var img_path = '../assets/games/mainspower/index.html'; var mainpowers = { trig_args: [], initGame: function(main,objects,spots){ var that= this; var img_counts =objects.length; objects.each(function(){ $.data($(this)[0],'pos', { top: $(this).css('top') , left: $(this).css('left') }); $(this).draggable({ stack: 'img[draggable]', containment: 'parent', start: function(){ that.outside=1; }, drag:function(){ main.attr('src',img_path+main.attr('id')+'.png'); $.each(objects,function(){ if($(this).attr('swap')=='yes') $(this).attr('src',img_path+$(this).attr('id')+'.png'); if($(this).attr('shade')=='yes') $('img[id='+$(this).attr('id')+'_shade]').hide(); }); }, stop: function(){ if(that.outside==1){ var orig_pos = $(this).data('pos'); $(this).animate(orig_pos,function(){ $(this).attr('dropped',0); }); } } }); }); spots.droppable({ accept: 'img[draggable]', drop: function(ev,ui){ var obj_pos={ top: $('#'+ui.draggable.attr('id')+'-center').css('top'), left: $('#'+ui.draggable.attr('id')+'-center').css('left') }; if($(this).attr('accept')==ui.draggable.attr('id')){ that.outside=0; ui.draggable.attr('dropped',1); ui.draggable.animate(obj_pos,function(){ if($('img[dropped=1]').length==img_counts ){ main.attr('src',img_path+main.attr('id')+'_swap.png'); $.each(objects,function(i,e){ if($(this).attr('swap')=='yes'){ $(this).attr('src',img_path+$(this).attr('id')+'_swap.png'); } if($(this).attr('shade')=='yes') { $('img[id='+$(this).attr('id')+'_shade]').show(); } }); } }); } else that.outside =1; }, out: function(ev, ui){ 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(); mainpowers.initGame($('#mains_container'), $('#main-container img[draggable]'), $('div[accept]')); });