$(document).ready(function() {

	
	if ($('.gtranslate').length > 0) {
		$('.gtranslate').click(function() {
			$('#google_translate_element').stop(true, true).slideToggle(200);
			$('.goog-te-menu2').click(function() {
				$('#google_translate_element').hide();
			});
		});
	}
	
	
	if ($('#print_page').length > 0) {
		$('#print_page').click(function() {
			window.print();
  			return false;
		});
	}
	
	
	
	// to be amended with ajax
	if ($('#font_size').length > 0) {
		$('#font_size li').click(function() {
			$(this).siblings().each(function() {
				$(this).removeClass('active');			
			});
			$(this).addClass('active');
			var id = $(this).attr('id').split('_');
			$.cookie("accessibility_font", id[1], { expires: 31, path: '/' });
			location.reload();			
		});
	}
	


	if ($('.viewing_trigger').length > 0) {		
		$('.viewing_trigger').click(function() {
			var w = $('#viewing_options').width();
			$('#viewing_items').width(w);
			$('#viewing_items').stop(true, true).slideToggle(200, function() {
				if ($(this).is(":visible")) {
					$('#viewing_items dl').children('dt').click(function() {
						var id = $(this).attr('id');
						$.cookie("accessibility_view", id, { expires: 31, path: '/' });
						location.reload();
					});
				}
			});
		});
	}
	
	
	$('#header').cycle({
		fx:'fade',
		speed:1200,
		timeout:10000,
		next:'#header',
		manualTrump:false,
		pause:1
	});
	
	
	if ($('.news_feed').length > 0) {
		$('.news_feed').cycle({
			fx:'scrollUp',
			speed:800,
			timeout:5000,
			pause:1
		});
	}
	
		
	if ($('#navigation').length > 0) {
		$('#navigation li').hover(function() {
			if ($(this).attr('id') != 'home' && $(this).parent().attr('id') == 'navigation' && $(this).has('dl')) {
				
				var wr = $(this).children('dl');
				var wo = wr.width();
				var wi = wo - parseInt(30); // padding of the left and right side of the a tag
				
				wr.children('dt').width(wo);
				wr.children('dt').children('a').width(wi);
								
				$(this).siblings().removeClass('current');			
				$(this).addClass('current');
				$(this).children('dl').stop(true, true).fadeIn(100);
			}
		}, function() {
			if ($(this).attr('id') != 'home' && $(this).parent().attr('id') == 'navigation' && $(this).has('dl')) {
				$(this).removeClass('current');
				$(this).children('dl').stop(true, true).fadeOut(100);
			}
		});
	}
	
	
	
	if ($('.pm').length > 0) {
		$('.pm').click(function() {
			var wrapper = $(this).parent('.heading');
			var id = $(this).attr('id');
			if ($(this).hasClass('minus')) {
				$(this).removeClass('minus');
				$(this).addClass('plus');
				$(this).parent().next('.block_right_inside').slideUp(200, function() {
					wrapper.addClass('heading_both_rounded');					
					$.cookie("panel[" + id + "]", 'closed', { expires: 31, path: '/' });
				});
			} else {
				$(this).removeClass('plus');
				$(this).addClass('minus');
				wrapper.removeClass('heading_both_rounded');
				$(this).parent().next('.block_right_inside').slideDown(200);
				$.cookie("panel[" + id + "]", 'open', { expires: 31, path: '/' });				
			}
		});
	}
	
	
	
	
	
	if ($('.hdpm').length > 0) {
		$('.hdpm').click(function() {
			var id = $(this).attr('id');
			if ($(this).hasClass('minus')) {
				$(this).removeClass('minus');
				$(this).addClass('plus');
				$('.' + id).slideUp(200, function() {					
					$.cookie("panel[" + id + "]", 'closed', { expires: 31, path: '/' });
				});
			} else {
				$(this).removeClass('plus');
				$(this).addClass('minus');
				$('.' + id).slideDown(200);
				$.cookie("panel[" + id + "]", 'open', { expires: 31, path: '/' });				
			}
		});
	}
	
	
	
	if ($('#poll').length > 0) {
		$('#form_poll label').click(function() {
			var v = $(this).children('input').val();
			$.ajax({
				type: "POST",
				url: '/poll/action/submit',
				data: ({ id : v }),
				dataType: 'json',
				success: function(data) {
					if (data.error) {
						$('#poll').slideUp(200, function() {
							$(this).html(data.message).slideDown(200);						
						});
					} else {
						$.cookie("poll", data.poll, { expires: 365, path: '/' });
						window.location.replace("/poll.html");
					}					
				}
			});	
			return false;				
		});
	}
	
	
	
	
	if ($('#alert').length > 0) {
		$('body').prepend('<a class="fb" href="#alert"></a>');
		$('.fb').fancybox({
			'overlayOpacity'	: 0.9,
			'overlayColor'		: '#000',
			'autoScale' 		: true,
			'scrolling' 		: 'no',
			'onStart'			: function() {
				$("#alert").show();
			},
			'centerOnScroll'	: true,
			'speedIn'			: 200, 
			'speedOut'			: 200,
			'hideOnOverlayClick': true,
			'onClosed'			: function() {
				$('.fb').remove();
				$('#alert').remove();
			}
		}).trigger('click');
	}
	
	
	
	
	
	
	if ($('.gallery_single_inside').length > 0) {
		$('.gallery_single_inside').after('<div class="gallery_single_navigation">').cycle({
			fx:'fade',
			speed:1200,
			timeout:10000,
			manualTrump:false,
			pause:1,
			pager:  '.gallery_single_navigation'
		});
	}
	
	
	
	
	
	
	if ($('.gallery_large').length > 0) {
		$('.gallery_large').fancybox({
			'overlayOpacity'	: 0.9,
			'overlayColor'		: '#000',
			'autoScale' 		: true,
			'scrolling' 		: 'no',
			'centerOnScroll'	: true,
			'speedIn'			: 200, 
			'speedOut'			: 200,
			'hideOnOverlayClick': true
		});
	}	
	
	
	
	
   	if ($('#totop').length > 0) {
	    $('#totop').click(function(){
	        $('html, body').animate({scrollTop:0}, 'slow');
	        return false;
	    });
    }
    
    
    
    if ($('.nav_tooltip').length > 0) {
    	$('.nav_tooltip').hover(function() {
    		$(this).children('.nav_tooltip_target').stop(true, true).fadeIn(100);
    	}, function() {
    		$(this).children('.nav_tooltip_target').stop(true, true).fadeOut(100);
    	});
    }
    
    
    
    
    if ($('.sitemap').length > 0) {
    	$('.sitemap_heading a.trigger').click(function() {
    		if ($(this).parent().hasClass('close')) {
    			$(this).parent().removeClass('close');
    			$(this).next('.sitemap_list').slideUp(200);
    		} else {
	    		$('.sitemap_list').each(function() {
	    			if ($(this).parent().hasClass('close')) {
	    				$(this).slideUp(200);
	    				$(this).parent().removeClass('close');
	    			}
	    		});
	    		$(this).parent().addClass('close');
	    		$(this).next('.sitemap_list').slideDown(200);
	    	}
	    	return false;
    	});    	
    }
	
	

});
