﻿jQuery.noConflict();
jQuery(document).ready(function($) {
		
	$('.explore-nav').hide();
	$('.hp-explore-nav').show();

	//Rotates the visible stories on the javascript feature area (On the homepage, up the top of division landing pages etc)
	if ($('.rotating-feature-menu').length>0) {							
		var $featureMenu = $('.rotating-feature-menu').find('ul > li')
		//Gets the li the user has moused over and makes that li the active one
		$featureMenu.mouseover(function() {
			$featureMenu.removeClass('active');
			var $index = $(this).addClass('active').index();
			//Makes the image and teaser text visible
			$('.feature-teaser').hide().eq($index).show();
		});
	};
	
	//Turns a selected list into a dynamic menu
	var $dynamicMenu = $('.dynamic-menu');
	if ($dynamicMenu.length>0) {
		$dynamicMenu.addClass('treeview');
		$("ul.treeview").simpletreeview();
	};

	//Toggles the Show/Hide full channel Index heading
	if($('.content-cluster').length>0) {
		var $channelTitle = $('.channel-introduction').find('h1').text();
		var $channelToggleLink = $('.content-index').find('h3 >a');
		var $channelHeadingLink = $('.content-index').find('.handle');
		//Sets the heading
		$channelToggleLink.text('Show Full '+$channelTitle+ ' Index');
		//Toggles the heading
		$channelHeadingLink.toggle (
			function () {
				$channelToggleLink.text('Hide Full '+$channelTitle+ ' Index');
			},
			function () {
				$channelToggleLink.text('Show Full '+$channelTitle+ ' Index');
			;});
	}
	
	//Moves down page content and displays pop out teaser text
	var $ChannelHighlights = $('.channel-highlights');
	if($ChannelHighlights.length>0) {
		//Hides 'Learn more>' JS alternative
		$ChannelHighlights.find('.headline').height(30).find('a').hide();
		$('.vert-teaser').mouseover(function() {								 
			//Moves down page content and displays teaser text	
   			var thisTeaser = $(this).addClass('active');
			$Originalheight = thisTeaser.height();
			thisTeaser.height(thisTeaser.find('.text').height()+$Originalheight)
			//Hides the other headings in the div
			thisTeaser
				.closest('.channel-highlights')
				.find('.headline')
				.css('visibility','hidden');
		});
		$('.vert-teaser').mouseout(function() {
			//Moves the page content back up and hides the teaser text								
			var thisTeaser = $(this).removeClass('active');
			thisTeaser.height($Originalheight);
			//Displays the other headings in the div
			thisTeaser.closest('.channel-highlights').find('.headline').css('visibility','visible');
		});
	};
	var $tabs=$('#tabs');
	if($tabs.length>0) {
		$(function() {
			// Hides the h4 tags that are meant to only be visible when JavaScript has been turned off
			$tabs.find('h4').hide();
			$tabs.tabs();
			
		});
	};

	ContentLength=550;
	//Carousel on the division pages.
	if($('.information-carousel').length>0) {
		
		$('.carousel-control,.carousel-more').show();
		$('.carousel-container').css({'position':'relative','width':'450px','height':'105px','overflow':'hidden'})
		var $delay=1000;
		var $DisabledButtonDelay=1050;
		
		//When the page loads, this puts the teasers into position
		$ActiveSlide = $('.information-carousel').find('.ui-tabs-panel');
		$ActiveSlide.each(function(i) {
			var SlideNumber = $(this).find('.carousel-content').length;
			var left = 0;
			for (i=0;i<SlideNumber;i++) {
				$(this).find('.carousel-content').eq(i).css('left',left+'px');
				left=left+ContentLength;
			}
		});
		//Function moves the blue dot to indicate which item in the carousel the user is currently looking at 
		function carouselDots(TeaserIndicatorNo) {
			var TeaserIndicatorNo = TeaserIndicatorNo;
			var ActivePanelList = $('.ui-tabs-panel:not(.ui-tabs-hide)').find('.jump-content > ul');
			//Removes the indicator from the previous teaser
			ActivePanelList
				.find('.active-indicator')
				.removeClass('active-indicator')
				.find('img')
				.attr("alt","Grey dot");
												  
			//Indicates the current teaser
			ActivePanelList
				.find('li')
				.eq(TeaserIndicatorNo)
				.addClass('active-indicator')
				.find('img')
				.attr("alt","Blue dot");
		}
		
		//Function spins the teasers in the carousel
		function moveCarousel (direction,divName,number) {
			var $TabPanel = $('.information-carousel').find('.ui-tabs-panel:not(.ui-tabs-hide)');
			var $Tabs = $('.information-carousel').find('.ui-state-default');
			var ActiveTab = $TabPanel.find('.carousel-container');
			var ActivePanelList = $TabPanel.find('.jump-content > ul >li');
			var CarouselContent = ActiveTab.find('.carousel-content');
			var number = 2;
			//alert(ActivePanelList.length);
			var maxLeft = (CarouselContent.length*ContentLength)-ContentLength;
			var Button = $TabPanel.find('.'+divName+'');
			//Disables the button until the teasers have finished moving
			if(!Button.hasClass("disabled")){
				Button.addClass('disabled');
				//Disables the tabs while the carousel is moving
				$Tabs.addClass('ui-state-disabled');
				//Moves the carousel forward
				if (direction == "forward"){
					CarouselContent.each(function (index) {
						var leftVal = this.style.left;		
						if(leftVal == "-"+ContentLength+"px"){
							this.style.left = maxLeft+"px";
						}
					});
					CarouselContent.animate({left: "-="+ContentLength+"px"},$delay);
				} else {
					//Moves the carousel backward
					CarouselContent.each(function (index) {
						var leftVal = this.style.left;	
						if(leftVal == maxLeft+"px"){
							this.style.left ="-"+ContentLength+"px";
						}});
					CarouselContent.animate ({left: "+="+ContentLength+"px"},$delay);
				
				}
				setTimeout(function() {
					//Moves the dots on the bottom of the carousel to indicate the current teaser		
					CarouselContent.each(function (index) {
					if(this.style.left == "0px") {
						var TeaserIndicatorNo=index;
						carouselDots(TeaserIndicatorNo)
					};
												   
				});					
				//Enables the tabs and left and right buttons	
				$Tabs.removeClass('ui-state-disabled');
				Button.removeClass('disabled');
				}, $DisabledButtonDelay); 			}
		}
		//Tells the moveCarousel function to move the carousel backwards
		$(".back-control").click( function functionName($delay) {
			moveCarousel('backward','back-control');
		});
		//Tells the moveCarousel function to move the carousel forwards
		$(".forward-control").click(function () {
			moveCarousel('forward','forward-control');								  
		});
		//Allows the user to navigate using the dots on the bottom of the carousel
		$(".jump-content > ul > li").click(function () {
			var $TabPanel=$('.information-carousel').find('.ui-tabs-panel:not(.ui-tabs-hide)');
			var ActivePanelList=$TabPanel.find('.disabled')
			//Checks to see if the buttons have been disabled (because the carousel is moving)
			if (ActivePanelList.html() == null) {
				var PanelLocation = $TabPanel.find('.carousel-container')
				var CarouselContent = PanelLocation.find('.carousel-content');
				var CarouselLength = CarouselContent.length;
				var maxLeft = (CarouselContent.length*ContentLength)-ContentLength;
				var activeIndex = $(this).index();
				//Resets the teasers left alignment to show the one the user has selected
				PanelLocation.find('.carousel-content').eq(activeIndex).css('left','0px');
				var TeaserIndex=activeIndex+1;
				var Left = 0;
				for (i=0;i<CarouselLength-1;i++) {	
					if (TeaserIndex == CarouselLength) {
						TeaserIndex = 0;
					}
					if (i != CarouselLength-2) {
						Left = Left+ContentLength;
					} else {
						Left =-ContentLength;
					}
					PanelLocation.find('.carousel-content').eq(TeaserIndex).css('left',Left+'px');
					TeaserIndex=TeaserIndex+1;
				}							 
			carouselDots(activeIndex)
			}
		});
	};

	//Function closes the Explore CSIRO popup
	function closePopup() {
		$(".explore-mega-menu").hide();
		$('.explore-active').remove();
	}
	
	//Closes the Explore CSIRO pop up when a user presses the Escape key
	$(document).bind("keydown",checkKeycode);
 	function checkKeycode(e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
 		else if (e) keycode = e.which;
 		if(keycode == 27) {
 			closePopup()
 		}
 	}
	
	//Closes the Explore CSIRO popup menu when the user clicks on the X image
	$(".close-popup").click(function () {						
		closePopup()
	});
	
	//Opens the Explore CSIRO menu and closes it if a user clicks outside of the div
	$(document).click(function(event) {
		var MegaMenu = $(".explore-mega-menu")
		var $parentClass = $(event.target).hasClass('explore-active');
		var $CloseMegaMenu = $(event.target).hasClass('explore-mega-menu');
		//If the explore CSIRO pop up window is not already active then show it
	  	if ($(event.target).closest('.explore-csiro').get(0) != null)  { 
	   		MegaMenu.show();
			$contentTitle = " ";
			MegaMenu.find('.explore-nav').show();
			$('<div class="explore-active">')
				.insertBefore($(".page"))
				.css({'height':$(document).height()+'px','width':$(document).width()+'px'});
				
	  		return false;
	   	} else if($parentClass||$CloseMegaMenu) {  
			//If a user clicks anywhere outside the Explore CSIRO menu close the pop up window
		   closePopup()
	   	}
	});
	//If the window is resized - close the pop up window
	$(window).bind('resize', function(){
	   closePopup()
	});
	
	//When a user clicks inside the search box make the Searc+'area' words disappear 
	var $contextualSearch = $('.contextual-search');
	if($contextualSearch.length>0) {
		$contextualSearch.find('input').click(function() {
			$(this).val('');
		});
	}

	//Do the same as above but for the global search
	$('.main-search').find('input').click(function() {
			$(this).val('');
	});
	
	//If javascript is working the inline media's preview should show
	var $preview = $('#preview');
	if($preview.length>0) {
		$preview.show();
	}
	
	//Social Bookmarking links
	if ($('.share-bookmarks').length>0) {
		var $contentTitle = $('.main-content-area').find('h1').html();
		var $pageURL=window.location.href;
                var $metadataDescription = $("meta[name='DC.Description']").attr('content');

		var $diggLink=$('.digg').find('a').attr('href','http://digg.com/submit?phase=2&url='+$pageURL+'&title='+$contentTitle);
		var $deliciousLink=$('.delicious').find('a').attr('href','http://www.delicious.com/save?jump=yes&v=2&url='+$pageURL+'&notes=&tags=&title='+$contentTitle);
		var $stumbleUpon=$('.stumbleupon').find('a').attr('href','http://www.stumbleupon.com/submit?url='+$pageURL+'&title='+$contentTitle);
		var $facebook=$('.facebook').find('a').attr('href','http://www.facebook.com/sharer.php?u='+$pageURL+'&t='+$contentTitle);
		var $twitter=$('.twitter').find('a').attr('href','http://twitter.com/?status=Currently%20Reading%20- '+$pageURL);
		var $reddit=$('.reddit').find('a').attr('href','http://www.reddit.com/submit?url='+$pageURL+'&title='+$contentTitle);
		var $linkedIn=$('.linkedin').find('a').attr('href','http://www.linkedin.com/shareArticle?mini=true&url='+$pageURL+'&title='+$contentTitle+'&summary='+$metadataDescription+'&source=http://www.csiro.au/');
                var $newsvine=$('.newsvine').find('a').attr('href','http://www.newsvine.com/_wine/save?popoff=1&u='+$pageURL+'&h'+$contentTitle);
		var $email=$('.mail').find('a').attr('href','mailto:'+' '+'?subject='+$contentTitle+'&body='+$contentTitle);
	};

	
	//Contacts Email Popup
	//Sets the contentTitle variable with the pages title. It is sourced from two different locations depending on whether its a 
	//standard or landing page
	if ($('.channel-introduction').length>0) {
		var $Title=$('.channel-introduction');
	} else if ($('.main-content-area').length>0) {
		var $Title = $('.main-content-area');
	}
	
	if ($('.main-content-area').length>0) {
		$contentTitle = $Title.find('h1').html();
		$('.contact-container').find('a').click(function() {
				var $ThisLink=$(this);										 
				EmailText($ThisLink)
		});
		$('.contact-inline-email').find('a').click(function() {
				var $ThisLink=$(this);
				EmailText($ThisLink)
		});
	}
	
	//Function sets the Email To,Subject and body fields
	function EmailText(ThisLink) {
		$ThisLink=ThisLink;
		//Original Link text captured because IE replaces text with href value
		$OriginalLinkText=$ThisLink.html();
		var $contactEmail=$ThisLink.attr('href','mailto:'+$ThisLink.html()+'?subject='+$contentTitle+'&body='+$contentTitle);
		//Link text restored to what it should be
		$ThisLink.text($OriginalLinkText);
	}
	//Only sets the mail to field if the explore csiro enquires contact is clicked
	$('.explore-nav').find('.contact-container > p > a').click(function() {
			var $ThisLink=$(this);
			//Original Link text captured because IE replaces text with href value
			$OriginalLinkText=$ThisLink.html();
			var $contactEmail=$ThisLink.attr('href','mailto:'+$ThisLink.html());
			//Link text restored to what it should be
			$ThisLink.text($OriginalLinkText);
	});
	
});
