copyright


  /*
    Copyright 2008 Riccardo Govoni battlehorse@gmail.com
  
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
  
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  */
  
  

organizer


  
  organizer = {};
  
  organizer.MagicCard = function(id, power, element) {
    this.id = id;
    this.power = power;
    this.element = element;
  };
  
  

model(s)


  
  organizer.models = [
    new organizer.MagicCard("mario-1",  4, ['earth']),
    new organizer.MagicCard("mario-2",  1, ['earth']),
    new organizer.MagicCard("mario-3",  3, ['earth','water']),
    new organizer.MagicCard("mario-4",  2, ['earth','water']),
    new organizer.MagicCard("bekijkt-0",  2, ['earth','water']),
    new organizer.MagicCard("bekijkt-1",  2, ['earth']),
    new organizer.MagicCard("bekijkt-2",  3, ['earth']),
    new organizer.MagicCard("bekijkt-3",  3, ['fire']),
    new organizer.MagicCard("bekijkt-4",  3, ['fire']),
    new organizer.MagicCard("fly-1", 5, ['fire','earth']),
    new organizer.MagicCard("fly-2", 3, ['fire','earth']),
    new organizer.MagicCard("fly-3", 5, ['fire','earth']),
    new organizer.MagicCard("fly-4", 1, ['fire','earth']),
    new organizer.MagicCard("fly-5", 1, ['fire','air']),
    new organizer.MagicCard("fly-6", 1, ['fire','air']),
    new organizer.MagicCard("splinter-1", 4, ['fire','air']),
    new organizer.MagicCard("splinter-2", 3, ['fire']),
    new organizer.MagicCard("unreal-1", 3, ['water']),
    new organizer.MagicCard("unreal-2", 2, ['water']),
    new organizer.MagicCard("unreal-3", 1, ['water']),
    new organizer.MagicCard("unreal-4", 2, ['water','air']),
    new organizer.MagicCard("unreal-5", 2, ['water','air']),
    new organizer.MagicCard("unreal-6", 2, ['water','air']),
    new organizer.MagicCard("unreal-7", 5, ['water','air']),
    new organizer.MagicCard("unreal-8", 3, ['water','air']),
    new organizer.MagicCard("unreal-9", 3, ['water']),
    new organizer.MagicCard("unreal-10", 4, ['water']),
    new organizer.MagicCard("unreal-11", 5, ['water']),
    new organizer.MagicCard("unreal-12", 5, ['air']),
    new organizer.MagicCard("unreal-13", 1, ['air']),
    new organizer.MagicCard("unreal-14", 1, ['air']),
    new organizer.MagicCard("unreal-15", 1, ['air']),
    new organizer.MagicCard("unreal-16", 4, ['air']),
    new organizer.MagicCard("unreal-17", 5, ['air']),
    new organizer.MagicCard("unreal-18", 5, ['air','fire']),
    new organizer.MagicCard("unreal-19", 3, ['air','fire']),
    new organizer.MagicCard("game-1-tekken-5", 1, ['air','fire']),
    new organizer.MagicCard("game-2-sims-2", 2, ['air','fire']),
    new organizer.MagicCard("game-4-super-monkey", 4, ['air']),
    new organizer.MagicCard("mario-1", 5, ['air'])
  ];
  
  

render -- watch dir(s)


  organizer.render = function(model) {
    var html = [];
    html.push("<div class='model'><div style='padding: 3px'>");
    html.push("<p style='font-size:10px'>");
    html.push("<img src='local/screens/game/" + model.id + ".jpg' width='85px' height='122px' >");
    html.push("</p></div></div>");
    return $(html.join(''));
  };
  
  

selection manager


  
  organizer.SelectionManager = function() {
    this.selectionMap_ = {};
  };
  
  organizer.SelectionManager.prototype.select = function(id) {
    this.selectionMap_[id] = id;
    $('#' + id).addClass('ui-selected');
  };
  
  organizer.SelectionManager.prototype.unselect = function(id) {
    delete this.selectionMap_[id];
    $('#' + id).removeClass('ui-selected');
  };    
  
  

selection manager (ctnd)


  
  organizer.SelectionManager.prototype.isSelected = function(id) {
    return this.selectionMap_[id];
  };        
  
  organizer.SelectionManager.prototype.allSelected = function() {
    return this.selectionMap_;
  };
  sel = new organizer.SelectionManager();
  
  

start


  
  organizer.start = function() {
    organizer.createModels();
    
    organizer.initDragSelection();
    
    organizer.initZooming();
    
    organizer.initPanning();
    
    organizer.initShuffleButton();
  };
  
  

create model(s0


  
  organizer.createModels = function() {
    jQuery.map(organizer.models, function(model) {
      var rendering = organizer.render(model);
  
      // initial positioning in the DOM and layout
      rendering.css("position", "absolute")
                  .css("top", 0)
                  .css("left", 0)
                  .attr("id", model.id);
  
      $("#universe").append(rendering);    
    })
  };
  

init drag


  
  organizer.initDragSelection = function() {
    $('.model').dblclick(function() {
      if (sel.isSelected(this.id)) {
        sel.unselect(this.id);
      } else {
        sel.select(this.id);
      }
    });      
  
  

selection(s)


    
    $('#container').selectable({
      selected: function(ev, ui) {
        if (ui.selected.id) {
          sel.select(ui.selected.id);
        }
      },
      unselected: function(ev, ui) {
        if (ui.unselected.id) {
          sel.unselect(ui.unselected.id);
        }
      },
      filter: '.model'
    });
    
  

draggable / style(s)


    $('.model').draggable({
      opacity: 0.5,
      cursor: 'move',
      zIndex: 10000,
      start: function(ev, ui) {
  
  

initial position(s)


        // figure out all the initial positions for the selected elements
        // and store them.
        if (sel.isSelected(ui.helper[0].id)) {
          for (id in sel.allSelected()) {
            $('#'+id).data(
              "top0", 
              parseInt($('#'+id).css("top"),10) - 
                parseInt($(ui.helper[0]).css("top"),10));
            $('#'+id).data(
              "left0", 
              parseInt($('#'+id).css("left"),10) - 
                parseInt($(ui.helper[0]).css("left"),10));
          }
        }
      },
  

function drag


      drag: function(ev, ui) {
        if (sel.isSelected(ui.helper[0].id)) {
          for (id in sel.allSelected()) {
            if (id != ui.helper[0].id) {
              $('#' + id).css('top',
                              $('#'+id).data("top0") + ui.position.top);
              $('#' + id).css('left', 
                              $('#'+id).data("left0") + ui.position.left);              
            }
          }
        }
      },
  

function step


      stop: function(ev, ui) {
        if (sel.isSelected(ui.helper[0].id)) {
          for (id in sel.allSelected()) {
           $('#'+id).removeData("top0");
           $('#'+id).removeData("left0");
          }
        }        
      }
    });  
  };
  

init zooming


  organizer.initZooming = function() {
    $('#container').mousewheel(function(event, delta) {
     
      var containerTopOffset = parseFloat($('#universe').css('top'));
      var containerLeftOffset = parseFloat($('#universe').css('left'));
      var scale = 0.1;
      var percent = 1 + delta*scale;        
  

rescale


      $('.model').each(function() {
        organizer.rescaleRelative(this, 
          'top', 
            event.clientY - 
            containerTopOffset - 
            parseFloat(this.css('height')) / 2,
          percent);
        organizer.rescaleRelative(this, 
          'left', 
            event.clientX - 
            containerLeftOffset - 
            parseFloat(this.css('width')) / 2, 
          percent);          
        
        organizer.rescale(this, 'width', percent);
        organizer.rescale(this, 'height', percent);
        organizer.rescale(this, 'font-size', percent);
      });
      
  

all images


      $('.model IMG').each(function() {
        organizer.rescale(this, 'width', percent);
        organizer.rescale(this, 'height', percent);      
      });
      event.preventDefault();
    });  
  };
  
  

rescale function


  organizer.rescale = function(selector, property, percent) {
    var oldValue = parseFloat(selector.css(property));
    selector.css(property, oldValue*percent);
  };
  
  organizer.rescaleRelative = function(selector, property, relative, percent) {
    var oldValue = parseFloat(selector.css(property)) - relative;
    selector.css(property, oldValue*percent + relative);      
  };
  
  

init panning


  organizer.initPanning = function() {
    var dragDelta = {
      top: $('#container').offset().top +
           $('#universe').offset().top,
      left: $('#container').offset().left +
            $('#universe').offset().left,
    };
  

scroll trigger


  
    $('#scroll-trigger').click(function() {
      this.hide();
      var viewport = $('#container');
      $('#scroll-overlay')
        .css('left', viewport.css('left'))
        .css('top', viewport.css('top'))
        .css('width', viewport.width())
        .css('height', viewport.height())
        .css('z-index', 99)
        .css('display', '');
  
  

overlay


      $('#scroll-overlay').draggable({
        helper: function() {
          return $("<div />");
        },
        start: function(ev, ui) {
          var offset = $('#universe').offset();
          $('#universe')
            .data("top0", offset.top)
            .data("left0", offset.left);
        },
  

overlay drag


        drag: function(ev, ui) {
          var universe = $('#universe');
          var offset = universe.offset();
  
          var dragTop = ui.position.top +
                        universe.data("top0") - dragDelta.top;
          var dragLeft = ui.position.left +
                         universe.data("left0") - dragDelta.left;
  
          $('#universe')
            .css('top', dragTop).css('bottom', -dragTop)
            .css('left', dragLeft).css('right', -dragLeft);
        },
        refreshPositions: false
      });
    });
  
  

click done


    $('#scroll-done').click(function() {
      $('#scroll-overlay')
        .css('z-index', -1)
        .css('display', 'none');
      $('#scroll-trigger').show();
    });  
  };
  
  

shuffle button


  organizer.initShuffleButton = function() {
    $('#shuffleCards').click(function() {
      // reset panning
      $('#universe').css('top', 0).css('left', 0).css('bottom', 0).css('right', 0);
  
      var maxWidth = Math.round($('#container').width()*0.3) ;
      var maxHeight = Math.round($('#container').height()*0.3);
  
  

model propagate


      $('.model').each(function(model) {
        var top = Math.round($('#container').height() / 3 +
                             Math.random()*maxHeight*2 - maxHeight);
        var left = Math.round($('#container').width() / 3 +
                              Math.random()*maxWidth*2 - maxWidth);
  
        var movement = {'top': top, 'left': left};
        this.animate(movement, 1000);
      });    
    })
  };