isAjax = true;
var hCarousel;

/*
hCarousel = new UI.Carousel("slideshow");
hCarousel.observe('scroll:ended',function()
{   
});
*/       

function showMore()
{
	$('text').style.display = 'block';
	
	return false;
} 
 
function showInfo()
{                
	$('info').className = 'active';        
	$('text').style.display = 'block';
	$('control').className = 'active';           
}
function hideInfo()
{           
	if($('more').innerHTML=='MORE') {    
		$('text').style.display = 'none';
		$('info').className = '';  	
		$('control').className = ''; 
	}             
}
function toggleInfo()
{          
	if($('more').innerHTML=='MORE') { 
		$('more').update("CLOSE"); 
	}else {
		$('more').update("MORE");
	}                        
}                

function showSlideshowCounter()
{ 
	 
	var cnt		= Math.ceil((hCarousel.currentIndex()/3)+1);
	var total	= Math.floor(hCarousel.elements.length/3);
	$('counter').innerHTML = cnt +'/'+ total;  
	setAddressStatus(hCarousel.currentIndex());
	
}

function init()
{  
	if($('slideshow')==undefined)
    	return;            

	var index = getAddressStatus();  
	
	hCarousel = new UI.Carousel("slideshow");          
	hCarousel.scrollTo(index-1);
	hCarousel.observe('scroll:started',function()
	{                                               
	}); 
	hCarousel.observe('scroll:ended',function()
	{                                               
		showSlideshowCounter();
	});  
	showSlideshowCounter();
}  
function setAddressListener()
{     
	/*
	if( typeof SWFAddress != 'undefined' ) {
		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, this.updateAddressStatus.bind(this)); 
	}
	*/  
}

function getAddressStatus()
{    
	if( typeof SWFAddress != 'undefined' ) {
		var path = SWFAddress.getValue();         
		if(path != "/") { 
			var parts = path.split('/');   
			return parseInt(parts[1],10)+1; 
		}               
	}    
}

function setAddressStatus(path)
{                                  
	if( typeof SWFAddress != 'undefined' && SWFAddress.getValue() != path) {     
		SWFAddress.setValue("/"+path);              
	}    
} 

function updateAddressStatus()
{
	if( typeof SWFAddress != 'undefined') { 
		var path = SWFAddress.getValue();
		if(path != "/") { 
			var parts 		= path.split('/');    
		
			var content	= parts[1] != undefined ? parts[1] : 0;
			var image	= parts[2] != undefined ? parts[2] : 0; 
	    
			if(content != this.contentKey || image != this.imageKey) {
				this.contentKey 	= content;
				this.imageKey 		= image;                     
			
				this.showContent();        
			
				var cnt = 0;          
				for(var i=0; i<=this.contentKey; i++) {
					for(var j=0; j<slideshowImages[i].length; j++) {   
						if(i==this.contentKey && j==this.imageKey)
							this.seek(cnt);
						cnt++;
					}
				}
			}                      
		} else {
			return;
		}  
	}
}  
 
function loadWork(workUrl, workId)
{
	requestUrl = baseUrl + '/works/json/'+workUrl+"/"+workId;                                                      
	request = new Ajax.Request(requestUrl, {asynchronous:true, onSuccess:showContent});
}    

function showContent(transport)
{                             
	var json = transport.responseText.evalJSON();  
	
	var container;
	if($('detailimage')) {
		container = $('detailimage');
	}else if($('video')){
		container = $('video');
	}else if($('pano')){
		container = $('pano');
	}     
         
	if(json.type == 'image') { 
		container.update('<img src="media/'+ json.image +'" alt="content" class="detail" />');
	}else if (json.type == 'video') {
		if( $('demo-video') ) {
			sublimevideo.unprepare("demo-video");
		}

		var html = '<div id="video">';   
		html += '<video id="demo-video" class="sublime" poster="'+ baseUrl+"/media/"+ json.image +'" preload="none" controls width="664" height="372">';
		html += '<source src="http://pfadfinderei.com.dd28314.kasserver.com/movies/'+ json.video +'" type="video/mp4" />';
		html += '</video>';
		html += '</div>';

		container.update(html); 
		//sublimevideo.prepareAndPlay("demo-video");
		sublimevideo.prepare("demo-video");

		/*
		var flashvars = {                                                        
   		  imageUrl: baseUrl+"/media/"+ json.image ,
		  videoUrl: "http://www.bpitchcontrol.de/pfadfinderei/movies/"+ json.video
		};
		var params = {
		  	allowScript: "true",
			allowFullScreen: "true",
			base: baseUrl+"/",
			menu: "false", 
			bgcolor: '0F0F0F'
		};      	
        container.update('<div id="video"></div>')
		swfobject.embedSWF(baseUrl+"/files/videoplayer.swf", 'video', json.width, json.height, "9.0.3", "expressInstall.swf", flashvars, params);
		*/
	}else if (json.type == 'panorama') {    
		var flashvars = {                                                        
		  xml_file: json.xml
		};
		var params = {
		  	allowScript: "true",    
			allowScriptAccess: "sameDomain",
			allowFullScreen: "true",
			base: baseUrl+"/",
			menu: "false", 
			bgcolor: '0F0F0F'
		};  

		swfobject.embedSWF(baseUrl+"/files/panorama/pano.swf", container.id, "664", "372", "9.0.3", "expressInstall.swf", flashvars, params, {id:'pano'});               
	} 
	
    $$('div.caption')[0].update(json.caption);
}
                                         

var contactrules = {
	'input.text' : function(element) { 
		if(element.id != null)
			new InputAdvisor(element.id, element.value);
	},	
	'input.error' : function(element) { 
		if(element.id != null)
			new InputAdvisor(element.id, element.value);
	},
	'textarea.text' : function(element) { 
		if(element.id != null)
			new InputAdvisor(element.id, element.value);
	}, 
	'textarea.errorArea' : function(element) { 
		if(element.id != null)
			new InputAdvisor(element.id, element.value);
	},
	'li.belonging a' : function(element) {   
		//ul#navigation  	
		element.onclick = function() {
			var url = this.href;	         
			var workUrl = url.split('/')[4];
			var workId = url.split('/')[5];   
       
			loadWork(workUrl, workId);
	
			return false; 
		}
	}, 
	'div#control a': function(element) {
		element.observe('mouseover', function(e) {
						showInfo();
						Event.stop(e);
					}.bind(this));
		element.observe('mouseout', function(e) {
						hideInfo();
						Event.stop(e);
					}.bind(this));
		element.observe('click', function(e) {
						toggleInfo();
						Event.stop(e);
					}.bind(this)); 
					
	} 
};                  

Behaviour.register(contactrules);
