/* jQuery.head - v0.2 - K Reeve aka BinaryKitten
*
*	makes a Head Request via XMLHttpRequest (ajax) and returns an object/array of headers returned from the server
*	$.head(url, [data], [callback])
*		url			The url to which to place the head request
*		data		(optional) any data you wish to pass - see $.post and $.get for more info
*		callback	(optional) Function to call when the head request is complete.
*					This function will be passed an object containing the headers with
*					the object consisting of key/value pairs where the key is the header name and the
*					value it's corresponding value
*		
*
*
* ------------ Version History -----------------------------------
* v0.2
* 	placed the function inside an enclosure
*
* v0.1
* 	The 1st version - based on $.post/$.get
*/

(function ($) {
  $.extend({
	head: function( url, data, callback ) {
	  if ( $.isFunction( data ) ) {
		  callback = data;
		  data = {};
	  }
  
	  return $.ajax({
		type: "HEAD",
		url: url,
		data: data,
		complete: function (XMLHttpRequest, textStatus) {
      var headers = XMLHttpRequest.getAllResponseHeaders()
      if (null != headers ) {
        var headers = headers.split("\n");
        var new_headers = {};
        var l = headers.length;
        for (var key=0;key<headers.length;key++) {
          if (headers[key].length != 0) {
            header = headers[key].split(": ");
            new_headers[header[0]] = header[1];
          }
        }
        if ($.isFunction(callback)) {
        callback(new_headers);
        }
      }
		}
	  });
	}
  });
})(jQuery);



function addSizeAndIcon(){
  // addSizes was written by Natalie Downe
  // http://natbat.net/2008/Aug/27/addSizes/
  // Copyright (c) 2008, Natalie Downe under the BSD license
  // http://www.opensource.org/licenses/bsd-license.php
  $('a[href$=".pdf"], a[href$=".doc"], a[href$=".mp3"], a[href$=".wmv"]').filter(function() {
		//Compare the anchor tag's host name with location's host name
	    return this.hostname && this.hostname == location.hostname;
	  }).each(function(){
    // looking at the href of the link, if it contains pdf, doc
    var link = $(this);
    var bits = this.href.split('.');
    var type = bits[bits.length -1];
    
    // then call the jquery-head and insert the size back into the link text
     $.head(this.href, function(headers){
      if(headers['Content-Length']) {
        var length = parseInt(headers['Content-Length'], 10);
        
        // divide the length into its largest unit
        var units = [
          [1024 * 1024 * 1024, 'GB'],
          [1024 * 1024, 'MB'],
          [1024, 'KB'],
          [1, 'bytes']
        ];
        
        for(var i = 0; i < units.length; i++){
          
          var unitSize = units[i][0];
          var unitText = units[i][1];
          
          if (length >= unitSize) {
            length = length / unitSize;
            // 1 decimal place
            length = Math.ceil(length * 10) / 10;
            var lengthUnits = unitText;
            break;
          }
        }
        
        // insert the text directly after the link and add a class to the link
        // note: if you want to insert the size into the link rather than after it change the following 'after' to 'append'
        link.after('&nbsp;<div class="' + type + '">(' + length + '&nbsp;' + lengthUnits + ')</div>');
        //link.addClass(type);
        link.attr("target", "_blank");
      }
    });
  });
    
  //update mail to links
	$("a[href^='mailto:']").after('&nbsp;<div class="email">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>');
 
	/*Add external link icon to external links - 
	$('a').filter(function() {
		//Compare the anchor tag's host name with location's host name
	    return this.hostname && this.hostname !== location.hostname;
	  }).addClass("external");
  */
}


$(document).ready(function(){
  addSizeAndIcon(); 
});
