var CKTslideshow = new Class({ 
	Implements: [Options, Events],
		options: {
			display_div_id:'monitor',
			thumbs_div_id: 'thumbs',
			show_thumbs:'No',
			thumbs_slider:0,
			thumbs_buttons:1,
			thumbs_focus:0,
			transition_period:'5000',
			transition_dur:'250',
			transition_type: Fx.Transitions.Sine.easeIn,
			autoplay:1,
			next_button_id:'',
			previous_button_id:'',
			pause_button_id:'',
			dot_div_id:'',
			numbered_div_id:'',
			display_method:'fixed',
			bg_imgs_folder:'background',
			bg_imgs_ratio:'1.9',
			thumbs_border:'No',
			display_light: 'No',
			light_imgs_folder:'high',
			thumb_imgs_folder:'thumbs'
		},
		initialize: function(options){
			this.setOptions(options);
			this.display_div=document.id(this.options.display_div_id); 
			this.thumbs_div=document.id(this.options.thumbs_div_id); 
			this.pause_button=document.id(this.options.pause_button_id); 
			this.next_button=document.id(this.options.next_button_id); 
			this.previous_button=document.id(this.options.previous_button_id); 
			this.dot_div=document.id(this.options.dot_div_id); 
			this.numbered_div=document.id(this.options.numbered_div_id);
			this.thumbs_a = this.thumbs_div.getElements('a');
			this.busy_atm=0;
			this.loadtoinj;
			this.bg_size_folder;
			this.slides_src=[];
			this.slides=[];
			this.a_thumbs=[];
			this.trans_effe='';
			this.current_slide=-1;
			this.div_array=[];
			this.lighanchor_array=[];
			this.prepare_slides();
			if(this.slides_src.length == 0 || !this.display_div){
				 return;
			}
			else if(this.slides_src.length == 1) {
				this.options.autoplay=0;
				this.pre_load(0,0);
			}
			else {
				if (this.options.show_thumbs == 'Yes') {this.prepare_buttons();}
				this.pre_load(0,0);
			};
			if (this.options.display_method=='liquid'){
				window.addEvent('resize', function(){
					var date = new Date();
					var curDate = null;
					do { curDate = new Date(); } 
					while(curDate-date < 50);
					this.resize_fullscreen_imgs();
				}.bind(this));
			}
		},
		pause_show: function(){
			clearTimeout(this.loadtoinj);
			this.options.autoplay=0;
		},
		purge: function(key){
			this.trans_effe=null;
			this.slides[this.current_slide]=null;
			if (this.options.display_light=='Yes') {
				this.lighanchor_array[this.current_slide].set('html', '');
			}
			else {
				this.div_array[this.current_slide].set('html', '');
			};
			this.current_slide=key;
			this.busy_atm=0;
			if (this.options.autoplay==1) {this.nextimg(this.options.transition_period);}
		},
		resize_fullscreen_imgs: function()	{
			var clwidth = this.display_div.clientWidth;
			var clheight = this.display_div.clientHeight;
			var ratio= clwidth/clheight;
			if (ratio < this.options.bg_imgs_ratio){
				he = clheight;
				wi = clheight*this.options.bg_imgs_ratio;
			}
			else {
				he = clwidth/this.options.bg_imgs_ratio;
				wi = clwidth;
			}
			var bgimagess = this.display_div.getElements('img');
			bgimagess.setStyle('height', he);
			bgimagess.setStyle('width', wi);
			bgimagess.setStyle('display', 'block');
		},
		thumbs_border: function(key)	{
			if (this.options.show_thumbs=='Yes' && this.options.thumbs_border=='Yes' ) {
				this.thumbs_a.setStyle('border', 'none');
				this.thumbs_a.setStyle('padding', '3px');
				this.a_thumbs[key].setStyle('border', '3px solid #FFF');
				this.a_thumbs[key].setStyle('padding', '0');
			}
		},
		img_transition: function(key){
			if (!Browser.ie) {
				this.trans_effe = new Fx.Tween(this.slides[key], {
					duration: this.options.transition_dur,
					transition: this.options.transition_type,
					onComplete: function() {
						this.slides[key].setStyle('z-index', 3);
						if (this.current_slide>=0) {this.purge(key);}
						else {
							this.current_slide=key;
							this.busy_atm=0;
							if (this.options.autoplay==1) {this.nextimg(this.options.transition_period);}
						}
					}.bind(this)
				});
				this.trans_effe.start('opacity',1);
			}
			else {
				this.slides[key].setStyle('opacity',1);
				this.slides[key].setStyle('z-index', 3);
				if (this.current_slide>=0) {
					this.purge(key);
				}
				else {
							this.current_slide=key;
							this.busy_atm=0;
							if (this.options.autoplay==1) {this.nextimg(this.options.transition_period);}
				}
			}
		},
		img_inject: function(key){
			this.busy_atm=1;
			this.slides[key].setStyle('opacity', 0);
			this.slides[key].setStyle('z-index', 4);
			if (this.options.display_light=='Yes') {
				this.slides[key].inject(this.lighanchor_array[key]);
			}
			else {
				this.slides[key].inject(this.div_array[key]);
			};
			if (this.options.display_method=='liquid') {this.resize_fullscreen_imgs();}
			this.thumbs_border(key);
			this.img_transition(key);
		},
		pre_load: function(key, inj_delay){
			if (this.busy_atm==0) {
				this.busy_atm=1;
				var start_time = new Date();
				if (this.options.show_thumbs=='Yes'){
					var spin = new Spinner(this.a_thumbs[key], {inject: {where: 'inside',target: null}});
					
					spin.show();
				}
				this.slides[key] = Asset.image(this.slides_src[key], {					
					onLoad: function(){
						var afterload_time = new Date();
						var load_time = (afterload_time-start_time);
						if (load_time>inj_delay) {load_time=inj_delay;}
						if (this.options.show_thumbs=='Yes'){
							spin.hide();
						}
						this.busy_atm=0;
						this.loadtoinj = (function (){this.img_inject(key)}).delay(inj_delay - load_time, this);
					}.bind(this)
				});
			}
		},
		nextimg: function(prel_delay){
			if(this.current_slide < this.slides_src.length-1) {this.pre_load(this.current_slide+1, prel_delay);}
			else {this.pre_load(0, prel_delay);}
			
		},
		previmg: function(){
			
		},
		gotoimg: function(){
			
		},
		prepare_buttons: function(){
			if(this.pause_button){
				this.pause_button.addEvents({
					click: function() {
						if (this.autoplay==0) {
							this.autoplay=1;
							this.start_show();
							this.pause_button.setStyle('background-image', 'url("\cktassets/img/galpause.png")');
						}
						else {
							this.autoplay=0;						
							this.pause_show();
							this.pause_button.setStyle('background-image', 'url("\cktassets/img/galplay.png")');
						}
					}.bind(this)
				});
			}
			if(this.next_button){
				this.next_button.addEvents({
					click: function() {
						this.pause_show();
						this.nextimage();
						if (this.pause_button) {
							this.pause_button.setStyle('background-image', 'url("\cktassets/img/galplay.png")');
						}
					}.bind(this)
				});
			}
			if(this.previous_button){
				this.previous_button.addEvents({
					click: function() {
						this.pause_show.bind(this);
						this.nextimage.bind(this);
						if (this.pause_button) {
							this.pause_button.setStyle('background-image', 'url("\cktassets/img/galplay.png")');
						}
					}.bind(this)
				});
			}
			this.a_thumbs.each( function(thumb,key) {
				thumb.addEvents({
					click: function(event) {
						event.stop()
						if (this.current_slide!=key){
							this.pause_show();
							this.pre_load(key);
						}
					}.bind(this)
				});
			}, this);		
		},
		prepare_slides: function(){
			if (this.options.show_thumbs=='No') {this.thumbs_div.setStyle('visibility', 'hidden');};
			this.monitor_size();
			Array.each(this.thumbs_a, function(thumb, key){
				this.a_thumbs[key] = thumb;
				this.div_array[key] = new Element('div', {id: this.options.display_div_id+key});
				this.div_array[key].inject(this.display_div);
				if (this.options.display_method=='fixed'){this.slides_src[key] = thumb.get('href');}
				if (this.options.display_method=='liquid'){
					this.slides_src[key] = this.bg_size_folder+thumb.get('href').substring(thumb.get('href').lastIndexOf("/")+1);
				}
				if (this.options.display_light=='Yes'){
					var a_step = thumb.get('href').substring(0,thumb.get('href').lastIndexOf("/")+1);					
					var c_step = a_step+this.options.light_imgs_folder+thumb.get('href').substring(thumb.get('href').lastIndexOf("/"));
					this.lighanchor_array[key] = new Element('a', {
						'href': c_step,
						'title': thumb.get('title'),
						'rel': 'milkbox:gal',
						'styles': {'display': 'block'}
					})
					this.lighanchor_array[key].inject(this.div_array[key]);
					milkbox.reloadGalleries();
				}
			}, this);		
		},
		monitor_size: function(){
			if (this.options.display_method=='liquid'){
				var screenhei=window.screen.height;		
				var screenwid=window.screen.width;		
				this.bg_size_folder=this.options.bg_imgs_folder+'/480/';
				if (screenwid>480){this.bg_size_folder=this.options.bg_imgs_folder+'/800/';}
				if (screenwid>800){this.bg_size_folder=this.options.bg_imgs_folder+'/1024/';}
				if (screenwid>1024){this.bg_size_folder=this.options.bg_imgs_folder+'/1280/';}
				if (screenwid>1280){this.bg_size_folder=this.options.bg_imgs_folder+'/1366/';}
				if (screenwid>1366){this.bg_size_folder=this.options.bg_imgs_folder+'/1440/';}
				if (screenwid>1440){this.bg_size_folder=this.options.bg_imgs_folder+'/1600/';}
				if (screenwid>1600){this.bg_size_folder=this.options.bg_imgs_folder+'/1680/';}
				if (screenwid>1680){this.bg_size_folder=this.options.bg_imgs_folder+'/1920/';}
				if (screenwid>1920){this.bg_size_folder=this.options.bg_imgs_folder+'/2560/';}			 
			}
		}
});

