window.addEvent('load', function(){
	
	// Slider initialization function
	
	function slider_init(slider_type) {
		
		// SETTING UP THE ANIMATIONS VARIABLES
		var duration_in = 700;
		var duration_out = 400;
		var duration_frame = 350;
		var duration_ctrl = 100;
		var duration_label = 500;
		var duration_plex = 200;
		var transition_in = Fx.Transitions.Back.easeInOut;
		var transition_out = Fx.Transitions.Back.easeIn;
		var transition_frame = Fx.Transitions.Expo.easeOut;
		var transition_ctrl = Fx.Transitions.Elastic.easeOut;
		var transition_label = Fx.Transitions.Expo.easeOut;
		var transition_plex = Fx.Transitions.Linear;
		
		// OFTEN USED FUNCTIONS -------------------------------------------------------
		
		function animate_in(el, attribute, a, b) {
			el.set('tween', {duration: duration_in, transition: transition_in});	
			el.tween(attribute, [a, b]);
		}
		
		function animate_out(el, attribute, a, b) {
			el.set('tween', {duration: duration_out, transition: transition_out});	
			el.tween(attribute, [a, b]);
		}
		
		function animate_frame(el, attribute, a) {
			el.set('tween', {duration: duration_frame, transition: transition_frame});	
			el.tween(attribute, a);
		}
		
		function animate_ctrl(el, attribute, a) {
			if ( Browser.Engine.trident )
			{
				el.set('tween', {duration: duration_ctrl, transition: transition_ctrl});	
				switch(a)
				{
					case 0.5: el.tween('top', '164px'); break;
					case 1: el.tween('top', '162px'); break;					
				}
				
			}
			else 
			{
				el.set('tween', {duration: duration_ctrl, transition: transition_ctrl});	
				el.tween(attribute, a);
			}
		}
		
		function animate_label(el, attribute, a) {
			el.set('tween', {duration: duration_label, transition: transition_label});	
			el.tween(attribute, a);
		}
		
		function animate_plex(el, attribute, a) {
			el.set('tween', {duration: duration_plex, transition: transition_plex});
			el.tween(attribute, a);
		}
		
		function el_width(el) { var width = parseInt(el.getStyle('width')); return width; }
		function el_height(el) { var height = parseInt(el.getStyle('height')); return height; }
		function el_left(el) { var left = parseInt(el.getStyle('left')); return left; }
		function el_right(el) { var right = parseInt(el.getStyle('right')); return right; }
		function el_top(el) { var top = parseInt(el.getStyle('top')); return top; }
		function el_bottom(el) { var bottom = parseInt(el.getStyle('bottom')); return bottom; }
		function console(msg) { var console = $('console').appendText(msg + ', '); }
		
		var slider_width = el_width($('duotive-slider'));
		var ctrl_left = $$('#duotive-slider #ctrl_left');
		var ctrl_right = $$('#duotive-slider #ctrl_right');
		var ctrl_left_pos = el_top(ctrl_left);
		var ctrl_right_pos = el_top(ctrl_right);
		var motion = true;
		var counter = 0;
		var imgs_nr = 0;
		var plex_slices = 20;
		var plex_direction = 'down';
		
		// SLIDER TYPE "THREE" FUNCTION -------------------------------------------------------
		
		function slider_three_start() {
		
			// SETTING UP THE FRAMES ---------------------------------------
			
			var frame_middle = $$('#duotive-slider .frame_middle');
			var frame_left = $$('#duotive-slider .frame_left');
			var frame_right = $$('#duotive-slider .frame_right');
			
			var frame_middle_pos = (slider_width / 2 ) - ( el_width(frame_middle) / 2);
			var frame_left_pos = 89;
			var frame_right_pos = 89;
			
			// SETTING UP THE FRAME IMAGES --------------------------------
			
			var frame_middle_imgs = $$('#duotive-slider .frame_middle a');
			var frame_left_imgs = $$('#duotive-slider .frame_left a');
			var frame_right_imgs = $$('#duotive-slider .frame_right a');
			
			imgs_nr = frame_middle_imgs.length;
			
			// MAKING THE DATABASE ARRAYS
			
			var middle_db = new Array();
			var left_db = new Array();
			var right_db = new Array();
			
			function make_db() {
				frame_middle_imgs.each(function (item, index){ middle_db[index] = [ item.get('html'), item.getProperty('href') ]; });
				frame_left_imgs.each(function (item, index){ left_db[index] = [ item.get('html'), item.getProperty('href') ]; });
				frame_right_imgs.each(function (item, index){ right_db[index] = [ item.get('html'), item.getProperty('href') ]; });
				frame_middle.set('html', '');
				frame_left.set('html', '');
				frame_right.set('html','');
			}

			make_db();
			
			// CHANGE IMAGES FUNCTION
			
			function change_imgs() {
						frame_left.set('html', '<a href="' + left_db[counter][1] + '">' + left_db[counter][0] + '</a>');
						frame_middle.set('html', '<a href="' + middle_db[counter][1] + '">' + middle_db[counter][0] + '</a>');
						frame_right.set('html', '<a href="' + right_db[counter][1] + '">' + right_db[counter][0] + '</a>');
			}
			
			// ADDING EVENTS TO THE LEFT/RIGH CONTROLS 
			
			ctrl_left.addEvents({
				mouseenter: function() {
					animate_ctrl(this, 'opacity', 0.5);
				},
				mouseleave: function() {
					animate_ctrl(this, 'opacity', 1);
				},
				click: function() {
					if ( motion == false ) {
						counter--;
						if ( counter >= 0 ) {
							motion = true;
							hide_frames();
							function delay_show() { show_frames(); } delay_show.delay(850);
						} else {
							counter = (imgs_nr - 1);
							motion = true;
							hide_frames();
							function delay_show() { show_frames(); } delay_show.delay(850);
						}
					}
				}
			});
			
			ctrl_right.addEvents({
				mouseenter: function() {
					animate_ctrl(this, 'opacity', 0.5);		
				},
				mouseleave: function() {
					animate_ctrl(this, 'opacity', 1);
				},
				click: function() {
					if ( motion == false ) {
						counter++;
						motion = true;
						hide_frames();
						function delay_show() { show_frames(); } delay_show.delay(850);
					}
				}
			});
			
			// SHOW/HIDE FUNCTIONS
			
			function show_frames() {
				
				if ( counter < imgs_nr ) {
					change_imgs();
				} else {
					counter = 0;
					change_imgs();
				}
				
				function motion_delay() { motion = false; } motion_delay.delay(500);
				animate_in(frame_middle, 'left', -el_width(frame_middle), frame_middle_pos);
				function left() { animate_in(frame_left, 'left', -el_width(frame_left), frame_left_pos); } left.delay(100);
				function right() { animate_in(frame_right, 'right', -el_width(frame_right), frame_right_pos); } right.delay(100);
			}
			
			function hide_frames() {
				animate_out(frame_middle, 'left', frame_middle_pos, -el_width(frame_middle));
				function left() { animate_out(frame_left, 'left', frame_left_pos, -el_width(frame_left)); } left.delay(300);
				function right() { animate_out(frame_right, 'right' ,frame_right_pos, -el_width(frame_right)); } right.delay(300);
			}
			
			// FRAMES HOVER EVENTS
			
			var frame_labels = $$('#duotive-slider .label');
			
			frame_labels.each(function(item, index){
				item.setStyles({'bottom': -200 });
			})		
			
			frame_left.addEvents({
				mouseenter: function(){
					if ( motion == false ) {
						animate_frame(frame_middle, 'left', frame_middle_pos + 50);
						animate_frame(frame_left, 'left', frame_left_pos - 25);
						animate_label($$('.frame_left .label'), 'bottom', 0);
					}
				},
				mouseleave: function(){
					if ( motion == false ) {
						animate_frame(frame_middle, 'left', frame_middle_pos);
						animate_frame(frame_left, 'left', frame_left_pos);
						animate_label($$('.frame_left .label'), 'bottom', -200 );

					}
				}
			});
			
			frame_right.addEvents({
				mouseenter: function(){
					if ( motion == false ) {
						animate_frame(frame_middle, 'left', frame_middle_pos - 50);
						animate_frame(frame_right, 'right', frame_left_pos - 25);
						animate_label($$('.frame_right .label'), 'bottom', 0);
					}
				},
				mouseleave: function(){
					if ( motion == false ) {
						animate_frame(frame_middle, 'left', frame_middle_pos);
						animate_frame(frame_right, 'right', frame_left_pos);
						animate_label($$('.frame_right .label'), 'bottom', -200 );
					}
				}
			});
			
			frame_middle.addEvents({
				mouseenter: function(){
					if ( motion == false ) {
						animate_label($$('.frame_middle .label'), 'bottom', 0);
					}
				},
				mouseleave: function(){
					if ( motion == false ) {
						animate_label($$('.frame_middle .label'), 'bottom', -200 );
					}
				}
			});
			
			show_frames();
				
		} // END OF slider_three_start();
		
		// SLIDER TYPE "ONE" MAIN FUNCTION -------------------------------------------------------
		
		function slider_one_start() {
			
			var frame_full = $$('#duotive-slider .frame_full');
			var frame_full_pos = (slider_width / 2 ) - ( el_width(frame_full) / 2);
			var frame_full_imgs = $$('#duotive-slider .frame_full a');

			imgs_nr = frame_full_imgs.length;

			// MAKING THE DATABASE ARRAY
			
			var full_db = new Array();
			function make_db() {
				frame_full_imgs.each(function (item, index){ 
					full_db[index] = [ item.get('html'), item.getProperty('href') ];
				});
				frame_full.set('html','');
			} 
			make_db();
			
			function change_img() {
				frame_full.set('html', '<a href="' + full_db[counter][1] + '">' + full_db[counter][0] + '</a>');
			}
			
			// SHOW FRAME FUNCTION
			
			function show_frame(direction) {
				if ( counter < imgs_nr ) {
					change_img();
				} else {
					counter = 0;
					change_img();
				}
				
				if (direction == 'right') {
					function motion_delay() { motion = false; } motion_delay.delay(500);
					animate_in(frame_full, 'left', -el_width(frame_full), frame_full_pos);
				} else {
					function motion_delay() { motion = false; } motion_delay.delay(500);
					animate_in(frame_full, 'left', slider_width, frame_full_pos);
				}
			}
			show_frame('right');
			
			// HIDE FRAME FUNCTION
			
			function hide_frame(direction) {
				if (direction == 'right') {
					animate_out(frame_full, 'left', frame_full_pos, slider_width);
				} else {
					animate_out(frame_full, 'left', frame_full_pos, -slider_width);
				}
			}
			
			// MOUSE EVENTS FOR THE FRAME
			
			frame_full.addEvents({
				mouseenter: function(){
					if ( motion == false ) {
						animate_label($$('.frame_full .label'), 'bottom', 0);
					}
				},
				mouseleave: function(){
					if ( motion == false ) {
						animate_label($$('.frame_full .label'), 'bottom', -300 );
					}
				}
			});
			
			// SLIDER NAVIGATION
			
			ctrl_left.addEvents({
				mouseenter: function() {
					animate_ctrl(this, 'opacity', 0.5);
				},
				mouseleave: function() {
					animate_ctrl(this, 'opacity', 1);
				},
				click: function() {
					if ( motion == false) {
						counter--;
						if ( counter >= 0 ) {
							motion = true;
							hide_frame('left');
							function delay_show() { show_frame('left'); } delay_show.delay(500);
						} else {
							counter = (imgs_nr - 1);
							motion = true;
							hide_frame('left');
							function delay_show() { show_frame('left'); } delay_show.delay(500);
						}
					}
				}
			});
			
			ctrl_right.addEvents({
				mouseenter: function() {
					animate_ctrl(this, 'opacity', 0.5);		
				},
				mouseleave: function() {
					animate_ctrl(this, 'opacity', 1);
				},
				click: function() {
					if ( motion == false ) {
						counter++;
						motion = true;
						hide_frame('right');
						function delay_show() { show_frame('right'); } delay_show.delay(500);
					}
				}
			});
			
		} // END OF slider_one_start();
		
		// SLIDER TYPE "PLEX" MAIN FUNCTION -------------------------------------------------------
		
		function slider_plex_start() {
			
			var frame_full = $$('#duotive-slider .frame_full');
			var frame_full_pos = (slider_width / 2 ) - ( el_width(frame_full) / 2);
			var frame_full_imgs = $$('#duotive-slider .frame_full a');
			var frame_full_img_height = 256;
			var frame_full_img_width = 760;
			plex_slices = parseInt(frame_full.getProperty('id'));
			
			// MAKING THE DATABASE ARRAY
			
			var plex_db = new Array();
			
			function make_plex_db() {
				frame_full_imgs.each(function(item, index){
					plex_db[index] = [item.getChildren('img').getProperty('src'), item.getChildren('span.label').get('html')];
					plex_db[index][1] = '<span class="label">' + plex_db[index][1] + '</span>';
					plex_db[index][2] = item.getProperty('href');
				});
				frame_full.set('html', '');
			}
			make_plex_db();
			
			// CREATING THE IMAGE SLICES
			
			var temp_height; var temp_opacity; var plex_multiplier;
			
			if ( plex_slices != 5 && plex_slices != 10 && plex_slices != 15 && plex_slices != 20 ) { plex_slices = 20; }
			
			switch (plex_slices) {
					case 5:
						duration_plex = 700;
						plex_multiplier = 95;
						break;
					case 10:
						duration_plex = 600;
						plex_multiplier = 75;
						break;
					case 15:
						duration_plex =  550;
						plex_multiplier = 60;
						break;
					case 20: 
						duration_plex = 500;
						plex_multiplier = 50;
						break;
			}
			
			(function create_slices() {
				var slice_holder = new Element('div', {'id':'slice_holder'}).injectInside(frame_full[0]);
				slice_width = frame_full_img_width / plex_slices;
				
				for (j = 0; j < plex_slices; j++) {
					var slice = new Element('div', {
						'id':  'slice_'+j,
						'class': 'plex_slice',
						'styles': {
							'width' : slice_width,
							'height' : temp_height,
							'left': j * slice_width,
							'opacity': 0,
							'background' : 'url(' + plex_db[counter + 1][0] + ') no-repeat ' + -(j * slice_width) + 'px top'
						}
					});
					slice.injectInside(slice_holder);
				}
				var anchor = new Element('a',{
					'href': plex_db[counter][2]
				});
				anchor.injectInside(slice_holder);
				anchor.set('html', plex_db[counter][1]);
			})();
			
			// SHOW FRAME FUNCTION
			
			var slices = $$('#duotive-slider .plex_slice');
			var slice_holder = $('slice_holder');
			var anchor = $$('#duotive-slider a');
			
			function show_frame(direction) {
				slice_holder.setStyle('background', 'url(' + plex_db[counter][0] + ') no-repeat');
				if (direction == 'right') { 
					animate_in(frame_full, 'left', -el_width(frame_full), frame_full_pos);
				} else { animate_in(frame_full, 'left', slider_width, frame_full_pos); }
				function motion_delay() { motion = false; } motion_delay.delay(600);
			}
			show_frame('right');
			
			// IMAGE CHANGER FUNCTION

			function change_img() {
				motion = true;
				// animating the slices
				slices.reverse();
				slices.each(function(item, index){
					function delay_animate() { 
						var morph = new Fx.Morph(item, {duration: duration_plex, transition: transition_plex});
						morph.start({
							'height' : temp_height,
							'opacity' : temp_opacity
						});
					} 
					delay_animate.delay(index * plex_multiplier);
				});
				anchor[0].setProperty('href', plex_db[counter][2]);
				anchor[0].set('html', plex_db[counter][1]);
				function motion_delay() { motion = false; } motion_delay.delay(1500);
			}
						
			// SLIDER NAVIGATION
			
			ctrl_right.addEvents({
				mouseenter: function() { animate_ctrl(this, 'opacity', 0.5); },
				mouseleave: function() {	animate_ctrl(this, 'opacity', 1);	},
				click: function() {
					if ( motion == false ) {
						counter++;
						if ( counter >= plex_db.length ) counter = 0;
						
						switch (plex_direction) {
							case 'down':
								 temp_height = frame_full_img_height; temp_opacity = 1; plex_direction = 'right';
								slices.setStyle('background-image', 'url(' + plex_db[counter][0] + ')');
								break;
							case 'right':
								temp_height = frame_full_img_height; temp_opacity = 0; plex_direction = 'left';
								slice_holder.setStyle('background', 'url(' + plex_db[counter][0] + ') no-repeat');
								break;
							case 'left':
								temp_height = frame_full_img_height; temp_opacity = 1; plex_direction = 'up';
								slices.setStyle('background-image', 'url(' + plex_db[counter][0] + ')');
								break;
							case 'up':
								temp_height = 0; temp_opacity = 0; plex_direction = 'down';
								slice_holder.setStyle('background', 'url(' + plex_db[counter][0] + ') no-repeat');
								break;
						}
						
						change_img();
					}
				}
			});
			
			ctrl_left.addEvents({
				mouseenter: function() {	animate_ctrl(this, 'opacity', 0.5); },
				mouseleave: function() {	animate_ctrl(this, 'opacity', 1);	},
				click: function() {
					if ( motion == false ) {
						if ( counter == 0 ) counter = plex_db.length;
						counter--;
						
						switch (plex_direction) {
							case 'down':
								 temp_height = frame_full_img_height; temp_opacity = 1; plex_direction = 'right';
								slices.setStyle('background-image', 'url(' + plex_db[counter][0] + ')');
								break;
							case 'right':
								temp_height = frame_full_img_height; temp_opacity = 0; plex_direction = 'left';
								slice_holder.setStyle('background', 'url(' + plex_db[counter][0] + ') no-repeat');
								break;
							case 'left':
								temp_height = frame_full_img_height; temp_opacity = 1; plex_direction = 'up';
								slices.setStyle('background-image', 'url(' + plex_db[counter][0] + ')');
								break;
							case 'up':
								temp_height = 0; temp_opacity = 0; plex_direction = 'down';
								slice_holder.setStyle('background', 'url(' + plex_db[counter][0] + ') no-repeat');
								break;
						}
						
						change_img();
					}
				}
			});
			
			// MOUSE EVENTS FOR THE FRAME
			
			frame_full.addEvents({
				mouseenter: function(){
					if ( motion == false ) {
						animate_label($$('.frame_full .label'), 'bottom', 0);
					}
				},
				mouseleave: function(){
					if ( motion == false ) {
						animate_label($$('.frame_full .label'), 'bottom', -300 );
					}
				}
			});
			
		} // END OF slider_plex_start();
		
		switch (slider_type) {
			case 'one': slider_one_start(); break;
			case 'three': slider_three_start();	break;
			case 'plex': slider_plex_start(); break;
		}
				
	} // END OF slider_init();
	
	function slider_start() {
		
		var slider = $('duotive-slider');
		// validates if the slider exists
		if (slider) { 
		
			// gets slider type by looking for element 'class'
			var slider_type = slider.getProperty('class');
			
			// if slider_type does not exist -> alert error
			if (!slider_type) { 
				alert("The slider doesn't seem to be set up correctly.");
			} else {
			
				// preloader function 
				var slider_imgs = $$('#duotive-slider img');
				var imgs_src = new Array;
				counter = 0;
				slider_imgs.each(function(element) {
					imgs_src[counter] = element.getProperty('src');
					counter++;
				});
				var image_assets = new Asset.images(imgs_src, {
					onComplete: function() {
							// when the images are loaded remove the loading animation
							// and start the main slider function
							$('duotive-slider').setStyles({'background':'none'});
							slider_init(slider_type); // CALLING THE MAIN FUNCTION
						}
					
					});

				}
		
		} else { alert("The slider elements were not loaded correctly. \nPlease check the settings."); } // slider does not exist
		
	} // END OF slider_start();
		
	slider_start();
		
}); // END OF file ;)
