var Gallery = Class.create({
	initialize: function(arr,container,version,shownImages,options) {
		this.imagePrefix = '/psources/';
	    this.galleryArray=arr;
	    this.galleryContainer = $(container); 
		this.galleryVersion=version;   //simple(galleryslider),photo(photogallery with fullsize image),video (videogallery with fullsize video)
		this.visibleImages=shownImages;
		this.ttcontent_uid = container.replace(/photo/,'');
		this.divArr=[];
		this.opts = {
			step:94,  //Stepsize (Image width + margin right)
			leftPos:0,  
			imgNr:0,
			prevInactive:false,    
			nextInactive:false,
			currentImg:0,
			currentVideo:0,
			activeVideo:0,
			prevVideo:0
        };
		Object.extend(this.opts, options);	

		/* ## INIT GALLERY ## */
		this.holder_div = this.galleryContainer.down('.contentOuterDiv');
		this.content_div = this.galleryContainer.down('.contentInnerDiv');
		this.content_div.style.width = (this.galleryArray.size()* this.opts.step)+ 'px';
		
		//Description text
		this.description_p = this.galleryContainer.down('.description');
	    if(this.galleryVersion=='video'){
		   this.description_p.down('span').innerHTML="&nbsp;";
		 }else {
		    this.description_p.innerHTML="&nbsp;";
		};
		
         // Fullsize Image
		if(this.galleryVersion=='photo')
			this.big_img =this.galleryContainer.down('.imgbig');

	     //Create HTML tags from gallery array
		for(i=0;i<this.galleryArray.size(); i++){
			 //container div
			this.item_div = document.createElement('div');
			this.content_div.appendChild(this.item_div); 
			this.divArr.push(this.item_div);
			
			//link
			if(this.galleryVersion=='simple'){
				//entscheide ob mit oder Ohne Lightbox
				if(this.galleryArray[i][3].endsWith('.jpg') || this.galleryArray[i][3].endsWith('.png') || this.galleryArray[i][3].endsWith('.jpeg')) {
					//Div Boxen erstellen
					//var boxes = new DivCreator(this.galleryArray,this.ttcontent_uid);
					//var motherDiv = $('photo' + uid);
					var lightboxDiv = 'hiddenDiv_' + this.ttcontent_uid + '_' + i
					this.galleryContainer.appendChild(
							this.CreateDiv(this.galleryArray[i],lightboxDiv)
					);
					
					//Lightbox!!
					this.target = new Element('a',{id: 'click_' + this.ttcontent_uid + '_' + i}).observe('click',function(event) {
						var lightbox = this.id.replace(/click/,'hiddenDiv');
						Event.stop(event);
						Lightbox.showBoxByID(lightbox);
					});
				} else {
					this.target = new Element('a',{href: this.galleryArray[i][3]});
				}
				this.item_div.appendChild(this.target);
				
				/*this.target= document.createElement('a');
				this.target.href=this.galleryArray[i][3];
				this.target.setAttribute("onclick","Lightbox.showBoxByID('hiddenDiv"+i+"');return false;");*/
			}
			
			//thumbnail
			this.image= document.createElement('img');
			if(this.galleryVersion=='simple'){
				this.target.appendChild(this.image);
			}else{
				this.item_div.appendChild(this.image);
			}
			this.image.src=this.galleryArray[i][1];
			this.image.setAttribute('alt',this.galleryArray[i][0]);
		
			//hover border image
			this.hoverImg= document.createElement('img');
			if(this.galleryVersion=='simple'){
				this.target.appendChild(this.hoverImg);
			}else{
				this.item_div.appendChild(this.hoverImg);
			}
			Element.extend(this.hoverImg);
			this.hoverImg.addClassName('hoverimage'); 
			this.hoverImg.src= this.imagePrefix + "img/modules/hover_border.gif";
			this.hoverImg.style.visibility="hidden";
		 };
	
	  	/*##EVENTHANDLING ##*/
	 	// Hovereffect	Image/Description
		this.divArr.each(function(obj){
			Event.observe(obj,'mouseover',this.ShowDescriptionImage.bindAsEventListener(this));  
		}.bind(this));
		this.divArr.each(function(obj){
			Event.observe(obj,'mouseout',this.HideDescriptionImage.bindAsEventListener(this)); 
		}.bind(this));
	
		 //Choose Foto
		 if(this.galleryVersion=='photo')  { 
			  this.divArr.each(function(obj){
					Event.observe(obj,'mouseup',this.ShowBigImage.bindAsEventListener(this)); 
			}.bind(this));
		 };	
		 
		 //Choose Video
		  if(this.galleryVersion=='video')  { 
			  this.divArr.each(function(obj){
					Event.observe(obj,'mouseup',this.ShowVideo.bindAsEventListener(this)); 
			}.bind(this));
		 };	
	
		// Gallery navigation
		this.prev_button = this.galleryContainer.down('.prevbt');
		this.next_button = this.galleryContainer.down('.nextbt');
		
		Event.observe(this.prev_button,'mouseup',this.ScrollLeft.bindAsEventListener(this)); 
		Event.observe(this.next_button,'mouseup',this.ScrollRight.bindAsEventListener(this)); 
	    Event.observe(this.next_button,'mouseover',this.NavHover.bindAsEventListener(this)); 
		Event.observe(this.next_button,'mouseout',this.NavUnhover.bindAsEventListener(this))
		Event.observe(this.prev_button,'mouseover',this.NavHover.bindAsEventListener(this)); 
		Event.observe(this.prev_button,'mouseout',this.NavUnhover.bindAsEventListener(this))
		
		//Fullsize Image navigation
		if(this.galleryVersion=='photo') { 
		
			this.imgprev_button = this.galleryContainer.down('.imgprevbt');
			this.imgnext_button = this.galleryContainer.down('.imgnextbt');
		
			Event.observe(this.imgprev_button,'mouseup',this.ImgLeft.bindAsEventListener(this)); 
			Event.observe(this.imgnext_button,'mouseup',this.ImgRight.bindAsEventListener(this)); 
	    	Event.observe(this.imgnext_button,'mouseover',this.ImgBtnHover.bindAsEventListener(this)); 
			Event.observe(this.imgnext_button,'mouseout',this.ImgBtnUnhover.bindAsEventListener(this))
			Event.observe(this.imgprev_button,'mouseover',this.ImgBtnHover.bindAsEventListener(this)); 
			Event.observe(this.imgprev_button,'mouseout',this.ImgBtnUnhover.bindAsEventListener(this))
		};
		
		/* GALLERY START POSITION (first item active) */
		this.holder_div.scrollLeft=this.opts.leftPos;
		this.opts.imgNr=this.visibleImages;
		this.NavButtonChange(this.opts.imgNr);
		this.divArr[0].down('img',1).style.visibility="visible";
		
		if(this.galleryVersion=='photo'){
			this.big_img.src=this.galleryArray[0][2];
			this.imgprev_button.style.visibility="hidden";
		};
		
		if(this.galleryVersion=='video'){
			this.description_p.down('span').innerHTML=this.galleryArray[0][0];
			
			//load SWFObject
			var videoObj = this.galleryArray[0][4];
			var playerWidth = videoObj.videoWidth + 2;
			var playerHeight = videoObj.videoHeight + 43;
			swfobject.embedSWF(this.galleryArray[0][2], "flash_teaser", String(playerWidth), String(playerHeight), "9.0.45.0", "", videoObj, {base:"."}, {});
		}else{
			this.description_p.innerHTML=this.galleryArray[0][0];
	    };				
    },
	
	 /*## FUNCTIONS ##*/
	 
	 //Choose Video (Video detail gallery only)
	 ShowVideo: function(evt){
		this.opts.activeVideo=this.divArr.indexOf(Event.element(evt).up('div'));
		this.description_p.down('span').innerHTML=this.galleryArray[this.opts.activeVideo][0];
	  
		//load SWF Object
		var videoObj = this.galleryArray[this.opts.activeVideo][4];
		var playerWidth = videoObj.videoWidth + 2;
		var playerHeight = videoObj.videoHeight + 43;
		swfobject.embedSWF(this.galleryArray[this.opts.activeVideo][2], "flash_teaser", String(playerWidth), String(playerHeight), "9.0.45.0", "", videoObj, {base:"."}, {});
			
		if(!(this.opts.prevVideo==this.opts.activeVideo)) 
			this.divArr[this.opts.prevVideo].down('img',1).style.visibility="hidden"; 

		this.opts.prevVideo=this.opts.activeVideo;
	},
	
	//Choose Fullsize Image (Photo detail gallery only)
	ShowBigImage: function(evt){
		this.opts.currentImg=this.divArr.indexOf(Event.element(evt).up('div'));
		this.big_img.src=this.galleryArray[this.opts.currentImg][2];
		this.description_p.innerHTML=this.galleryArray[this.opts.currentImg][0];
		
		if (this.opts.currentImg==0){
			this.imgprev_button.style.visibility="hidden";
			this.imgnext_button.style.visibility="visible";
		}else if (this.opts.currentImg== this.galleryArray.size()-1){
			this.imgnext_button.style.visibility="hidden";
			this.imgprev_button.style.visibility="visible";
		}else{
			this.imgnext_button.style.visibility="visible";
			this.imgprev_button.style.visibility="visible";
		};
	},
	
	//Fullsize Image navigation and Hovereffect (Photo detail gallery only)
	ImgLeft:function (){
		if(this.opts.currentImg > 0){	
			this.opts.currentImg-=1; 
  			this.big_img.src=this.galleryArray[this.opts.currentImg][2];
			this.description_p.innerHTML=this.galleryArray[this.opts.currentImg][0];
			if (this.opts.currentImg == 0)
				this.imgprev_button.style.visibility="hidden";			
		};
		this.imgnext_button.style.visibility="visible";
	},
	ImgRight: function(){	
	 	if(this.opts.currentImg < this.galleryArray.size()-1){
			this.opts.currentImg+=1;	
	  		this.big_img.src=this.galleryArray[this.opts.currentImg][2];
			this.description_p.innerHTML=this.galleryArray[this.opts.currentImg][0];
			if (this.opts.currentImg == this.galleryArray.size()-1)
				   	this.imgnext_button.style.visibility="hidden";
		 };
		this.imgprev_button.style.visibility="visible";
		this.divArr[0].down('img',1).style.visibility="hidden";
	},
	ImgBtnHover:function (btn){
		if (Event.element(btn).match('span')){
			var button=Event.element(btn);//IE6
		}else{
			//other
			var button=Event.element(btn).up('span');
		};
		
		if(button.hasClassName('imgnextbt')){
			this.imgnext_button.removeClassName('imgnextbt');				   
			this.imgnext_button.addClassName('imgnextbt_hover');
		};
		if(button.hasClassName('imgprevbt')){
			this.imgprev_button.removeClassName('imgprevbt');				   
			this.imgprev_button.addClassName('imgprevbt_hover');
		};
		
	},
	ImgBtnUnhover:function (btn){	
	      
	   if (Event.element(btn).match('span')){
			var button=Event.element(btn);//IE6
		}else{
			//other
			var button=Event.element(btn).up('span');
		};

	   if(button.hasClassName('imgnextbt_hover')){
	   		this.imgnext_button.removeClassName('imgnextbt_hover');
			this.imgnext_button.addClassName('imgnextbt');
	   };
		if(button.hasClassName('imgprevbt_hover')){
			this.imgprev_button.removeClassName('imgprevbt_hover');
			this.imgprev_button.addClassName('imgprevbt');
		};
	},
	
	// Thumbnail and Description Hovereffect
	ShowDescriptionImage: function(evt){	
		if(this.divArr[0].down('img',1).style.visibility="visible"){
		   this.divArr[0].down('img',1).style.visibility="hidden";
		};
		
		var obj = Event.element(evt).up('div');
		obj.down('img',1).style.visibility="visible";
		if(this.galleryVersion=='simple') 
			this.description_p.innerHTML=obj.down('img',0).getAttribute('alt');	
	   
	   if(this.galleryVersion=='video') { 
	   		if(this.opts.prevVideo==this.opts.activeVideo)
				this.divArr[this.opts.activeVideo].down('img',1).style.visibility="visible"; 
	     	this.opts.currentVideo=this.divArr.indexOf(Event.element(evt).up('div'));
		 	this.description_p.down('span').innerHTML=obj.down('img',0).getAttribute('alt');
	   };
	},
	HideDescriptionImage: function(evt){
		var obj= Event.element(evt).up('div');
		obj.down('img',1).style.visibility="hidden"; 
		
		if(this.galleryVersion=='video') {
			if(this.opts.activeVideo==this.opts.currentVideo){
				obj.down('img',1).style.visibility="visible"; 
			}else{
				obj.down('img',1).style.visibility="hidden"; 
			}
			this.description_p.down('span').innerHTML="";			
		};
		if(this.galleryVersion=='simple') 
			this.description_p.innerHTML="&nbsp";		 
	},
	
	
	//Gallery Navigation
	NavHover:function (btn){
		var button=Event.element(btn);
		
		if((!this.opts.nextInactive)&&(button.hasClassName('nextbt'))){
	      this.next_button.src= this.imagePrefix + "img/modules/next_button_hover.gif";
		  this.prev_button.next('.gallery_navtext').down('span',1).style.color="#fabf01";
		}else  if((!this.opts.prevInactive)&&(button.hasClassName('prevbt'))){
		 this.prev_button.src= this.imagePrefix + "img/modules/prev_button_hover.gif";
		 this.prev_button.next('.gallery_navtext').down('span',0).style.color="#fabf01";
	    };
	},
	NavUnhover:function (btn){	
		var button=Event.element(btn);

	    if((!this.opts.nextInactive)&&(button.hasClassName('nextbt'))){
	       this.next_button.src= this.imagePrefix + "img/modules/next_button.gif";
		   this.prev_button.next('.gallery_navtext').down('span',1).style.color="#999999";
		}else  if((!this.opts.prevInactive)&&(button.hasClassName('prevbt'))){
	 	   this.prev_button.next('.gallery_navtext').down('span',0).style.color="#999999";
		   this.prev_button.src= this.imagePrefix + "img/modules/prev_button.gif";
	   }else{
		   this.NavButtonChange(this.opts.imgNr);
	   };
	},
	ScrollLeft:function (){	  
		if(this.opts.imgNr> this.visibleImages){	    
  			this.holder_div.scrollLeft=this.opts.leftPos-this.opts.step;
  			this.opts.leftPos-=this.opts.step;
			this.opts.imgNr-=1;
			this.NavButtonChange(this.opts.imgNr);
		};
		
		 this.next_button.style.cursor="pointer";
		 this.next_button.src= this.imagePrefix + "img/modules/next_button.gif";	
		 this.prev_button.next('.gallery_navtext').down('span',1).style.color="#999999"; 
	},
	ScrollRight: function(){	
	 	if(this.opts.imgNr < this.galleryArray.size()){
	  		this.holder_div.scrollLeft=this.opts.leftPos+this.opts.step;
  			this.opts.leftPos+=this.opts.step;
			this.opts.imgNr+=1;
			this.NavButtonChange(this.opts.imgNr);
		};
		
		if(this.galleryVersion=='video') 
			this.description_p.down('span').innerHTML="&nbsp";
	
		if(this.galleryVersion=='simple') 	
			this.description_p.innerHTML="&nbsp";

		this.prev_button.style.cursor="pointer";
		this.prev_button.src= this.imagePrefix + "img/modules/prev_button.gif";	
		this.prev_button.next('.gallery_navtext').down('span',0).style.color="#999999"; 
	},
	NavButtonChange: function(num) {
	     // no navigation buttons needed
		if (this.visibleImages >= this.galleryArray.size()){
		   if (this.galleryContainer.down('div.gallery_navigation_container').down('div').hasClassName('optionalLink')){
			   this.galleryContainer.down('.gallery_navigation').style.display="none";
		   }else {
				this.galleryContainer.down('.gallery_navigation_container').style.display="none";
		   };
		};
		 // last Image 
		if (num == this.galleryArray.size()){
		   this.next_button.src= this.imagePrefix + "img/modules/next_button_off.gif";
		   this.next_button.style.cursor="auto";	       
		   this.next_button.previous('.gallery_navtext').down('span',1).style.color="#cccccc";
		   this.opts.nextInactive=true;
	    }else {
		   this.opts.nextInactive=false;
		};
		
		// first Image 
		if (num == this.visibleImages ){
		   this.prev_button.src= this.imagePrefix + "img/modules/prev_button_off.gif";
		   this.next_button.previous('.gallery_navtext').down('span',0).style.color="#cccccc";
		   this.prev_button.style.cursor="auto";
		   this.opts.prevInactive=true;
	    } else{
		   this.opts.prevInactive=false;
		};
	},
	
	CreateDiv: function(imageData,i) {
		var div = new Element('div',{
			'id': i
		}).hide();
		
		var image = new Element('img', {
			'src': imageData[3]
		})
		
		var head = new Element('h3').update(imageData[0]);
		
		div.appendChild(image);
		div.appendChild(head);
		return div;
	}
});


var DivCreator = Class.create({});


var browserName=navigator.appName;var browserVer=parseInt(navigator.appVersion);var version="";var msie4=(browserName=="Microsoft Internet Explorer"&&browserVer>=4);if((browserName=="Netscape"&&browserVer>=3)||msie4||browserName=="Konqueror"||browserName=="Opera"){version="n3";}else{version="n2";}
function blurLink(theObject){if(msie4){theObject.blur();}}
function decryptCharcode(n,start,end,offset){n=n+offset;if(offset>0&&n>end){n=start+(n-end-1);}else if(offset<0&&n<start){n=end-(start-n-1);}
return String.fromCharCode(n);}
function decryptString(enc,offset){var dec="";var len=enc.length;for(var i=0;i<len;i++){var n=enc.charCodeAt(i);if(n>=0x2B&&n<=0x3A){dec+=decryptCharcode(n,0x2B,0x3A,offset);}else if(n>=0x40&&n<=0x5A){dec+=decryptCharcode(n,0x40,0x5A,offset);}else if(n>=0x61&&n<=0x7A){dec+=decryptCharcode(n,0x61,0x7A,offset);}else{dec+=enc.charAt(i);}}
return dec;}
function linkTo_UnCryptMailto(s){location.href=decryptString(s,3);}
