// SeViR Simple Horizontal Accordion @2007
// http://letmehaveblog.blogspot.com

jQuery.fn.extend({
  haccordion: function(params){

    var jQ = jQuery;
    var params = jQ.extend({
      speed: 500,
      headerclass: "header",
      contentclass: "content",
	  firstopen: null,
      contentwidth: 250
    },params);

	 // This function runs for every element that has the .haccordion-function executed on itself.
    return this.each(function(){

	  // The stuff to be executed whenever the <p> inside the header-marked elements are clicked.
      jQ("."+params.headerclass+" p", this).click(function(){
        
		// This refers to the .haccordion-div
		var p = jQ(this).parent().parent()[0];

		// If a menu is allready open, close it.
		if (p.opened != undefined){          
			
			// If the same menu is clicked again, return false.
			if(jQ(this).parent()[0] === p.opened[0]) return false;
			
			jQ(p.opened).next("div."+params.contentclass).animate({
            width: "0px"
          },params.speed);
		
        }
		// Set the opened menu, to the header-DIV of the currently clicked paragraph 
        p.opened = jQ(this).parent();
		
		// Animate open the next div with the class .content, that is after the currently clicked header.
		jQ(p.opened).next("div."+params.contentclass).animate({
          width:  jQ(p.opened).next("div."+params.contentclass).attr("contentwidth")
        }, params.speed);
	
		return false;
      });

	  // Open the menu that has the same id as the body class
	  // After DOM loads
	  $(document).ready(function() {
		
		  //If a firstopen parameter is present
		  if (params.firstopen !== null && params.firstopen !== "") {

			// Store the original speed parameter
			var original_speed = params.speed;
			// Set the speed of the first open to zero (because we don't want it to slide every time it loads)
			params.speed = 0;
			// Onload, click the defined firstopen parameter
			jQ('div#' + params.firstopen + " p").click();

			// Reset the global slidespeed back to the original speed
			params.speed = original_speed;	   
		  }

	  });
	
    });
  }
});