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

		initialize: function( options )	{

			this.setOptions(options);
			var ops = this.options;
			
			this.lgh = this.options.images.length;
			this.index = -1;

			this.flash_video = new YoutubeZoom({
				color: '999999',
				clip_id: 'ZuqPOluaJS8',
				size:{
					width: 580,
					height:465
				},
				container:ops.container_id,
				title_id:ops.title_id,
				back_reference:this
			});
			
			this.image_zoom = new ImageZoom({
				container:ops.container,
				title_id:ops.title_id,
				back_reference:this
			});
			
			
			
			if ( ops.indicator ) {
				this.indicator = ops.indicator.getElement('b');
				this.indicator.set('html', '1');
				ops.indicator.getElement('sub').set( 'html','/'+this.lgh );
			}
			
			// blank links
			ops.images.each( function(el,i) {
				el.addEvent('click', function(ev) {
					ev.stop();
					this.showItem( i );
				}.bind(this) );
			}.bind(this) );
			
			if ( ops.rgt_button ) {
				ops.rgt_button.addEvent('click', function(ev) {
					ev.stop();
					this.nextObject();
				}.bind(this) );
			}

			if ( ops.lft_button ) {
				ops.lft_button.addEvent('click', function(ev) {
					ev.stop();
					this.prevObject();
				}.bind(this) );
			}
			
		},
		
		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 )	{
			if ( this.index == index ) {
				return;
			}
			this.index = index;
			if ( this.indicator )
				this.indicator.set('html', index +1.0 );
			
			var ops = this.options;
			
			var list = ops.images;
			var link = list[index].get('href');
			var big = list[index].get('rel');
			
			var type = link.substring(0, link.indexOf(':') );
			
			if ( type == '' || type == 'http' || type == -1 ) { // image
				var l = link;
				var nazwa = list[index].get('title');
				if ( this.flash_video.opened == 1 ) {
					this.flash_video.removeObj();
					this.image_zoom.getObj( link, big, nazwa, this.index ).inject( ops.container, 'bottom');
				} else {
					this.image_zoom.setTarget( link, big, nazwa, this.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, type, this.index );
			} else if ( type == 'video' ) {
				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, type, this.index );
			}
			
		}
		
});


var VimeoZoom = new Class({
		Implements: Options,
		options: {
			color: 'e7e91d',
			clip_id: '',
			container:'slides',
			flash_container:'flash_obj',
			size:{
				width: 398,
				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 size = w+'x'+h;
				
				c.destroy();
				
				this.setObj( clip_id, size );
			} else {
				
			}
			
		},
		
		setObj: function( clip_id, size, nazwa, indx ) {
			swfobject.removeSWF( this.options.container );

			this.index = indx;

			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 YoutubeZoom = new Class({
		Implements: Options,
		options: {
			color: 'e7e91d',
			clip_id: '',
			container:'slides',
			flash_container:'flash_obj',
			size:{
				width: 580,
				height:465
			}
		},

		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 size = w+'x'+h;
				
				c.destroy();
				
				this.setObj( clip_id, size );
			} else {
				
			}
			
		},
		
		setObj: function( clip_id, size, nazwa, type, indx ) {
			swfobject.removeSWF( this.options.container );

			this.index = indx;

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

			var s = size.split('x');
			if ( type == 'youtube' ) {
				var src = "http://www.youtube.com/v/"+clip_id+"&hl=en&fs=1&rel=0&color1=0x3a3a3a&color2="+this.options.color;
				var par = { 
					allowfullscreen:"true",
					allowscriptaccess:"always"
				};
			} else if ( type == 'video' ) {
				var src = "/flash/v.swf";
				var par = {
					flashvars:"playList=/videoXml/"+clip_id,
					allowfullscreen:"true",
					allowscriptaccess:"always"
				};
			}
			var att = { data:src, width:s[0], height:s[1] };
			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;

			this.options.back_reference.options.images.each( function (im) {
					im.removeClass('selected');
			});
			this.options.back_reference.options.images[ this.index ].addClass('selected');
		},
		
		removeObj: function () {
			this.opened = 0;
			swfobject.removeSWF( this.options.flash_container );
		}
		
		
});


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

		initialize: function( options )	{
			this.setOptions( options );
			
			var ops = this.options;
			
			if ( ops.container.getElement('a') ) {
				
				this.obj = ops.container.getElement('a');
				this.img = this.obj.getElement('img');
				
			} else {
			
				this.obj = new Element('a', {
						href:'#',
						id:'zoom',
						rel:'zoom'
				});
				this.img = new Element('img');
				this.obj.grab(this.img);
				
			}

			SqueezeBox.assign(this.obj);
			
			this.big = '';
			
			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, big, nazwa, indx )	{
			this.setTarget(src, big, nazwa, indx);
			return this.obj;
		},
		
		setTarget: function( src, big, nazwa, indx ) {
			var s = this.img.getSize();

			this.big = big;
			this.nazwa = nazwa;
			
			this.index = indx;

			if ( s.x != '' ) {
				this.preload.setStyles({
					left:(this.size.x - s.x)/2,
					width:s.x,
					height:s.y
				});
			} else {
				this.preload.setStyles({
					left:0,
					top:0,
					width:580,
					height:465
				});
			}
			
			this.preload.inject( this.options.container, 'top' );
			this.preloadimg.set('src', src );
		},
		
		show: function()	{
			this.img.set( 'src', this.preloadimg.src );
			this.options.back_reference.options.images.each( function (im) {
					im.removeClass('selected');
			});
			this.options.back_reference.options.images[ this.index ].addClass('selected');
		},
		
		showReal: function()	{
			if ( $(this.options.title_id) ) {
				$(this.options.title_id).set( 'text', this.nazwa );
			}
			
			this.obj.set( 'href', this.big );
			if ( this.preload )
				this.preload = this.preload.dispose();
		},
		
		grabObj: function ( obj ) {
			this.obj = obj.dispose();
		}
		
});




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

	if ( $('movie_chooser') && $('movie_container') ) {
	
		var s = new Switcher({
				images:$$('#movie_chooser a'),
				container:$('movie_container'),
				zoom_link:$('zoom'),
				zoom_obj:$$('#zoom img')[0],
				container_id:'movie_container',
				title_id:'file-title',
				rgt_button:null,
				lft_button:null,
				indicator:null
		});
		
		if ( typeof(onswichload) == 'function' ) {
			var t = onswichload();
			if ( t == 'video' ) {
				s.showItem(0);
				console.log( t );
			}
		}
	}

});

