/**
 * @author DGrayston@TenAlps
 */
var accordion = {
	
	working : false ,
	
	init : function(){
		
		var x = 0;
		
		$('#accordion .acc-sub').each(function(){
			$(this).attr('id','acc-link-' + x);
			x++;
		});
		
		$('#accordion a').click(function(){
			if (!accordion.working) {
				$('#accordion a.active').removeClass('active');
				$(this).addClass('active');
			}
		});
		
		$('#accordion .acc-top-link').click(function(){
			
			if (!accordion.working) {
				accordion.working = true;
				
				var url = $(this).attr('href');
				
				if (url != '#') {
					var ajaxUrl = $(this).attr('href') + '/element/rightcol';
					getjson(ajaxUrl, '#gallery');
				}
				
				// Show text
				var showElem = $(this).next('.acc-sub');
				
				// Hide .active top level text
				$('#accordion').children('.acc-nav').children('div.active').each(function(){
					if ($(this).attr('id') != showElem.attr('id')) 
						$(this).slideUp(300);
				});
				
				$('#accordion').children('.acc-nav').children('.acc-sub').children('.acc-nav').children('div.active').slideUp(300, function(){
				});
				
				var showDesc = showElem.children('.acc-desc:first');
				var divHtml = $.trim(showDesc.html());
				
				if (divHtml.length > 0) {
					showDesc.slideDown(300, function(){
						showElem.slideDown(300, function(){
							$(this).addClass('active');
							accordion.working = false;
						});
					});
				}
				else 
					if (showElem.find('.acc-nav').length > 0) {
						showElem.slideDown(300, function(){
							$(this).addClass('active');
							accordion.working = false;
						});
					}
				else{
					accordion.working = false;
				}
			}
							
			return false;
			
		});
		
		
		$('#accordion .acc-sub-link').click(function(){
			
			if (!accordion.working) {
				accordion.working = true;
				
				var url = $(this).attr('href');
				
				if (url != '#') {
					var ajaxUrl = $(this).attr('href') + '/element/rightcol';
					getjson(ajaxUrl, '#gallery');
				}
				
				// Show text
				var showElem = $(this).next('.acc-sub');
				
				// Hide .active top level text
				$('#accordion').children('.acc-nav').children('.active').children('.acc-desc').slideUp(300);
				showElem.parent('.acc-nav').parent('.acc-sub').children('.acc-nav').children('div.active').slideUp(300);
				
				showElem.slideDown(300, function(){
					$(this).addClass('active');
					accordion.working = false;
				});
			}
			
			return false;
		});
				
	} 
	
};

$(document).ready(function(){
	accordion.init();
});
