$(function(){

	$('.menu li a').live('click',function(){
		if ($(this).parent().hasClass('active')) return false;
		
		$('.menu li a').parent().removeClass('active');
		$(this).parent().addClass('active');
		
		$rid = $(this).attr('id').substr(1);
      $all = $('.menu li a'); 
      $worldcl = $all[0].getAttribute('id').substr(1);
		if($rid != $worldcl) { // concrete region is selected
         $('.menu').css('left','24px');
         $('.menu').css('top','50px');
         for($i=1; $i < $all.length; $i++) {
            if ($all[$i].getAttribute('id') != 'r'+$rid)
               $all[$i].parentNode.style.display = 'none';
         }
      } else {
         $('.menu').css('left','225px');
         $('.menu').css('top','16px');
         for($i=1; $i < $all.length; $i++) {
            $all[$i].parentNode.style.display = 'block';
         }
      }      
		$('.region.current').removeClass('current');
		$('#region'+$rid).addClass('current');
		return false;
	});
	

	$('.country').live('click',function(){
		$('#photo_container, #viewer h2, #viewer p').empty();
		$cid = $(this).attr('id').substr(7);
		$('#photos'+$cid).children().clone().hide().appendTo('#photo_container');
		$('#photo_container').children('img:first').show();
		$('#viewer h2').text($(this).attr('title'));
		$('#viewer p').text($('#photo_container img:first').attr('alt'));
		if ($('#photo_container').children('img').size() >1 )
		{
			$('#viewer .next').show();
			$('#viewer .prev').show();
		}
		else
		{
			$('#viewer .next').hide();
			$('#viewer .prev').hide();
		}
		$('#viewer').show();
		
		return false;
	});

	currentclick=0;	
	
	$('#photo_container').bind('click', nextPhoto);
	
	$('#viewer .next').bind('click', nextPhoto);
	$('#viewer .prev').bind('click', prevPhoto);
	
	function prevPhoto(){
		
		if (currentclick == 1) 
		{
			currentclick = 0;
			return false;
		}
		currentclick = 1;
		
		$photo_conainer = $('#photo_container');
		
		$count_photos = $photo_conainer.children().size();
		$curphoto = $photo_conainer.children(':visible');
		$curphoto_idx = $curphoto.index();
		$curphoto.hide();
		
		if ($curphoto_idx-1 < 0)
			$next_photo_idx = $count_photos-1;
		else
			$next_photo_idx = $curphoto_idx-1;
		
		$next_photo = $photo_conainer.children(':eq('+$next_photo_idx+')');
		$('#viewer p').text($next_photo.attr('alt'));
		$next_photo.show();
		
		return false;
	}
	
	function nextPhoto(){

		if (currentclick == 1) 
		{
			currentclick = 0;
			return false;
		}
		currentclick = 1;
		
		$photo_conainer = $('#photo_container');
		
		$count_photos = $photo_conainer.children().size();
		$curphoto = $photo_conainer.children(':visible');
		$curphoto_idx = $curphoto.index();
		$curphoto.hide();
		
		if ($curphoto_idx+1 > $count_photos-1)
			$next_photo_idx = 0;
		else
			$next_photo_idx = $curphoto_idx+1;
		
		$next_photo = $photo_conainer.children(':eq('+$next_photo_idx+')');
		$('#viewer p').text($next_photo.attr('alt'));
		$next_photo.show();
		
		return false;
	}
   
	
	$('.close_fotos').live('click',function(){
		$(this).parent().hide();
		return false;
	});
   
/* Futher lines code for a gallery on a charity page  */
   function nextPhotoPlus(){

		if (currentclick == 1) 
		{
			currentclick = 0;
			return false;
		}
		currentclick = 1;
		
		$photo_conainer = $('#photo_container');
		
		$count_photos = $photo_conainer.children().size();
		$curphoto = $photo_conainer.children(':visible');
		$curphoto_idx = $curphoto.index();
		$curphoto.hide();
		
		if ($curphoto_idx+1 > $count_photos-1)
			$next_photo_idx = 0;
		else
			$next_photo_idx = $curphoto_idx+1;
		
		$next_photo = $photo_conainer.children(':eq('+$next_photo_idx+')');
		$('#viewer p').text($next_photo.attr('alt'));
      
      var imh = $next_photo.attr('height');
         
      $('#viewer').css('height', imh + 150+'px');
      $('#photo_container').css('height', imh + 50 +'px');
		
      $next_photo.show();
		
		return false;
	}
   
   function prevPhotoPlus(){
		
		if (currentclick == 1) 
		{
			currentclick = 0;
			return false;
		}
		currentclick = 1;
		
		$photo_conainer = $('#photo_container');
		
		$count_photos = $photo_conainer.children().size();
		$curphoto = $photo_conainer.children(':visible');
		$curphoto_idx = $curphoto.index();
		$curphoto.hide();
		
		if ($curphoto_idx-1 < 0)
			$next_photo_idx = $count_photos-1;
		else
			$next_photo_idx = $curphoto_idx-1;
		
		$next_photo = $photo_conainer.children(':eq('+$next_photo_idx+')');
		$('#viewer p').text($next_photo.attr('alt'));
      
      var imh = $next_photo.attr('height');
         
      $('#viewer').css('height', imh + 150+'px');
      $('#photo_container').css('height', imh + 50 +'px');
      
		$next_photo.show();
		
		return false;
	}
   
   function blurBack(){
      var value = 4;
      var hgt = document.body.scrollHeight + 'px';
      var wdt = document.body.scrollWidth + 'px';
      if (document.getElementById('blurDiv')) $('#blurDiv').remove();
      var bl = document.createElement('DIV');
      bl.id = 'blurDiv';
      bl.style.width = wdt;
      bl.style.position = 'absolute';
      bl.style.top = 0;
      bl.style.left = 0;
      bl.style.backgroundColor = '#006CB0';
      bl.style.height = hgt;
      bl.style.opacity = value/10;
      bl.style.filter = 'alpha(opacity=' + value*10 + ')';
      document.body.appendChild(bl);
   }
   
   var currentclick_p = 0;
   
   function prepareCont(dir){
   	if (currentclick_p == 1) 
		{
			currentclick_p = 0;
			return false;
		}
		currentclick_p = 1;
      
      var active = $('#photo_container img[val="a"]');
      var imh = 0;
      if (dir && dir > 0) {
         active.attr('val','');
         active.next().attr('val','a');         
         imh = active.next().attr('height');
      }
      if (!dir) {
         imh = active.attr('height');
      }
      
      $('#viewer').css('height', imh + 150+'px');
      $('#photo_container').css('height', imh + 50 +'px');
   }
   
   $('.pica').bind('click', function(){ 
      blurBack();
      var photo_container = document.getElementById('photo_container').getElementsByTagName('img');
      var ind = $(this).attr('ref')*1 - 1;
      photo_container[ind].style.display = 'block';
      photo_container[ind].val = 'a';
      $('#viewer').css('height', photo_container[ind].height + 150+'px');
      $('#photo_container').css('height', photo_container[ind].height + 50 +'px');
      $('#viewer p').text('');
      $('#viewer').show();
   });
   $('.close_fotos_plus').bind('click',function(){
		$(this).parent().hide();
      $('#photo_container img').each(function(){ $(this).hide(); $(this).attr('val','') });
      $('#blurDiv').remove();
		return false;
	});
   $('#viewer .next_plus').bind('click', function(){ nextPhotoPlus();});
	$('#viewer .prev_plus').bind('click', function(){ prevPhotoPlus();});
	
});
