// ON DOM LOAD

$(function(){
	cimexGlobal.minorTweaks();
	cimexGlobal.setSearchKeywordField();
	cimexGlobal.setMapSearchField();
	cimexGlobal.navGlow();
	$('#bookmark-component ul li a').each(function(){
		cimexGlobal.initTooltip(this);
	});
	$('.follow-links a').each(function(){
		cimexGlobal.initTooltip(this);
	});
	cimexGlobal.animatePointers();
	cimexGlobal.related_topics_tooltip();
	cimexGlobal.tag_sort_switcher();
});

//  FUNCTIONS 

	var cimexGlobal = {		
		
		minorTweaks: function() {
			// hide input value for search submit button
			$("input[value='submit']").attr('value', '');
		},
		
		setSearchKeywordField: function(){
			if ( ! $('.search input[type=text]').val() ) {
				$('.search input[type=text]').attr({ value: 'Search' }).focus(function(){
					if($(this).val()=='Search'){$(this).val("");}
					}).blur(function(){
						if($(this).val()==""){$(this).val('Search');}
					});				
			}
		},

		setMapSearchField: function(){
			$('#map-text').attr({ value: 'Enter your postcode or location' }).focus(function(){
				if($(this).val()=='Enter your postcode or location'){$(this).val("");}
				}).blur(function(){
					if($(this).val()==""){$(this).val('Enter your postcode or location');}
				});
		},
		
		
		animatePointers: function(){
			$('.pointers li a').hover(
				function(){
					$(this).parents('li').animate({
						backgroundPosition: '2px 3px',
						easing: 'easeOutSine'
						}, 100);
			 },
			function(){
				$(this).parents('li').animate({
						backgroundPosition: '0px 3px',
						easing: 'easeOutSine'
						}, 100);
			});
		},
		
		navGlow: function(){
			// filter out selected item
			var nav = $('ul#nav li').filter(function(i) {
			                  return $(this).attr("class") == '' || $(this).attr("class") == 'first' ||  $(this).attr("class") == 'last';
			          });
			// attach hover to anchor tags
			nav.children('a').hover(
				function(){
					 $(this).animate({
						color: '#fff'
					 }, 500);
				},
				function(){
					 $(this).animate({
						color: '#999'
					 }, 500);
				});
		},
		
		
		init_scrollable: function(list_count) {
	 
			$("div.scrollable").removeClass('nojs').scrollable({
				size: list_count,
				items: '.scroll-items',  
				hoverClass: 'hover',
				easing: 'easeInOutQuad',
				loop: false,
				interval: 0,
				speed: 500,
				clickable: false
			});

			$('.item-description')
				.hover(function(){
					$(this).css({'cursor':'pointer'})
				})	
				.click(function(){
					var anchor = $(this).prev('a').attr('href');
					location.href = anchor;
				});
		},

		fancyBox: function(fancyLink) {
			fancyLink = $(fancyLink);
			fancyLink.fancybox({ 
				'zoomSpeedIn': 300, 
				'zoomSpeedOut': 300, 
				'zoomOpacity':true, 
				'overlayShow': true, 
				'overlayOpacity':0.6 
			}); 
		},

		correspondingRollovers: function(divContainingRollovers) {

			divContainingRollovers = $(divContainingRollovers);

			divContainingRollovers.each(function(){
				$(this).find('a').each(function(){
					leftList = $('#topics ul');
					$(this).hover(function(){
						leftList.find('li.' + $(this).attr('class')).addClass('selected')},
						function(){
							leftList.find('li.' + $(this).attr('class')).removeClass('selected')
						});
					});
			});
		},

		initTooltip: function(tooltipString) {

			tooltipTrigger = $(tooltipString);
			tooltipTrigger.tooltip({ 

			 bodyHandler: function() { 
				  return $($(this).attr("rel")).html(); 
			 }, 
			 fixPNG: true,
			 fade: 250,
				top: 20, 
				left: -40,
			 
			 showURL: false 
			});
		},
		
		tag_sort_switcher: function() {

			$items_popularity = new Array();
			$items_alphabetical = new Array();
			$('#top_tags li').each(function(){
				$items_popularity.push($(this).html());
				$items_alphabetical.push($(this).html());
			});
			$items_alphabetical = $items_alphabetical.sort();

			$('#tag_sort_pop').click(function(){
				$.cookie('top_tags_sort', null);
				for($x=0; $x < $items_popularity.length; $x++){
					$('#top_tags li:eq('+$x+')').html($items_popularity[$x]);
				}
				$(this).addClass('selected');
				$('#tag_sort_alpha').removeClass('selected');
				$.cookie('top_tags_sort', 'popularity', { path: '/' } );
				return false;
			});
			
			$('#tag_sort_alpha').click(function(){
				$.cookie('top_tags_sort', null);
				for($x=0; $x < $items_alphabetical.length; $x++){
					$('#top_tags li:eq('+$x+')').html($items_alphabetical[$x]);
				}
				$(this).addClass('selected');
				$('#tag_sort_pop').removeClass('selected');
				$.cookie('top_tags_sort', 'alphabetical', { path: '/' } );
				return false;
			});
			
			if ($.cookie('top_tags_sort') && $.cookie('top_tags_sort') == 'alphabetical'){
				$('#tag_sort_alpha').click();				
			}

		},
		
		related_topics_tooltip: function() {
			
			$related_topics = $('p.related-topics');
			
			if ($related_topics.length > 0)
			{
				// Hide all tag links
				//
				$('span.taglinks').hide();
				
				// Equalise tooltip positions to heights 
				// of boxes
				//
				$max_height = 0;
				$heights = new Array();
				$('#search-results li').each(function( i ){
					$heights[i] = $(this).innerHeight();
					$max_height = ( $heights[i] > $max_height ) ? $heights[i] : $max_height ;
				});
				$('#search-results li').each(function( i ){
					$height_diff = $max_height - $heights[i];
					$(this).css({ 'height':$max_height });
					$(this).find('p.related-topics').css({ 'margin-top':$height_diff });
				});
				
				// 1. Move all taglinks to outside the flow 
				//    of the document
				// 2. Give them each a unique ID
				// 3. Set their positions
				//
				$related_topics.each(function( i ){
					$tag_span_id = 'taglinks_'+i;
					$tag_span = $(this).find('span.taglinks:first');

					$offset = $(this).offset();
					$top = $offset.top + 23;
					$left = $offset.left + 10;

					$tag_span.attr('id',$tag_span_id).css({ 'top':$top, 'left':$left });

					$('body').prepend($tag_span.clone());
					$tag_span.remove();		
				});
				
				// Ensure the taglinks remain visible while 
				// the mouse is over them
				//
				$('span.taglinks').each(function(){
					$(this).hover(
						function() { $(this).show(); },
						function() { $(this).hide(); } 
					);
				});
				
				// Set the show/hides for each taglink span
				// when the summary is hovered over
				//
				$related_topics.each(function( i ){
					$(this).hover(
						function(){
							$tag_span_id = 'taglinks_'+i;
							$('#'+$tag_span_id).show();
						},
						function(){
							$tag_span_id = 'taglinks_'+i;
							$('#'+$tag_span_id).hide();
						}
					);
				});
			}
		}

	};