window.addEvent("load",function(){
	$$(".gkIsWrapper-gk_appsprotech").each(function(el){
		var elID = el.getProperty("id");
		var wrapper = document.id(elID);
		var $G = $Gavick[elID];
		var slides = [];
		var links = [];
		var loadedImages = (wrapper.getElement('.gkIsPreloader')) ? false : true;
		$G['blank'] = false;
		$G["base_x"] = 0;
		$G["base_y"] = 0;
		
		if(!loadedImages){
			var imagesToLoad = [];
			
			wrapper.getElements('.gkIsSlide').each(function(el,i){
				links.push(el.getElement('a').getProperty('href'));
				var newImg = new Element('img',{
					"title":el.getProperty('title'),
					"class":el.getProperty('class'),
					"style":el.getProperty('style')
				});
				
				newImg.setProperty('alt',el.getChildren()[0].getProperty('href'));
				el.getElement('a').destroy();
				newImg.setProperty("src",el.innerHTML);
				imagesToLoad.push(newImg);
				newImg.inject(el, 'after');
				el.destroy();
			});
			
			if(wrapper.getElement('.gkIsText')) {
				wrapper.getElements('.gkIsText').fade('out');
			}
			
			var time = (function(){
				var process = 0;				
				imagesToLoad.each(function(el,i){
					if(el.complete) process++;
 				});
 				
				if(process == imagesToLoad.length){
					clearInterval(time);
					loadedImages = process;
					(function(){
						wrapper.getElement('.gkIsPreloader').fade('out');
						wrapper.addClass('loaded');
						if(wrapper.getElement('.gkIsText')) {
							wrapper.getElement('.gkIsText').fade('in');
							wrapper.getElement('.gkIsText').addClass('active');
						}
					}).delay(400);
				}
			}).periodical(200);
		}
		
		var time_main = (function(){
			if(loadedImages){
				clearInterval(time_main);
				
				wrapper.getElements(".gkIsSlide").each(function(elmt,i){
					slides[i] = elmt;
					if($G['slide_links']){
						elmt.addEvent("click", function(){window.location = elmt.getProperty('alt');});
						elmt.setStyle("cursor", "pointer");
					}
				});
				
				$G["base_x"] = slides[0].getSize().x;
				$G["base_y"] = slides[0].getSize().y;
				
				slides.each(function(el,i){
					if(i != 0) el.set('opacity', 0);
				});
				
				slides[0].addClass('active');
				
				$G['actual_slide'] = 0;
				
				if(wrapper.getElement(".gkIsNext")) {
					document.addEvent('keydown', function(e) {
						if(e.key == 'left') {
							gk_is_appsprotech_anim(wrapper, slides, $G['actual_slide']-1, $G);
							$G['blank'] = true;
						}
						
						if(e.key == 'right') {
							gk_is_appsprotech_anim(wrapper, slides, $G['actual_slide']+1, $G);
							$G['blank'] = true;
						}
					});
					
					wrapper.getElement(".gkIsNext").addEvent('click', function() {					
						gk_is_appsprotech_anim(wrapper, slides, $G['actual_slide']+1, $G);
						$G['blank'] = true;
					});
					
					wrapper.getElement(".gkIsPrev").addEvent('click', function() {
						gk_is_appsprotech_anim(wrapper, slides, $G['actual_slide']-1, $G);
						$G['blank'] = true;
					});
					
					wrapper.getElements('.gkIsPagination li')[0].setProperty('class', 'active');
					
					wrapper.getElements('.gkIsPagination li').each(function(li, i) {
						li.addEvent('click', function(e) {
							e.stop();
							gk_is_appsprotech_anim(wrapper, slides, i, $G);
							$G['blank'] = true;
						});
					});
				}
				
				if($G['autoanim']){
				    (function(){
				        if(!$G['blank']) {
				        	gk_is_appsprotech_anim(wrapper, slides, $G['actual_slide']+1, $G);
				        } else {
				        	$G['blank'] = false;
				        }
				    }).periodical($G['anim_interval']+$G['anim_speed']);
				}
			}
		}).periodical(250);
	});
});

function gk_is_appsprotech_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[$G['actual_slide']].setStyle("z-index",max+1);
		
		slides[actual].set('tween', {duration: $G['anim_speed']});
		slides[which].set('tween', {duration: $G['anim_speed']});
		
		slides[actual].fade('out');
		
		if(slides[actual].hasClass('active')) {
			slides[actual].removeClass('active');
		}
		
		slides[which].fade('in');	
		
		if(!slides[which].hasClass('active')) {
			slides[which].addClass('active');
		}
		
		var txts = wrapper.getElements(".gkIsText");
		
		if(txts.length > 0) {
			txts[actual].fade('out');
			if(txts[actual].hasClass('active')) {
				txts[actual].removeClass('active');
			}
			
			txts[which].fade('in');
			if(!txts[which].hasClass('active')) {
				txts[which].addClass('active');
			}
		}
				
		if(wrapper.getElement('.gkIsPagination')) {
			wrapper.getElements('.gkIsPagination li').setProperty('class', '');
			wrapper.getElements('.gkIsPagination li')[which].setProperty('class', 'active');
		}		
				
		(function(){
			slides[$G['actual_slide']].setStyle("z-index", $G['actual_slide']);
		}).delay($G['anim_speed']);
	}
}