/*
Customized jQuery actions
*/

$(document).ready(function() { //perform actions when DOM is ready
//$(function() {

/* ///////////////////////////  HOMEPAGE IMAGE ROTATION  ////////////////////////// */
/* http://usejquery.com/posts/3/create-a-unique-gallery-by-using-z-index-and-jquery */
/* //////////////////////////////////////////////////////////////////////////////// */ 
  var z = 0; //for setting the initial z-index's
  var inAnimation = false; //flag for testing if we are in a animation
  
  $('#pictures img').each(function() { //set the initial z-index's
    z++; //at the end we have the highest z-index value stored in the z variable
    $(this).css('z-index', z); //apply increased z-index to <img>
  });

  function swapFirstLast(isFirst) {
    if(inAnimation) return false; //if already swapping pictures just return
    else inAnimation = true; //set the flag that we process a image
    
    var processZindex, direction, newZindex, inDeCrease; //change for previous or next image
    
    if(isFirst) { processZindex = z; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
    else { processZindex = 1; direction = ''; newZindex = z; inDeCrease = -1; } //set variables for "previous" action
    
    $('#pictures img').each(function() { //process each image
      if($(this).css('z-index') == processZindex) { //if its the image we need to process
        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
          $(this).css('z-index', newZindex) //set new z-index
            .animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
              inAnimation = false; //reset the flag
            });
        });
      } else { //not the image we need to process, only in/de-crease z-index
        $(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
          $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
        });
      }
    });
    
    return false; //don't follow the clicked link
  }
  
  $('#next a').click(function() {
    return swapFirstLast(true); //swap first image to last position
  });
  
  $('#prev a').click(function() {
    return swapFirstLast(false); //swap last image to first position
  });
/* /////////////////////////////////////////////////////////////////////////////// */  
  
  
/* /////////////////  FOR TABS ON HOMEPAGE  ///////////////// */
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	//Set height of tab_container based on tallest tab content
	var largestHeight = 0;
	if ($("#tab1").height() > largestHeight) {
		largestHeight = $("#tab1").height();
	} 
	if ($("#tab2").height() > largestHeight) {
		largestHeight = $("#tab2").height();
	} 
	if ($("#tab3").height() > largestHeight) {
		largestHeight = $("#tab3").height();
	}
	if ($("#tab4").height() > largestHeight) {
		largestHeight = $("#tab4").height();
	} 
	if (largestHeight > 325) {
		$(".tab_container").height(largestHeight);
	} 
/* ////////////////////////////////////////////////////////// */
  
  
/* /////////////////  FOR SEARCH BAR  ///////////////// */
	var clearMePrevious = "";
	
	// clear input on focus
	$('.searchfield').focus(function()
	{
		if($(this).val()==$(this).attr('title'))
		{
			clearMePrevious = $(this).val();
			$(this).val("");
		}
	});
	
	// if field is empty afterward, add text again
	$('.searchfield').blur(function()
	{
		if($(this).val()=="")
		{
			$(this).val(clearMePrevious);
		}
	});
/* //////////////////////////////////////////////////// */


/* /////////////////  FOR CALENDAR PAGE  ///////////////// */  
  	var l = 0;
	$('.calendarcell').each(function() {
		if ($(this).height() > l) {
			l = $(this).height();
		} 
	});
	// Set the height of all calendar cells to the tallest among all instances. This prevents the cells from being different heights when show titles get long.
	$(".calendarcell").height(l);
	
/* ////////////////////////////////////////////////////// */

/* ///////////////// HOVER PHOTO GALLERY ///////////////// */ 
	$('.photogroup a').hover(function() {

		var largePath = $(this).attr("href");
        var largeAlt = $(this).attr("title");

        $("#largePhoto").attr('src', largePath);
		$("#largePhoto").attr('alt', largeAlt);
		
		$(".photomain a").attr('href', largePath);
		$(".photomain a").attr('title', largeAlt);	
		
		return false;
	});
	
	$('.photogroup a').click(function() {
		return false;
	});

/* /////////////////////////////////////////////////////// */

/* ///////////////// CUSTOM CHECKBOXES ///////////////// */ 
	$("input:checkbox").each( function() {
		if (this.checked) {
			$("#fake"+this.id).addClass('checked');	
		} else {
			$("#fake"+this.id).addClass('unchecked');	
		}
	});
	$(".fakebox").click(function(){
		if ($(this).hasClass('checked')) { 
			$(this).removeClass('checked');
			$(this).addClass('unchecked');
		} else if ($(this).hasClass('unchecked')) { 
			$(this).removeClass('unchecked');
			$(this).addClass('checked');
		}
		$(this.hash).trigger("click");
		if ($(this).hasClass('autosubmit')) {
			$('form').submit();
		} else {
			return false;
		}
	});
	
	$("#theater").hide();
	$("#music").hide();
	$("#dance").hide();
	$("#festival").hide();
	$("#opera").hide();
	$("#spco").hide();
	$("#schubert").hide();
	$("#special").hide();
	$("#submitter").hide();
/* ///////////////////////////////////////////////////// */

/* /////////////// FOR SELECTING SEASON IN CENTER COLUMN OF PERFORMANCES PAGE ////////////// */
	$('#s1').addClass('selectedseason');
	$('#s2').addClass('unselectedseason');
	$('#season2').hide();

	$('#s1').click(function(){
		$('#s1').addClass('selectedseason');
		$('#s2').addClass('unselectedseason');
		$('#s1').removeClass('unselectedseason');
		$('#s2').removeClass('selectedseason');
		$('#season1').fadeIn();
		$('#season2').hide();
		return false;
	});
	
	$('#s2').click(function(){
		$('#s2').addClass('selectedseason');
		$('#s1').addClass('unselectedseason');
		$('#s2').removeClass('unselectedseason');
		$('#s1').removeClass('selectedseason');
		$('#season2').fadeIn();
		$('#season1').hide();
		return false;
	});


/* ///////////////////////////////////////////////////////////////////////////////////////// */

/* ////////////////////////////////////////////////////////////////////////// */
/* ///// For fixing transparent PNG's in IE6 // superslieight.plugin.js ///// */
/* ////////////////////////////////////////////////////////////////////////// */
	if ($.browser.msie && $.browser.version == '6.0') {
    	$('#container').supersleight(); 
  	}	
/* ////////////////////////////////////////////////////////////////////////// */


/* /////////////////  FOR CALENDAR/PERFORMANCES SIDEBAR SELECTOR IN ADMIN SECTION ///////////////// */ 
	//--// Checkbox submit //
	$(".perfCheckbox").click(function() {
		$("form#performances").submit();
	});
	//^^// Hide Submit Button for those who have javascript enabled //^^//
	//$("#submitter").hide();
	
/* ////////////////////////////////////////////////////////////////////////////// */

//**// Mouse over change text //
	$('a.tool-hover').mouseover(function() {
		$('#bubble').html(this.id);
		$("#bubble").removeClass('bubble-hide');
	});
	$('a.tool-hover').mouseout(function() {
		$('#bubble').html('&nbsp;');
		$("#bubble").addClass('bubble-hide');
	});
	
	$("#bubble").addClass('bubble-hide');
	
//^^// Hide and unhide some random stuff for those who have javascript enabled //^^//
	$(".inlineContent").hide();
	$(".pre-post").show();
	//$(".showblock-links").show();
//^^// end hide Submit Button //^^//	
	
	
});

