(function($) {

  $.fn.iconoline = function(options) {
    // Extend our default options with those provided.
    // Note that the first arg to extend is an empty object -
    // this is to keep from overriding our "defaults" object.
  	var opts = $.extend({}, $.fn.iconoline.defaults, options);
    
    // if the "autoIcon" behavior is enabled (it is by default)
    // then consult the "mapping" function for matching patterns and add appropriate classes
    if(opts.autoIcon){
      $.each( mapping, function(k, v) {
        $(k).addClass(v);
      });
    }

    // if no elements are specified, default to elements with class starting with 'icon-'
    // otherwise use specified elements
    
    var selectedElements = ((this.size() == 1) && (!this.parent().length)) ? $('[class*=icon-]') : this;
    
    
    // find all elements with a class matching the "icon-folder_name-image_name" convention
    selectedElements.filter (function(){
      return $(this).attr('className').match(/icon-(\w*)-(\w*)/);
    }).each( function(){ 
      var i = 0;
      var classNames = $(this).attr('className').split(' ');
      var valid_class = '';
      while(i <= classNames.length - 1){ 
        if(classNames[i].match(/icon-(\w*)-(\w*)/)){
          valid_class = classNames[i];
        }
        i = i + 1;
      }
      var icon_path = valid_class.split('-');
      
      if(customSets[icon_path[1]] == undefined){
  
        $(this).css('background-image','url(' + opts.defaultSetsPath + defaultSets[icon_path[1]]['path'] + icon_path[2] + '.' + defaultSets[icon_path[1]]['format'] + ')');
        
      } else {
        $(this).css('background-image','url(' + customSets[icon_path[1]]['path'] + icon_path[2] + '.' + customSets[icon_path[1]]['format'] + ')');
      }
    
      // add the "iconic" class which will set up appropriate spacing, padding, margins, etc.
      $(this).addClass('iconic');
    })

    // disable auto icon behavior for a single element
    $('.no-icon').css({backgroundImage:"none", margin:0, padding:0});

    // disable the auto icon behavior for an entire table
    $('table.no-icons a').css({backgroundImage:"none", margin:0, padding:0});

    // disable auto icon behavior for a list - note that we aren't setting the margin and padding
    // to 0 because ul's often represent navigation
    $('ul.no-icons a, ol.no-icons a').css({backgroundImage:"none"});

  };
  
  // default mapping for common document types
  // add more or override in onReady...
  // Example:  $.fn.iconoline.autoIconMap['a[href$=".my_extension"]'] = 'icon-doctypes-whatever'
  var mapping = $.fn.iconoline.autoIconMap = {
	  'a[href^="mailto:"]':'icon-silk-email',
	  'a[href$=".pdf"]':'icon-silk-page_white_acrobat',
	  'a[href$=".doc"]':'icon-silk-page_white_word',
	  'a[href$=".docx"]':'icon-silk-page_white_word',
	  'a[href$=".xls"]':'icon-silk-page_white_excel',
	  'a[href$=".xlsx"]':'icon-silk-page_white_excel',
	  'a[href$=".txt"]':'icon-doctypes-txt',
	  'a[href$=".ppt"]':'icon-doctypes-ppt',
	  'a[href$=".zip"]':'icon-doctypes-zip',
	  'a[href$=".jpg"]':'icon-silk-picture'
	};
	
	
	
	// these are the default icon sets that ship with Iconoline
	// you can override these sets directly or via the custom sets (recommended)
  var defaultSets = $.fn.iconoline.defaultSets = {
	  'silk':{ path:'silk/gif/', format:'gif' },
	  'circular_icons':{ path:'circular_icons/gif/', format:'gif' },
	  'diagona':{ path:'diagona/gif/', format:'gif' },
	  'doctypes':{ path:'doctypes/gif/', format:'gif' },
	  'flags':{ path:'flags/gif/', format:'gif' },
	  'fugue':{ path:'fugue/gif/', format:'gif' },
	  'mini':{ path:'mini/gif/', format:'gif' },
	  'vaga':{ path:'vaga/gif/', format:'gif' },
	  'custom':{ path:'custom/', format:'gif' }
	};
	
	
	// CUSTOM ICON SETS //
	/* add custom icon set locations in the onReady
	Example 1 - Add sets one-by-one:
	  $.fn.iconoline.customSets['great_icons'] = { path:'/images/somefolder/mycons/'}, format:'png' }
	Example 2 - Add multiple sets at once: 
	$.fn.iconoline.customSets = {
	   'great_icons':{ path:'/images/somefolder/mycons/'}, format:'png' },
	   'another_set':{ path:'/assets/differentfolder/someset/'}, format:'gif' }
	}
	*/
	var customSets = $.fn.iconoline.customSets = {
	  // you'll enter these in the onReady
	};
	

  // DEFAULTS //
  // scripts can now get at these defaults with...
  $.fn.iconoline.defaults = {
		
		// set the initial path for all "default" icon sets (defaultSets)
		// Example:  $.fn.iconoline.defaults.defaultSetsPath = '/iconoline/icons/'
		defaultSetsPath: '/admin/images/icons/',
		
		// enable/disable the auto-inclusion of icons based upon patterns
		// included in the mapping object
		// Example: $.fn.iconoline.defaults.autoIcon = false
		autoIcon: true
	};

})(jQuery);