/*HOVER COLOURING*/
function hoverColourFade(elem, c_base, c_hovered)
{
	$(elem).live("mouseenter", function(e) {
	    e.preventDefault();
	    $(this).css({color:c_base});
	    $(this).animate({color:c_hovered},{queue:false,duration:250});
	}).live("mouseleave", function(e) {
	    e.preventDefault();
	    $(this).animate({color:c_base},{queue:false,duration:250});
	});
}
function hoverColourFadeEvent()
{
	$('ul.event-list li').live("mouseenter", function(e) {
	    e.preventDefault();
	    $('h3, .right, .date', this).css({color:'#222'});
	    $('h3, .right, .date', this).animate({color:'#DB001A'},{queue:false,duration:250});
	}).live("mouseleave", function(e) {
	    e.preventDefault();
	    $('h3, .right, .date', this).animate({color:'#222'},{queue:false,duration:250});
	});
}
function hoverColourFadeBlockList()
{
	$('ul.block-list li').live("mouseenter", function(e) {
	    e.preventDefault();
	    $('h3, .venue, .date, p', this).css({color:'#222'});
	    $('h3, .venue, .date, p', this).animate({color:'#DB001A'},{queue:false,duration:250});
	}).live("mouseleave", function(e) {
	    e.preventDefault();
	    $('h3, .venue, .date, p', this).animate({color:'#222'},{queue:false,duration:250});
	});
}
function hoverColourFadeDropDown()
{
	$('#performance-content ul.block-list li').live("mouseenter", function(e) {
	    e.preventDefault();
	    $('h3, .venue, .date', this).css({color:'#ccc'});
	    $('h3, .venue, .date', this).animate({color:'#fff'},{queue:false,duration:250});
	}).live("mouseleave", function(e) {
	    e.preventDefault();
	    $('h3, .venue, .date', this).animate({color:'#ccc'},{queue:false,duration:250});
	});
}

/*FANCYBOX TITLE FORMAT*/
function fancyboxFormatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<span id="fancybox-title-over"><a href="javascript:;" onClick="$.fancybox.prev()" class="left fancybox-nav-prev">&larr;</a><a href="javascript:;" onClick="$.fancybox.next()" class="left fancybox-nav-next">&rarr;</a><a class="fancybox-close right" href="javascript:;" onclick="$.fancybox.close();">x</a>' + (title && title.length ? title : '' ) + ' Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</span>';
}


/*FULLSCREEN CAROUSEL*/
function fullscreencarousel_initCallback(carousel)
{
	// Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });

	//bind external nav controls
    $('ul#fullscreen-banner-controls li a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
    });
}

/*youtube pause function*/
function ytpause() {
	if(typeof ytplayer != 'undefined'){
		if(ytplayer.pauseVideo){
			ytplayer.pauseVideo();
		}
	}
}

function fullscreencarousel_stepCallback()
{
	if($('#fullscreen-banner').hasClass('homepage')){
		
		ytpause();
		
		if($('.vim').length > 0){
			$f($('.vim')[0]).api('pause');
		}
	}
}

function fullscreencarousel_itemVisibleInCallback_beforeAnimation(carousel, activeLi, index, state)
{
	var navli = $(activeLi).attr("rel");
	$(navli).addClass('active');
	$('span.fullscreen-banner-caption', navli).fadeIn(250);
}

function fullscreencarousel_itemVisibleOutCallback_beforeAnimation(carousel, activeLi, index, state)
{
	var navli = $(activeLi).attr("rel");
	$(navli).removeClass('active');
	$('span.fullscreen-banner-caption', navli).fadeOut(150);
}

/*MAIN WORK MENU*/
function menuShowHide(hoveredElement)
{
	//store the hovered element
	var he = hoveredElement;
	
	//get the id of the hover content we're wanting
	var hc = he.attr('rel');


	//show the work-panel-wrapper if its not already
	if(!$('.work-panel-wrap').hasClass('open'))
		$('.work-panel-wrap').addClass('open').slideDown(500);

	//sort the menu out
	if(!$('.work-menu-trigger.inactive').length > 0)
		$('.work-menu-trigger').not(he).addClass('inactive').animate({'background-color':'#333', 'color':'#fff'});


	//do the content hide/show if not already shown
	if(!$(hc).hasClass('open')){
		//do the menu links
		if(he.hasClass('inactive')){
			//make other links inactive
			$('.work-menu-trigger').not(he).addClass('inactive').animate({'background-color':'#333', 'color':'#fff'});
			
			//remove own inactive state
			he.removeClass('inactive').animate({'background-color': '#EFEFEC', 'color':'#808080'});
		}


		//do the content
		if($('.hover-content').hasClass('open')) {
			$('.hover-content.open').removeClass('open').fadeOut(500, function(){
				$(hc).addClass('open').fadeIn(500);
			});
		}else{
			$(hc).addClass('open').fadeIn(500);
		}
	}
}


/*WHAT'S ON WIDGET*/

function wo(elem)
{
	//store the element the user is on
	var ue = elem;
	//get the id of the content we're wanting
	var c = ue.attr('rel');
	//do the tabs if not already shown
	if(!$(c).hasClass('open')){
		//do the nav
		$('ul.wo-tab-nav li.active').removeClass('active');
		$(ue).addClass('active');
		//do the tab content
		$('.wo-tab.open').removeClass('open').hide();
		$(c).addClass('open').show();
	}
}


/*SIMPLE TABS*/

function simpleTabs(elem)
{
	var tc = elem.attr('rel');
	if(!$(tc).hasClass('open')){
		$('ul.simple-tab-nav li.active').removeClass('active');
		$(elem).addClass('active');
		$('.simple-tab.open').removeClass('open').hide();
		$(tc).addClass('open').show();
	}
}

$(function(){
	/*
	 * Colour hover fades
	 */
	hoverColourFade($('.main-menu a:not(.active)'), '#fff', '#00A6AE');
	hoverColourFade($('.footer-menu a:not(.active)'), '#000', '#DB001A');
	hoverColourFadeEvent();
	hoverColourFadeBlockList();

	/*
	 * what's on widget
	 */
	//make initial tab active and hide other tabs
	var woTabActive = $('ul.wo-tab-nav li.active').attr('rel');
	$(woTabActive).addClass('open');
	$('.wo-tab').not('.open').hide();

	//for clicks
	$("ul.wo-tab-nav li").click(function(e){
		e.preventDefault();
		wo($(this));
	});

	/*
	 * simple tabs
	 */
	if(window.location.hash != '')
	{
		//strip out default active states
		$('ul.simple-tab-nav li.active').removeClass('active');

		//get hash from url
		var hash = window.location.hash;
		$('ul.simple-tab-nav').find("li[rel='"+hash+"']").addClass('active');

		//make the hashed tab active and hide all others
		var simpleTabActive = $('ul.simple-tab-nav li.active').attr('rel');
		$(simpleTabActive).addClass('open');
		$('.simple-tab').not('.open').hide();
	}
	else
	{
		//make initial tab active and hide other tabs
		var simpleTabActive = $('ul.simple-tab-nav li.active').attr('rel');
		$(simpleTabActive).addClass('open');
		$('.simple-tab').not('.open').hide();
	}

	//for clicks
	$("ul.simple-tab-nav li").click(function(e){
		window.location.hash = $(this).attr('rel');
		e.preventDefault();
		simpleTabs($(this));
	});

	$(".simple-tab-static-link").click(function(e){
		var hash = $(this).attr('rel');
		window.location.hash = hash;
		e.preventDefault();
		simpleTabs($(this));
		$("ul.simple-tab-nav li[rel='"+hash+"']").addClass('active');
	});

	/*
	 * work menu
	 */
	
	//for clicks
	$(".work-menu-trigger").click(function(e){
		e.preventDefault();
		var hc = $(this).attr('rel');
		
		if($(hc).hasClass('open')){
			//reset the menu links
			$('.work-menu-trigger.inactive').removeClass('inactive').animate({'background-color': '#EFEFEC', 'color':'#808080'});
			
			//close up the content
			$(hc).removeClass('open').fadeOut(500, function(){
				$('.work-panel-wrap').removeClass('open').slideUp(500);
			});
		}else{
			menuShowHide($(this));
		}
	});
	
	/*
	 * fullscreen carousel
	 */
	//if there is more than one li do the carousel
	if ( $('#fullscreen-banner ul.carousel').children().size() > 1 ) {
		//could do with some easing
		$('#fullscreen-banner ul.carousel').jcarousel({
			scroll: 1,
			auto: 4,
			animation: 1200,
			wrap: 'circular',
			itemFallbackDimension: 945,
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: fullscreencarousel_initCallback,
			animationStepCallback: fullscreencarousel_stepCallback,
			itemVisibleInCallback: {
			  onBeforeAnimation: fullscreencarousel_itemVisibleInCallback_beforeAnimation
			},
			itemVisibleOutCallback: {
			  onBeforeAnimation: fullscreencarousel_itemVisibleOutCallback_beforeAnimation
			}
		});
	}
	if( $('#fullscreen-banner ul.carousel').children().size() <= 1 ){
		//remove nav
		$('#fullscreen-banner-controls').remove();
	}
	$('.vim').click(function(e){
		e.preventDefault();
		//alert('clicked!');
	});
	
	
	
	/*
	 * sidebar related entries carousel
	 */
	if ( $('#similar-stuff ul.carousel').children().size() > 1 ) {
		$('#similar-stuff ul.carousel').jcarousel({
			scroll: 1,
			auto: 6,
			animation: 1200,
			wrap: 'circular',
			itemFallbackDimension: 285,
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
	}
	
	
	/*
	 * hero gallery
	 */
	//init
	var hero_letter_ul = $('#hero-gallery ul.letterboxes');
	var hero_letter_li = $('#hero-gallery ul.letterboxes li');
	if ( hero_letter_ul.children().size() > 1 ) {
		hero_letter_li.css({'opacity':0}).hide();
		hero_letter_ul.cycle();
	}
	
	//hover and show - using hoverIntent
	var hero_gallery_config = {    
		over: function(){
			hero_letter_li.css({'opacity':0}).hide();
			var rel = '#'+$(this).attr('rel');
			$(rel).show().css({'opacity':1});
		},
		timeout: 100,
		out: function(){
			//do nothing
		}
	};
	$("#hero-gallery ul.banners li a").hoverIntent( hero_gallery_config );
	$("#hero-gallery ul.banners").hover(function(){
		hero_letter_ul.cycle('pause');
	}, function(){
		hero_letter_ul.cycle('resume');
	});

	hero_letter_ul.hover(function(){
		hero_letter_ul.cycle('pause');
	}, function(){
		hero_letter_ul.cycle('resume');
	});
	
	
	/*
	 * fading fullscreen banners
	 */
	//init
	var fading_fullscreen_ul = $('ul.fading-fullscreen-banner');
	var fading_fullscreen_li = $('ul.fading-fullscreen-banner li');
	if ( fading_fullscreen_ul.children().size() > 1 ) {
		fading_fullscreen_li.css({'opacity':0}).hide();
		fading_fullscreen_ul.cycle();
	}
	
	/*
	 * fancybox gallery
	 */
	$(".gallery-trigger").click(function(e) {
		e.preventDefault();

		//build array of images
		var galleryImagesArray = [];
		$("#gallery-images a").each(function() {
			galleryImagesArray.push(
				{
					'href'	: $(this).attr('href'),
					'title'	: '<b>'+$(this).attr('title')+'</b> ' + $(this).attr('name')
				}
			);
		});

		//launch fancybox with the array
		$.fancybox(galleryImagesArray, {
			'padding'			: 0,
			'type'              : 'image',
			'showCloseButton'	: false,
			'showNavArrows'		: false,
			'titlePosition' 	: 'over',
	 		'titleFormat'		: fancyboxFormatTitle
		});
	});

	/*
	 * Support panel
	 */
	$('#support-trigger').click(function(){
		if($('#support-inner').hasClass('open')){
			$('#support-inner').slideUp(1000).removeClass('open');
		}else{
			$('#support-inner').addClass('open').slideDown(1000);
			$("html, body").animate({ scrollTop: $(document).height() },{ duration: 1000});
		}
	});

	/*
	 * Make long lists/menus smaller
	 */
	$('.article-sidebar ul, ul.sub-menu, ul.sub-menu2, ul.sidebar-list').not('.full-length').each(function(){
		if($('li', this).size() > '8'){
			$('li:gt(7)', this).hide();
			$(this).append('<li class="list-more"><a href="#">more</a></li>');
		}
	});
	$('.list-more').click(function(e){
		e.preventDefault();
		var ths = $(this).parent('ul');
		$('li.list-more', ths).fadeOut(500, function(){
			$('li:gt(7):not(.list-more)', ths).slideDown(500);
		});
	});

	/*
	 * Field Clearing
	 */
	$('.input-clear').live('focus', function () {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	});
	$('.input-clear').live('blur', function () {
		if (this.value === '') {
			this.value = this.defaultValue;
		}
	});

	$('.input-clear-keywords').live('focus', function () {
		if (this.value == "Keywords") {
			this.value = '';
		}
	});
	$('.input-clear-keywords').live('blur', function () {
		if (this.value === '') {
			this.value = "Keywords";
		}
	});
	
	/*
	 * If search still has its default value, send it with nothing in the keywords field
	 */
	$('form.search').live('submit', function(){
		if($('.input-clear-keywords').val() == "Keywords") {
			$('.input-clear-keywords').val('');
		}
	});
	$('form#site-search').live('submit', function(){
		if($('.input-clear').val() == "Search") {
			$('.input-clear').val('');
		}
	});
	
	/*
	 * Search js stuff
	 */
	if(!$("select#event-type option#category_class").is(':selected')){
		$('.class-options option').removeAttr('selected');
		$('.class-options').hide();
	}
	
	$('select#event-type').change(function(){
		if($("select#event-type option#category_class").is(':selected')){
			$('.class-options').fadeIn(200);
		}else{
			$('.class-options option').removeAttr('selected');
			$('.class-options').hide();
		}
	});
	//activate chosen
	$(".chzn-select").chosen();
	
	/*
	 * Top nav Search thing
	 */
	$('.search-box .trigger').click(function(e){
		e.preventDefault();
		$('.search-box .trigger').hide();
		$('.search-box .btn-input').show();
		$('.search-box').animate({width:"140px"},{duration:600, queue:false});
		$('.search-box .text-input').show().focus().css({width:"100px"});
	});
	
	/*
	 * Datepicker
	 */
	$('.date-input').datepicker({ dateFormat: 'yy-mm-dd' });
});
