$(document).ready(function(){
	Gallery.init();
});



var Gallery = {
	clicked:0,
	init:function(open){
		//prepinac na videa
		$('div.gallery>ul>li>a').click(function(){return Gallery.setSelectedTab(this)});
		//rozbalit thumb
		$('.area .items a').click(function() {return Gallery.revealThumb(this)});
		
		$('div.gallery').each(function(){
			var s=new Scroller(),
			firstMedia;
			s.init($('.area:visible',this));
			this.scrollInstance=s;
			Gallery.switchSeries($('.holder'),this);
			if($(this).is('.open') || (typeof(open)!='undefined') && open){
				firstMedia=$('.gallery .items:visible a:first');
				if(firstMedia.length){
					Gallery.revealThumb(firstMedia);
				}
			}
		});
		
		$('div.media-holder .gotoscreenshots').click(function(){
			window.open(this.href);
			return false;
		});
	},
	getParentGallery:function(elm){
		return $(elm).parents('div.gallery:eq(0)');
	},
	setSelectedTab : function(elm){
		$(elm).parents('li:eq(0)').siblings('li').removeClass('selected');
		$(elm).parents('li').addClass('selected');
		var galleryParent=this.getParentGallery(elm); 
		if($(elm).parents('li:eq(0)').is('.video')){
			$('div.photos',galleryParent).fadeOut('fast',function(){
				$('div.videos',galleryParent).fadeIn('fast',function(){
					Gallery.getScrollInstance($(this)).init(this);
				});
			});
		}else{
			$('div.videos', galleryParent).fadeOut('fast',function(){
				$('div.photos',galleryParent).fadeIn('fast',function(){
					Gallery.getScrollInstance($(this)).init(this);
				});
			});
		}
		$('.controls',galleryParent).hide();
		return false;
	},
	revealThumb : function(elm){
		var galleryParent = this.getParentGallery(elm);
		this.showMedia($(elm));
		return false;	
	},
	getScrollInstance:function(el){
		if(el.is('.gallery')){
			return el[0].scrollInstance;
		}
		return el.parents('div.gallery')[0].scrollInstance;
	},
	showMedia:function(source){
		var areaElement=source.parents('div.area'),
		holder=$('.media-holder',areaElement),
		anchor=$('.gotoscreenshots',holder),
		media=$('img',holder),
		prevImg=source.prev(),
		nextImg=source.next(),
		prevBtn=$('.prev-media',areaElement),
		nextBtn=$('.next-media',areaElement),
		preloader=$(new Image()),
		mediaHeight,
		size,
		imgNum;
		holder.fadeIn('slow');
		if(media.length){
			media.css('visibility','hidden');
			media.attr('src',base_static_dir+'images/empty.gif');
			preloader.load(function(){
				media.attr('src',preloader.attr('src'));
				media.css('visibility','visible');
			});
			preloader.attr('src',source.attr('href'));
			size=source.attr('class').replace(/ selected/,'').split(';');
			
			imgNum=source.attr('id').replace(/image-/,'');
			if(anchor.length){
				anchor
					.css('width',size[0]+'px')
					.attr('href',anchor.attr('href').replace(/\?img=[0-9]+#img[0-9]+/,'')+'?img='+imgNum+'#img'+imgNum);
			}
			media.attr('width',size[0]);
			media.attr('height',size[1]);
			mediaHeight=media.height();
		}else{
			var fc=$('.flashcontainer',holder);
			fc.html(this.getEmbedCode($(source).attr('id').replace(/^id/,'')));
			mediaHeight=fc.height();
		}
		this.getScrollInstance(areaElement).scrollToMedia(source);
		if(prevImg.length){
			prevBtn.show();
		}else{
			prevBtn.hide();
		}
		if(nextImg.length){
			nextBtn.show();
		}else{
			nextBtn.hide();
		}
		$('.prev-media,.next-media',areaElement)
			.css('height',mediaHeight+'px')
			.unbind('click')
			.click(function(){
				if($(this).is('.prev-media')){
					Gallery.showMedia(prevImg);
				}else{
					Gallery.showMedia(nextImg);
				}
				return false;
			});
		$('.selected',areaElement).removeClass('selected');
		source.addClass('selected');
		if(!this.clicked){
			this.clicked=1;
			var targetOffset=areaElement.offset().top+areaElement.height()+parseInt(areaElement.css('padding-top'))+parseInt(areaElement.css('padding-bottom'))-$(window).height()+(parseInt(($(window).height()-areaElement.height())/2));
			$('html,body').animate({scrollTop:targetOffset},1000);
		}
	},
	getEmbedCode: function(code){
		return '<object width="540" height="344"><param name="movie" value="http://www.youtube.com/v/'+code+'&hl=cs&fs=1&showsearch=0&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+code+'&hl=cs&fs=1&showsearch=0&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="540" height="344"></embed></object>';
	},
	switchSeries:function(p){
		var sel=$('select',p),
		count,left,right,setArrows,
		self=this;
		if(sel.length){
			count=$('option',p).length,
			left=$('.left',p),
			right=$('.right',p),
			setArrows=function(){
				var index=sel[0].selectedIndex;
				left.show();
				right.show();
				if(index==0){
					left.hide();
				}else if(index==count-1){
					right.hide();
				}
			},
			reload=function(){
				var gal=self.getParentGallery(sel),
				open=$('.media-holder:visible',gal).length;
				$('.area:visible',gal).addClass('loading');
				gal.load(window.location.protocol+'//'+window.location.host+sel.parent().attr('action')+'/',{ajax:'',s:$('option:selected',sel).val()},function(){
					Gallery.init(open);
				});
			};
			setArrows();
			sel.change(function(){
				reload();
			});
			left.click(function(){
				if(sel[0].selectedIndex>0){
					sel[0].selectedIndex-=1;
					setArrows();
					reload();
				}
			});
			right.click(function(){
				if(sel[0].selectedIndex<count-1){
					sel[0].selectedIndex+=1;
					setArrows();
					reload();
				}
			});
		}
	}
}

var Scroller=function(){
	this.parent='div.photos';
	this.init=function(p){
		if(typeof(p)!='undefined'){
			this.parent=p;
		}
		this.it=$('.items',this.parent);
		this.itc=$('.items-cutter',this.parent);
		this.controls=$('.controls',this.parent);
		this.scroller=$('span.scroll',this.parent);
		this.scc=$('.scroll-container',this.parent);
		this.iw=this.it.width();
		this.icw=this.itc.width();
		this.itw=this.iw-this.icw;
		if(this.itw>0){
			this.controls.show();
			this.sccw=this.scc.width();
			this.sw=this.scroller.width();
			this.scw=this.sccw-this.sw;
			this.scroll();
		}
	}
	this.scroll=function(){
		var self=this;
		this.scroller
			.draggable({addClasses:false,axis:'x',containment:'parent'})
			.unbind('drag').unbind('click')
			.bind('drag',function(e,ui){
				self.itc.scrollLeft(parseInt(parseInt(ui.position.left)/self.scw*self.itw));
			});
		$('.left,.right',this.controls).click(function(){
			var sl=self.itc.scrollLeft(),
			a=parseInt(self.icw/2),
			x,y;
			if($(this).is('.left')){
				x=sl-a<0?0:sl-a;
			}else{
				x=sl+a>self.itw?self.itw:sl+a;
			}
			y=parseInt(x/self.itw*self.scw);
			self.itc.stop(true).animate({scrollLeft:x});
			self.scroller.stop(true).animate({left:y+'px'});
			return false;
		});
		self.scc.click(function(e){
			if(!$(e.target).is('.scroll')){
				var k=(e.pageX-parseInt(self.scc.offset().left))/self.sccw,
				x=parseInt(k*self.itw),
				y=parseInt(k*self.scw);
		 		self.itc.stop(true).animate({scrollLeft:x});
		 		self.scroller.stop(true).animate({left:y+'px'});
			}
		});
	}
	this.scrollToMedia=function(media){
		var prevMedias=media.prevAll(),
		margin=parseInt(media.css('marginRight')),
		prevWidth=0,
		tk,k,x,y;
		if(this.itw>0){
			prevMedias.each(function(){
				prevWidth+=$(this).width()+margin;
			});
			prevWidth-=parseInt(this.icw/2-(media.width()+margin)/2);
			tk=prevWidth/this.itw;
			k=tk>1?1:tk<0?0:tk;
			x=parseInt(k*this.itw);
			y=parseInt(k*this.scw);
	 		this.itc.stop(true).animate({scrollLeft:x});
	 		this.scroller.stop(true).animate({left:y+'px'});
	 	}
	}
}