	
	var context_menu_data_photos = new Array();
	var context_menu_data_names = new Array();
	var context_menu_instance = null;
	
	var list_photos_big = new Array();

	var showId = null;
  	var timer = 0;
  	
	function getActorId(element){
		if(	element.nodeName.toLowerCase() == 'span' || element.nodeName.toLowerCase() == 'img'){
			element = element.up();
		};
		actor_id = element.id.substring(element.id.lastIndexOf('-')+1);
		return actor_id;
	}
  	
  	/**
  	 * creates an instance of LivePipe ContextMenu
  	 * 
  	 * @param mode wheter photo or name (creates slightly different menus)
  	 * @param actor_id 
  	 * @return bool success (true) or error (false)
  	 */
  	function createContextMenu(mode,event){
  		
  		var element = event.element();
		actor_id = getActorId(element);
  		
  		var data = null;
  		if(mode == "photo"){
  			offsetX = offsetY = 5;
  			data = context_menu_data_photos[actor_id];
  		}else{
  			offsetX = 10; 
  			offsetY = 0;
  			data = context_menu_data_names[actor_id];
  		}
  		  		
  		if(data == null || typeof data == "undefined"){
  			return null;
  		}
  		
  		if(Control.ContextMenu.current){
  			//kill any existing instance
  			context_menu_instance = Control.ContextMenu.current;
  			context_menu_instance.close(event);
  			context_menu_instance.destroy();
  		}
  		
  		context_menu_instance = null;
  			  		
  		var context_menu_instance = new Control.ContextMenu(element,
	  		{
	  			leftClick: false,
	  			menuOffsetX: offsetX,
	  			menuOffsetY: offsetY
	  		}
  		);

  		for (var index = 0; index < data.length; index++) {
  			var item = data[index];
 		
			if(typeof item[2] == "undefined"){
				context_menu_instance.addItem({
	  				label: item[0],
	  				enabled: item[1]
				});	
			}else{
				context_menu_instance.addItem({
					label: item[0],
	  				enabled: item[1],
	  				callbackUrl: item[2], 
	  				callback: function(){
	  					 window.location = this.callbackUrl;
	  				}
				});
			}
		};
		
		return context_menu_instance;
  	}

  	function actorListMouseOver(event){
		var element = event.element();
		actor_id = getActorId(element);
		
		replaceContentById(actor_id);
		postloadPhoto(actor_id);
		
		if($('sidebar_photo_big_container').getStyle('display') == "none" ){						
			effectOptions = {duration: 0.25};
			Effect.Appear('sidebar_photo_big_container', effectOptions);
		}
  	}
  	
  	function postloadPhoto(id){
  		
  		imageString = list_photos_big[id];

  		var div = document.createElement('div');
  		div.innerHTML = imageString;
  		imageSrc = div.firstChild.src;
  		
  		
  		if($('ajax_placeholder-'+id) != null){
  			new Ajax.Request(imageSrc,
  				  {
  				    method:'get',
  				    onSuccess: function(transport){
  						//console.log('Successfully loaded "'+imageSrc+'"');
  						if($$('#photo-big-'+id+' div.ajax_load').size() > 0){
  							$$('#photo-big-'+id+' div.ajax_load').first().replace(div.innerHTML);
  						}	
  						replaceContentById(actor_id);
  				    },
  				    onFailure: function(){ 
  				    	console.error('AJAX-Request for "'+imageSrc+'" failed'); 
  				    }
			  });
  		}	
  	}

  	function runEffect(id){

		effectOptions = 
			{
				duration: 0.25,
				queue: 
				{ 
					position: 'end', 
					scope: 'sidebarPhoto' 
				}				 
			}; 
		afterEffectOptions = {};
		
		if($('sidebar_photo_big_container').getHeight() == 0 ){
			replaceContentById(id);
			effectOptions['duration'] = 0.25;
			Effect.SlideDown('sidebar_photo_big_container', effectOptions);
		}else{
			var queue = Effect.Queues.get('sidebarPhoto');
			queue.each(function(effect) { effect.cancel(); });
			
			effectOptions['from'] = 1.0;
			effectOptions['to'] = 0.01;
			
			effectOptions['afterFinish'] = function(){ 
				replaceContentById(id);
			}
			
			if($('sidebar_photo_big_container').getStyle('opacity') == 1  ){
				new Effect.Fade('sidebar_photo_big_container', effectOptions);				
			}			
			afterEffectOptions = {};
			
			afterEffectOptions['duration'] 	= effectOptions['duration'];
			afterEffectOptions['queue'] 	= effectOptions['queue'];
			afterEffectOptions['from'] = 0.01;
			afterEffectOptions['to'] = 1.0;
			
			new Effect.Appear('sidebar_photo_big_container',afterEffectOptions);				
		}
	}
  	
  	function actorListMouseOut(event){
		window.clearTimeout(timer);
  	}
	
	function replaceContentById(id){
		$('sidebar_photo_big_container').update($('photo-big-'+id).innerHTML);
		if($$('#sidebar_photo_big_container div.ajax_load').size()>0){
			$$('#sidebar_photo_big_container div.ajax_load').first().writeAttribute('id','');
		}	
	}
	
	function displayContextMenuPhoto(event){
		
		var queue = Effect.Queues.get('sidebarPhoto');
		queue.each(function(effect) { effect.cancel(); });

		window.clearTimeout(timer);
		actorListMouseOver(event);
		
		context_menu_instance = createContextMenu('photo', event)
		
		if(context_menu_instance == null ){
			return false; //some error occured and we can't show the menu
		}
		context_menu_instance.open(event);
		
		return false;
	}
	
	function displayContextMenuName(event){	
		
		context_menu_instance = createContextMenu('names', event);
		
		if(context_menu_instance == null ){
			alert('No instance found');
			return false; //some error occured and we can't show the menu
		}
		context_menu_instance.open(event);
		return false;
	}
	
	function hideContextMenu(event){
		if(context_menu_instance != null){
			context_menu_instance.close(event);
			context_menu_instance.destroy();	
		}		
		context_menu_instance = null;
	}

	function swapSideBarActorPhoto(event){
		var element = event.element();

		if($('sidebarActorPhotoBig').src == element.src){
			return false;
		}

		var queue = Effect.Queues.get('sidebarPhoto');
		queue.each(function(effect) { effect.cancel(); });

		new Effect.Fade('sidebarActorPhotoBig',{duration: 0.25, from: 1.0, to:0.01, queue: { position: 'end', scope: 'sidebarPhoto' },
					afterFinish: function(){
							$('sidebarActorPhotoBig').src = element.src;
							new Effect.Appear('sidebarActorPhotoBig',{duration: 0.25, from: 0.01, to:1.0,queue: { position: 'end', scope: 'sidebarPhoto' }});
						}
				}
		);
		
		return false;
	}
	
	function adjustToSidebar(){
		height = $('sidebar').getHeight();
		if(height > $('content').getHeight()){
			$('content').setStyle({height: ""+height+"px"});
		}	
	}
	
	function blurSidebarPhotos(){ 
		$$('#sidebar_photos_small li img').each(
				function(element){
					element.setOpacity(0.4);
					
					element.observe('mouseover',unblurSidebarPhoto);
					element.observe('mouseout',blurSidebarPhoto);
				}
		);
	}
	
	function blurSidebarPhoto(event){
		var element = event.element();
		new Effect.Fade(element,{duration: 0.25, from: element.getOpacity(), to:0.5});
	}
	
	function unblurSidebarPhoto(event){
		var element = event.element();
		new Effect.Appear(element,{duration: 0.25, from: element.getOpacity(), to:1.0});
	}
	
	function openLink(event){
		var element = event.element();
	
		if(typeof element.href == "string"){
			window.location = element.href;	
		}else{
			window.location = element.ancestors()[0].href;
		}
				
	}
	
	/**
	 * opens the link in another window
	 * 
	 * @param event click event
	 * @return boolean false (to prevent the opening of within the original page)
	 */
	function handleExternalLink(event){
		var element = event.element();
		
		window.open( element.href, "external" );
		event.stop(); //don't follow the link within the same window
		return false;	
	}
	
