// JavaScript Document
// Play well with other libraries, use jQuery instead of jQuery
jQuery.noConflict();


// Variables for Category List Scroller
var cat_count;
var cat_interval;
var old_cat = 0;
var current_cat = 0;

jQuery(document).ready(function(){
								
	// rollovers
	PEPS.rollover.init();
	
		// set div links
	jQuery(".divlink").click( function(){
		window.location=jQuery(this).find("a").attr("href"); return false;
	});
	
	// Checkbox Hilite
	jQuery('.user_chart_checkbox').each(function() {
		this.checked = false;
	});
	
	jQuery('ul.user_chart_data input.user_chart_checkbox[@checked]').each(function() {
		jQuery(this).parents().filter('ul').addClass('yellow_hilite');
	});
	
	jQuery('ul.user_chart_data').click(function() {
		jQuery(this).toggleClass('yellow_hilite');
		jQuery(this).find('input').each(function(){
			this.checked = !this.checked;
		});
		jQuery('#check_all_link').removeClass('clicked');
	});
	// Category List Toggle View
	jQuery('h2.category_list_header').click(function() {
		jQuery(this).toggleClass('expanded');
		jQuery(this).toggleClass('closed');
		jQuery(this).next().slideToggle('slow');
	});
	// Hide Search Users
	jQuery('#hide_user_search').click(function(){
		jQuery('#search_users_container').slideUp('fast', function(){
			jQuery('#show_search').show();
		});
			
	});
	// Show Search Users
	jQuery('#show_search').click(function(){
		jQuery('#search_users_container').slideDown('fast', function(){
			jQuery('#show_search').hide();
		});
			
	});
	
	// Hide Announcements
	jQuery('#hide_announcements').click(function(){
		jQuery('#announcement_box').fadeOut('slow', function(){
			jQuery('#show_announcements').show();
		});
			
	});
	// Show Announcements
	jQuery('#show_announcements').click(function(){
		jQuery('#show_announcements').hide();
		jQuery('#announcement_box').fadeIn('slow');
	});
	
	// Quick Reference Toggle View
	jQuery('#qr_toggle').click(function(){
		jQuery(this).toggleClass('show');
		if(jQuery(this).text() == 'Show'){
			jQuery('#quick_ref_box > div.ref').slideToggle('fast');
			jQuery(this).text('Hide');
		}else{
			jQuery('#quick_ref_box > div.ref').slideToggle('fast');
			jQuery(this).text('Show');
		}
		
	});
	// Hover classes
	//User Chart
	jQuery('ul.data').hover(function() {
		jQuery(this).not('.yellow_hilite').addClass('grey_hilite');
	}, function() {
		jQuery(this).removeClass('grey_hilite');
	});
	jQuery('#check_all_link').click(function() {
		if(jQuery(this).hasClass('clicked')){
			jQuery(this).removeClass('clicked');
			jQuery('.user_chart_checkbox').each(function(){
				jQuery(this).parents().filter('ul').removeClass('yellow_hilite');
				this.checked = false;
			});
			
		}else{
			jQuery(this).addClass('clicked');
			jQuery('.user_chart_checkbox').each(function(){
				jQuery(this).parents().filter('ul').addClass('yellow_hilite');
				this.checked = true;
			});

		}
	});
	
	// Form Button Background Rollovers
	jQuery('input.robutton').hover(function(){
			jQuery(this).toggleClass('rob_hover');
		}, function(){
			jQuery(this).toggleClass('rob_hover');									
	});
	
	// Category Scroller for Select Category form
	
	cat_count = jQuery("div.option").size();
	jQuery("div.option:eq("+current_cat+")").css('top', '0px');
	
	// Category Scroller Up Button
	jQuery('#square_category_list_down').click(function(){
		current_cat = (old_cat + 1) % cat_count;
		jQuery("div.option:eq("+ current_cat +")").css('top', '26px');
  		jQuery("div.option:eq(" + old_cat + ")").animate({top: -26},"fast");
  		jQuery("div.option:eq(" + current_cat + ")").animate({top: 0},"fast", function() {
			var cval = jQuery(this).attr('id');
			jQuery('#squares_category').val(cval);
  			old_cat = current_cat;
		});
	});	
	
	// Category Scroller Down Button
	jQuery('#square_category_list_up').click(function(){
		if(old_cat <=0){
			current_cat = cat_count - 1;
		}else{
			current_cat = (old_cat - 1) % cat_count;
		}
		jQuery("div.option:eq("+ current_cat +")").css('top', '-26px');
  		jQuery("div.option:eq(" + old_cat + ")").animate({top: 26},"fast");
  		jQuery("div.option:eq(" + current_cat + ")").animate({top: 0},"fast", function() {
			var cval = jQuery(this).attr('id');
			jQuery('#squares_category').val(cval);
  			old_cat = current_cat;
		});
	});
	
	// Clear Hinted Textfields
	jQuery('input.clearfield').one("click", function(){
		jQuery(this).val('');
		jQuery(this).removeClass('clearfield');
	});
	
	// Profile Form Submit Button
	jQuery("#profile_save_button").click(function() {
		jQuery('#client_profile_form').submit();
		return false;
	});

	
	
});






// Rollover function
PEPS = {};

PEPS.rollover =
{
   init: function()
   {
      this.preload();
      
      jQuery(".ro").hover(
         function () { jQuery(this).attr( 'src', PEPS.rollover.newimage(jQuery(this).attr('src')) ); },
         function () { jQuery(this).attr( 'src', PEPS.rollover.oldimage(jQuery(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      jQuery(window).bind('load', function() {
         jQuery('.ro').each( function( key, elm ) { jQuery('<img>').attr( 'src', PEPS.rollover.newimage( jQuery(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)/) ) + '_o' + src.match(/(\.[a-z]+)/)[0];
   },
   
   oldimage: function( src )
   {
      return src.replace(/_o/, '');
   }
};
