jQuery(document).ready(function($){

	// Homepage Graphic Carousel
	var activeImg = 1;

	var aniCar = setInterval(function()
	{
		curr = activeImg +1;
		if (curr > jQuery('#carouselImages').children().size()){
			curr = 1;
		}
		jQuery('#img-'+activeImg).removeClass('active-image');
		jQuery('#img-'+curr).css('opacity', 0.2);
		jQuery('#img-'+curr).addClass('active-image');
		jQuery('#img-'+curr).fadeTo('500', 1);
		
		jQuery('#control-'+activeImg).removeClass('activeControl');
		activeImg = curr;
		jQuery('#control-'+activeImg).addClass('activeControl');

	}, 5000);
	
	jQuery('.controller').mouseover(function () {


	clearInterval(aniCar);
		// get ID of clicked hero
		var curr = jQuery(this).attr('id').substring(8);
		
		// add/remove active class from slideshow images and fade in new one
		if (curr != activeImg) {
			jQuery('#img-'+activeImg).removeClass('active-image');
			jQuery('#img-'+curr).css('opacity', 0.2);
			jQuery('#img-'+curr).addClass('active-image');
			jQuery('#img-'+curr).fadeTo('500', 1);
			
			jQuery('#control-'+activeImg).removeClass('activeControl');
			activeImg = curr;
			jQuery('#control-'+activeImg).addClass('activeControl');
		}
	});

	jQuery('.controller a').click(function () {
		// return false;
	});

	
	// FORMS
	$("form.jqtransform").jqTransform();

	
	// Footer slide drawer
	
	$("#footer-main-section").hide();
		
	$("#footer-top-section h2 a").click(function(){
	
		if ( $(this).hasClass('active') )	{
			$("#footer-main-section").slideUp();
			$(this).removeClass("active");
			return false;
			
		} else {
			
			$('#footer-main-section').slideDown('fast', function() {
			    $.scrollTo('#page-bottom', {duration:500} );
			  });
			$(this).addClass("active");
			return false;
		} 
	});	
	
	
	// Rollover on Input buttons
	// Submit rollover state

	$('.Newsletter-button').mouseover(function() {
	$(this).attr('src', 'http://www.cancertechnology.com/style/images/buttons/subscribe/subscribe_two.gif');
	});
	
	$('.Newsletter-button').mouseleave(function() {
	$(this).attr('src', 'http://www.cancertechnology.com/style/images/buttons/subscribe/subscribe_one.gif');
	});
	
	$('.search-button').mouseover(function() {
	$(this).attr('src', 'http://www.cancertechnology.com/style/images/buttons/fOpportunity2.png');
	});
	
	$('.search-button').mouseleave(function() {
	$(this).attr('src', 'http://www.cancertechnology.com/style/images/buttons/fOpportunity1.png');
	});


	$('.search-tool-advanced-search').hide();	
	
	$('.search-tool-keyword-search-link').click(function(){
		$('.search-tool-keyword-search').show();
		$('.search-tool-advanced-search').hide();
		$(this).addClass('active');
		$('.search-tool-advanced-search-link').removeClass('active');
		return false;
	});

	$('.search-tool-advanced-search-link').click(function(){
		$('.search-tool-advanced-search').show();
		$('.search-tool-keyword-search').hide();	
		$(this).addClass('active');
		$('.search-tool-keyword-search-link').removeClass('active');
		return false;
	});

	// ------------------------------------------------------------------------
	
	// Tabbed Search on Research Tool page
	

/*

$(li.someCalss).click function	{

	all the others get made non active
	all the others get hidden
	$(li.thisClass).hide()
	$(li.thisClass).removeClass(active)
	
	this one becomes active
	this one is shown	
	$(this).show();
	$(this).addClass(active);
}

*/	

	$('.Cell-Lines-search').hide();	
	$('.Mouse-Cell-Lines-search').hide();	
	
	$('.Antibodies-link').click(function(){
		$('.Antibodies-search').show();
		$('.Cell-Lines-search').hide();
		$('.Mouse-Cell-Lines-search').hide();
		$(this).addClass('active');
		$('.Cell-Lines-link').removeClass('active');
		$('.Mouse-Cell-Lines-link').removeClass('active');
		return false;
	});

	$('.Cell-Lines-link').click(function(){
		$('.Cell-Lines-search').show();
		$('.Antibodies-search').hide();
		$('.Mouse-Cell-Lines-search').hide();	
		$(this).addClass('active');
		$('.Antibodies-link').removeClass('active');
		$('.Mouse-Cell-Lines-link').removeClass('active');
		return false;
	});
	
	$('.Mouse-Cell-Lines-link').click(function(){
		$('.Mouse-Cell-Lines-search').show();
		$('.Antibodies-search').hide();
		$('.Cell-Lines-search').hide();	
		$(this).addClass('active');
		$('.Antibodies-link').removeClass('active');
		$('.Cell-Lines-link').removeClass('active');
		return false;
	});

	// ------------------------------------------------------------------------
	
	// show/hide Research tools Filtration links
	
	// Initial hide of all lists
	$("ul.ResearchFilters").hide();


	// Behaviour when user clicks on the link (active == open)
	$(".filterOpen").click(function(){

		if ( $(this).hasClass('active') )	{
			// If already open, then close this list
			$(this).parent().next('ul.ResearchFilters').hide();
			$(this).removeClass("active");
			return false;
		} else {
			// Nope, it's closed, so go ahead and open it
			$(this).parent().next('ul.ResearchFilters').show();
			$(this).addClass("active");
			return false;
		} 
	});	


	/* 	Loop through lists, and if there is an element in there that is 
		already selected we need to open that li's parent list, and add
		the relevant active class */
	$('ul.ResearchFilters li.selected').each(function(index) {
		$(this).parent().show();
		$(this).parent().parent().find('h3 a.filterOpen').addClass('active');
	});


	// ------------------------------------------------------------------------
		
		// Hide / Show home page top feature 
		
		$("#topfeat-show").hide();
		
		$("a.Closetopfeat") .click(function(){
			$('#topfeat-graphic-carousel').hide();
			$('#topfeat-show').show();
			return false;
		});

		$("#topfeat-show a") .click(function(){
			$('#topfeat-graphic-carousel').show();
			$('#topfeat-show').hide();
			return false;
		});
		
	// ------------------------------------------------------------------------
	
	
	// Homepage Feature Panel Carousel
	var topactiveImg = 1;

	var topaniCar = setInterval(function()
	{
		topCurr = topactiveImg +1;
		if (topCurr > jQuery('#topfeat-carouselImages').children().size()){
			topCurr = 1;
		}
		jQuery('#topfeat-img-'+topactiveImg).removeClass('topfeat-active-image');
		jQuery('#topfeat-img-'+topCurr).css('opacity', 0.2);
		jQuery('#topfeat-img-'+topCurr).addClass('topfeat-active-image');
		jQuery('#topfeat-img-'+topCurr).fadeTo('500', 1);
		
		jQuery('#topfeat-control-'+topactiveImg).removeClass('topfeat-activeControl');
		topactiveImg = topCurr;
		jQuery('#topfeat-control-'+topactiveImg).addClass('topfeat-activeControl');

	}, 5000);
	
	jQuery('.topfeat-controller').mouseover(function () {
	
	clearInterval(topaniCar);
		// get ID of activated hero
		var topCurr = jQuery(this).attr('id').substring(16);

		
		// add/remove active class from slideshow images and fade in new one
		if (topCurr != topaniCar) {
			jQuery('#topfeat-img-'+topaniCar).removeClass('topfeat-active-image');
			jQuery('#topfeat-img-'+topCurr).css('opacity', 0.2);
			jQuery('#topfeat-img-'+topCurr).addClass('topfeat-active-image');
			jQuery('#topfeat-img-'+topCurr).fadeTo('500', 1);
			
			jQuery('#topfeat-control-'+topaniCar).removeClass('topfeat-activeControl');
			topaniCar = topCurr;
			jQuery('#topfeat-control-'+topaniCar).addClass('topfeat-activeControl');
		}
	});

	jQuery('.topfeat-controller a').click(function () {
		return false;
	});


	// ------------------------------------------------------------------------
	
	// Add odd/even classes to Tools search results, as the exp:weblog:entries is too complex to 'snippetise'
	
	$('div.serp-result:even').addClass('odd');



	// Start the Carousel on page : discovery/discovery_laboratories
		$(function(){
		$('#loopedSlider').loopedSlider({});
	});


	// ------------------------------------------------------------------------

	// To defeat crappy CMS glitch - if called on an element, and that element is empty (html() evaluates to "") element is hidden

	$('.hide_empty_element').each(function(){
		var content = $(this).html();
		if (content == "")	{
			$(this).parent('.empty_element_hider').hide();		
		}
	});


	// Homepage twitter
	$('#tweets').find('p:first').text('Loading Tweet...');
	var profile = 'CRT_News';
	$.getJSON('http://twitter.com/status/user_timeline/'+profile+'.json?count=3&callback=?',function(resp){
	  $('#tweets').find('p:first').text( resp[0].text ).tweetify();
	});
	
	
	// ------------------------------------------------------------------------
	
	// Research Tools => Clone Page Navigation	
	
	$('.paglinks').clone().appendTo('#second_nav');

	
}); // ENDS Document Ready




// Consoldiates all FlowPlayer calls and allows us to standardise Video Look & Feel across the site
// Basically nesting flowplayer function inside our own

function cc_do_flowplayer(target)	{

	flowplayer(target, "http://releases.flowplayer.org/swf/flowplayer.commercial-3.1.5.swf", {
	
		// product key
		key: '#$dfc1b234b4282b83b72',

			clip:	{
				autoPlay: false
			},
		    
		     canvas: { 
        		backgroundImage: 'url(../style/images/ui/videoBG.jpg)' 
    		}, 
			
			plugins: 	{ 
				
				controls: {
			    	backgroundColor: '#cad2e4',
			     	backgroundGradient: 'none',
			     	timeColor: '#043882'
			   	}
			}
	});	
}


// Licensing Opps auto-complete search functionality

function lookup(inputString) {

	if( inputString.length > 2 )	{

		if ( ! jQuery('#suggestions').hasClass('visible') )	{
			jQuery('#suggestions').addClass('visible');
			jQuery('#suggestions').html('<p class="loading-div">Searching...</p>');
			jQuery('#suggestions').fadeIn();

			jQuery.post("http://www.cancertechnology.com/licensing/search/&ajaxsearch=true", {kwd: ""+inputString+""}, function(data) { 
				// Do an AJAX call
				jQuery('#suggestions').html(data); // Fill the suggestions box
				}); 

		}

	} else {

		if ( jQuery('#suggestions').hasClass('visible') )	{
			jQuery('#suggestions').fadeOut();	
			jQuery('#suggestions').removeClass('visible');
		}

	}
}

// Google Analytics Tracking for Research Tools Area

function trackToolSearch(type,inputId)	{
	var searchTerm = jQuery('#'+inputId).attr('value');
	pageTracker._trackEvent('Tools Search', type, searchTerm);
}



// Twitter
//original at http://css-tricks.com/snippets/jquery/jquery-tweetify-text/
$.fn.tweetify = function() {
  return this.each(function() {
    $(this).html(
      $(this).html()
        .replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a target="_blank" rel="external" href="$1">$1</a>')
        .replace(/(^|\s)#(\w+)/g,'$1<a target="_blank" rel="external" href="http://search.twitter.com/search?q=#$2">#$2</a>')
        .replace(/(^|\s)@(\w+)/g,'$1<a target="_blank" rel="external" href="http://twitter.com/$2">@$2</a>')
    );
  });
}
