/**
 * An accordion function made by info.nl
 * 
 * Setting class 'active' to a title sets it open by default.
 * 
 * Options
 * -------
 * speed      : the speed in miliseconds
 * titleTag   : The html tag of the title (f.ex dt or h3)
 * contentTag : The html tag the content (f.ex dd or div)
 * easing     : The easing method. Default jQuery: linear, swing. Use a plugin 
 *              for more easing methods.
 * 
 * @param {Object} opts
 */
(function($){
  $.fn.infoCordion = function(opts){
    opts = $.extend({
      speed: 300,
      titleTag: 'dt',
      contentTag: 'dd',
      printContent: false,
	  printBtn: '<a href="#" class="print">print</a>',
      openClass: 'active',
      easing: 'linear'
    }, opts || {});
    
    var obj = $(this);
    var hasActive = false;
    
    return init(); // Call the init function & return obj for chaining (returned by init)
    
    /**
     * Initialises accordion
     * Is activated at bottom of plugin
     */
    function init(){
      obj.find(opts.contentTag).each(function(){
        var el = $(this);
        el.show();

        var h  = el.height();

        // The first title that has .active will be shown by default
        if( el.prev(opts.titleTag).hasClass(opts.openClass) && !hasActive ){
          hasActive = true;
		  if (opts.printContent) {
		    addPrintBtn(el);
	      }
        }
        else {
          el.css('display', 'none');
          el.height(0);
        }
        bindEvents(el, h);  
      });
      
      return obj; // Return obj for chaining
    }
	
    
    /**
     * Binds events to the title and content elements
     * 
     * @param {Object} content The object that is the 
     * @param {string} height The height of the content, before it's hidden
     */
    function bindEvents(content, height){
		if($(content).is(':hidden')) {
			content.prev(opts.titleTag).find('.print').hide();
		}
      content.prev(opts.titleTag).click(function(){
        if( content.css('display') == 'none' )
          open(content, height);
        else
          close(content);
      });
      
      content.click(function(){
        close($(this));
      });
    }
    
    /**
     * Opens the content area
     * @param {Object} content
     */
    function open(content, height){
      content.css('display', 'block');
      content.prev(opts.titleTag).addClass('active'); // make title active
	  if (opts.printContent) {
		addPrintBtn(content);
	  }
      content.animate({ height: height }, opts.speed, opts.easing);
      
      close(content.siblings(opts.contentTag +':visible'));
    }
	
	    
    /**
     * Closes the content area
     * @param {Object} content
     */
    function close(content){
      content.each(function(){
		if (opts.printContent) {
		  content.prev(opts.titleTag).find('.print').hide();
		}
        $(this).animate({ height: 0 }, opts.speed, opts.easing, function(){
          $(this).css('display', 'none');
          content.prev(opts.titleTag).removeClass('active'); // deactivate title
		 
        });
      });
    }
	
	
	/**
     * Adds a print button te the content
     * @param {Object} content
     */
	function addPrintBtn(content) {
		content.prev(opts.titleTag).find('.print').show().click(function(){
			// in the page which is opened by this popup there is a check is window name == printpopup and then print
			var popup=window.open($(this).attr('href'),'printpopup','left=5000,top=200,width=1,height=1')
		  	return false;
		  });
	}
	
  }
})(jQuery);