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 frontendNewGalleryRenderer = Class.create();

frontendNewGalleryRenderer.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, url) {
		this.url = url;
		this.obj = obj;
		this.id = id;
		this.bv = this.bvv();
		if(!obj.isJSON()) return false;
		this.galleryid = id.replace('gallery_','');
		this.element = $(id);
		if(!this.element) return false;
		this.uniqID = frontendGallery.num++;
		this.playlist = obj.evalJSON();
		
		this.imagelist = [];
		this.videolist = [];
		this.gallerylist = [];
		this.galleryContent = [];
		
		this.images = false;
		this.videos = false;
		this.curpos = -1;
		
		this.curpl = false;
		this.carousel = null;
		
		this.videoContent = [];
		
		if (this.playlist.galtype) this.galtype='minigallery';
		else this.galtype='gallery';
		
		if(typeof(this.playlist.galleryList)!=undefined && this.playlist.galleryList.length>0){
			this.galleryList = this.playlist.galleryList;
			this.images = true;
			this.videos = true;
			for(i = 0; i < this.galleryList.length; i++){
				if(this.galleryList[i][0] == 'IMAGE'){
					this.galleryContent[i] = new Image(1,1);
					this.galleryContent[i].src = this.galleryList[i][1];
				}else if(this.galleryList[i][0] == 'VIDEO'){
				
					this.galleryContent[i] = Builder.node('div', {className: 'video_'+this.galleryList[i][4], style: 'display: none;'}, Builder.node('iframe', {src: "/media/"+this.galleryList[i][4]+"/"+(this.galtype=='gallery'?"450x350":"300x200")+"/", className: 'video_'+this.galleryList[i][4], frameborder: 0, width: (this.galtype=='gallery'?450:300), height: (this.galtype=='gallery'?350: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();
		this.activate();
		

		
		
		this.tmppos = getURLParam('foto');
		if ( this.tmppos >0 && this.tmppos.length>0){
		
			for( var i = 0; i < this.galleryList.length; i++){
				if(this.galleryList[i][0]=='IMAGE') this.tmppos--;
				this.next();
				if(this.tmppos==0) break;
			}
		}
		
		this.tmpvpos = getURLParam('video');
		if ( this.tmpvpos >0 && this.tmpvpos.length>0){
			for( var i = 0; i < this.galleryList.length; i++){
				if(this.galleryList[i][0]=='VIDEO') this.tmpvpos--;
				this.next();
				if(this.tmpvpos==0) break;
			}
		}

		this.tmpfpos = getURLParam('fotka');
		if ( this.tmpfpos >0 && this.tmpfpos.length>0){
			for( var i = 0; i < this.tmpfpos; i++){
				this.next();
			}
		}

		
		
	/*	
	
		this.render();
		if (this.images) this.activateImages();
		if (!this.images && this.videos) this.activateVideos();
		
		this.tmppos = getURLParam('cnt');
		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('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.galleryContent), 
						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);
		}
		if (this.galtype=='gallery') {
			var tmp = Builder.node('ul', {className: 'gallery', id: 'frontendGallery_'+this.uniqID}, [
					Builder.node('li', {className: 'prev'}),
					Builder.node('li', {id : 'centerContainer', className: 'container'}, [
						Builder.node('div', {id: 'cont_'+this.uniqID, className: 'cont', style: 'display:none;'},
							Builder.node('div',{className :'contain'}, [
								Builder.node('div', {className: 'count'}, 'count'),
								Builder.node('img', {className: 'imgBig', src: '/images/spacer.gif'}),
								Builder.node('div', {id: 'vicont_'+this.uniqID, className: 'vicont', style: 'display:none;'}, this.galleryContent),
								Builder.node('div', {className: 'ieg'}, [
									Builder.node('div', { className: 'desc'}, '.'),
									Builder.node('div', {className: 'copy'}, 'copy')
									])
								]))
						]),
						
					Builder.node('li', {className: 'next'}),
					Builder.node('div', {className: 'cb'})
				]);				
			this.element.update(tmp);
		}
		
		//this.element.down('.videoContainer').update(this.videoContent);
		this.element.down('.gallery').hide();
		this.element.down('.copy').hide();

			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.galtype=='gallery') {

			
			if(this.element.down('.imgBig')) this.element.down('.imgBig').observe('click', this.showBig.bindAsEventListener(this));
			if(this.element.down('.vicont')) this.element.down('.vicont').observe('click', this.showBig.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 = this.element.select('.desc')[0];
		this.copy = this.element.select('.copy')[0];
		this.count = this.element.select('.count')[0];
		this.contain = this.element.select('.cont')[0];
		
		//this.activateImages();
		
	},
	
	addCarousel: function(od) {
		this.carousel = od;
	},
	
	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'});
	},
	
	activate: function(){
		$(this.element.down('.gallery')).show();
		/*if(!this.ie==1 && this.bv<=6){
			Effect.Appear(this.element.down('.gallery'));
		}else{
			$(this.element.down('.gallery')).show();
		}*/
		this.next();
	},
		
	updatecontainer: function(){
		this.element.select('.copy')[0].show();
		try{
			this.desc.update(this.galleryList[this.curpos][2]);
			
			this.copy.update("Foto: " + this.galleryList[this.curpos][3]);

		}catch(e){}
		try {
			this.count.update((this.curpos+1)+'/'+(this.galleryList.length));
		} catch(e) {}
		if(this.galleryList[this.curpos][0]=='IMAGE'){
			try{
				this.container.hide();
			}catch(e){}
			try{
				this.vicontainer.hide();
			}catch(e){}
			try{
				if($(this.container.down('img'))) {
					$(this.container.down('img')).src = this.galleryContent[this.curpos].src;
				}else {
					this.container.update(Builder.node('img', {src: '/images/spacer.gif'}));
					$(this.container.down('img')).src = this.galleryContent[this.curpos].src;
				}
				this.container.down('img').show();
			} catch(e) {}
			try {
				this.appear();
			} catch(e) {}
		}
		if(this.galleryList[this.curpos][0]=='VIDEO'){
			this.container.down('img').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.galleryList[this.curpos][4]).show();
			this.vicontainer.show();
			this.container.show();
		}/*
		alert(this.contain.clientHeight);
		alert(450 + this.desc.clientHeight + "px");
	
		alert(this.contain.clientHeight);
		alert(this.contain.style.height);-*/
			this.contain.style.height = 500 + this.desc.clientHeight + "px";
	},

	next: function(){
		if(this.galleryList.length ==1 && this.curpos >=0)return
		this.curpos++;
		if(this.curpos >= this.galleryList.length) this.curpos = 0;
		preloader = this.curpos + 1;
		if(preloader >= this.galleryList.length) preloader = 0;
		try{
			//var pp = new Image(1,1);
			//pp.src = this.galleryList[preloader][1];
		}catch (e){}
		if(this.carousel)this.carousel.showImageBorder(this.curpos);
		this.updatecontainer();
	},

	prev : function(){
		if(this.galleryList.length==1 && this.curpos >=0) return
		if(this.curpos < 0) this.curpos = this.galleryList.length-1;
		this.curpos--;
		if(this.carousel)this.carousel.showImageBorder(this.curpos);
		this.updatecontainer();
	},
	
	showImageIndex:function(id){
		this.curpos = id;
		this.updatecontainer();
	},
	
	appear: function() {
		if(this.ie && this.bv<=6) {
			this.container.show();
			this.desc.show();
			return;
		}
				this.container.show();
			this.desc.show();
			return;	
	},
	
	showPopUp: function() {
 	//	alert(href = this.url + '?cnt=' + (this.curpos));
	 	window.location.href = this.url + '?cnt=' + (this.curpos);
	},
	//http://blesk.exus.webdev2/img/1/gallery/10575_obr4.jpg
	showBig: function(){
		//window.location.href = 
	}
}

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);
}		