 
     # initialize oldItemSetBlocks, so we can compare to see
     # if change has occured
-    $scope.oldItemSetBlocks = $scope.itemSetBlocks
+    $scope.oldItemSetBlocks = deepClone($scope.itemSetBlocks)
 
   $scope.performFilter()
   $scope.initItemSetBlocks()

  , true)

  $scope.$watch('itemSetBlocks', () ->
    if _.isEqual($scope.itemSetblocks, $scope.oldItemSetBlocks)
      $scope.hasChanged = true
  , true)

  # sends an ajax request to the rails web server to
  # sync the current item set to the ones stored
  # on the rails server
  $scope.updateItemSet = () ->
    obj = buildJSON($scope.itemSetBlocks, $scope.mapOption, $scope.selectedMap)
    console.log(obj)

itemSetNamespace = angular.module('itemSet', ["checklist-model"])

# from
# http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object
deepClone = (obj) ->
  
  # Handle the 3 simple types, and null or undefined
  return obj  if null is obj or "object" isnt typeof obj
  
  # Handle Date
  if obj instanceof Date
    copy = new Date()
    copy.setTime obj.getTime()
    return copy
  
  # Handle Array
  if obj instanceof Array
    copy = []
    i = 0
    len = obj.length

    while i < len
      copy[i] = deepClone(obj[i])
      i++
    return copy
  
  # Handle Object
  if obj instanceof Object
    copy = {}
    for attr of obj
      copy[attr] = deepClone(obj[attr])  if obj.hasOwnProperty(attr)
    return copy
  throw new Error("Unable to copy obj! Its type isn't supported.")

$(document).on('ready page:load', ->
  angular.bootstrap(document.body, ['itemSet'])
