var Switcher = new Class({
	Implements: Options,
	options: {
		images: new Array(),
		container:'',
		container_id:'slides',
		rgt_button: null,
		lft_button: null,
		indicator:null
	},

	initialize: function( options )	{

		this.setOptions(options);
		var ops = this.options;
		
		if ( this.options.container == undefined || this.options.container == null ) {
			return false;
		}
		
		this.caption_container = $('foto_caption');

		this.flash_video = new YoutubeZoom({
			color: 'e7e91d',
			clip_id: '1050139',
			size:{
				width: 550,
				height:333
			},
			container:ops.container,
			title_id:ops.title_id
		});

		this.image_zoom = new ImageZoom({
			container:ops.container,
			title_id:ops.title_id,
			control:this
		});

		this.lgh = this.options.images.length;
		this.index = 0;

		this.image_zoom.addEvent('loaded', this.loadedObj.bind(this) );

	},
	
	indicator: '',
	
	setup: function() {
		this.disableNextPrevious();
		this.setNextPrevious();
		this.setIndicator();
		this.showItem( this.index );
		this.setThumbnails(this);
//		this.image_zoom.removeEvents('loaded');
	},
	
	setThumbnails: function( that )	{
		// blank links
		var that = this;
		that.options.images.each( function(el,i) {
			el.removeEvents('click');
			el.addEvent('click', function(ev,i) {
				ev.stop();
				that.index = i;
				that.showItem( i );
			}.bindWithEvent( that, i ));
		}.bind(that));
	},
	
	setNextPrevious: function() {
		this.options.rgt_button.addEvent('click', function(ev) {
			ev.stop();
			this.nextObject();
		}.bind(this) );

		this.options.lft_button.addEvent('click', function(ev) {
			ev.stop();
			this.prevObject();
		}.bind(this) );
	},
	
	disableNextPrevious: function() {
		this.options.rgt_button.removeEvents('click');
		this.options.lft_button.removeEvents('click');
	},
	
	setIndicator: function() {
		if ( this.options.indicator != undefined && this.options.indicator != null ) {
			this.indicator = this.options.indicator.getElement('span.indicator');
			this.indicator.set('html', this.index.toInt()+1 );
			this.options.indicator.getElement('span.all').set( 'html','/'+this.lgh );
		}
	},
	
	nextObject: function()	{
		this.index++;
		if ( this.index >= this.lgh ) {
			this.index = 0;
		}
		this.showItem( this.index );
	},
	
	prevObject: function()	{
		this.index--;
		if ( this.index == -1 ) {
			this.index = this.lgh - 1.0;
		}
		this.showItem( this.index );
	},
	
	showItem: function( index )	{
		this.index = index;
		this.indicator.set('html', index +1.0 );
		
		var ops = this.options;
		
		var list = ops.images;
		var link = list[index].get('href');
		
		var type = link.substring(0, link.indexOf(':') );

		if ( type == '' || type == -1 || type == 'http' ) { // image
			var l = link;
			var nazwa = list[index].get('title');
			if ( $('flash_obj') ) {
				this.flash_video.removeObj();
				this.image_zoom.getObj( link, nazwa, index ).inject( ops.container, 'bottom' );
			} else {
				this.image_zoom.setTarget( link, nazwa, index );
			}
		} else if ( type == 'youtube' ) {
			var obj;
			var clip = link.substring(link.indexOf(":")+1, link.length ).split(',');
			var clip_id = clip[0];
			var size = clip[1];
			var nazwa = clip[2];
			
			if ( obj = ops.container.getElement('a') ) {
				this.image_zoom.grabObj(obj);
			} else {
			}
			this.flash_video.setObj( clip_id, size, nazwa );
		}
		this.caption_container.set('html', nazwa );
	},

	loadedObj: function()	{
//		console.log( this.index );
		// thumbnail
		this.setThumbnail( this.indx );
	},
	
	setThumbnail: function( index ) {
		// blank links
		this.options.images.each( function(el,i) {
			var p = el.getParent();
			var s;
			if ( i == index ) {
				if ( s = p.getElement('span') ) {
					s.dispose();
				}
				s = new Element('span');
				p.grab(s);
			} else {
				s = p.getElement('span');
				if ( s )
					s.dispose();
			}
		});
	}
	
});

var YoutubeZoom = new Class({
		Implements: Options,
		options: {
			color: 'e7e91d',
			container:'slides',
			flash_container:'flash_obj',
			size:{
				width: 550,
				height:333
			}
		},

		initialize: function( options )	{
			this.setOptions( options );
			
			this.opened = 0;
			
			var ops = this.options;

			var c = ops.container.getElement('div');
			if ( c ) {

				var x = c.get('html');
				

				var reg = /http:\/\/www.youtube.com\/v\/([^&]*)/;
				var clip_id = reg.exec(x)[1];
				reg = /width="?([0-9]*)/;
				var w = reg.exec(x)[1];
				reg = /height="?([0-9]*)/;
				var h = reg.exec(x)[1];
				var nazwa = '';//$('file-title').get('text');

				var size = w+'x'+h;

				c.destroy();
				
				this.setObj( clip_id, size, nazwa );
			} else {
				
			}
			
		},
		
		setObj: function( clip_id, size, nazwa ) {
			swfobject.removeSWF( this.options.container );

			if ( !$(this.options.flash_container) ) {
				var d = new Element('div',{
						id:this.options.flash_container
				});
				d.inject( $(this.options.container), 'top' );
			}
			
			var src = "http://www.youtube.com/v/"+clip_id+"&hl=en_US&fs=1";
			var s = size.split('x');

			var att = { data:src, width:s[0], height:s[1] };
			var par = { 
					allowfullscreen:"true",
					allowscriptaccess:"always"
			};
			var id = this.options.flash_container;
			
			this.obj = swfobject.createSWF(att, par, id);
			
			if ( $(this.options.title_id) ) {
				$(this.options.title_id).set( 'text', nazwa );
			}
			this.opened = 1;
		},
		
		removeObj: function () {
			this.opened = 0;
			swfobject.removeSWF( this.options.flash_container );
		}

});


var VimeoZoom = new Class({
		Implements: Options,
		options: {
			color: 'e7e91d',
			clip_id: '',
			container:'slides',
			flash_container:'flash_obj',
			size:{
				width: 540,
				height:300
			}
		},

		initialize: function( options )	{
			this.setOptions( options );
			
			this.opened = 0;
			
			var ops = this.options;

			var c = $(ops.container).getElement('div');
			if ( c ) {

				var x = c.get('html');

				var reg = /clip_id=([0-9]*)/;
				var clip_id = reg.exec(x)[1];
				reg = /width="?([0-9]*)/;
				var w = reg.exec(x)[1];
				reg = /height="?([0-9]*)/;
				var h = reg.exec(x)[1];
				var nazwa = $('file-title').get('text');

				var size = w+'x'+h;

				c.destroy();
				
				this.setObj( clip_id, size, nazwa );
			} else {
				
			}
			
		},
		
		setObj: function( clip_id, size, nazwa ) {
			swfobject.removeSWF( this.options.container );

			if ( !$(this.options.flash_container) ) {
				var d = new Element('div',{
						id:this.options.flash_container
				});
				d.inject( $(this.options.container), 'top' );
			}

			var src = "http://vimeo.com/moogaloop.swf?clip_id="+clip_id+"&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color="+this.options.color+"&amp;fullscreen=1";
			var s = size.split('x');

			var att = { data:src, width:s[0], height:s[1] };
			var par = { 
					allowfullscreen:"true",
					allowscriptaccess:"always"
			};
			var id = this.options.flash_container;
			
			this.obj = swfobject.createSWF(att, par, id);
			
			if ( $(this.options.title_id) ) {
				$(this.options.title_id).set( 'text', nazwa );
			}
			this.opened = 1;
		},
		
		removeObj: function () {
			this.opened = 0;
			swfobject.removeSWF( this.options.flash_container );
		}

});


var ImageZoom = new Class({
		Implements: [Options,Events],
		options: {
			
		},

		initialize: function( options )	{
			this.setOptions( options );
			
			var ops = this.options;
			
			if ( ops.container.getElement('img') ) {
				this.img = ops.container.getElement('img');
			} else {
				this.img = new Element('img');
			}
			
			this.indx = '';
			
			this.size = this.options.container.getSize();

			this.preload = new Element('div', {});
			this.preload.addClass('preloader');
			this.preload.setStyle('opacity',0.6);

			this.preloadimg = new Element('img', {
				events: {
					load: function(){
						this.show.delay(100, this)
					}.bind(this)
				}
			});

			this.img.addEvent('load', function(ev) {
					this.showReal.delay(10, this)
			}.bind(this));
			
		},
		
		getObj: function( src, nazwa, indx )	{
			this.setTarget(src, nazwa, indx);
			return this.img;
		},
		
		setTarget: function( src, nazwa, indx ) {
			var s = this.img.getSize();
			this.src = src;
			this.nazwa = nazwa;
			this.indx = indx;
			
			this.preload.setStyles({
				left:0,
				width:s.x,
				height:s.y
			});
			
			this.preload.inject( this.options.container, 'top' );
			this.preloadimg.set('src', src );
		},
		
		show: function()	{
			this.img.set( 'src', this.preloadimg.src );
		},
		
		showReal: function()	{
			this.fireEvent('loaded');
			if ( this.preload )
				this.preload = this.preload.dispose();
		},
		
		grabObj: function ( obj ) {
			this.obj = obj.dispose();
		}
		
});

var GalleryControler = new Class({
	Implements: Options,
	options: {
		galleries:null,
		switchlink:null,
		thumbslist:null,
		maska:null
	},
	
	initialize: function( options ) {

		this.setOptions(options);

		this.slider = new Fx.Morph( this.options.maska, {
			duration:1000,
			transition:'quint:out'//'elastic:out'
		});

		this.options.switchlink.each( function(el,i) {
			el.addEvent( 'click', function(e, i) {
				e.stop();
				if ( i != this.activeGallery )
					this.setupGallery(i);
			}.bindWithEvent(this,i) );
		}.bind(this) );

		$$('.close_btn').each( function(el) {
			el.addEvent('click', function(e) {
				e.stop();
				this.slider.start( { 'height': 0 } );
				$('thumbs').removeClass('button_pressed');
				$('thumbs').addClass('button');
			}.bind(this));
		}.bind(this));

		this.activeGallery = 0;

		this.setupGallery(this.activeGallery);

		$('thumbs').addEvent('click', this.showThumbs.bind(this) );
	
	},
	
	setupGallery: function( gallery ) {
		this.activeGallery = gallery;
		var g = this.options.galleries[ this.activeGallery ];

		// thumbsy
		this.options.thumbslist.each( function(el) {
			el.setStyle('display', 'none');
		});
		this.options.thumbslist[ this.activeGallery ].setStyle( 'display', 'block' );
		var dim = this.options.thumbslist[ this.activeGallery ].getCoordinates();
		var height = dim.height;
		var h = this.options.maska.getStyle('height').toInt();

		if (  h > 0 && h !=  height ) { // otwarte
			this.slider.start( { 'height': height } );
		}
		g.setup();
	},
	
	showThumbs: function(e)	{
		e.stop();
		this.slider.pause();
		if ( this.options.maska.getStyle('height').toInt() > 0 ) {
			$('thumbs').removeClass('button_pressed');
			$('thumbs').addClass('button');
			this.slider.start( { 'height': 0 } );
		} else {
			$('thumbs').addClass('button_pressed');
			$('thumbs').removeClass('button');
			var dim = this.options.thumbslist[ this.activeGallery ].getCoordinates();
			var height = dim.height;
			this.slider.start( { 'height': height } );
		}
		
	}
	
});


var goer;

window.addEvent('domready', function()	{

	var gals = [];
	if ( $$('.thumbs_container').length > 1 ) {
		var s_w = new Switcher({
			images:$$('#thumbs_container_wideo div.grid_2 a'),
			container:$('gallery_img'),
			container_id:'gallery_img',
			rgt_button:$('nxt'),
			lft_button:$('prv'),
			indicator:$('count')
		});
		gals.push(s_w);
	}
	
	var s_z = new Switcher({
		images:$$('#thumbs_container_zdjecia div.grid_2 a'),
		container:$('gallery_img'),
		container_id:'gallery_img',
		rgt_button:$('nxt'),
		lft_button:$('prv'),
		indicator:$('count')
	});
	gals.push(s_z);
	
	var gals = new GalleryControler({
		galleries:gals,
		switchlink: $('gallery_menu').getElements('a'),
		thumbslist:$$('.thumbs_container'),
		maska:$('thumbs_mask')
	});
	

});

