var activity_uploader;

var $j = jQuery.noConflict();

jQuery.fn.exists = function(fun) { 
    if(jQuery(this).length>0) { 
        if(fun) { 
            jQuery(document).ready(function() {
                fun(); 
             });
        } else { 
            return true;  
        } 
    } 
};

jQuery.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

function buttonstyleOver(e)
{
  e.className = "buttonstyleOver";
}

function buttonstyleOut(e)
{
  e.className = "buttonstyleOut";
}

// Anonymous function
(function($)
  {

    var images = ['img1','img2','img3','img4','img5','img6','img7','img8','img9','img10'];
    var current_image = 0;

    $.loading.maskCss.background = "#FFF";
    $.loading.maskCss.opacity = 0.50;

    var newActivities = [];
    var activityQueue = [];
        var processingCount = 0;
    var processedCount = 0;
    var newCount = 0;
    var opperation = "";

    var ActivityUploader = function(options) {
      this.initialize(options);
    };

    ActivityUploader.prototype = {
      initialize: function(options)
      {

        this.key = options.key;
        this.check_url = options.check_url;
        this.post_url = options.post_url;
        this.default_required_plugin_version = [2,6,3,1]; //[2,5,1,0]

        try {

          this.controller = new ActivityController();
          this.gc = new Garmin.DeviceControl();
          this.gc.register(this.controller);
          this.gc.unlock(this.key);

          this.verify_plugin_version(this.default_required_plugin_version);

        } catch(e) {
          this.controller.onException(e);
        }

      },

      start: function(opp) {
        opperation = opp;

        if(this.gc !== undefined) {
          this.gc.findDevices();
        }
      },

      reset: function() {

        if (this.gc) {
          this.gc.cancelFindDevices();
          this.gc.cancelReadFromDevice();
        }
        newActivities = [];
        activityQueue = [];
        processingCount = 0;
        processedCount = 0;
        newCount = 0;
        $('#upload-progress-bar').hide();
        $("#tracks").hide();
        this.setPluginRequiredVersion(this.default_required_plugin_version);
      },

      setPluginRequiredVersion: function (reqVersionArray) {
        Garmin.DevicePlugin.REQUIRED_VERSION.versionMajor = reqVersionArray[0];
        Garmin.DevicePlugin.REQUIRED_VERSION.versionMinor = reqVersionArray[1];
        Garmin.DevicePlugin.REQUIRED_VERSION.buildMajor = reqVersionArray[2];
        Garmin.DevicePlugin.REQUIRED_VERSION.buildMinor = reqVersionArray[3];
      },

      verify_plugin_version : function(plugin_version) {
        try {
          this.setPluginRequiredVersion(plugin_version);
          this.gc.validatePlugin();
          return true; // won't get here if invalid
        } catch(e) {
          this.controller.onException(e);
        }
      },

      read_from : function(device_number,r) {
        r.controller.deviceNumber = device_number;
        $('#upload-messages').text("Found Device... " + r.controller.devices[device_number].displayName);
        $("#tracks").children().remove();
        $("#tracks").hide();
        this.controller.set_type((this.gc.checkDeviceReadSupport(Garmin.DeviceControl.FILE_TYPES.tcxDir))? "TCX" : "FIT");
        this.controller.read(r);
        // r.controller.readDataFromDevice(Garmin.DeviceControl.FILE_TYPES.tcxDir);
      },

      write_to : function(device_number,r) {
        r.controller.deviceNumber = device_number;
        $('#upload-messages').text("Found Device... " + r.controller.devices[device_number].displayName);
        $("#tracks").children().remove();
        $("#tracks").hide();
        $.get('/routes/crs/' + activity_id, {}, function(data, status) {
            if(status == "success") {

              r.controller.writeFitnessToDevice(data, "course.crs");
            }
        },"text");

      } // END function write_to()
    }; // END ActivityUpload class

    var ActivityMapController = Class.create({
        initialize: function(container, activity) {
          this.activity = activity;
          this.alternate = 1;
          this.all_tracks = [];
          try {
            var map = new GMap2(document.getElementById(container));
            map.addMapType(G_PHYSICAL_MAP);
            map.setMapType(G_PHYSICAL_MAP);
            map.addControl(new GLargeMapControl());
            var mapControl = new GHierarchicalMapTypeControl();

            // Set up map type menu relationships
            mapControl.clearRelationships();
            mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);

            // Add control after you've specified the relationships
            map.addControl(mapControl);
            var last_point;
            for(var n=0;n<activity.laps.length;n++) {
              var tracks = [];
              var icon = new GIcon();
              icon.image = '/images/numbers/' + (n+1) + '.png';
              icon.iconSize = new GSize(13,13);
              icon.iconAnchor = new GPoint(6,6);
              var first_point = false;
              for(var i=0;i<activity.laps[n].tracks.length;i++) {
                if(Math.abs(activity.laps[n].tracks[i].latitude_degrees)) {
                  last_point = new GLatLng(activity.laps[n].tracks[i].latitude_degrees, activity.laps[n].tracks[i].longitude_degrees);
                  if(first_point === false) {
                    first_point = last_point;
                  }
                  tracks.push(last_point);
                  this.all_tracks.push(last_point);
                }
              }
              var marker = new GMarker(first_point, {icon:icon});
              var polyline = new GPolyline(tracks, (this.alternate)? "#FF0000" : "#990000", 2, 1);
              map.addOverlay(polyline);
              map.addOverlay(marker);
              this.alternate = (this.alternate === 0)? 1 : 0;
            }

            var finish = new GIcon();
            finish.image = '/images/numbers/finish.png';
            finish.iconSize = new GSize(13,13);
            finish.iconAnchor = new GPoint(6,-6);
            finish_marker = new GMarker(last_point, {icon:finish});
            map.addOverlay(finish_marker);

            // Zoom Hack!
            var polyline = new GPolyline(this.all_tracks, "#FFFFFF", 3, 1);
            map.setCenter(polyline.getBounds().getCenter(), map.getBoundsZoomLevel(polyline.getBounds()));
            //map.addOverlay(polyline);
            window.unload = GUnload;
          } catch(e) {
            alert("Something bad happened... " + e);
          }
        } // END function intitalize()
    });

    var BaseActivityController = {
      _processDirListing: function(r) {
        var this_alias = this;
        $.post(this.check_url, {xml:r.controller.gpsDataString}, function(data,status) {
            if(status == 'success') {
              newActivities = newActivities.concat(data.activities);
              activityQueue = activityQueue.concat(data.activities);
              //newActivities.sort().reverse();
              //activityQueue.sort().reverse();
              if(newActivities.length < 1) {
                this_alias._noActivitiesToUpload(r);
              } else {
                this_alias._trackListings(r);
                this_alias._processNextTracks(r);
              }
            } else {
              var error_message = 'Something went wrong. If this problem persists, please contact customer <a href="mailto:Plus3@Plus3network.com">support</a>';
              this_alias._errorMessage(error_message);
            }
        }, 'json');
      },// END function _processTracks()

      _createTrackListings: function(r) {
        var this_alias = this;
        for(i=0;i<newActivities.length;i++){
          activity = newActivities[i];
          if(activity !== undefined && activity.md5 !== undefined) {
                var li = $('<li id="' + activity.md5 + '"/>');
                var listing = $('<div class="routeListing"></div>').appendTo(li);

                identifier = this._listingIdentifier(activity);

                $('<strong>Date:</strong><span> ' + identifier + ' </span>').appendTo(listing);
                var tracks = $("#tracks");
                tracks.show();
                tracks.prepend(li);
                li.hide();
          }
        }
    
      },

      _updatePaging: function(r) {
        if(activityQueue.length >0) {
          var this_alias = this;
          var next;
          var text;

          next = (activityQueue.length >4) ? 5 : activityQueue.length;
          text = 'Next ' + next + ' ';
          text += (next == 1) ? 'activity' : 'activities';
          $('#page-tracks').html(text);
          $('#page-tracks').show();
          $('#page-tracks').click(function() {
            this_alias._processNextTracks(r);
          });
        } else {
          $('#page-tracks').hide();
        }        
      },

      _listingIdentifier: function(activity) {
        var identifier = 'undefined';
        if(activity.filename === null) {
          identifier = activity.date_ft;
        } else {
          identifier = activity.filename;
        }
        return identifier;
      },

      _createTrackUpload: function(r, activity, type)
      {
        var post_url = this.post_url; // Need to set the post url because of this scope gets overidden.
        var this_alias = this;
        
        if(type == 'TCX') {
          var url = this.activity_post_url;
          var xml = r.controller.gpsDataString;
        }

        if(activity !== null) {
          var div = $('<div class="upload-form"/>');
          var button = $('<p><div id="upload-an-activity" class="button ui-state-default ui-corner-all"><span class="ui-icon ui-icon-transferthick-e-w"></span>Upload Activity</div></p>');
          var name = $('<p><label>Name: </label><input type="text" name="name" class="input-text" id="' + activity.md5 + '-name"/></p>');
          var desc = $('<p><label>Description: </label><textarea id="' + activity.md5 + '-desc"></textarea></p>');
          //var priv = $('<p><label><input type="checkbox" value="yes" id="' + activity.md5 + '-private"/> Keep this private!</label></p>');
          // if($('#is_private:checked').val() === '1') {
          //   priv.find('input[type=checkbox]').attr('checked','checked');
          // }
          var tags = $('<p><label>Tags (comma separated): </label><input type="text" name="tags"  class="input-text" id="' + activity.md5 + '-tags"/></p>');
          var sports = $('<p/>');
          var sports_label = $('<label> Sport: </label>').appendTo(sports);
          var sports_pulldown = $("#gps_sports").clone().appendTo(sports);
          $(sports_pulldown).attr('id', activity.md5 + '-sport');

          var privacy = $('<p/>');
          $('<label> Privacy:</label>').appendTo(privacy);
          $('#privacy').clone().attr('id', activity.md5+'-privacy').appendTo(privacy);

          $(div).append(sports);
          $(div).append(name);
          $(div).append(desc);
          $(div).append(tags);
          $(div).append(privacy);
          $(div).append(button);

          $(button).click(function() {
              $('#' + activity.md5).loading({
                  mask:true,
                  align:"center",
                  img: "/images/bigWaiting.gif"
              });

              if(type == 'FIT') {
                var data = JSON.stringify(activity.data);
              } else if(type == 'TCX') {
                var data = xml;
              }
              fields = {
                data: data,
                md5: activity.md5,
                name: $("#" + activity.md5 + "-name").val(),
                sport_id: $("#" + activity.md5 + "-sport").val(),
                tag_list: $("#" + activity.md5 + "-tags").val(),
                description: $("#" + activity.md5 + "-desc").val(),
                privacy: $("#" + activity.md5 + "-privacy").val()
              };
              $.post(post_url, fields, function(data,status) {
                  if(status == 'success'){
                    $('#' + activity.md5).loading();
                    $("#" + activity.md5).html('<a href="' + data.link + '">' + data.name + '</a> uploaded successfully. ' + data.sponsor + ' moved another <strong>$' + data.dollars_raised + '</strong> to ' + data.cause + '.');
                    refresh_activities();
                    if(type == 'FIT') {
                      this_alias._processNextTrack(r);
                    }
                  } else {
                    identifier = "undefined";
                    for(var i=0; i<newActivities.length; i++) {
                      if (newActivities[i].md5 == activity.md5) {
                        identifier = this_alias._listingIdentifier(newActivities[i]);
                      }
                    }
                    var track = $('#' + activity.md5);
                    var error_message = 'Could not upload ' + identifier + '. If this problem persists, please contact customer <a href="mailto:Plus3@Plus3network.com">support</a>';
                    this_alias._errorMessage(error_message, track);
                  }
              }, 'json');
          });

          var route = $('<div class="route"><div id="' + activity.md5 + '-map" class="chooser-map"/></div>');

          $('<strong>Date:</strong><span> ' + activity.date_ft + ' </span>').appendTo(route);
          $('<strong>Dist:</strong><span> ' + activity.dist + ' </span>').appendTo(route);
          $('<strong>Time:</strong><span> ' + activity.time_ft + ' </span>').appendTo(route);

          var track = $('#' + activity.md5);
          track.empty();
          track.append(route);
          track.append(div);
          track.show();
          if(type == 'FIT'){
            var map = new ActivityMapController(activity.md5 + '-map',activity.data);
          } else if(type == 'TCX'){
            var mapController = new Garmin.MapController(activity.md5 + '-map');
            mapController.drawTrack(activity.data);
          }

          newCount++;
          $('#upload-messages').text(newCount + ' new track(s) found');
          this._updateProgressBar(-1, r);

          return true;
        }
      }, // END _createTrackUpload();

      _updateProgressBar: function(change, r) {
        if(change != null){
          processingCount += change;

          if(processingCount <= 0){
            this._finishedProcessing(r);
          } else {
            $("#upload-progress-bar").progressbar('option', 'value', (100/processingCount));
          }
        }
        // $(".ui-widget-overlay").height($('#dialog-upload').height() + $('#dialog-upload').scrollTop() + 50);
      },

      _startProcessing: function() {
        if(!$('#upload-progress-bar').is(":visible") == true){
          $('#upload-progress-bar').show();
          $('#upload-messages').text('Processing track(s)');
          $('#page-tracks').hide();
        }
      },

      _finishedProcessing: function(r) {
        $('#upload-progress-bar').hide();
        $('#upload-messages').text('Finished processing.');
        this._updatePaging(r);
      },

      _finishedUploading: function(r) {
        $("#tracks").show();
        $('<li>All the available tracks have been processed. Give Yourself Permission to Go Out and Play.</li>').appendTo("#tracks");
      },

      _noActivitiesToUpload: function(r) {
        this._finishedProcessing(r);
        var li = $("<li>Hmmm. We can't find anything to upload -- See if it's already uploaded, then go out and have some fun!</li>");
        $('#tracks').append(li);
        $("#tracks").show();
        // $(".ui-widget-overlay").height($('#dialog-upload').height() + $('#dialog-upload').scrollTop() + 50);
      }, // END _noActivitiesToUpload

      _errorMessage: function(message, el) {
        if(el == null) { el = $('#tracks'); }

        var li = $(message);
        li = $('<li>' + message + '</li>');
        el.html(li);
        el.show();
      }

    }; // END BaseActivitiyController

    var TCXActivityController = Class.create(BaseActivityController,{
        initialize: function()
        {
          this.check_url = "/garmin/tcx_check";
          this.post_url = "/garmin/tcx_upload";
        }, // END function initialize

        read: function(r)
        {
          r.controller.readDataFromDevice(Garmin.DeviceControl.FILE_TYPES.tcxDir);
        }, // END function read

        onFinishReadFromDevice: function(r)
        {
          if(r.success) {
            switch(r.controller.gpsDataType) {
            case Garmin.DeviceControl.FILE_TYPES.tcxDir:
              this._processDirListing(r);
              break;
            case Garmin.DeviceControl.FILE_TYPES.tcxDetail:
              var activities = Garmin.TcxActivityFactory.parseDocument(r.controller.gpsData);
              var activity = this._getActivity(activities[0].attributes.activityName);
              var series = activities[0].getSeries();
              if(series.length != 0) { 
                activity.data = series[0];
                this._createTrackUpload(r, activity, 'TCX');
              } else {
                this._updateProgressBar(-1, r);
              }

              if(activityQueue.length >0){
                this._processNextTrack(r);
              } else{
                this._finishedProcessing(r);
              }
              break;
            default:
              break;
            }
          } else {
            //TODO add error handling.
          }
        }, // END function on FinishReadFromDevice()

        _getActivity: function(id)
        {
          for(i=0;i<newActivities.length; i++) {
            if(id == newActivities[i].id) {
              return newActivities[i];
            }
          }
          return null;
        }, // END function _getActivity()

        //TCX cannot handle simultaneous requests.
        //We just display all activities for TCX devices
        _processNextTracks: function(r) {
          this._updateProgressBar(1, r);
          this._processNextTrack(r);
        },

        _processNextTrack: function(r)
        {
          if(activityQueue.length >0) {
            //this._startProcessing();
            var activity = activityQueue.pop();
            if(activity) {
              this._updateProgressBar(1, r);
              r.controller.readDetailFromDevice(Garmin.DeviceControl.FILE_TYPES.tcxDetail, activity.id);
            }
          }
        }, // END function _processTrack()

        _trackListings: function(r) {
          this._createTrackListings(r);
        }

    }); // END TCXActivityController class


    var FITActivityController = Class.create(BaseActivityController,{
        initialize: function()
        {
          this.check_url = "/garmin/fit_check";
          this.post_url = "/garmin/fit_upload";
          this.convert_url = "/garmin/fit_convert";
          this.activities = new Array();
          this.total_available = 0;
        }, // END function initialize

        read: function(r)
        {
          var validPlugin = activity_uploader.verify_plugin_version(this.getPluginRequiredVersion());
          if ( validPlugin )
            r.controller.readDataFromDevice(Garmin.DeviceControl.FILE_TYPES.fitDir);
          else
            activity_uploader.reset();
        }, // END function read()

        getPluginRequiredVersion: function()
        {
          return [2,9,2,0];
        },

        onFinishReadFromDevice: function(r) {
          if(r.controller.gpsDataType == Garmin.DeviceControl.FILE_TYPES.fitDir) {
            // Send the GPS Data String to the server for processing.
            this._processDirListing(r);
          } else if(r.success) {
            // Send the FITBinary to the server for processing
            this._processFITBinary(r);
          }
        }, // END function onFinishReadFromDevice()

        _processFITBinary: function(r){
          var this_alias = this;
          var data = r.controller.gpsDataString;
          $.post(this.convert_url, {"data[]": data}, function(json, status) {
              var data = JSON.parse(json);
              if(status == 'success' && data.activities[0].status == "success") {
                this_alias._createTrackUpload(r, data.activities[0], 'FIT');
              } else {
                activity = data.activities[0];

                for(var i=0; i<newActivities.length; i++) {
                  if (newActivities[i].md5 == activity.md5) {
                    filename = newActivities[i].filename;
                  }
                }
                if(filename == null){ filename = "undefined"; }

                var track = $('#' + activity.md5);
                var error_message = 'Could not process ' + filename + '. If this problem persists, please contact customer <a href="mailto:Plus3@Plus3network.com">support</a>';
                this_alias._errorMessage(error_message, track);
                this_alias._updateProgressBar(-1, r);
                this_alias._updatePaging(r);
              }
          });
        },

        _processNextTracks: function(r) {
          for(i=0;i<5;i++){
            this._processNextTrack(r);
          }
        },

        _processNextTrack: function(r){
          if(activityQueue.length >0) {
            this._startProcessing();
            var activity = activityQueue.pop();
            if(activity) {
              r.controller.getBinaryFile(r.controller.deviceNumber, activity.filename);
              this._updateProgressBar(1, r);
            }
          } else {
            this._finishedProcessing(r);
          }
        },

        _trackListings: function(r) {
          this._createTrackListings(r);
          
          var tracks = $("#tracks");
          li = $('<li></li>')
          li.appendTo(tracks);
          var link = $('<a href="#" id="page-tracks"></a>');
          link.appendTo(li);
          this._updatePaging(r);
        }
    }); // END FITActivityController class


    // This is a dummy controller. It's used to get us past the activity finder.
    var ActivityController = Class.create({
        initialize: function()
        {
          this.target = null;
        }, // END function initialize

        set_type: function(type)
        {
          switch(type) {
          case 'FIT':
            this.target = new FITActivityController();
            break;
          default:
            this.target = new TCXActivityController();
            break;
          }
        },

        onStartFindDevices: function(r) { },
        onCancelFindDevices: function(r) { },
        onInteractionWithNoDevice: function(r) { },
        onStartReadFromDevice: function(r) { },
        onFinishReadFromDevice: function(r) { this.target.onFinishReadFromDevice(r); },
        onWaitingReadFromDevice: function(xml_message, r) { },
        onCancelReadFromDevice: function(r) { },

        read: function(r) { this.target.read(r); },

        onFinishFindDevices: function(r)
        {

          if(r.controller.numDevices == 0) {
            $("#upload-messages").html("We were unable to find any devices.");
          } else if(r.controller.numDevices == 1) {
            $('#upload-messages').text("Found Device... " + r.controller.devices[0].displayName);

            if(opperation == 'read') {
              activity_uploader.read_from(r.controller.deviceNumber,r);
            } else if(opperation == "write") {
              activity_uploader.write_to(r.controller.deviceNumber,r);
            }

          } else {
            for(n=0;n<r.controller.numDevices;n++) {
              $("<li>" + r.controller.devices[n].displayName + " <div id=\"device-" + n + "\"class=\"button-right ui-state-default ui-corner-all\"><span class=\"ui-icon ui-icon-play\"></span>Use this Device</div></li>").appendTo('#tracks');
              $("#device-" + n).click(function() {
                  var number = this.id.replace(/device-/, '');
                  if(opperation == "read") {
                    activity_uploader.read_from(number,r);
                  } else if (opperation == "write") {
                    activity_uploader.write_to(number,r);
                  }

              });
            }
            $('#upload-messages').html('Please choose a device to use.');
            $("#tracks").show();

          }
        }, // END function onFinishFindDevices()

        onProgressReadFromDevice: function(r)
        {
          try {
            if(r.progress != undefined) {
              if(r.progress.text != undefined) {
                if(r.progress.text[0] != undefined) {
                  if(r.progress.percentage != undefined) {
                    if(r.progress.percentage < 100) {
                      if(r.progress.text[0] != undefined) {
                        $('#upload-messages').text(r.progress.text[0]);
                      }
                    } else {
                      $('#upload-messages').text('Processing tracks.');
                    }
                  }

                  $('#upload-progress-bar').show();

                  if(r.progress.percentage != undefined) {
                    $("#upload-progress-bar").progressbar('option', 'value', r.progress.percentage);
                  }

                  if(r.progress != undefined) {
                    if(r.progress.text[0] != undefined && r.progress.text.length > 1) {
                      var message = r.progress.text[0] + '... ';
                      message += r.progress.text[1];
                      $('#upload-messages').text(message);

                      if(r.progress.text[0].match(/^100/)) {
                        $("#upload-progress-bar").progressbar('option', 'value', 100);
                      }
                    }
                  }
                }
              }
            }
          } catch(e) {
            // Do nothing!
          }
        }, // END function onProgressReadFromDevice()

        onProgressWriteToDevice: function(r)
        {
          this.onProgressReadFromDevice(r);
        }, // END function onProgressWriteToDevice()

        onException: function(error)
        {
          // console.log(error);
          var errorStatus;
          var hideFromBrowser = false;
          if(error.name == "BrowserNotSupportedException") {
            errorStatus = "<h1>Oops!</h1><p>It appears your browser is not supported with this version of the Garmin plugin. If you have the latest version of the Garmin Plugin then you will need to try a different browser. Otherwise you should download the latest version from <a href=\"http://www8.garmin.com/products/communicator/\">Garmin's Website</a>.</p>";
          } else if (error.name == "PluginNotInstalledException" || error.name == "OutOfDatePluginException") {
            errorStatus = "<h1>Oops!</h1><p>It appears you either don't have the Garmin Communicator Plugin installed or it's out of date. Please visit <a href=\"http://www8.garmin.com/products/communicator/\">Garmin's Website</a> to download the latest Communicator plug-in.</p>";
            errorStatus += "<p>" + error.message + "</p>";
          } else if (Garmin.PluginUtils.isDeviceErrorXml(error)) {
            errorStatus = "<h1>Oops!</h1>";
            errorStatus += "<p>" + Garmin.PluginUtils.getDeviceErrorMessage(error) + "</p>";
          } else {
            errorStatus = "<h1>Oops!</h1>";
            errorStatus += "<p>Something terrible happened... </p>";
          }
          $('#upload-messages').html(errorStatus);
        },

        onFinishWriteToDevice: function(r) {
          $('#upload-progress-bar').hide();
          $('#upload-messages').text('Congratulations! The course was successfully saved to your device.');
        } // END function onFinishWriteToDevice()

    }); // END ActivityController class

    $.fn.wait = function(time, type) {
      time = time || 1000;
      type = type || "fx";
      return this.queue(type, function() {
          var self = this;
          setTimeout(function() {
              $(self).dequeue();
          }, time);
      });
    };




    function inArray(val, arr) {
      for(i=0;i<arr.length;i++) {
        if(val == arr[i]) {
          return true
        }
      }
      return false;
    }

    var errors = $('<ul/>');

    function clear_entry_form() {
      allFields = $([]).add("#activity_name").add("activity_sport").add("#activity_description").add("#activity_hrs").add("#activity_mins").add("#activity_secs").add("#activity_distance").add("#activity_tag_list");
      $("#activity_private").attr('checked', false);
      allFields.val("").removeClass('ui-state-error');
      $('#dialog-entry-form div.errors').remove();
      $('#activity_hrs').val('00');
      $('#activity_mins').val('00');
      $('#activity_secs').val('00');
      errors.empty();
    }

    function check_not_null(field, label) {
      if(field.val().length <= 0) {
        field.addClass('ui-state-error');
        errors.append("<li>" + label + " is required.</li>");
        return false;
      }
      field.removeClass('ui-state-error');
      return true;
    }

    function check_date_not_future(field)
    {
      var now = new Date();
      var target = Date.parse(field.val());

      if(now.getTime() < target) {
        field.addClass('ui-state-error');
        errors.append("<li>The start time can not be in the future.</li>");
        return false;
      }
      return true;
    }

    function check_less_than(field, value, message)
    {
      if(field.val() > value) {
        field.addClass('ui-state-error');
        errors.append("<li>" + message + "</li>");
        return false;
      }

      return true;
    }

    function show_errors(dialog_id) {
      $(dialog_id).prepend('<div class="errors ui-state-error"/>');
      $(dialog_id + " div.errors").append('<h4>Please fix the following errors:</h4>');
      $(dialog_id + " div.errors").append(errors);
    }

    function show_confirmation(dialog_id, message) {
      $(dialog_id).prepend('<div class="info-message">' + message + '</div>');
      $(dialog_id + " div.info-message").wait(6000).fadeOut(500);
    }

    function refresh_activities() {
      if($('#activities').hasClass('home')) {
        $.get('/api/get_activities', {view:$("#current_view").val(), page:1}, function(data, status) {
            if(status = 'success') {
              $('#activities > ul').replaceWith("<ul>"+data+"</ul>");
            }
        }, 'html');
      } else if ($("#my-routes-content").length > 0) {
        updateMyRoutes(1,true);
      }
    }

    function submit_entry_form(type) {
      var valid = true;
      $('#dialog-entry-form div.errors').remove();
      errors.empty();

      valid = check_not_null($("#activity_name"), "Activity name") && valid;
      valid = check_date_not_future($("#timestamp_of_run")) && valid;
      valid = check_less_than($("#activity_hrs"), 10, "Wow! Are you sure you exercised for " + $("#activity_hrs").val() + " hours?") && valid;
      valid = check_less_than($("#activity_mins"), 59, "You can't enter more then 59 minutes.") && valid;
      valid = check_less_than($("#activity_secs"), 59, "You can't enter more then 59 seconds.") && valid;
      if($("#activity_distance_units").val() == 'miles') {
        valid = check_less_than($("#activity_distance"), 210, "You can only enter up to 210 miles on a hand enter activity.") && valid;
      } else if ($("#activity_distance_units").val() == 'meters') {
        valid = check_less_than($("#activity_distance"), 16093, "You can only enter up to 16,093 meters on a hand enter activity.") && valid;
      }


      if(valid) {
        allFields = $([]).add("#activity_name").add("#activity_sport").add("#activity_description").add("#timestamp_of_run").add("#activity_hrs").add("#activity_mins").add("#activity_secs").add("#activity_distance").add('#activity_private').add('#activity_distance_units').add("#activity_tag_list");
        $.post('/api/activity', allFields.serialize(), function(data, text_status) {
            refresh_activities();
            clear_entry_form();
            var message = 'Right on! ' + data.sponsor + ' will move another <strong>$' + data.dollars_raised + '</strong> to ' + data.cause + '. Every day, you and all of Plus 3 are Making It Count. Give us a couple of minutes and you\'ll see your Kudos on the leader board.';
            if(type == "done") {
              show_confirmation('#main', message);
            } else {
              show_confirmation('#dialog-entry-form', message);
            }

        }, 'json');
        return true;
      } else {
        show_errors("#dialog-entry-form");
        return false;
      }
    }

    $("div.info-message").ready(function() {
        $("div.info-message").wait(6000).fadeOut(500);
    });


    $('#dialog-entry-form').ready(function() {

        $('#dialog-entry-form').dialog({
            bgiframe: true,
            autoOpen: false,
            modal: true,
            draggable: false,
            width: 625,
            resizable: false,
            title: "Hand Enter an Activity",
            buttons: {
              "Enter and Add Another Activity" : function() {
                submit_entry_form("another");
              },

              "Enter and Done" : function() {
                if(submit_entry_form("done")) {
                  $(this).dialog('close');
                }
              }

            },
            close: function() {
              clear_entry_form();
            }
        });
    });

    $(".date-select").ready(function() {
        $(".date-select").change(function() {
            var month = $("#date_month").val();
            if(month < 10) {
              //month = "0" + month;
            }
            var day = $("#date_day").val();
            if(day < 10) {
              //day = "0" + day;
            }
            var year = $("#date_year").val();
            var hour = $("#date_hour").val();
            var minute = $("#date_minute").val();
            var ampm = $("#date_ampm").val();

            $("#timestamp_of_run").val(month+"/"+day+"/"+year+" "+hour+":"+minute+" "+ampm);
        });
    });

    $('#dialog-edit-details').ready(function() {

        $('#dialog-edit-details').dialog({
            bgiframe: true,
            autoOpen: false,
            modal: true,
            draggable: false,
            width: 625,
            resizable: false,
            title: "Edit Activity Details",
            buttons: {
              "Save Details" : function() {
                var valid = true;
                $('#dialog-edit-details div.errors').remove();
                errors.empty();

                valid = check_not_null($("#activity_name"), "Activity name") && valid;
                valid = check_date_not_future($("#timestamp_of_run")) && valid;

                if(valid) {
                  allFields = $([]).add("#id").add("#activity_name").add("#activity_sport").add("#activity_description").add('#activity-privacy').add("#timestamp_of_run");
                  $.post('/api/update_activity', allFields.serialize(), function(data, status) {
                      if(status = 'success') {
                        $("#" + $("#id").val() + "-title").html($("#activity_name").val());
                        $("#" + $("#id").val() + "-description").html($("#activity_description").val().replace(/\n/g, '<br/>'));
                        $("#sport").html(data.sport);
                        $("#amount").html(data.points);
                        $("#timestamp_of_run-sum").html(data.timestamp);
                        $("#dollars-raised").html("$" + data.points);
                        
                        switch($('#activity-privacy').val()) {
                          case 'private':
                            $("#privacy").text("Only you can see this.");
                            break;
                          case 'friends':
                            $("#privacy").text("Only friends can see this.");
                            break;
                          case 'clubhouse':
                            $("#privacy").text("Only co-workers and friends can see this.");
                            break;
                          default:
                            $("#privacy").text("Anyone can see this.");
                            break;
                        }

                        show_confirmation('#main', 'Right on! ' + data.sponsor + ' will move another <strong>$' + data.dollars_raised + '</strong> to ' + data.cause + '. Every day, you and all of Plus 3 are Making It Count.');
                        $('#dialog-edit-details').dialog('close');
                      }
                  }, 'json');
                } else {
                  show_errors("#dialog-edit-details-form");
                }
              },

              "Cancel" : function() {
                $('#dialog-edit-details').dialog('close');
              }

            }
        });
    });

    $("#edit-details-btn").ready(function() {
        $("#edit-details-btn").click(function() {
            $('#dialog-edit-details').dialog('open');
        });
    });

    $('#dialog-upload').ready(function() {
        $('#dialog-upload').dialog({
            /* bgiframe: true, */
            autoOpen: false,
            modal: true,
            position: "top",
            draggable: false,
            width: 800,
            minHeight: 50,
            resizable: true,
            title: "Upload an Activity",
            close: function() {
              activity_uploader.reset();
              $('#upload-messages').text("Searching for devices...");
              $("#upload-progress-bar").progressbar('option', 'value', 0);
              $("#tracks").children().remove();
            },
            open: function(event,ui) {
              $("#upload-progress-bar").progressbar({ value: 0 });
            }
        });
    });

    $('#dialog-email-route').ready(function() {

        $('#dialog-email-route').dialog({
            bgiframe: true,
            autoOpen: false,
            modal: true,
            draggable: false,
            width: 600,
            resizable: false,
            title: "Email a Friend",
            buttons: {
              "Send" : function() {
                var users = $.map($('.user-token'), function(item,index) {
                    return $(item).val();
                });

                var groups = $.map($('.group-token'), function(item,index) {
                    return $(item).val();
                });

                var emails = $.map($('.email-only-token'), function(item,index) {
                    return $(item).val();
                });

                var fields = {
                  "users[]" : users,
                  "groups[]" : groups,
                  "emails[]" : emails,
                  "id" : $('input[name=id]').val(),
                  "message": $('textarea[name=message]').val()
                }


                $.post('/api/email_activity', fields, function(data,status) {
                    if(status == 'success') {
                      $('#dialog-email-route').dialog('close');
                      show_confirmation('#main', 'Your email, eloquent and pithy, was sent.');
                    }
                }, 'json');
              },

              "Cancel" : function() {
                $('#dialog-email-route').dialog('close');
              }

            },
            close: function() {
              $('.email-token').remove();
              $("#message").val("");
            }
        });
    });

    $('#dialog-email-route form').ready(function() {
        $('#dialog-email-route form').submit(function() {
            return false;
        });
    });

    $("#email-to-friend").ready(function() {
        $("#email-to-friend").click(function() {
            $("#dialog-email-route").dialog("open");
        });
    })

    function resize_token() {
      $("#token").width($("#token").val().length*.5 + 2.4 + "em");
    }

    function add_email_only_token() {
      if($("#token").val().match(/(.+)@(.+)\.(.+)/)) {
        $("#token-borderless").before('<div class="email-token ui-corner-all"><span class="remove-token ui-icon ui-icon-close"></span>' + $("#token").val() + '<input type="hidden" name="email" class="email-only-token" value="' + $("#token").val() + '"/></div>');
        $(".remove-token").click(function() {
            $(this).parent().fadeOut();
            $(this).parent().remove();
        });
        $("#token").val("");
        resize_token();
      } else {
        $("#token").val("");
      }
    }

    $("#email-field").ready(function() {
        $("#email-field").click(function() {
            $("#token").focus();
        });
    });

    $("#token").exists(function() {
        $("#token").keydown(function(){
            resize_token();
        });

        $("#token").blur(function() {
            add_email_only_token();
        });

        $("#token").keyup(function(event) {
            if(event.keyCode == 13) {
              add_email_only_token();
            }
        });

        $("#token").autocomplete(contact_list, {
            minChars: 0,
            width: 210,
            matchContains: "word",
            autoFill: false,
            formatItem: function(row, i, max) {
              return row.name + "<span class=\"ac_email\">" + row.to + "</span>";
            },
            formatMatch: function(row, i, max) {
              return row.name + " " + row.to;
            }
        }).result(function(event,item) {
          if(item.to == "Send to group") {
            $("#token-borderless").before('<div class="email-token ui-corner-all"><span class="remove-token ui-icon ui-icon-close"></span>'+item.name+'<input name="group" type="hidden" class="group-token" value="' + item.id + '"/></div>');
          } else {
            $("#token-borderless").before('<div class="email-token ui-corner-all"><span class="remove-token ui-icon ui-icon-close"></span>'+item.name+'<input name="user" type="hidden" class="user-token" value="' + item.id + '"/></div>');
          }
          $(".remove-token").click(function() {
              $(this).parent().fadeOut();
              $(this).parent().remove();
          });
          $("#token").val("");
          resize_token();
        });
    });

    $("#hand-enter-an-activity").ready(function() {
        $("#hand-enter-an-activity").click(function() {
            $('#dialog-entry-form').dialog("open");
        })
    });

    $("#upload-an-activity").ready(function() {
        $("#upload-an-activity").click(function() {
            $('#dialog-upload').dialog("open");
            activity_uploader = new ActivityUploader({
                key: garmin_key,
                check_url:"/api/check_activities",
                post_url:"/api/upload_activity"
            });
            activity_uploader.start("read");
            return false;
        });
    });

    $("#use-in-event").ready(function() {
        $("#use-in-event").click(function() {
            $('#dialog-event').dialog("open");
        })
    });

    $('#dialog-event').ready(function() {

        $('#dialog-event').dialog({
            bgiframe: true,
            autoOpen: false,
            modal: true,
            draggable: false,
            width: 400,
            resizable: false,
            title: "Use in event",
            buttons: {
              "Attach to Event" : function() {
                $("#dialog-event form").validate();
                if($("#dialog-event form").valid()) {
                  $.post('/routes/use_in_event', $('#dialog-event form').serialize(), function(data,status) {
                      if(status == 'success') {
                        $("#dialog-event").dialog("close");
                        show_confirmation('#main', 'The route has been added to the selected event. Nicely done!');
                      }
                  }, 'json');
                }
              },

              "Cancel" : function() {
                $("#dialog-event form select").val("");
                $('#dialog-event').dialog('close');
              }

            }
        });

    });

    $("#save-to-device").ready(function() {
        $("#save-to-device").click(function() {
            $('#dialog-upload').dialog('option', 'title', 'Save to Device');
            $('#dialog-upload').dialog('option', 'width', 500);
            $('#dialog-upload').dialog("open");
            activity_uploader = new ActivityUploader({
                key: garmin_key,
                check_url:"/api/check_activities",
                post_url:"/api/upload_activity"
            });
            activity_uploader.start('write');
        })
    });

    $("div.button,div.button-right").ready(function() {
        $("div.button,div.button-right").hover(
          function() { $(this).addClass('ui-state-hover'); },
          function() { $(this).removeClass('ui-state-hover'); }
          );
    });

    $("#start-address-map-btn").ready(function() {
        $("#start-address-map-btn").click(function() {
            ResetMap();
            $("#pace-controls").show();
        });
    });

    $("form#start-map-form").ready(function() {
        $("form#start-map-form").submit(function() {
            ResetMap();
            $("#pace-controls").show();
            return false;
        });
    });

    $("#save-map-btn").ready(function() {
        $("#save-map-btn").click(function() {
            save_route(activity_id,timestamp_of_activity);
        });
    });

    $("#reset-map-btn").ready(function() {
        $("#reset-map-btn").click(function() {
            $("#map").html('<div class="ui-overlay"><div class="ui-widget-overlay"></div><div id="map-controls" class="ui-dialog-content ui-widget-content ui-corner-all"><h3>Would you like draw your route on a map?</h3><p>Enter a staring address in the field below and click the "Get&nbsp;Started!" button. Once the map appears, click once on the map for the starting point. Then continue to click to create your route.</p><form id="start-map-form"><p><label>Address (or just city, state):</label> <input type="text" id="city_state" name="city_state"/></p></form><div id="start-address-map-btn" class="button-right ui-state-default ui-corner-all"><span class="ui-icon ui-icon-play"></span>Get Started!</div></div></div>');
            $("#distance_span").text('0.00 miles');
            $("#pace-controls").hide();
            $("form#start-map-form").submit(function() {
                ResetMap();
                $("#pace-controls").show();
                return false;
            });
            $("#start-address-map-btn").click(function() {
                ResetMap();
                $("#pace-controls").show();
            });
        });
    });

    $("div#map.gps-data").ready(function() {
        if($("div#map.gps-data")[0]) {
          $.get('/activity/data/' + activity_id, function(data, status) {
              var map = new ActivityMapController("map", JSON.parse(data));
          }, 'json');
        }
    });

    $('#activity_sport.entry-form-sports').ready(function() {
        $('#activity_sport.entry-form-sports').change(function() {
            if(inArray(this.value, time_based_sports)) {
              $("#activity-distance-field").hide();
            } else {
              $("#activity-distance-field").show();
            }
        });
    });

    $('span.editable').ready(function() {
        $('span.editable').editable({
            type: "text",
            submit: "Save",
            cancel: "Cancel",
            onSubmit: function (content) {
              id = $(this)[0].id.replace(/\-title/,"");
              $.get("/api/save_activity_attribute", {name:"name", value:content.current, id:id});
              $(this).addClass('highlight');
            },
            onEdit: function() { $(this).removeClass('highlight'); },
            onCancel: function() { $(this).addClass('highlight'); }
        });
    });

    $('div.description.editable').ready(function() {
        $("div.description.editable").editable({
            type: "textarea",
            submit: "Save",
            cancel: "Cancel",
            onSubmit: function (content) {
              id = $(this)[0].id.replace(/\-description/,"");
              $.get("/api/save_activity_attribute", {name:"description", value:content.current, id:id});
              $(this).addClass('highlight');
              $(this).removeClass('empty');
              var new_content = content.current.replace(/\n/g, '<br/>');
              $(this).html(new_content);
            },
            onEdit: function() {
              if(this.hasClass("empty")) {
                $("div.description.editable textarea").val("");
              } else {
                var content = $("div.description.editable textarea").val().replace(/<br>/g, "\n");
                $("div.description.editable textarea").val(content);
              }
              $(this).removeClass('highlight');
            },
            onCancel: function() {
              $(this).addClass('highlight');
            }
        });
    });

    $("#add-tag-btn").ready(function() {
        $("#add-tag-btn").click(add_tag);
    });


    function add_tag() {
      var fields = $([]).add("#id").add("#tag_list");
      $.post('/api/tag_activity', fields.serialize(), function(data,text_status) {
          if(text_status == "success") {
            $("#tag_list").val("");

            for(n=0;n<data.tags.length;n++) {
              var tag = $('<li><a href="/search/activities/?type=everyone&amp;tag[]=' + data.tags[n].replace(/\s/,'%20') + '">' + data.tags[n] + '</a><span class="delete-tag ui-icon ui-icon-close" title="Delete Tag"></span></li>').appendTo('#activity-tags');
            }

            $("span.delete-tag").click(function() {
                delete_tag(this);
            });
          }



      }, "json");
    }

    $("span.delete-tag").ready(function() {
        $("span.delete-tag").click(function() {
            delete_tag(this);
        });
    });

    function delete_tag(obj) {
      tag = $(obj).siblings().text();
      if(confirm("Are you sure you want to remove the '" + tag + "' tag?")) {
        $.post('/api/remove_tag', {id:$("#id").val(), tag:tag}, function(data,text_status) {
            if(text_status == 'success') {
              $(obj.parentNode).fadeOut(1000);
            }
        }, "json");
      }
    }

    $("form#add-tag-form").ready(function() {
        $("form#add-tag-form").submit(function() {
            add_tag();
            return false;
        });
    });

    $("#add-comment-btn").ready(function() {
        $("#add-comment-btn").click(function() {
            $("#add-comment form").validate();
            if($("#add-comment form").valid()) {
              $("#add-comment form").submit();
            }
        });
    });

    $("span.delete-comment").ready(function() {
        $("span.delete-comment").click(function() {
            var fields = {
              id: $("#add-comment form input[name=id]").val(),
              remark_id: this.id.replace(/delete\-comment\-/,'')
            }
            $.post('/routes/delete_comment', fields, function(data,status) {
                if(status == 'success') {
                  $("#remark-" + data.remark_id).fadeOut(1000, function() {
                      $(this).remove();
                      if($("#comments li").length == 0) {
                        $("#comments").remove();
                      }
                  });
                }
            }, 'json');
        });
    });


    function activate_views(event) {
      var obj = event.currentTarget;
      var previous_view = $("#current_view").val();
      $(".show-filter.activities").loading({
          mask:true,
          align:"center",
          img: "/images/waiting.gif"
      });
      $.get('/api/get_activities', {view:obj.id, page:1}, function(data,status) {
          if(status == "success") {
            $(".show-filter.activities").loading();
            $(obj).removeClass('active');
            $(obj).click(function() {});
            $("#"+previous_view).addClass('active').click(activate_views);
            // $("#activities h2").text($(obj).attr('title'));
            $("#current_view").val(obj.id);
            $("#current_page").val(1);
            $("#activities > ul").replaceWith('<ul>'+data+'</ul>');
            if($("#activities li.none").length > 0) {
              $("#activities > ul").replaceWith("<ul><li class=\"none\">You don't have any activities... You should really get out there and do something!</li></ul>");
              $('div.load-more').hide();
            } else if ($("#activities li.done").length > 0) {
              $('div.load-more').hide();
            } else {
              $('div.load-more').show();
            }

          }
      });
    }

    $(".activities span.view.active").ready(function() {
        $(".activities span.view.active").click(activate_views);
    });

    function lb_views(event) {
      var obj = event.currentTarget;
      var previous_view = $("#current_lb_view").val();
      $(".month-leader-board-widget .show-filter").loading({
          mask:true,
          align:"center",
          img: "/images/waiting.gif"
      });
      $.get('/api/leader_board', {view:obj.id.replace(/lb-/,"")}, function(data,status) {
          if(status == "success") {
            $(".month-leader-board-widget .show-filter").loading();
            $(obj).removeClass('active');
            $(obj).click(function() {});
            $("#"+previous_view).addClass('active').click(lb_views);
            $("#current_lb_view").val(obj.id);
            $(".month-leader-board-widget > ul").replaceWith('<ul>'+data+'</ul>');
          }
      });
    }

    $(".month-leader-board-widget span.view.active").ready(function() {
        $(".month-leader-board-widget span.view.active").click(lb_views);
    });

    $("div.load-more").ready(function() {
        $("div.load-more").click(function() {
            var previous_page = parseInt($("#current_page").val());
            $(this).loading({
                mask:true,
                align:"center",
                img: "/images/waiting.gif"
            });
            $.get('/api/get_activities', {view:$("#current_view").val(), page:previous_page+1}, function(data,status) {
                if(status == "success") {
                  $('div.load-more').loading();
                  $("#current_page").val(previous_page+1);
                  $(data).appendTo('#activities > ul');
                  if($('li.done').length > 0) {
                    $('div.load-more').hide();
                  }
                }
            });
        });
    });

	$('#add-friend-dialog').exists(function() {
	    $('#add-friend-dialog').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			draggable: false,
			width: 400,
			position: 'top',
			resizable: false,
			title: "Send Friend Request",
			buttons: {
				"Cancel" : function() {
				    $('#add-friend-dialog').dialog('close');
				},
				
				"Send Request" : function() {
                    $.post('/people/friend_add/', $('#add-friend-dialog form').serialize(), function(data,status) {
                       if(data == 'Your friend request has been sent.') {
                           $('<div class="info-message header-with-image">'+data+'</div>').prependTo('#main');
                           $("div.info-message").wait(6000).fadeOut(500);
                           $('#add-friend-dialog').dialog('close');
                           $('#user-'+$('#friend_id').val()+' .friend-me .buttons').replaceWith('<span class="pending">Pending Friend</span>');
                       }
                    });
				}
			},
			close: function() {
				
			}
		});
	});
	
	$('.friend-me a.buttons').exists(function() {
	    $('.friend-me a.buttons').click(function(e){
	        var id = e.currentTarget.id;
	        $('#friend_id').val(id);
	        var name = $('#user-'+id+' h3 > a').text();
	        $('#add-friend-dialog .name')[0].innerHTML = name;
	        window.scrollTo(0,0);
	        $('#add-friend-dialog').dialog('open');
	    }); 
	});
	
	$('#create-group-dialog').exists(function() {
	    $('#create-group-dialog').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			draggable: false,
			width: 500,
			resizable: false,
			title: "Create a Group",
			buttons: {
				"Cancel" : function() {
				    $('#create-group-dialog').dialog('close');
				},
				
				"Create Group" : function() {
				    var users = $.map($('#create-group-dialog form .user-token'), function(item,index) {
                        return $(item).val();
                    });

                    var groups = $.map($('#create-group-dialog form .group-token'), function(item,index) {
                        return $(item).val();
                    });

                    var emails = $.map($('#create-group-dialog form .email-only-token'), function(item,index) {
                        return $(item).val();
                    });

                    var fields = {
                      "users[]" : users,
                      "groups[]" : groups,
                      "emails[]" : emails,
                      "name" : $('#create-group-dialog form input[name=name]').val(),
                      "type": $('#create-group-dialog form select option:selected').val(),
                      "crumb": $('#create-group-dialog form input[name=crumb]').val()
                    }
                    $.post('/groups/create', fields, function(data,status) {
                        window.location.reload();
                    }, 'json');
				}
			},
			close: function() {
				
			}
		});
	});
	
	$('#add-member-dialog').exists(function() {
	   $('#add-member-dialog').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			draggable: false,
			width: 500,
			resizable: false,
			title: "Add Members",
			buttons: {
				"Cancel" : function() {
				    $('#add-member-dialog').dialog('close');
				},
				
				"Add Members" : function() {
                    $.post('/groups/add_members', $('#add-member-dialog form').serialize(), function(data,status) {
                        window.location.reload();
                    }, 'json');
				}
			},
			close: function() {
				
			}
		});
	});
	
	$('#add-group-member').exists(function() {
	  $('#add-group-member').click(function() {
	      $('#add-member-dialog').dialog('open');
	  }); 
	});
	
	$('#create-group-btn').exists(function() {
	  $('#create-group-btn').click(function() {
	      $('#create-group-dialog').dialog('open');
	  }); 
	});
	
	$('.picker.primary').exists(function() {
           $('.picker.primary').ColorPicker({
               onBeforeShow: function () {
                   $(this).ColorPickerSetColor($('.picker.primary input').val());
               },
               onShow: function (colpkr) {
                   $(colpkr).fadeIn(500);
                   return false;
               },
               onHide: function (colpkr) {
                   $(colpkr).fadeOut(500);
                   return false;
               },
               onChange: function (hsb, hex, rgb) {
                   $('.picker.primary').css('backgroundColor', '#' + hex);
                   $('.picker.primary input').val('#' + hex);
               }
           });
       });
       
     $('.group-members').exists(function() {
        $('.group-members .remove').click(function() {
           return confirm('Are you sure you want to remove this user from the group?'); 
        });
        
        $('.group-members .block').click(function() {
           return confirm('Are you sure you want to block this user?'); 
        });
        
        $('.group-members .unblock').click(function() {
           return confirm('Are you sure you want to unblock this user?'); 
        });
     });
     
     $('#header-search-form').exists(function() {
        $('#header-search-form div.buttons').click(nav_search);
        $('#header-search-form').submit(nav_search);
     });
     
     function nav_search() {
         var url = '/search/'+$('#header-search-form select[name="content"] option:selected').val()+'?type=everyone&query='+$('#header-search-form input[name="query"]').val();
         window.location = url;
     }
     
     $('a.remove-friend-btn').exists(function() {
         $('a.remove-friend-btn').click(function() {
             return confirm('Are you sure you want to remove this friend?');
         })
     });

     $('.main-images').exists(function() {
        var timer = window.setInterval(function() {
            if(current_image < 9) {
                switch_images(images[current_image],images[++current_image]);
            } else {
                switch_images(images[current_image],images[0]);
                current_image = 0;
            }
        }, 10000);

     });

     $('.small-icons').exists(function() {
       $('.small-icons').click(function() {
          next = images.indexOf(this.id);
          switch_images(images[current_image],images[next]);
          current_image = next;
       });
     });

     function switch_images(current, next) {
        $('.large-img').removeClass(current);
        $('.large-img').addClass(next);
        $('.small-icons.'+current).removeClass('on');
        $('.small-icons.'+current).addClass('off');
        $('.small-icons.'+next).removeClass('off');
        $('.small-icons.'+next).addClass('on');
     }
     
     $('#current_view').exists(function() {
         /* var refresh_id = window.setInterval(function() {
             if($("#current_page").val() === '1') {
                 refresh_activities();
             } else {
                 window.clearInterval(refresh_id);
             }
             
         },5000); */
     });
     
     $('#signup form').exists(function() {
         $('#signup form').validate({
             onsubmit: true,
             rules: {
                 'first_name': 'required',
                 'last_name': 'required',
                 'agree-with-privacy-and-tou': 'required',
                 'email': {
                     required: true,
                     email: true,
                     remote: '/signup/validate-email'
                 },
                 'email_confirm': {
                     required: true,
                     email: true,
	            	     equalTo: '#email'
                 },
                 'password': {
                     'required': true,
                     minlength: 5
                 },
                 'password_confirm': {
                     required: true,
                     minlength: 5,
                     equalTo: '#password'
                 },
                 'causes_sponsors_id': {
                    required: true,
                    remote: {
                      url: '/signup/validate-causes-sponsors', 
                      data: {
                        email: function () {  return $('#email').val(); }
                      }
                    }
                 }
             },
             messages: {
                 'password_confirm': {
                     equalTo: "Enter the same password."
                 },
                 'email': {
                     remote: "This email has already been registered."
                 },
                 'agree-with-privacy-and-tou': ' This field is required.',
                 'causes_sponsors_id' : {
                    remote: 'For this Cause and Sponsor combo you need to use an email domain that matches the employer\'s corporate email domain.'
                 }
             }
         });
         
     });
     
     $('ul.carousel').exists(function() {
         $('ul.carousel').jcarousel({
             scroll: 1,
             visible: 1,
             buttonNextHTML: '<div class="arrows">&#9656;</div>',
             buttonPrevHTML: '<div class="arrows">&#9666;</div>',
             wrap: 'circular',
             itemVisibleInCallback: {
               onAfterAnimation: function (carousel, item, index, state) {
                   $('#causes_sponsors_id').val(item.id);
                   if(state != 'init') {
                      $('#signup form').validate().element('#causes_sponsors_id');
                   }
               }
             },
             initCallback: function(carousel) {
                 $('#cause-sponsor-select .buttons').click(function() {
                     carousel.scroll($.jcarousel.intval(this.id.replace(/index-/,'')));
                     $('#cause-sponsor-select').addClass('hidden');  
                     $('#modal').addClass('hidden');
                     $('#signup form').validate().element('#causes_sponsors_id');
                     return false; 
                  });
             }
         });
     });
     
     $('#cause-sponsor-select .close').click(function() {
       $('#cause-sponsor-select').addClass('hidden');  
       $('#modal').addClass('hidden');
       return false;
     });
     
     $('#show-cause-sponsor-select').click(function() {
        $('#cause-sponsor-select').removeClass('hidden');  
        $('#modal').removeClass('hidden');
        return false;
     });

    $('dialog-edit-details-form').exists(function() {
      function resetForm(id) {
        $j('#'+id).each(function(){
                this.reset();
        });
      }
    });

  })(jQuery); // END Anonymous function

