 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'])


    # initialize oldItemSetBlocks, so we can compare to see
    # if change has occured
    $scope.oldItemSetBlocks = $scope.itemSetBlocks

  $scope.performFilter()
  $scope.initItemSetBlocks()

    $http.put("/item_sets/#{gon.itemSetId}/update_json", data).success(
      (data) ->
        if data.status == 'success'
          $scope.oldItemSetBlocks = deepClone($scope.itemSetBlocks)
          $scope.hasChanged = false
        else
          console.log('error occured while trying to update')
    )
