function debouncer( func , timeout ) {
   var timeoutID , timeout = timeout || 300;
   return function () {
      var scope = this , args = arguments;
      clearTimeout( timeoutID );
      timeoutID = setTimeout( function () {
          func.apply( scope , Array.prototype.slice.call( args ) );
      } , timeout );
   }
}
var greyscaleMapTypeId = 'greyscale';
var greyscale_style = [
  {
    featureType: "all",
    elementType: "all",
    stylers: [
      { saturation: -97 },
      { gamma: 0.57 }
    ]
  },{
    featureType: "all",
    elementType: "all",
    stylers: [

    ]
  }
];
var greyscaleMapOptions = {
  name: "Greyscale"
};

(function( $ ){
  $.fn.bindJsonResponse = function( options ) {
    var settings = {
      before         : function(){},
      success        : false,
      error          : false
    };

    return this.each(function() {
      if ( options ) { 
        $.extend( settings, options );
      }
      $(this).bind('ajax:before', function() {
        settings.before();
      }).bind('ajax:success', function(event, data, textStatus, xhr) {
        if(data.status == "ok") { 
          if(settings.success) {
            settings.success();
          } else {
            createFlash('notice', data.message)
          }
        } else if(data.status == "error") {
          if(settings.error) {
            settings.error();
          } else {
            createFlash('error', data.message);
          }
        } else if(data.status == "redirect") {
          window.location = data.to;
        }
      });
    });
  };
})(jQuery);

function createFlash(type, message) { 
  $('#page-main-container').append('<div class="flash-message ' + type + '" style="display:none;">' + message + '</div>')
  $.scrollTo('0px', 500, {
    onAfter: function() {
    $('.flash-message').delay(1000).slideDown().delay(2500).slideUp();      
    }
  });
}

(function( $ ){
  $.fn.bindAjaxPopup = function( options ) {
    var settings = {
      title        : '',
      height       : 'auto',
      width        : 1000,
      open         : function () {},
      requireUser  : true
    };
    
    if ( options ) { 
      $.extend( settings, options );
    }
    
    if(settings.requireUser && window.loggedIn == false) {
      $(this).click(function() {
        window.location = this.attr('href');
      });
    } else {
      var $caller = this;
      var $div = $('<div style="display:none;"></div>').appendTo('#page-wrapper');
      var $dialog = $div.dialog({ autoOpen    : false,
                                  modal       : true,
                                  resizable   : false,
                                  title       : settings.title,
                                  height      : settings.height,
                                  width       : settings.width,
                                  open        : settings.open
                                });
      
      $.get(this.attr('href'), {layout:'false'}, function(data){
        $div.append(data)
        if(!settings.title) {
          $dialog.dialog('option', 'title', $('.section-header h1',$div).text());
        }
        $caller.click(function(){
          $dialog.dialog('open');
          return false;
        });
      });
    }
  };
})(jQuery);
