function toggle_visibility(id) {
   var e = document.getElementById(id);
   if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';
}

(function ($) {
	$(document).ready(function(){
		$("a.gallery").fancybox();

		$.fn.setCursorPosition = function(pos) {
			if ($(this).get(0).setSelectionRange) {
				$(this).get(0).setSelectionRange(pos, pos);
			} else if ($(this).get(0).createTextRange) {
				var range = $(this).get(0).createTextRange();
				range.collapse(true);
				range.moveEnd('character', pos);
				range.moveStart('character', pos);
				range.select();
	        }
	    }

		$('#site-search label, #page-search label, #dashboard-datepick label').each(function (i) {
			$(this).next('input').attr('value',$(this).html());
			$(this).hide();
		});

		$('#site-search input, #page-search input, #dashboard-datepick input').focus(function() {
			if($(this).prev('label').html() == this.value){
				$(this).val('').setCursorPosition(0);
			}
		});

		$('#site-search input, #page-search input, #dashboard-datepick input').keypress(function() {
			if($(this).prev('label').html() == this.value){
				this.value = '';
			}
		});

		$('#site-search input, #page-search input, #dashboard-datepick input').blur(function() {
			if(this.value == ''){
				this.value = $(this).prev('label').html();
			}
		});

		
		$(function () {
			
			var tabContainers = $('#content-tabs .single-tab');
			tabContainers.hide().filter(':first').show();
			var tab = $('<span class="tab-right" />');

			$('#content-tabs .tabmenu a').click(function () {
				tabContainers.hide();
				tabContainers.filter(this.hash).show();
				$('.tabmenu a').removeClass('active');
				$(this).addClass('active').append(tab);
			    return false;
			}).filter(':first').click();
		});



		$('#from, #to, #site-search-startdate, #site-search-enddate').datepicker({
			prevText:'«',
			nextText:'»'
		});



		$('#news-categories').selectbox();



		$('#glossary-tabs .tabmenu').after('<div class="top" />');
		$('#glossary-tabs .single-tab:last-child').after('<div class="bottom" />');



		$('#structure>li>ol>li>ol').parent().css({'padding':'0','-moz-border-radius':'0','-webkit-border-radius':'0','border-radius':'0','background':'none','-moz-box-shadow':'none','-webkit-box-shadow':'none','box-shadow':'none','border':'0'});



		$('#invest-menu>h1').each(function(){
			$(this).click(function(e){
				var list = $(this).next();
				var preview = $('#' + list.attr('id') + '-preview');
				var content = $('#' + list.attr('id') + '-content');

				if (list.is(':visible')) {
					list.slideUp();
					content.slideUp();
					preview.removeClass('open').animate({'height':'100px'});
					$('table',preview).css('visibility','visible')
					$(this).removeClass('open').addClass('closed').animate({'height':'76px'});
				} else {
					list.slideDown();
					content.slideDown();
					preview.addClass('open').animate({'height':'50px'});
					$('table',preview).css('visibility','hidden')
					$(this).removeClass('closed').addClass('open').animate({'height':'26px'});
				};
			});
		});



		var expand = true;
		var scroll = $('.preview table').width() - $('.preview').width();
		
		$('#expand-view a').click(function(e){
			e.preventDefault();
			if (expand == true) {
				$('#investments').animate({'width':'854px','left':'-281px'});
				$('#invest-graph').animate({'width':'714px','left':'-281px'});
				expand = false;
				$(this).css('background-position','-15px 50%');
				$('#range').slider('option', 'max', scroll-281);
			} else {
				$('#investments').animate({'width':'573px','left':'0'});
				$('#invest-graph').animate({'width':'433px','left':'0'});
				expand = true;
				$(this).css('background-position','6px 50%');
				$('#range').slider('option', 'max', scroll);
			}
		});
		
		// Range slider
		$('#range').slider({
			min:0,
			max:scroll,
			step:1,
			value:0,
			slide:function(event, ui){
				$('#investment-scope-content, .investment-scope-content-2, .preview, .content, .scrolltable, .tablecontainer, .tableoverflow, #total-investments').scrollLeft(ui.value);
			}
		});

		var tooltip = null,
			ttto = null;
		var hideTT = function(){
			$(tooltip).fadeOut(function(){ $(this).remove(); });
		};

		$('.content .chart[title]')
			.mouseenter(function(e){
				clearTimeout(ttto);
				if (typeof $(this).data('tt') == 'undefined') {
					//hideTT();
					$(this).data('title', $(this).attr('title'));
					$(this).attr('title', '');

					var pos = $(this).offset();
					tooltip = $('<div class="tooltip">' + $(this).data('title') + '</div>');
					$(this).data('tt', tooltip);
					$('body').append(tooltip);
					tooltip.css({'left':pos.left + 'px','top': pos.top + 'px'}).mouseenter(function(){ clearTimeout(ttto); }).mouseleave(function(e){
						ttto = setTimeout(hideTT, 20);
					});
				}
			})

			.mouseleave(function(e){
				ttto = setTimeout(hideTT, 20);
				$(this).attr('title', $(this).data('title'));
			});


			//Set default open/close settings
			$('.acc_container').hide(); //Hide/close all containers
			//$('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container

			//On Click
			$('.acc_trigger').click(function(){
				$('.acc_trigger').removeClass('active').next().slideUp();
				if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
					$('.acc_trigger').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
					$(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
				}
				return false; //Prevent the browser jump to the link anchor
			});
		
		// Tooltip
		/*
		$("img[title]").tooltip({
		   offset: [10, 2],
		   effect: 'slide'
		}).dynamic({ bottom: { direction: 'down', bounce: true } });
		*/
	});
	
}) (jQuery);
