var frontendGallery = {
	version: '0.2',
	num: 0,
	lang: {
			photogallery: 'FOTO', 
			videogallery: 'AUDIO/VIDEO',
			photoopensmall: 'otevřít fotogalerii',
			videoopensmall: 'přehrát audio a video',
			photo: 'Foto: ',
			video: 'Zdroj: ',
			title: 'GALERIE K ČLÁNKU'
	}
}

var frontendGalleryRenderer = Class.create();

frontendGalleryRenderer.prototype = {
	ie: Prototype.Browser.IE ? 1 : 0,
	bvv: function () {
			if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
			return new Number(RegExp.$1)}
	},
	bv: 0,
 
	initialize: function(obj, id, none, url) {
		this.galleryId = id.replace("gallery_", "");
		this.url = (typeof url == 'undefined' || url == null) ? '' : url+"/";
		this.bv = this.bvv();
		if(!obj.isJSON()) return false;
		
		this.element = $(id);
		if(!this.element) return false;
		this.uniqID = frontendGallery.num++;
		this.playlist = obj.evalJSON();
		
		this.imagelist = [];
		this.videolist = [];
		
		this.images = false;
		this.videos = false;
		this.curpos = -1;
		
		this.curpl = false;
		
		
		this.imageContent = [];		
		this.videoContent = [];
		
	
		this.galtype='gallery';

		if(typeof(this.playlist.galleryList)!=undefined && this.playlist.galleryList.length>0){
			this.galleryList = this.playlist.galleryList;
			for(i = 0; i < this.galleryList.length; i++){
				if(this.galleryList[i][0] == 'IMAGE'){
					this.images = true;
					this.imagelist[this.imagelist.length] = this.galleryList[i];
					this.imageContent[this.imagelist.length-1] = new Image(1,1);
					this.imageContent[this.imagelist.length-1].src = this.galleryList[i][1];
				}else if(this.galleryList[i][0] == 'VIDEO'){
					this.videos = true;
					this.videolist[this.videolist.length] =  this.galleryList[i];
					this.videoContent[this.videoContent.length] = Builder.node('div', {className: 'video_'+this.galleryList[i][4], style: 'display: none;'}, Builder.node('iframe', {src: "/media/"+this.galleryList[i][4]+"/300x200/", className: 'video_'+this.galleryList[i][4], frameborder: 0, width: 300, height:
 200}));
				}
			}
		}
/*
		
		if(typeof(this.playlist.IMAGE)!='undefined' && this.playlist.IMAGE.length>0) {
			this.imagelist = this.playlist.IMAGE;
			this.images = true;
			this.imageContent = [];
			for(i=0;i<this.imagelist.length;i++) {
				this.imageContent[i] = new Image(1,1);
				this.imageContent[i].src = this.imagelist[i][0];
			}
		}
		
		if(typeof(this.playlist.VIDEO)!='undefined' && this.playlist.VIDEO.length>0) {
			this.videolist = this.playlist.VIDEO;
			this.videos = true;
			for(i=0;i<this.videolist.length;i++) {
				this.videoContent[this.videoContent.length] = Builder.node('div', {className: 'video_'+this.videolist[i][3], style: 'display: none;'}, Builder.node('iframe', {src: "/media/"+this.videolist[i][3]+"/"+(this.galtype=='minigallery'?"450x350":"300x200")+"/", className: 'video_'+this.videolist[i][3], frameborder: 0, width: (this.galtype=='minigallery'?450:300), height: (this.galtype=='minigallery'?350:200)}));
			}
		}
		*/
		this.render();
		if (this.images) this.activateImages();
		if (!this.images && this.videos) this.activateVideos();
		
		this.tmppos = getURLParam('foto');
		if ( this.tmppos >0 && this.tmppos.length>0){
			
			this.tmppos;
			for ( var myI = 0; myI < this.tmppos; myI++ ){
				this.next();
			}
		}

	},
	
	render: function() {
		if(!this.images && !this.videos) {
			this.element.hide();
			return false;
		}
		
		if (this.galtype=='minigallery') {

			var tmp = Builder.node('div', {className: 'galleryBox', style:'clear:both'}, [
				Builder.node('table', {className: 'gallery', id: 'frontendGallery_'+this.uniqID}, [
					Builder.node('tbody',[
					Builder.node('tr',[
						Builder.node('td', {className: 'prev'}, [
							Builder.node('a',{onclick:"return false", className: 'prevdiv'})]),
						Builder.node('td', {className: 'container'}, [
							Builder.node('div', {id: 'cont_'+this.uniqID, className: 'cont', onclick: 'window.close();', style: 'display:none;'}, Builder.node('img', {'class': 'cont_img', src: '/images/spacer.gif'})), 
							Builder.node('div', {id: 'vicont_'+this.uniqID, className: 'vicont',  onclick: 'window.close();', style: 'display:none;'}, this.videoContent), 
							Builder.node('div', {id: 'desc_'+this.uniqID, className: 'desc'}, '.'),]),
						Builder.node('td', {className: 'next'}, [
							Builder.node('a',{onclick:"return false", className: 'nextdiv'})]),
					]),
					])
				]),
					Builder.node('ul', {className: 'gallery_status'}, [
						Builder.node('li', {className: 'copy'}, 'copy'),
						Builder.node('li', {className: 'count'}, 'count')
					]),
					Builder.node('div', {className: 'playlist',style: ((this.videos && this.images)?'':'display:none')}, this.getPlaylistThumb()),
					//Builder.node('div', {style: 'display: none', className: 'videoContainer'}, this.videoContent),
					Builder.node('div', {className: 'cb'})
			]);
			this.element.update(tmp);
		}
		if (this.galtype=='gallery') {
			var tmp = Builder.node('div', {className: 'galleryBox', style:'clear:both'}, [
				//Builder.node('div', {className: 'balTitle'}, frontendGallery.lang.title),
				Builder.node('ul', {className: 'gallery', id: 'frontendGallery_'+this.uniqID}, [
					Builder.node('li', {className: 'prev'}),
					Builder.node('li', {className: 'container'}, [
						Builder.node('div', {id: 'cont_'+this.uniqID, className: 'cont', style: 'display:none;'}, Builder.node('img', {'class': 'cont_img', src: '/images/spacer.gif'})), 
						Builder.node('div', {id: 'vicont_'+this.uniqID, className: 'vicont', style: 'display:none;'}, this.videoContent), 
						Builder.node('div', {id: 'desc_'+this.uniqID, className: 'desc'}, '.'),]),
					Builder.node('li', {className: 'next'}),
					Builder.node('div', {className: 'cb'})
				]),				
				Builder.node('ul', {className: 'gallery_status'}, [
					Builder.node('li', {className: 'copy'}, 'copy'),
					Builder.node('li', {className: 'popit'}),
					Builder.node('li', {className: 'count'}, 'count')
				]),
				Builder.node('div', {className: 'playlist',style: ((this.videos && this.images)?'':'display:none')}, this.getPlaylistThumb()),
				//Builder.node('div', {style: 'display: none', className: 'videoContainer'}, this.videoContent),
				Builder.node('div', {className: 'cb'})
			]);
			this.element.update(tmp);
		}
		
		//this.element.down('.videoContainer').update(this.videoContent);
		this.element.down('.gallery').hide();
		this.element.down('.gallery_status').hide();
		
		if (this.galtype=='gallery') {
			this.element.down('.prev ').observe('mouseover', this.mouseover.bindAsEventListener(this));
			this.element.down('.next').observe('mouseover', this.mouseover.bindAsEventListener(this));
	
			this.element.down('.prev').observe('mouseout', this.mouseout.bindAsEventListener(this));
			this.element.down('.next').observe('mouseout', this.mouseout.bindAsEventListener(this));
	
			this.element.down('.prev').observe('click', this.mouseclick.bindAsEventListener(this));
			this.element.down('.next').observe('click', this.mouseclick.bindAsEventListener(this));
			
			if(this.element.down('.cont')) this.element.down('.cont').observe('click', this.showPopUp.bindAsEventListener(this));
			if(this.element.down('.vicont')) this.element.down('.vicont').observe('click', this.showPopUp.bindAsEventListener(this));
			
		}
		if (this.galtype=='minigallery') {
			this.element.down('.prevdiv').observe('mouseover', this.mouseover.bindAsEventListener(this));
			this.element.down('.nextdiv').observe('mouseover', this.mouseover.bindAsEventListener(this));
	
			this.element.down('.prevdiv').observe('mouseout', this.mouseout.bindAsEventListener(this));
			this.element.down('.nextdiv').observe('mouseout', this.mouseout.bindAsEventListener(this));
	
			this.element.down('.prevdiv').observe('click', this.mouseclick.bindAsEventListener(this));
			this.element.down('.nextdiv').observe('click', this.mouseclick.bindAsEventListener(this));
		}

		if(this.element.down('.imThumb')) this.element.down('.imThumb').observe('click', this.activateImages.bindAsEventListener(this));
		if(this.element.down('.viThumb')) this.element.down('.viThumb').observe('click', this.activateVideos.bindAsEventListener(this));
		if(this.element.down('.popit')) this.element.down('.popit').observe('click', this.showPopUp.bindAsEventListener(this));
		if(this.element.down('.popit')) this.element.down('.popit').innerHTML='&nbsp;';
			
		this.container = $('cont_'+this.uniqID);
		this.vicontainer = $('vicont_'+this.uniqID);
		
		this.desc = $('desc_'+this.uniqID);
		this.copy = this.element.select('.copy')[0];
		this.count = this.element.select('.count')[0];
		
		//this.activateImages();
		
	},
	
	mouseover: function(e) {
		var el = Event.element(e);
		el.addClassName('over');
	},
	
	mouseout: function(e) {
		var el = Event.element(e);
		el.removeClassName('over');
	},
	
	mouseclick: function(e) {
		var tmp = Event.element(e);
		if(tmp.hasClassName('prev') || tmp.hasClassName('prevdiv')) {
			this.prev();
		}
		if(tmp.hasClassName('next') || tmp.hasClassName('nextdiv')) {
			this.next();
		}
	},
	
	getPlaylistThumb: function() {
		var im = '';
		var vi = '';
		if(this.images && this.videos) {
			var ret = Builder.node('ul', {className: 'galleryThumbs'}, [
				Builder.node('li', {className: 'imThumb'}, [Builder.node('img', { src: this.playlist.ico.IMAGE}), Builder.node('div', {className: 'plDesc'}, [
					Builder.node('strong', frontendGallery.lang.photogallery),
					Builder.node('br'),
					frontendGallery.lang.photoopensmall
					//this.imagelist[0][1]
				])]),
				Builder.node('li', {className: 'viThumb'}, [Builder.node('img', {src: this.playlist.ico.VIDEO}), Builder.node('div', {className: 'plDesc'}, [
					Builder.node('strong', frontendGallery.lang.videogallery),
					Builder.node('br'),
					frontendGallery.lang.videoopensmall
					//this.videolist[0][1]
				])])
			]);
			
			return ret;
		}
		if(this.images) {
			var ret = Builder.node('ul', {className: 'galleryThumbs'}, [
				Builder.node('li', {className: 'imThumb'}, [Builder.node('img', { src: this.playlist.ico.IMAGE}), Builder.node('div', {className: 'plDesc'}, [
					Builder.node('strong', frontendGallery.lang.photogallery),
					this.playlist.desc.IMAGE
				])])
			]);
			
			return ret;
		}
		if(this.videos) {
			var ret = Builder.node('ul', {className: 'galleryThumbs'}, [
				Builder.node('li', {className: 'viThumb'}, [Builder.node('img', {src: this.playlist.ico.VIDEO}), Builder.node('div', {className: 'plDesc'}, [
					Builder.node('strong', frontendGallery.lang.videogallery),
					this.playlist.desc.VIDEO
				])])
			]);
			
			return ret;
		}
		return Builder.node('div', {className: 'cb'});
	},
	
	activateImages: function() {
		if(this.curpl!='im') {
			if(!(this.ie==1 && this.bv<=6)) {
				Effect.Appear(this.element.down('.gallery'));
			} else {
				$(this.element.down('.gallery')).show();
			}
			this.curpl = 'im';
			this.curpos = -1;
			this.next();
		}
	}, 
	
	activateVideos: function() {
		if(this.curpl!='vi') {
			if(!(this.ie==1 && this.bv<=6)) {
				Effect.Appear(this.element.down('.gallery'));
			} else {
				$(this.element.down('.gallery')).show();
			}
			this.curpl = 'vi';
			this.curpos = -1;
			this.next();
		}
	},
	
	next: function() {
		if(this.curpl=='im')
			this.nextImage();
		if(this.curpl=='vi')
			this.nextVideo();
	},

	updateContainer: function() {
		this.element.select('.gallery_status')[0].show();
		if(this.curpl=='im') {
			try {
				this.container.hide();
			} catch(e) {}
			try {
				this.vicontainer.hide();
			} catch(e) {}
			//this.container.update(Builder.node('img', {src: this.imagelist[this.curpos][0]}));
			try {
			if($(this.container.down('img'))) {
				$(this.container.down('img')).src = this.imageContent[this.curpos].src;
			}else {
				this.container.update(Builder.node('img', {src: '/images/spacer.gif'}));
				$(this.container.down('img')).src = this.imageContent[this.curpos].src;
			}
			} catch(e) {}
			try {
				this.desc.update(this.imagelist[this.curpos][2]);
			} catch(e) {}
			try {
				this.copy.update('<span>'+frontendGallery.lang.photo+'</span>'+this.imagelist[this.curpos][3])
			} catch(e) {}
			try {
				this.count.update((this.curpos+1)+'/'+(this.imagelist.length));
			} catch(e) {}
			try {
				this.appear();
			} catch(e) {}
		}
		if(this.curpl=='vi') {
			this.container.hide();
			var tmp = this.vicontainer.select('div');
			if(tmp.length>0) {
				for(i=0;i<tmp.length;i++) tmp[i].hide();
			}
			this.vicontainer.down('.video_'+this.videolist[this.curpos][4]).show();
			this.vicontainer.show();
			this.desc.update(this.videolist[this.curpos][2]);
			this.copy.update('<span>'+frontendGallery.lang.video+'</span>'+this.videolist[this.curpos][3])
			this.count.update((this.curpos+1)+'/'+(this.videolist.length));
		}
	},
	
	nextImage: function() {
		var preloader = 0;
		if(this.imagelist.length==1 && this.curpos>=0) return
		this.curpos++
		if(this.curpos >= this.imagelist.length) this.curpos = 0;
		preloader = this.curpos + 1;
		if(preloader >= this.imagelist.length) preloader = 0;
		try {
			var pp = new Image(1,1);
			pp.src = this.imagelist[preloader][0];
		} catch(e) {}
		this.updateContainer();
	},
	
	nextVideo: function() {
		if(this.videolist.length==1 && this.curpos>=0) return
		this.curpos++
		if(this.curpos >= this.videolist.length) this.curpos = 0;
		this.updateContainer();
	},
	
	prev: function() {
		if(this.curpl=='im')
			this.prevImage();
		if(this.curpl=='vi')
			this.prevVideo();
	},
	
	prevImage: function() {
		if(this.imagelist.length==1 && this.curpos>=0) return
		this.curpos--
		if(this.curpos < 0) this.curpos = this.imagelist.length-1;
		this.updateContainer();
	},
	
	prevVideo: function() {
		if(this.videolist.length==1 && this.curpos>=0) return
		this.curpos--
		if(this.curpos < 0) this.curpos = this.videolist.length-1;
		this.updateContainer();
	},
	
	appear: function() {
		if(this.ie && this.bv<=6) {
			this.container.show();
			this.desc.show();
			return;
		}
		
		this.container.hide();
		Effect.Appear(this.container);
		this.desc.hide();
		Effect.Appear(this.desc);
	},
	
	showPopUp: function() {
		var urlBase = '/galerie/'+this.url+this.galleryId+'/';
		if(this.curpl=='im')
			window.location.href = urlBase + '?foto='+(this.curpos);
		else
			window.location.href = urlBase + '?video='+(this.curpos);
	}
	
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}		