// Switch to no conflict mode
jQuery.noConflict();

// Show header panel
function show_header_panel() {

  var fade_tab_on_tab_hover_out;
  var fade_panel_on_tab_hover_out;
  var fade_panel_on_panel_hover_out;

  // Preload header images
  var image1 = jQuery('<img />').attr('src', '/images/app/header_line.gif');
  var image2 = jQuery('<img />').attr('src', '/images/app/header_line_hover.gif');
  var image3 = jQuery('<img />').attr('src', '/images/app/header_panel.png');
  var image4 = jQuery('<img />').attr('src', '/images/app/go.png');
  var image5 = jQuery('<img />').attr('src', '/images/app/button_left.png');
  var image6 = jQuery('<img />').attr('src', '/images/app/button_right.png');

  // TABS
  jQuery("#nav li").hover(
    
    // HOVER OVER
    function () {
      
      // Get active tab/panel
      var active = jQuery(this).attr("id");
      
      // If active panel is fading out, stop fade and reset opacity
      clearTimeout(fade_panel_on_panel_hover_out); 
      jQuery('#panels .panel.' + active).stop();
      jQuery('#panels .panel.' + active).css('opacity', '1');
      
      // Remove highlight from other tabs
      jQuery('#nav li').each(function (i) {
        if (jQuery(this).attr("id") != active) {
          jQuery(this).removeClass('hovered');
        }
      });
            
      // Hide all panels (except the panel for the selected tab)
      jQuery('#panels .panel').each(function (i) {
        if (jQuery(this).attr("class").split(' ')[0] != active) {
          jQuery(this).hide();
        }
      });
      
      // Show active panel
      jQuery('#panels .' + active).show();
    }, 
    
    // HOVER OUT
    function () {
      
      // Get active tab/panel
      var active = jQuery(this).attr("id");
      
      // Remove highlight from tab
      fade_tab_on_tab_hover_out = setTimeout(function() {jQuery('#' + active).removeClass('hovered')}, 50);       
      
      // Fade out panel (if tab or panel isn't selected)
      fade_panel_on_tab_hover_out = setTimeout(function() {jQuery('#panels .panel.' + active).fadeOut(500)}, 50);             
      
    }
  );
  
  // PANELS
  jQuery("#panels .panel").hover(
    
    // HOVER OVER
    function () {
      
      // Get active tab/panel
      var active = jQuery(this).attr("class").split(' ')[0];
            
      // Stop panels from fading (if mousing down from tabs)
      clearTimeout(fade_tab_on_tab_hover_out); 
      clearTimeout(fade_panel_on_tab_hover_out);
      clearTimeout(fade_panel_on_panel_hover_out); 
      
      // If panel is fading out, stop fade and reset opacity
      jQuery('#panels .panel').stop();
      jQuery('#panels .panel').css('opacity', '1');
    
      // Select tab (since we're not hovering over it)
      jQuery("#" + active).addClass('hovered');
            
    }, 
    
    // HOVER OUT
    function () {
      
      // Get active tab/panel
      var active = jQuery(this).attr("class").split(' ')[0];
            
      // Fade out panel and tab
      fade_panel_on_panel_hover_out = setTimeout(function() {
        
        jQuery("#" + active).removeClass('hovered');
        jQuery('#panels .panel.' + active).fadeOut(1000);
        
      }, 700);       
      
    }
  );
  
}

// Show header panneaus
function show_header_panneaus() {

  var fading_tab_on_tab_hover_out;
  var fading_panel_on_tab_hover_out;
  var fading_panel_on_panel_hover_out;

  // Preload header images
  var image1 = jQuery('<img />').attr('src', '/images/app/header_line.gif');
  var image2 = jQuery('<img />').attr('src', '/images/app/header_line_hover.gif');
  var image3 = jQuery('<img />').attr('src', '/images/app/header_panel.png');
  var image4 = jQuery('<img />').attr('src', '/images/app/go.png');
  var image5 = jQuery('<img />').attr('src', '/images/app/button_left.png');
  var image6 = jQuery('<img />').attr('src', '/images/app/button_right.png');

  // TABS
  jQuery("#navig li").hover(
    
    // HOVER OVER
    function () {
      
      // Get active tab/panel
      var active = jQuery(this).attr("id");
      
      // If active panel is fading out, stop fade and reset opacity
      clearTimeout(fading_panel_on_panel_hover_out); 
      jQuery('#panneaus .panneau.' + active).stop();
      jQuery('#panneaus .panneau.' + active).css('opacity', '1');
      
      // Remove highlight from other tabs
      jQuery('#navig li').each(function (i) {
        if (jQuery(this).attr("id") != active) {
          jQuery(this).removeClass('hovered');
        }
      });
            
      // Hide all panels (except the panel for the selected tab)
      jQuery('#panneaus .panneau').each(function (i) {
        if (jQuery(this).attr("class").split(' ')[0] != active) {
          jQuery(this).hide();
        }
      });
      
      // Show active panel
      jQuery('#panneaus .' + active).show();
    }, 
    
    // HOVER OUT
    function () {
      
      // Get active tab/panel
      var active = jQuery(this).attr("id");
      
      // Remove highlight from tab
      fading_tab_on_tab_hover_out = setTimeout(function() {jQuery('#' + active).removeClass('hovered')}, 50);       
      
      // Fade out panel (if tab or panel isn't selected)
      fading_panel_on_tab_hover_out = setTimeout(function() {jQuery('#panneaus .panneau.' + active).fadeOut(500)}, 50);             
      
    }
  );
  
  // PANELS
  jQuery("#panneaus .panneau").hover(
    
    // HOVER OVER
    function () {
      
      // Get active tab/panel
      var active = jQuery(this).attr("class").split(' ')[0];
            
      // Stop panels from fading (if mousing down from tabs)
      clearTimeout(fading_tab_on_tab_hover_out); 
      clearTimeout(fading_panel_on_tab_hover_out);
      clearTimeout(fading_panel_on_panel_hover_out); 
      
      // If panel is fading out, stop fade and reset opacity
      jQuery('#panneaus .panneau').stop();
      jQuery('#panneaus .panneau').css('opacity', '1');
    
      // Select tab (since we're not hovering over it)
      jQuery("#" + active).addClass('hovered');
            
    }, 
    
    // HOVER OUT
    function () {
      
      // Get active tab/panel
      var active = jQuery(this).attr("class").split(' ')[0];
            
      // Fade out panel and tab
      fading_panel_on_panel_hover_out = setTimeout(function() {
        
        jQuery("#" + active).removeClass('hovered');
        jQuery('#panneaus .panneau.' + active).fadeOut(1000);
        
      }, 700);       
      
    }
  );
  
}

// Show form hints (with inputs using "title" attribute)
function show_form_hints() {
  var el = jQuery('input[Title]');

  // Show display text
  el.each(function(i) {
    jQuery(this).attr('value', jQuery(this).attr('title'));
  });

  // Hook up the blur & focus
  el.focus(function() {
    if (jQuery(this).attr('value') == jQuery(this).attr('title'))
      jQuery(this).attr('value', '');
  }).blur(function() {
    if (jQuery(this).attr('value') == '')
      jQuery(this).attr('value', jQuery(this).attr('title'));
  });
}

// Switches content in tab sets
function switch_tabs() {
  
  jQuery('#flipbox a').hover(
    function() {
    
    // Get a list of tab names
    var tab_names = Array();
    jQuery('#flipbox a').map(function(){
      tab_names.push(jQuery(this).attr("id").replace("tab_",""));
    });

    // Hide all tabs
    for (i = 0; i < tab_names.length; i++) {
      jQuery('#' + tab_names[i]).hide();
      jQuery('#tab_' + tab_names[i]).parent().removeClass('active');
    }
    
    // Show the selected tab
    jQuery(jQuery(this).attr("id").replace("tab_","#")).show();
    jQuery(this).parent().addClass('active');
    
    },
    
    function() {
    }
  );
  
}


function search_tool() {
  
  // Switch to website search
  jQuery('.website').click(function() {
    jQuery('.repertoire').attr('checked', false);
    jQuery('#website').show();
    jQuery('#repertoire').hide();
  });
  
  // Switch to repertoire search
  jQuery('.repertoire').click(function() {
    jQuery('.website').attr('checked', false);
    jQuery('#website').hide();
    jQuery('#repertoire').show();
  });
  
  // Keep search query fields in sync
  jQuery('#web_search').change(function() {
  	jQuery('#rep_search').val(jQuery('#web_search').val());
  });
  jQuery('#rep_search').change(function() {
  	jQuery('#web_search').val(jQuery('#rep_search').val());
  });

}

function make_pngs_work_in_explorer() {
  
  // Set transparent pixel
  jQuery.ifixpng('/images/app/pixel.gif');
  
  // Set pngs to make transparent
  jQuery('img, #overlay, .go').ifixpng();

}







// Let's roll!
jQuery(document).ready(function(){
    
  show_header_panel();
  
  show_header_panneaus() 
  
  show_form_hints();
  
  animate_page_title();
  
  search_tool();

  switch_calendar_to_selected_month();
  
  highlight_mini_calendar_on_hover();
  
  toggle_sidebar_lists();
  
  switch_tabs();

  what_is_bmi_panel();

  make_pngs_work_in_explorer();
  
  // Genres slideshow (if slides are present)
  if (jQuery('#features').length) {    
    setInterval("genres_slideshow()", 7000 );
  }
  
  // Slideshow (if slides are present)
  if (jQuery('#feature').length) {
    
    // Slideshow
    slides = setInterval(slideshow, 7000);
    
    // Jump to slide
    jQuery("#feature .labels li").click(function () { 
      
      // Stop slideshow
      clearInterval(slides);

      // Reshuffle slides
      reshuffle_slides(jQuery(this));
                
      // Restart slideshow at current position
      slides =  setInterval(slideshow, 7000);
      
    });
    
  }

});



