Fx.Opacity = Fx.Style.extend({initialize: function(el, options){this.now = 1;this.parent(el, 'opacity', options);},toggle: function(){return (this.now > 0) ? this.start(1, 0) : this.start(0, 1);},show: function(){return this.set(1);}});

window.addEvent("load",function(){
	$$(".gk_is_wrapper-style1").each(function(el){
		var elID = el.getProperty("id");
		var wrapper = $(elID);
		var $G = $Gavick[elID];
		var slides = [];
		var contents = [];
		var loadedImages = false;
		var imagesToLoad = [];
		var tooltips_content = [];
		var tooltip = null;
		var tooltip_fx = null;
		var tooltip_flag = false;
		var mouseover = false;
				
		$ES('.gk_is_slide', wrapper).each(function(elm,i){
			var newImg = new Element('img',{ "title": elm.getProperty('title') });
			newImg.setProperty("src",elm.innerHTML);
			imagesToLoad.push(newImg);
			elm.innerHTML = '';
			newImg.injectInside(elm);
		});
		
		tooltips_content = $ES('.gk_is_slide_content', wrapper);
		
		if($E('.gk_is_tooltip', wrapper)){
			tooltip = $E('.gk_is_tooltip', wrapper);
			
			tooltip_fx = new Fx.Opacity(tooltip,{duration:200});
			tooltip.injectInside(document.body);
			
			$ES('.gk_is_slide', wrapper).each(function(elm,i){			
				elm.addEvent('mouseenter', function(e){
					var evt = new Event(e);
					tooltip.innerHTML = '';
				 	tooltips_content[i].getElement('div').clone().injectInside(tooltip);
					tooltip.setProperty('class','gk_is_tooltip');
					tooltip_fx.start(1);
					tooltip.setStyles({
						"left" : elm.getCoordinates().left + $G['tooltip_x'] + "px",
						"top" : elm.getCoordinates().top + $G['tooltip_y'] + "px"
					});	
					mouseover = true;
				});
				
				elm.addEvent('mouseleave', function(e){
					mouseover = false;
					
					(function(){
						if(!tooltip_flag && !mouseover){
							(function(){tooltip.setProperty('class','gk_is_tooltip gk_unvisible');}).delay(200);
							tooltip_fx.start(0);
						}	
					}).delay(250);
				});	
			});
			
			tooltip.addEvent('mouseenter',function(){
				tooltip_flag = true;
			});
				
			tooltip.addEvent('mouseleave',function(){
				tooltip_flag = false;
				$E('.gk_is_slide', wrapper).fireEvent('mouseleave');
			});
		}
		
		if($G['slide_links']){
			$ES('.gk_is_slide', wrapper).each(function(elm,i){		
				elm.addEvent("click", function(){
					window.location = tooltips_content[i].getElement('.gk_vm_tooltip_name').getProperty('href');
				});
			});
		}
		
		var time = (function(){
			var process = 0;				
			imagesToLoad.each(function(el,i){
				if(el.complete) process++;
			});
			
			if(process == imagesToLoad.length){
				$clear(time);
				loadedImages = process;
				(function(){new Fx.Opacity($E('.gk_is_preloader', wrapper)).start(1,0);}).delay(400);
			}
		}).periodical(200);
		
		var time_main = (function(){
			if(loadedImages){
				$clear(time_main);
				
				wrapper.getElementsBySelector(".gk_is_slide_group").each(function(elmt,i){
					slides[i] = elmt;
				});
				
				slides.each(function(el,i){
					if(i != 0) 
						el.setOpacity(0);
				});
				
				$G['actual_slide'] = 0;
				
				if($G['autoanimation']){
					$G['actual_animation'] = (function(){
						gk_is_style1_anim(wrapper, slides, $G['actual_slide']+1, $G);
					}).periodical($G['anim_interval']+$G['anim_speed']);
				}
				
				if($E('.gk_is_pagination', wrapper)){
					$ES('.gk_is_pagination span', wrapper).each(function(el,i){
						el.addEvent("click", function(){
							$clear($G['actual_animation']);
						
							gk_is_style1_anim(wrapper, slides, i, $G);
							
							if($E('.gk_is_tooltip', wrapper)) tooltip.fireEvent('mouseleave');
							
							if($G['autoanimation']){
								$G['actual_animation'] = (function(){
									gk_is_style1_anim(wrapper, slides, $G['actual_slide']+1, $G);
								}).periodical($G['anim_interval']+$G['anim_speed']);
							}
						});
					});		
					
					$E('.gk_is_pagination span',wrapper).setProperty("class", "active");
				}
			}
		}).periodical(250);
	});
});

function gk_is_style1_anim(wrapper, slides, which, $G){
	if(which != $G['actual_slide']){
		var max = slides.length-1;
		if(which > max) which = 0;
		if(which < 0) which = max;
		var actual = $G['actual_slide'];
		
		$G['actual_slide'] = which;
		slides[which].setStyle("z-index",max+1);
		new Fx.Opacity(slides[actual],{duration: $G['anim_speed']}).start(1,0);
		new Fx.Opacity(slides[which],{duration: $G['anim_speed']}).start(0,1);	
		
		switch($G['anim_type']){
			case 'opacity': break;
			case 'top': new Fx.Style($ES('.gk_is_slide_group',wrapper)[actual],'margin-top',{duration: 0.25 * $G['anim_speed'], transitions:Fx.Transitions.Circ.easeOut}).start(0, wrapper.getSize().size.y);break;
			case 'bottom': new Fx.Style($ES('.gk_is_slide_group',wrapper)[actual],'margin-top',{duration: 0.25 * $G['anim_speed'], transitions:Fx.Transitions.Circ.easeOut}).start(0, (-1) * wrapper.getSize().size.y);break;
		}
		
		if($E('.gk_is_pagination',wrapper)){
			$ES('.gk_is_pagination span',wrapper).setProperty("class", "");
			$ES('.gk_is_pagination span',wrapper)[which].setProperty("class", "active");
		}
		
		(function(){slides[$G['actual_slide']].setStyle("z-index",$G['actual_slide']);}).delay($G['anim_speed']);
	}
}
