$(function(){

    // search field - clear on focus
    $("#search .text-input").focus(function() {
        if($(this).val() == "Search") {
            $(this).val("");
        }
    });



	$.fn.slideshow = function() {

		var this_slideshow = $(this);
		$('.slide:first', this_slideshow).show();
		
		var random = 5+(Math.random()*(20-5));
		if ( $('.slide',this_slideshow).size() > 1 ) {
			var slide_interval = setInterval(autoSlide,(random * 1000));
		}
		
		function autoSlide() {	
			nextSlide();		
			$('.details, .controls', this_slideshow).fadeOut('slow');
			setTimeout(function() { $('.slide:visible .details, .controls', this_slideshow).fadeIn('slow'); }, 3000);
		}
				
		function showDetail(){
		if ( $('.slide',this_slideshow).size() > 1 ) {		
			$('.slide:visible .details, .controls', this_slideshow).fadeIn('slow');
		} else {
			$('.slide:visible .details', this_slideshow).fadeIn('slow');		
		}
			clearInterval(slide_interval);
		};
		
		$('.details, .controls', this_slideshow).hide();

		if ( $('.slide',this_slideshow).size() > 1 ) {		
			setTimeout(function() { $('.slide:visible .details, .controls', this_slideshow).fadeIn('slow'); }, 3000);
		} else {
			setTimeout(function() { $('.slide:visible .details', this_slideshow).fadeIn('slow'); }, 3000);		
		}
		
		$(this).mouseenter(function(){
			showDetail();
		});
		
		//Actions on slideshow controls		
		function changeSlide(slide_number) {
			$('.slide', this_slideshow).not(this).fadeOut(2000);
			$(".slide:eq(" + slide_number + ")", this_slideshow).fadeIn(2000);
			$(".controls li", this_slideshow).removeClass('active');
			$(".controls li:eq(" + slide_number + ")", this_slideshow).addClass('active');

		}
		
		//Number of slides
		var slide_count = $('.slide', this_slideshow).size();
		
		//Go to next slide
		function nextSlide() {
			var current_slide = $('.controls ul li.active', this_slideshow).index();		
			if (current_slide == (slide_count -1)) {
				changeSlide(0);
			} else {
				changeSlide(current_slide + 1);
			}	
		}

		//Go to previous slide
		function prevSlide() {
			var current_slide = $('.controls ul li.active', this_slideshow).index();		
			if (current_slide == 0) {
				changeSlide(slide_count - 1);
			} else {
				changeSlide(current_slide - 1);
			}
		}
				
		//Action on clicking next arrow
		$('.controls .next', this_slideshow).click(function(){		
			clearInterval(slide_interval);
			nextSlide();
			showDetail();
			return false;
		});	
		
		//Action on clicking previous arrow
		$('.controls .prev', this_slideshow).click(function(){
			clearInterval(slide_interval);
			prevSlide();
			showDetail();
			return false;
		});	

		//Action on clicking position bullet
		$('.controls li', this_slideshow).click(function(){
			clearInterval(slide_interval);
			changeSlide($('.controls li', this_slideshow).index(this));
			showDetail();
			return false;
		});
		
		$('.controls li').css({'cursor': 'pointer'});
	
	}
	
	$('#slide-full').slideshow();
	$('#slide-collection').slideshow();
	$('#slide-landing').slideshow();
	$('#slide-main').slideshow();
	$('#slide-half').slideshow();
	$('#slide-quart').slideshow();
	

	// Collapsable content
	
	$('ul.collapsable .window').hide();
	$('ul.collapsable .toggle').click(function(){
		$(this).toggleClass('toggle-on').next('.window').slideToggle();
	}).filter(':first').click();
	
	$('div.collapsable .listingDesc,div.collapsable .listingInfo').hide();
	$('div.collapsable .toggle').click(function(){
		$(this).toggleClass('toggle-on').nextAll('.listingDesc, .listingInfo').slideToggle();
	});
/*	if (self.document.location.hash){
	   $(self.document.location.hash).children().not('h3,.toggle').toggle();
	   $(self.document.location.hash + ' .toggle').toggleClass('toggle-on');
	}*/
	
	// Collapsable arrows in  search tips list
	$('.tips ul li').prepend('<a class="toggle"></a>');
	$('.tips ul li').not('.open').children().not('h3,.toggle').hide();
	$('.tips ul li .toggle, .tips ul li h3').click(function(){
		$(this).parent('li').toggleClass('open').children().not('h3,.toggle').toggle();
		return false;
	});

	// Period box in advanced search
	$('.jsPeriodBox input:text').focus(function(){
		$(this).slideUp();
		$('.jsPeriodBox .periodBox').slideDown();
	});
	$('.periodBox ul li').prepend('<a class="toggle"></a>');
	$('.periodBox>ul>li ul').hide();
	$('.periodBox .toggle').click(function(){
		$(this).parent('li').toggleClass('open').children('ul').toggle();
	});
	$('.jsPeriodBox .periodBox a').not('.toggle').click(function(){
		$('.jsPeriodBox input:text').slideDown().val($(this).text());
		$('.jsPeriodBox .periodBox').slideUp();
		return false;
	});
	
	//Calendar accordion
	$('.calendarAccordianBody').hide();
	$('.calendarAccordianHeader').click(function(){
		$(this).toggleClass('calendarAccordianHeaderOpen').siblings('.calendarAccordianBody').slideToggle();
	}).filter(':first').click();
	
	//Collapsable event details
	$('.event-listing .event-item .show-more').hide();
	$('.event-listing .event-item .show-more').closest('.event-item').prepend('<a href="#" class="toggle"></a>');
	$('.event-listing .event-item .toggle').click(function(){
		$(this).toggleClass('toggle-open').parent().find('.show-more').toggle();
		return false;
	});
	
	// Create active link on parent element	
	function findLink(e) {
		var link = e.find('a:first').attr('href');
		if (link) {
			window.location = link;
		}		
	}

	//Create active link on parent -- beware incompatible with CTRL click, etc.
    $('.overlay table tr').click(function(){
		findLink($(this));
	});	
	
	//Scroll to top of page
	function goToByScroll(){
	     	$('html,body').animate({scrollTop: $("body").offset().top},'fast');
	}
	
	// Artwork collapsable details
	$('#article-info dl dt.toggle').css({'cursor': 'pointer'}).next().hide();
	$('#article-info dl dt.toggle').click(function(){
		$(this).toggleClass('toggle-on').next().slideToggle();
	});
	
	//Major exhibition accordion
	
	$('#ee-accordion .panel-toggle').prepend('<a href="#" class="toggle"></a>').next('.panel').hide();
	$('#ee-accordion .panel-toggle').click(function(){
		$(this).next('.panel').slideToggle();
		$('.toggle',this).toggleClass('toggle-open');
		return false;
	});
		
	// Collapsable arrows faq
	$('dl.faq dt').prepend('<a class="toggle"></a>');
	$('dl.faq dd').hide();
	$('dl.faq dt').click(function(){
		$(this).children('.toggle').toggleClass('toggle-open');
		$(this).next('dd').slideToggle();
		return false;
	}).filter(':first').click();
	
	
	// Keep main artwork always visible
    // $.fn.scrollArt = function() {
    //  $('#article-display .frame').css({'position':'fixed', 'width': '516px'});
    //  $('#article-display .frameBox').css('marginTop',$('#article-display .frame').height() + 40);
    //  var offsetOriginal = $('#article-display .frame').offset().top;
    //  if ($(window).scrollTop() > 0) {
    //      offsetOriginal = (offsetOriginal - $(window).scrollTop());
    //  }
    //  $(window).scroll(function(){
    //      var offsetAmount = 0;
    //      if (offsetOriginal < $(window).scrollTop()) {
    //          var offsetAmount = 20;
    //      } else {
    //          var offsetAmount = (offsetOriginal - $(window).scrollTop());
    //      }
    //      $("#article-display .frame").css('top', offsetAmount);
    //  });
    // }
    // if ($('#article-display').length > 0){
    //      $('#article-display').scrollArt();
    // }



});
