function viewBlock(b){
	$$('.block').each(function(block){
		if (block != $(b)) {
			var skope = block.id
			//block.hide()
			Effect.Fade(block, {duration: .2})
		}
	});
}
function restoreIndices(bString){
	var title = bString + '_index_title'
	var body = bString + '_index_body'
	zoomed = 1;
	window.location.hash = '.'
	savedHash = window.location.hash
	//javascript:history.back(1) 
	$(bString).select('.ajax-loaded').each( function(ajaxThing){
		//Effect.BlindUp(ajaxThing)
		ajaxThing.remove()
	});
	//$$('.close-button').each( function(button){ button.hide() });
	$(bString).removeClassName('loaded')
	$(bString).select('.index-title').each( function(t) {
		Effect.Appear(t, {duration: 0.2})
	});
	
	// this here opens up everything when bString is just the page id
	// so, to fix we'll switch the selector if bString is just a page
	if (bString.split('_').size() < 4) {
		var bodySelector = '#index .toggleable-content'
		$(bString).select('.toggleable-content').each( function(body) {
			body.hide()
		});
	} else {
		var bodySelector = '.toggleable-content'
	}
	$(bString).select(bodySelector).each( function(body) {
		Effect.Appear(body, {duration: 0.2})
	});
	$$('.block').each(function(block){
		if (block != $(bString)){
			block.setStyle({opacity: 1})
			Effect.Appear(block, {duration: .2})
		}
	});
	observeAccordion();
}
function andClose(body, title){
	setTimeout( function(){
		activeToggle(body, '.toggleable-content', title)
	}, 200 )
}

function closeAll(){}
function rm(id){
	$('ajax-loaded-' + id).remove();
}
function unimpose(ultimate, penultimate){
	var superimposedId = 'ajax-loaded-' + ultimate
	var thisObj = this
	Effect.Fade(superimposedId, {duration: 0.2, queue: 'front'})
	$$('.ajax-loaded-body').each( function(body) {
		Effect.Appear(body, {duration: 0.2, queue: 'end'})
	});
	$(penultimate + '_index_title').select('.close-button').each( function(button){button.show()});
	setTimeout(function(thisObj){$(superimposedId).remove();}, 200)
	zoomed = 1
}
function changeImagesButtonTo(side, limb) {
	$(limb + '-more-images-button').setStyle({'float': side});
	$(limb + '-more-images-button').setStyle({background: 'url(/images/more_images_' + side + '.png) no-repeat'});
}
function moreImages(limb){
	$(limb + '-product-thumbnails').select('.cog').each( function(leaf){ 
		if (leaf.hasClassName('visible-true')) {
			leaf.removeClassName('visible-true');
			leaf.addClassName('visible-false');
		} else if (leaf.hasClassName('visible-false')) {
			leaf.removeClassName('visible-false');
			leaf.addClassName('visible-true');
		}
	});
	var side = $(limb + '-more-images-button').getStyle('float');
	if (side=='left') var newSide = 'right';
	if (side=='right') var newSide = 'left';
	changeImagesButtonTo(newSide, limb);
	
	//var top = $(limb + '-product-thumbnails').style.top
	//var height = $(limb + '-product-thumbnails-window').getStyle('height');
	//var topNum = parseInt(top);
	//var heightNum = parseInt(height);
	//var bottomOffset = topNum + heightNum
	//var setTop = '-' + heightNum + 'px'
	//if (topNum > -1){
	//	setTop = (topNum - heightNum) + 'px'
	//	$(limb + '-product-thumbnails').setStyle({top: setTop});
	//	changeImagesButtonTo('left')
	//} else {
	//	setTop = (topNum + heightNum) + 'px'
	//	$(limb + '-product-thumbnails').setStyle({top: setTop});
	//	changeImagesButtonTo('right')
	//}
	
	
}
function showMoreImagesButton() {	
	
}

function activeToggle(id, sel, title) {
	var wasClosed = true

	$('wrapper').select('.arrow-down').each( function(t) {
		t.removeClassName('arrow-down');
		t.select('[type="radio"]').each( function(box) {
			if (box.checked)
			{
				box.checked = false;
			}
		});
		if ($(title) == t) wasClosed = false
	});
	
	if (wasClosed) $(title).addClassName('arrow-down');

	$('wrapper').select('.open').each( function(e) { 
		if ($(id) != e){
			Effect.BlindUp(e, { duration: .2, queue: 'front' }); 
			e.removeClassName('open'); 
		}
	});

	Effect.toggle(id, 'blind', { duration: .2, queue: 'end' });
	$(id).addClassName('open');
	$(title).select('[type="radio"]').each( function(box) {
		if(wasClosed){
			box.checked = true;
		} else {
			box.checked = false;
		}
	});

	window.location.hash = $(title).getAttribute('name');
	savedHash = window.location.hash
}

function sortIndex(selector, reverse){
	var rows = $('index_index_window').select('.indexables-row')
	var sel = ''
	var newRows = rows.sortBy(function(s) { 
		return $(s.id + '_' + selector).innerHTML
	});
	if (reverse == '1'){
	 	newRows = newRows.reverse();
	} 
	rows.each( function(row) { row.remove(); });
	newRows.each( function(newRow) { 
		var newI = new Element( 'i' );
		newI.update(newRow);
		$('index_index_window').insert( { 'bottom' : newRow }); 
//		$('wrapper').insert( {'bottom' : $(newRow).select(selector).first });
		
	});
}

function monitorUrl(){
	if(savedHash != window.location.hash){
		params = window.location.pathname.split('/')
		
		currentHash = window.location.hash
		var forcedHistoryURL = currentHash.replace("#", '/')
		if (forcedHistoryURL == '/'){
			var params = window.location.pathname.split('/')
			forcedHistoryURL = params[0]
		}
		if(currentHash == ''){
			window.location = window.location.pathname
		} else {
			window.location = '/' + params[1] + forcedHistoryURL
		}
	}
	savedHash = window.location.hash
}

function observeAccordion() {
	$('wrapper').select('.block').each(function(e){
		var title = e.id + '_index_title'
		var body = e.id + '_index_body'
		Event.stopObserving(title, 'click');
		Event.observe(title, 'click', function(){activeToggle(body, '.toggleable-content', title)})
	});
}

function stopObservingAccordion(){
	$('wrapper').select('.block').each(function(e){
		var title = e.id + '_index_title'
		title.stopObserving('click');
	});
}


Event.observe(window, 'load', function() {
	setInterval("monitorUrl()", 1000);
	observeAccordion();
});

