/**
 * Please keep this if you choose to use this code.
 * Creator: Ryan Kahn
 * Source Site: http://ryan-kahn.com
 */

/**
 * Initializes blind items on window load.
 */
Event.observe(window, 'load', function() {
	initializeBlinds();
});

/**
 * Contracts every blind item on pageload. They load expanded so people without javascript suffer no losses.
 */
function initializeBlinds(){
	//Foreach blindItem we want to add the observer for clicks and hide the elements
	counter = 1;
	$$(".blindItem").each(function (blindItem){
		if(counter%2 == 0){
			blindItem.addClassName("blindItemOdd");
		}
		counter = counter + 1;	
		blindItem.down("div.title").observe("click",toggleItem);
		//Scriptaculous has issues with padding on elements with blind down and up. they suggest using a wrapper.
		//I dont like having the user add this wrapper so I will use js to add the wrapper
		wrappedItem = blindItem.down("div.content").wrap('div',{'class':'content'});
		//Add the padded classname
		wrappedItem.down("div.content").addClassName("paddedContent");
		//Now remove the content class on the wrapped item
		wrappedItem.down("div.content").removeClassName("content");
		//Hide the wrapped item
		wrappedItem.hide();
	});
}

/**
 * Toggles specified blind menu item
 * @param blindItemId
 */
function toggleItem(){
	//Locate the blindItem, since the thing being clicked on is the title we know its one up!
	blindItemId = this.up().identify();
	//Scriptaculous queues are amazing, they allow me to only have one animation happening at a time! So while its expanding they cant contract and break things
	var queue = Effect.Queues.get(blindItemId);
	//If there is nothing in the queue we can do our function
	if(queue.size() == 0){
		var animationDuration = .4;
		var content = $(blindItemId).down("div.content");
		var title = $(blindItemId).down("div.title");
		if(title.hasClassName("expanded")){//If its expanded
			//Hide the content
			Effect.BlindUp(content.identify(),{duration:animationDuration, queue: { limit: 1, scope: blindItemId}});
			//It's no longer expanded so go back to the default +
			title.removeClassName("expanded");
		}else{//If its contracted
			if(this.up().hasAttribute("blindGroup") && this.up().getAttribute("blindGroup") != ""){
				siblings = $$(".blindItem[@blindGroup="+this.up().getAttribute("blindGroup")+"]");
				siblings.each(function(sibling){
					//Hide everything but the one we want to have shown!
					if(sibling.identify() != blindItemId){
						var siblingTitle = sibling.down("div.title");
						if(siblingTitle.hasClassName("expanded") && Effect.Queues.get(sibling.identify()).size() == 0){
							var siblingContent = sibling.down("div.content");
							//Hide the content
							Effect.BlindUp(siblingContent.identify(),{duration:animationDuration, queue: { limit: 1, scope: sibling.identify()}});
							//It's no longer expanded so go back to the default +
							siblingTitle.removeClassName("expanded");
						}
					}
				});
			}
			//Show the content
			Effect.BlindDown(content.identify(),{duration:animationDuration, queue: { limit: 1, scope: blindItemId}});
			//Its expanded so we display the -
			title.addClassName("expanded");
		}
	}
}


