window.addEvent("load",function(){
	$$(".gk_is_wrapper-template").each(function(el){
		if(!el.hasClass('activated')) {
			el.addClass('activated');
			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;
					
			wrapper.getElements('.gk_is_slide').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 = wrapper.getElements('.gk_is_slide_content');
			
			if(wrapper.getElement('.gk_is_tooltip')){
				tooltip = wrapper.getElement('.gk_is_tooltip');
				
				tooltip_fx = new Fx.Style(tooltip,'opacity',{duration:200});
				tooltip.injectInside(document.body);
				
				wrapper.getElements('.gk_is_slide').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;
					wrapper.getElement('.gk_is_slide').fireEvent('mouseleave');
				});
			}
			
			if($G['slide_links']){
				wrapper.getElements('.gk_is_slide').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.Style(wrapper.getElement('.gk_is_preloader'), 'opacity').start(1,0);}).delay(400);
				}
			}).periodical(200);
			
			var time_main = (function(){
				if(loadedImages){
					$clear(time_main);
					
					wrapper.getElements(".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(wrapper.getElement('.gk_is_pagination')){
						wrapper.getElements('.gk_is_pagination span').each(function(el,i){
							el.addEvent("click", function(){
								$clear($G['actual_animation']);
							
								gk_is_style1_anim(wrapper, slides, i, $G);
								
								if(wrapper.getElement('.gk_is_tooltip')) 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']);
								}
							});
						});		
						
						wrapper.getElement('.gk_is_pagination span').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.Style(slides[actual],'opacity',{duration: $G['anim_speed']}).start(1,0);
		new Fx.Style(slides[which],'opacity',{duration: $G['anim_speed']}).start(0,1);	
		
		switch($G['anim_type']){
			case 'opacity': break;
			case 'top': new Fx.Style(wrapper.getElements('.gk_is_slide_group')[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(wrapper.getElements('.gk_is_slide_group')[actual],'margin-top',{duration: 0.25 * $G['anim_speed'], transitions:Fx.Transitions.Circ.easeOut}).start(0, (-1) * wrapper.getSize().size.y);break;
		}
		
		if(wrapper.getElement('.gk_is_pagination')){
			wrapper.getElements('.gk_is_pagination span').setProperty("class", "");
			wrapper.getElements('.gk_is_pagination span')[which].setProperty("class", "active");
		}
		
		(function(){slides[$G['actual_slide']].setStyle("z-index",$G['actual_slide']);}).delay($G['anim_speed']);
	}
}
