var table = {
	
	options : {},
		
	isMainPanelToggling : false,
	isChildPanelToggling : false,
	
	//bind all events
	bindAccordionEvents : function()
	{

		$('.accordion').find('.parent-panel').mouseover(function(){
			if(!$(this).find('.bg_click').is(':visible')){
				$(this).find('.bg_over').fadeIn('fast');	
				$(this).removeClass('mouseleft').removeClass('hiding');
			}
		});
		
		$('.accordion').find('.parent-panel').bind("mouseleave", function(){
			if($(this).next().is(':visible') && !$(this).hasClass('hiding'))
				return;
			$(this).find('.bg_over').fadeOut('fast');
			$(this).find('.bg_click').fadeOut('fast');
			$(this).addClass('mouseleft');
		});
		
		$('.accordion').find('.parent-panel').click(function(){
			
			var next = $(this).next();
			
			if(!next.is(':visible')){
				$(this).find('.bg_over').hide();
				$(this).find('.bg_click').show();
			}
			else{
				$(this).addClass('hiding');
			}
				
			next.slideToggle(table.options.toggleSpeed, function(){
				if(!$(this).is(':visible')){
					var prev = $(this).prev();
					prev.find('.bg_click').hide();
					if(!prev.hasClass('mouseleft'))
						prev.find('.bg_over').show();
					prev.removeClass('hiding');
				}
			});		
		});
		
		
		
		
		
		$('.accordion').find('.child-panel').mousemove(function(){
		
			if(!$(this).find('.bg_click').is(':visible')){
				$(this).find('.bg_click').show();	
				$(this).removeClass('mouseleft').removeClass('hiding');
			}

		});
		

		$('.accordion').find('.child-panel').bind("mouseleave", function(){
			if($(this).next().is(':visible') && !$(this).hasClass('hiding'))
				return;
			$(this).find('.bg_click').hide();
			$(this).addClass('mouseleft');			
		});
		

		$('.accordion').find('.child-panel').click(function(){
			
			var lastSub = $(this).parent().find('.child-panel:last');
			var next = $(this).next();	
			
			if(!next.is(':visible')){
				$(this).find('.bg_click').show();
			
				if(lastSub.attr('class') == $(this).attr('class')){
					next.css('border-bottom', '1px solid #c44977');
					$(this).css('border-bottom', 'none');
				}
			}
			else{
				$(this).addClass('hiding');
			}
				
			next.slideToggle(table.options.toggleSpeed, function(){
				if(!$(this).is(':visible')){	
					var prev = $(this).prev();
					
					prev.find('.bg_click').hide();
					if(!prev.hasClass('mouseleft'))
						prev.find('.bg_click').show();
					prev.removeClass('hiding');
					if(lastSub.attr('class') == prev.attr('class'))
						prev.css('border-bottom', '1px solid #c44977');
				}
			});		
		});
		
	
	},
	
	bindOtherEvents : function()
	{
		
		$('input[type=radio]').click(function(){
			table.changeCurrency($(this).attr('id'));
		});
		
		$('.reset-accordion').click(function(){
			
			$('.child-panel').each(function(){
				$(this).next().hide();
				$(this).find('.bg_click').hide();
				$(this).removeClass('mouseleft').removeClass('hiding');
				
			});
			
			$('.parent-panel').each(function(){
				$(this).next().hide();
				$(this).removeClass('mouseleft').removeClass('hiding');			
				$(this).find('.bg_over').hide();
				$(this).find('.bg_click').hide();
			});		
			
			return false;
		});
		
	},
	
	changeCurrency : function(currency)
	{
		$('.accordion').find('.dollars').hide();
		$('.accordion').find('.euros').hide();
		$('.accordion').find('.pounds').hide();
		
		$('.accordion').find('.' + currency).show();
	},
	
	setup : function(options)
	{
	
		if(options == undefined)
			options = {};
		
		if(options.toggleSpeed == undefined)
			options.toggleSpeed = 400;
		
		//set options
		table.options = options;
		
		//setup main panels
		var parentPanels = $('.accordion').find('h3');
		
		$(parentPanels).each(function(){				
			var html = '<div class="parent-panel ' + $(this).attr('class') + '"><h3 class="hidden">' + $(this).html() + '</h3><div class="bg_over hidden"></div><div class="bg_click hidden"></div></div>';
			html += '<div class="parent-box"><div class="content">' + $(this).next().html() + '</div></div>';
			$(this).next().remove();
			$(this).replaceWith(html);			
		});
		
		var childPanels = $('.accordion').find('.parent-box').find('h4');
		
		$(childPanels).each(function(){
			var html = '<div class="child-panel ' + $(this).attr('class') + '"><h4 class="hidden">' + $(this).html() + '</h4><div class="bg_click hidden"></div></div>';
			html += '<div class="child-box"><div class="content">' + $(this).next().html() + '</div></div>';
			$(this).next().remove();
			$(this).replaceWith(html);	
			
		});
		
		$('.parent-panel').each(function(){		
			$(this).next().find('.child-panel:last').css('border-bottom', '1px solid #c44977');
		});
			
		table.bindAccordionEvents();
	}
	
};

$(function(){
 
	table.setup({toggleSpeed : 600});
	table.changeCurrency($('input[type=radio]:checked').attr('id'));
	table.bindOtherEvents();
	
});