var Halbox;

window.addEvent('domready', function() {
    if($('slider_enabled'))
	{
		$$('div.slider_holder').each(function(item){
			item.getElement('img.left_arrow').addEvent('click',function(){
				
				slider_size=this.getParent().getSize().x-162
				cont=this.getNext('div.slider').getChildren()[0]
				
				photo_count=cont.getChildren().length-1
				margin_max=photo_count*146-slider_size
				
				cont.anim=new Fx.Tween(cont,{duration: 500,transition: Fx.Transitions.Quad.easeIn})
				curr_margin=parseInt(cont.getStyle('margin-left'))
				
				cont.anim.start('margin-left',curr_margin,safe_val(curr_margin+slider_size,-margin_max,0))
			});
			
			item.getElement('img.right_arrow').addEvent('click',function(){
				
				slider_size=this.getParent().getSize().x-162
				cont=this.getNext('div.slider').getChildren()[0]
				
				photo_count=cont.getChildren().length-1
				margin_max=photo_count*146-slider_size
				
				cont.anim=new Fx.Tween(cont,{duration: 500})
				curr_margin=parseInt(cont.getStyle('margin-left'))
				
				cont.anim.start('margin-left',curr_margin,safe_val(curr_margin-slider_size,-margin_max,0))
			});
		})
		
		$$('div.content a').addEvent('click',function(evt){
			new Event(evt).stop();
			Halbox.loadImage(this)
		})
		
		Halbox=new HalGal();
		
	}
});

var HalGal = new Class({
	Implements: [Options, Events]
	,options: {
			
   	}
	,initialize: function(options){
   		this.setOptions(options);
		this.createViewer()
	}
	,viewerInitialised: false
	,viewerInstance: null
	,anim : null
	,imgSize : null
	,createViewer: function()
	{
		if(!this.viewerInitialised)
		{
			this.imgBox=new Element('div',{id : 'halbox_img_show',styles: {padding: '20px'}});
			
			v_styles={position : 'fixed'
					,width : 400
					,height : 400
					,'background-color': '#fff'
					,top: '50%'
					,left: '50%'
					,margin: '-200px 0 0 -200px'
					,paddding: 10
					}
			
			this.blenda=new Element('div',{styles: {
				position: 'fixed',
				top: 0,
				left: 0,
				background: '#000',
				opacity: 0,
				visibility : 'hidden',
				width: '100%',
				height: '100%'
			}});
			
			this.blenda.inject(document.body);
			
			this.blenda.addEvent('click',this.hideElements.bindWithEvent(this, 100))
			
			this.nextButton = new Element('a', {
				'id' : 'halbox_next',
				'html': '>>',
				styles: {
					float: 'right',
					margin : '0 20px 0 0',
					'font-size' : '14px',
					'font-weight' : 'bold',
					color : '#fff',
					background: '#000',
					cursor: 'pointer',
					border: '1px solid #000'
				}
			});
			
			this.prevButton = new Element('a', {
				'id': 'halbox_prev',
				'html' : '<<',
				styles: {
					float: 'left',
					margin : '0 0 0 20px',
					'font-size' : '14px',
					'font-weight' : 'bold',
					color : '#fff',
					background: '#000',
					cursor: 'pointer',
					border: '1px solid #000'
				}
			})
			
			this.nextButton.addEvent('click',this.showNext.bindWithEvent(this, 100))
			this.prevButton.addEvent('click',this.showPrev.bindWithEvent(this, 100))
			
			this.viewerInstance=new Element('div',{styles : v_styles});
			this.anim=new Fx.Morph(this.viewerInstance,{duration : 600});
			this.viewerInstance.fade('hide');
			this.viewerInstance.inject(document.body);
			this.imgBox.inject(this.viewerInstance);
			this.imgLoadBox=new Element('div',{styles : {width: 1,height : 1,overflow : 'hidden'}});
			this.imgLoadBox.inject(this.viewerInstance);
			
			this.nextButton.inject(this.viewerInstance);
			this.prevButton.inject(this.viewerInstance);
			
		}
	}
	,hideElements : function()
	{
		this.blenda.fade('out')		
		this.viewerInstance.fade('out');
		this.hideControls();
		this.currimg.destroy();
		this.imgBox.set('html','');
	}
	,showNext: function()
	{
		if(this.nextImg)
			this.loadImage(this.nextImg)
	}
	,showPrev: function()
	{
		if(this.prevImg)
			this.loadImage(this.prevImg)
	}
	,loadImage: function(link)
	{
		url=link.get('href')
		
		this.nextImg=link.getNext()
		this.prevImg=link.getPrevious()
		
		this.currimg=new Element('img',{src : url});
		this.currimg.addEvent('load',this.imgLoaded.bindWithEvent(this, 100))
		this.currimg.inject(this.imgLoadBox);
	}
	,showControls : function()
	{
		if(this.nextImg) this.nextButton.fade('in')
		if(this.prevImg) this.prevButton.fade('in')
	}
	,hideControls : function()
	{
		this.nextButton.fade('hide')
		this.prevButton.fade('hide')
	}
	,imgLoaded : function(){
		this.getImageSize();
		this.resizeWindow();
	}
	,getImageSize: function()
	{
		this.imgSize=this.currimg.getSize()
	}
	,resizeWindow: function()
	{
		//viewport width
		vpWidth=$(window).getSize();
		
		//czy robimy resize ??
		boxHeight=this.imgSize.y+80
		if(vpWidth.y-80 < this.imgSize.y)
		{
			this.currimg.setStyle('height',vpWidth.y-80)
			boxHeight=vpWidth.y-80
		}
		
		sizeX=this.imgSize.x+40;
		sizeY=boxHeight;
		
		this.blenda.fade(0.6)
		this.viewerInstance.fade('in');
		this.anim.addEvent('complete',this.animComplete.bindWithEvent(this, 100));
		this.hideControls();
		if(this.imgBox.get('html')) this.imgBox.set('html','');
		this.anim.start({'width' : sizeX,'height' : sizeY,'margin-top' : -(sizeY/2),'margin-left' : -(sizeX/2)})
		
	}
	,animComplete: function()
	{
		this.imgBox.set('html','');
		
		this.currimg.fade('hide');
		this.currimg.inject(this.imgBox);
		this.anim.removeEvent('complete',this.animComplete)
		this.currimg.fade('in');
		this.showControls();
	}
});



function safe_val(val,min,max)
{
	if(val > max) return max;
	if(val < min) return min;
	return val;
}
