Advertisement
Guest User

Untitled

a guest
Aug 6th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function init() {
  2.     var input = $("#inputs").children();
  3.  
  4.     for (var i=0; i<10; i++) { input.eq(i).val(Math.floor((Math.random()*99)+1)) }
  5. }
  6.  
  7. function disableButtons(disable) {
  8.     var buttons = $("#buttons").children();
  9.  
  10.     if (disable == null) {
  11.         buttons.prop("disabled",true);
  12.     } else {       
  13.         buttons.removeAttr("disabled");
  14.     }
  15. }
  16.  
  17. function swap(a,b,swap) {
  18.     var compare = $("#compare");
  19.     var input = $("#inputs").children();
  20.     var a_item = $("#inputs").find("[data-order='"+a+"']");
  21.     var b_item = $("#inputs").find("[data-order='"+b+"']");
  22.     var a_position = a_item.position();
  23.     var b_position = b_item.position();
  24.     var a_destination = "370px";
  25.     var b_destination = "514px";
  26.     var ab_top = "140px";
  27.  
  28.     for (var i=0; i<10 ;i++) { input.eq(i).attr({
  29.         "readonly":"readonly","tabIndex":"-1"
  30.     }) }
  31.  
  32.     a_item.css({
  33.         "box-shadow" : "0px 0px 15px #6699FF",
  34.         "border" : "1px solid #6699FF"
  35.     });
  36.     b_item.css({
  37.         "box-shadow" : "0px 0px 15px #FF3333",
  38.         "border" : "1px solid #FF3333"
  39.     });
  40.  
  41.     function goTo(element,x,y,back) {
  42.         if (!back) {
  43.             element.animate({ top: y }, 150)
  44.             .animate({ left: x }, 350).delay(1000);
  45.         } else {
  46.             element.animate({ left: x }, 350)
  47.             .animate({ top: y }, 150);
  48.         }
  49.     }
  50.  
  51.     goTo(a_item,a_destination,ab_top);
  52.     goTo(b_item,b_destination,ab_top);
  53.  
  54.     if ( parseInt(a_item.val()) > parseInt(b_item.val()) ) {
  55.         compare.text(">").delay(500).fadeIn(150);
  56.     } else if ( parseInt(a_item.val()) < parseInt(b_item.val()) ) {
  57.         compare.text("<").delay(500).fadeIn(150);
  58.     } else { compare.text("=").delay(500).fadeIn(150) }
  59.  
  60.     if (!swap) {
  61.         compare.delay(500).fadeOut(300);
  62.         goTo(a_item,a_position.left,a_position.top,true);
  63.         goTo(b_item,b_position.left,b_position.top,true);
  64.     } else {
  65.         compare.delay(500).fadeOut(300);
  66.         goTo(a_item,b_position.left,b_position.top,true);
  67.         goTo(b_item,a_position.left,a_position.top,true);
  68.         a_item.attr("data-order",b);
  69.         b_item.attr("data-order",a);
  70.     }  
  71.  
  72.     setTimeout(function() { a_item.css({
  73.         "box-shadow" : "0px 0px 15px #fff",
  74.         "border" : "1px solid #fff" });
  75.     },2700);
  76.     setTimeout(function() { b_item.css({
  77.         "box-shadow" : "0px 0px 15px #fff",
  78.         "border" : "1px solid #fff" });
  79.     },2700);
  80. }
  81.  
  82. $(document).ready(function() {
  83.     init();
  84.  
  85.     $("#random").click(function() {
  86.         var input = $("#inputs").children();
  87.  
  88.         for (var i=0; i<10; i++) { input.eq(i).val(Math.floor((Math.random()*99)+1)) }
  89.     });
  90.  
  91.     $("#desc").click(function() {
  92.         var input = $("#inputs").children();
  93.         var values = new Array();
  94.  
  95.         for (var i=0; i<10; i++) { values[i] = Math.floor((Math.random()*99)+1) }
  96.         values.sort(function(a,b){ return b - a });
  97.         for (var i=0; i<10; i++) { input.eq(i).val(values[i]) }
  98.     });
  99.  
  100.     $("#nearly_sorted").click(function() {
  101.         var input = $("#inputs").children();
  102.         var values = new Array();
  103.         var unsorted_items = 3;
  104.  
  105.         for (var i=0; i<10; i++) { values[i] = Math.floor((Math.random()*99)+1) }
  106.  
  107.         values.sort(function(a,b){ return a - b });
  108.         for (var i=0; i<unsorted_items; i++) {
  109.             values[Math.floor((Math.random()*10)+1)] = Math.floor((Math.random()*99)+1);
  110.         }
  111.            
  112.         for (var i=0; i<10; i++) { input.eq(i).val(values[i]) }
  113.     });
  114.  
  115.     $("#start_button").click(function() {
  116.         var animation_length = 3000;
  117.         var input = $("#inputs").children();
  118.  
  119.         $("#animation_box").animate({"height":260},1000);
  120.         input.attr("readonly","true"); 
  121.         disableButtons();          
  122.  
  123.         var n = 10;
  124.         setTimeout(function(){doIt()},1000);
  125.         function doIt() {
  126.             var i = 0;
  127.             function swaps() {
  128.                 var a_item = $("#inputs").find("[data-order='"+i+"']");
  129.                 var b_item = $("#inputs").find("[data-order='"+(i+1)+"']");
  130.  
  131.                 if ( parseInt(a_item.val()) > parseInt(b_item.val()) ) {
  132.                     swap(i,i+1,true); } else { swap(i,i+1); }
  133.  
  134.                 i++;
  135.  
  136.                 if (i<=(n-2)) { setTimeout( function() {swaps()}, animation_length) }
  137.  
  138.             } swaps();
  139.  
  140.            
  141.            
  142.             if (n>2) { setTimeout(function(){n--; doIt();},animation_length*(n-1)) }
  143.             else {
  144.                 setTimeout(function() {
  145.                     $("#animation_box").animate({"height":110},1000,       
  146.                         function() {
  147.                             disableButtons(false);
  148.                             input.removeAttr("readonly");  
  149.                         });
  150.                 },animation_length);
  151.             }
  152.         };
  153.     });
  154.  
  155.  
  156. $("#inputs").children().bind("keypress",function() {
  157.     if (this.value.length > 1) return false;
  158. });
  159.  
  160.  
  161. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement