jQuery(function(e){
  $('a.save-email-alert').click(function(e) {
    e.preventDefault();
    var href = $(this).attr('href');
    $.fancybox({
      padding: 0,
      href: href,
      modal: false,
      width: 484,
      height: 672,
      titleShow: false,
      showCloseButton: true,
      scrolling: 'no'
    });
  });

  $('a.myljh-shortlist-add').live('click', function(e){
      e.preventDefault();
      
      if(this.href.match(/#add-(.+)/)){
        var upn    = this.href.match(/#add-(.+)/)[1];
        $(this).hide();

        $.get('/myljhooker:myshortlist?f=add&upn='+upn, function(data) {
          if(data.match(/ERROR: (.+)/)){
            window.location = '/myljhooker:login';
          } else if(data.match(/OK: (.+)/)){
            alertDialog(data.match(/OK: (.+)/)[1]);
          } else {
            alertDialog('Unknown error');
          }
        });            
      }
  }); 
  
  $('a.myagents-add').live('click', function(e){
    e.preventDefault();

    if(this.href.match(/#add-(\d+)/) && !$(this).hasClass('processing')){
      var agent = this.href.match(/#add-(\d+)/)[1];
      var link   = $(this);
      
      $(this).addClass('processing');
      $('p.message').hide();
      $('p.error').hide();
      
      $.post('/myljhooker:offices', { p: 'myagents', a: 'add', agent: agent }, function(data){
        if(data.match(/OK: (.*)/)){
          var message = data.match(/OK: (.*)/)[1];
          messageDialog(message)
          link.hide();
        } else if(data.match(/ERROR: (.*)/)){
          var error   = data.match(/ERROR: (.*)/)[1];
          messageDialog(message);          
        } else if(data.match(/LOGIN: (.+)/)){
          window.location = '/myljhooker:login';
        } else {
          $('p.message').html('An unknown error occurred').show();
        }
        
        link.removeClass('processing');
      });
    }
  });  

  $('a.property-contact-agent').live('click', function(e){
      e.preventDefault();

      if(this.href.match(/#contact-(\w+)/)){
        var upn = this.href.match(/#contact-(\w+)/)[1];
        loadDialog('/'+upn+'?d=contact');
      }
   });

  $('a.myoffices-add').live('click', function(e){
    e.preventDefault();
    
    addMyOffices(this);
  });
});

function addMyOfficesClick(e){
  e = $.event.fix(e);

  e.preventDefault();

  var target;

  if(!e.currentTarget)
    target = $(e.target).closest('a')[0];
  else 
    target = e.currentTarget;

  addMyOffices(target);
}

function addMyOffices(link){
  if(link.id.match(/#add-(\d+)/) && !$(link).hasClass('processing')){
    var office = link.id.match(/#add-(\d+)/)[1];

    $(link).addClass('processing');
    $('p.message').hide();
    $('p.error').hide();

    link = $(link);

    $.post('/myljhooker:offices', { p: 'myoffices', a: 'add', office: office }, function(data){
      if(data.match(/OK: (.*)/)){
        var message = data.match(/OK: (.*)/)[1];

        messageDialog(message);
        link.trigger('myljh.added');
        link.hide();
      } else if(data.match(/ERROR: (.*)/)){
        var error   = data.match(/ERROR: (.*)/)[1];        
        alertDialog(error);
      } else if(data.match(/LOGIN: (.*)/)){
        window.location = '/myljhooker:login';
      } else {
        alertDialog('Unknown error');
      }

      link.removeClass('processing');
    });
  }
}
  
var loadDialog = function(href){
  var opts    = (arguments.length > 2) ? arguments[2] : {};
  var def     = { padding:         0,
                  href:            href,
                  modal:           false,
                  titleShow:       false,
                  showCloseButton: true,
                  scrolling:       'no' };
  var o       = $.extend({}, def, opts);
  
  $.fancybox(o);
};

var dialog = function(container, message){
  var opts = (arguments.length > 2) ? arguments[2] : {};
  
  $(container).find('*.message').html(message);

  $.fancybox({
    padding: 0,
    modal: true,
    width: 400,
    height: 500,
    content: $(container).html(),
    titleShow: false,
    scrolling: 'no',
    onComplete: function(){
      $('div#fancybox-wrap button.yes-button').one('click', function(e){
        e.preventDefault();
        $.fancybox.close();
        if(typeof opts.yes == 'function')
          opts.yes();
      });
      
      $('div#fancybox-wrap button.no-button').one('click', function(e){
        e.preventDefault();
        $.fancybox.close();
        if(typeof opts.no == 'function')
          opts.no();
      });
    }
  });
};

var confirmDialog = function(message, yes, no){
  dialog('div.confirm-dialog', message, { yes: yes, no: no });
};

var alertDialog   = function(message){
  dialog('div.alert-dialog', message);
}

var messageDialog   = function(message){
  dialog('div.message-dialog', message);
}


