//closes the surrounding dialog popup on click
jQuery.fn.addCloser = function(identifier) {
  if(!identifier) {
    identifier = '.close';
  }
  return this.each(function() {
    jQuery(this).find(identifier).click(function() {
      var targetHref = this.href || this.oldHref;      
      if(targetHref) {
        window.location.href = targetHref;
        return false;
      }
      jQuery(this).parents('div.dialog').remove();
      jQuery('#dialog-cover').hide();
      jQuery('#main').fadeTo(1, 1); 
      return false;
    })
  });
};

//attaches a click event to class close within the node
jQuery.fn.addHider = function(identifier) {
  if(!identifier) {
    identifier = '.close';
  }
  return this.each(function() {
    jQuery(this).find(identifier).click(function() {
      jQuery(this).parents('div.dialog').hide();
      jQuery('#dialog-cover').hide();
      jQuery('#main').fadeTo(1, 1); 
      return false;
    })
  });
};

//@TODO: improve this so the inputhint is only called on the jquery collection
jQuery.fn.addInputHint = function(){
  new inputHint();
  return this;
};

//makes links look like buttons
jQuery.fn.link2Button = function() {
  jQuery('a.button', this).each(function() {
    var link = this.href;
    var attr = {};
    attr['className'] = this.className;
    if(this.id) {
      attr['id'] = this.id;
    }
    if(this.title) {
      attr['title'] = this.title;
    }

    //add the button as link replacement
    var btn = jQuery('<button type="button">' + this.innerHTML + '</button>')    
       .addClass('green-btn')  
       .insertBefore(this);
    
    //and remove the link
    jQuery(this).remove();
    
    //make the button look the same
    btn.attr(attr) 
       .addClass('green-btn')   
       .removeClass('button');          
    btn.get(0).oldHref = link;
  });
  return this;
}


jQuery.fn.makePopup = function() {
  jQuery('#main').css('opacity', 0.5);
  return this.css({marginTop : jQuery(document).scrollTop() + 50,
                  position: 'relative'})
             .link2Button()            
             .appendTo(jQuery('#dialog-cover').css('height', jQuery(document.body).css('height')).show())
             .addCloser()
             .addJsEvents()
             .addInputHint()
             .jqDrag('h2')
             ;
};

//used for debugging to highlight elements ina collection
jQuery.fn.color = function(color) {
  color = color || 'red';
  return this.each(function() {
    jQuery(this).css({border: '1px ' + color + ' solid',
                      backgroundColor: '#faa'});
  });
};

//reaplces everything within two braces with an ampersand, used for email obsfuscation
jQuery.fn.defuscate = function() {
   return this.each(function(){
     var email = jQuery(this).html().replace(/\s*\(.+\)\s*/, "@");
     jQuery(this).before('<a href="mailto:' + email + '">' + email + "</a>").remove();
   });
};

//a simple if statement for jquery
jQuery.fn.jIf = function(condition) {
  return (condition) ? this.pushStack( this ) : this.pushStack( [] ); 
};

/**** source: http://dev.iceburg.net/jquery/jqDnR/ *******/
(function($){
  $.fn.jqDrag=function(h){return i(this,h,'d');};
  $.fn.jqResize=function(h){return i(this,h,'r');};
  $.jqDnR={dnr:{},e:0,
  drag:function(v){
   if(M.k == 'd')E.css({left:M.X+v.pageX-M.pX,top:M.Y+v.pageY-M.pY});
   else E.css({width:Math.max(v.pageX-M.pX+M.W,0),height:Math.max(v.pageY-M.pY+M.H,0)});
    return false;},
  stop:function(){E.css('opacity',M.o);$().unbind('mousemove',J.drag).unbind('mouseup',J.stop);}
  };
  var J=$.jqDnR,M=J.dnr,E=J.e,
  i=function(e,h,k){return e.each(function(){h=(h)?$(h,e):e;
   h.bind('mousedown',{e:e,k:k},function(v){var d=v.data,p={};E=d.e;
   // attempt utilization of dimensions plugin to fix IE issues
   if(E.css('position') != 'relative'){try{E.position(p);}catch(e){}}
   M={X:p.left||f('left')||0,Y:p.top||f('top')||0,W:f('width')||E[0].scrollWidth||0,H:f('height')||E[0].scrollHeight||0,pX:v.pageX,pY:v.pageY,k:d.k,o:E.css('opacity')};
   $().mousemove($.jqDnR.drag).mouseup($.jqDnR.stop);
   return false;
   });
  });},
  f=function(k){return parseInt(E.css(k))||false;};
})(jQuery);
