
var lastSection = 'section_0';
var lastTheme = 'section_0_theme_0';
var speed = 5000;
var slide;

function showNextTheme() {
	var newTheme = jQuery('.selected_theme:first').next('.toptheme_selector');
	if (!newTheme.attr('rel')) {
		newTheme = jQuery('.selected_theme').parents('.toptheme_selectors:first').find('.toptheme_selector:first');
	}
	newTheme.trigger('click');
}

jQuery(document).ready(function() {
	jQuery('.toptheme').tooltip({
		bodyHandler: function() {
			return jQuery(this).find('.copyright').html();
		}
	});
	jQuery('.topsection_selector').click( function() {
		var showId = jQuery(this).attr('rel');
		clearTimeout(slide);
		jQuery('#'+lastSection).fadeOut('def',function() {
			jQuery('#'+showId).fadeIn();
			lastTheme = lastSection + '_theme_0';
			jQuery('.toptheme').hide();
			jQuery('#'+lastTheme).show();
			jQuery('.selected_theme').removeClass('selected_theme');
			jQuery('#'+lastSection+' .toptheme_selector:first').addClass('selected_theme');
			slide = setTimeout("showNextTheme()",speed);
		});
		lastSection = showId;
	});
	jQuery('.toptheme_selector').click( function() { 
		clearTimeout(slide);
		var showId = jQuery(this).attr('rel');
		jQuery('#'+lastTheme).fadeOut('def',function() {
			jQuery('.toptheme').hide();
			jQuery('#'+showId).fadeIn();
			slide = setTimeout("showNextTheme()",speed);
		});
		jQuery('.toptheme_selector').removeClass('selected_theme');
		jQuery(this).addClass('selected_theme');
		lastTheme = showId;
	});
	jQuery('.toptheme').hover(
      function () {
			clearTimeout(slide);
      }, 
      function () {
			clearTimeout(slide);
			slide = setTimeout("showNextTheme()",speed);
      }
    );
	jQuery('.toptheme_selectors').hover(
      function () {
			clearTimeout(slide);
      }, 
      function () {
			clearTimeout(slide);
			slide = setTimeout("showNextTheme()",speed);
      }
    );
	
	//init
	jQuery('.toptheme_section').hide();
	jQuery('#'+lastSection).show();
	
	jQuery('.toptheme').hide();
	jQuery('#'+lastTheme).show();
	jQuery('#'+lastSection+' .toptheme_selector:first').addClass('selected_theme');
	//var slide = jQuery.timer(speed,showNextTheme());
	slide = setTimeout("showNextTheme()",speed);
});