// JavaScript Document
jQuery(function() { 

// INITIALIZE VARIABLES

var thisFilesLocation = "http://"+location.host+"/adjhjquery/";

var plus	= thisFilesLocation + "images/plus.png";
var minus	= thisFilesLocation + "images/minus.png";
var closer	= thisFilesLocation + "images/close.png";
var more    ="More...";
var hide    ="Hide...";
var css = '<link rel="stylesheet" type="text/css" href="'+thisFilesLocation + 'accordion/accordion.css" />';

var sameLevelQuery = "h1.accordion, h2.accordion, h3.accordion, h4.accordion, p.accordion, li.accordion";
var nextLevelQuery = "span.accordion, em.accordion, strong.accordion";

var moreLevelQuery = "h1.more, h2.more, h3.more, h4.more, p.more, li.more";
var spanLevelQuery = "span.more, em.more, strong.more";

// ATTACH THE STYLESHEET
jQuery("head").append(css);

// AN IMAGE TOGGLE FUNCTION FOR PLUS MINUS IMAGES
var toggleImage = function(image) 
	{
		if ( image.attr("src") === plus)
		{
			image.attr("src", minus);
		} else {
			image.attr("src", plus);
		}
	return image;
	}
	
//SAME LEVEL
// An accordion selector followed by the sibling revealed content...

	// wrap the next element in a <div> if the next element is not a <div> processing thereafter can be the same!
var sameLevel = jQuery(sameLevelQuery);
var sameLevelNextNotDiv = sameLevel.next(":not(div)");
sameLevelNextNotDiv.wrap(document.createElement("div"));

sameLevelDiv = sameLevel.next("div");
sameLevelDiv.map( function () {
							jQuery(this).css("margin-left",parseInt(jQuery(this).prev().css("margin-left"))+12);
							});
sameLevelDiv.hide();
sameLevel.append("<img  class='accordion-toggle' src='"+plus+"'alt='Reveal' />");
sameLevelDiv.append("<img class='accordion' src='"+closer+"' alt='Conceal' />");
sameLevelDiv.addClass("accordion-div");

sameLevel.click(function()
{
	toggleImage( jQuery(this).children("img.accordion-toggle") )
	jQuery(this).next("div").children("img.accordion").first().data("scrollpos",jQuery("body").scrollTop());
	jQuery(this).next("div").slideToggle(200);
});

// NEXT LEVEL
// An accordion selector span em or strong within an element whose next sibling is the revealed content...

	// wrap the next element in a <div> if the next element is not a <div> processing thereafter can be the same!
var nextLevel = jQuery(nextLevelQuery);
var nextLevelNextNotDiv = nextLevel.parent().next(":not(div)");
nextLevelNextNotDiv.wrap(document.createElement("div"));

nextLevelDiv = nextLevel.parent().next("div");
nextLevelDiv.map( function () {
							jQuery(this).css("margin-left",parseInt(jQuery(this).prev().css("margin-left"))+12);
							});
							
nextLevelDiv.hide();
nextLevel.append("<img  class='accordion-toggle' src='"+plus+"'alt='Reveal' />");
nextLevelDiv.append("<img class='accordion' src='"+closer+"' alt='Conceal' />");
nextLevelDiv.addClass("accordion-div");

nextLevel.click(function()
{
	toggleImage(jQuery(this).children("img.accordion-toggle") );
	jQuery(this).parent().next("div").children("img.accordion").first().data("scrollpos",jQuery("body").scrollTop());
	jQuery(this).parent().next("div").slideToggle(200);
});

// SPAN LEVEL
// An accordion selector span em or strong within an element whose next sibling is the revealed content...
var spanLevel = jQuery(spanLevelQuery);
spanLevel.text(more);

	// wrap the next element in a <div> if the next element is not a <div> processing thereafter can be the same!
var spanLevelNextNotDiv = spanLevel.parent().next(":not(div)");
spanLevelNextNotDiv.wrap(document.createElement("div"));

spanLevelDiv = spanLevel.parent().next("div");
spanLevelDiv.map( function () {
							jQuery(this).css("margin-left",parseInt(jQuery(this).prev().css("margin-left"))+12);
							});

spanLevelDiv.hide();
spanLevelDiv.append("<img class='more' src='"+closer+"' alt='Conceal' />");
spanLevelDiv.addClass("more-div");

spanLevel.click(function()
{
	if (jQuery(this).text()=== hide) {
		jQuery(this).text(more);
	} else { 
		jQuery(this).text(hide);
	}
	jQuery(this).parent().next().slideToggle(200);
	jQuery(this).parent().next().children("img.more").first().data("scrollpos",jQuery("body").scrollTop());
});



//MORE LEVEL
// An accordion selector h1, h2, h3, h4, p or li whose next sibling is the revealed content... Adding a span with More... or Hide...
var moreLevel = jQuery(moreLevelQuery);

moreLevel.append('<span class="more"></span>');
var moreLevelSpan = moreLevel.children("span.more");

	// wrap the next element in a <div> if the next element is not a <div> processing thereafter can be the same!
moreLevelSpan.text(more);
var moreLevelNextNotDiv = moreLevel.next(":not(div)");
moreLevelNextNotDiv.wrap(document.createElement("div"));

moreLevelDiv = moreLevel.next("div");
moreLevelDiv.map( function () {
							jQuery(this).css("margin-left",parseInt(jQuery(this).prev().css("margin-left"))+12);
							});

moreLevelDiv.hide();
moreLevelDiv.append("<img class='more' src='"+closer+"' alt='Conceal' />");
moreLevelDiv.addClass("more-div");

moreLevelSpan.click(function()
{
	if (jQuery(this).text()=== hide) {
		jQuery(this).text(more);
	} else { 
		jQuery(this).text(hide);
	}
	jQuery(this).parent().next().slideToggle(200);
	jQuery(this).parent().next().children("img.more").first().data("scrollpos",jQuery("body").scrollTop());
});

jQuery("img.more").click( function()
{
	var pai = jQuery(this).parent();
	pai.prev().children(spanLevelQuery).text(more);
	pai.slideToggle(200);
	jQuery("body").scrollTop(jQuery(this).data("scrollpos"));
});

jQuery("img.accordion").click( function()
{
	var pai = jQuery(this).parent();
	toggleImage(pai.prev().children("img.accordion-toggle") );
	pai.slideToggle(200);
	jQuery("body").scrollTop(jQuery(this).data("scrollpos"));
});

});
