window.addEvent("load",function(){
	$$(".gk_is_wrapper-style3").each(function(el){
		var elID = el.getProperty("id");
		var wrapper = $(elID);
		var $G = $Gavick[elID];
		var thumbs_array = wrapper.getElements('.gk_is_thumbs a');
		var images_array = wrapper.getElements('.gk_is_image');
		var popup_initialized = false;
		
		var popup_handler = wrapper.getElement('.gk_is_popup-style3');
		if(popup_handler) popup_handler.injectTop(document.body);
		var overlay_handler = wrapper.getElement('.gk_is_overlay-style3');
		if(overlay_handler) overlay_handler.injectTop(document.body);
		
		thumbs_array.each(function(elm,j){
			elm.addEvent("click", function(e){
				new Event(e).stop();
				gk_is_s3_popup(popup_handler, $G['width'], $G['height'], overlay_handler, images_array[j].getElement('.gk_is_image_path').innerHTML, images_array[j].getElement('.gk_is_text_block'),j,images_array,popup_initialized);
				popup_initialized = true;
			});
		});
	});
});

function gk_is_s3_popup(popup_id, x, y, overlay, image, content, num, images_array, init){
	var p = popup_id;
 	var layer = p.getElement('.overlay');
 	var actual = num;
 	var prev = p.getElement('.next');
 	var next = p.getElement('.prev');
 	
  	prev.setStyle("top",(y - prev.getStyle("height").toInt()) / 2);
	next.setStyle("top",(y - next.getStyle("height").toInt()) / 2);
	layer.setStyle("height",y+"px");
	init = true;
	next.removeEvents("click");
	prev.removeEvents("click");
	    
	next.addEvent("click", function(){
		if(actual == 0) actual = images_array.length - 1;
		else actual--;
		gk_is_s3_popup(popup_id, x, y, overlay, images_array[actual].getElement('.gk_is_image_path').innerHTML, images_array[actual].getElement('.gk_is_text_block'), actual, images_array, init);
	});

	prev.addEvent("click", function(){
	    if(actual == images_array.length - 1) actual = 0;
		else actual++;
		gk_is_s3_popup(popup_id, x, y, overlay, images_array[actual].getElement('.gk_is_image_path').innerHTML, images_array[actual].getElement('.gk_is_text_block'), actual, images_array, init);
	});
 	
	if(p.getStyle("display") != "block"){
		var img = new Asset.image(image,{onload:function(){
			new Fx.Style(layer,'opacity',{duration:350}).start(0);
			p.getElement('.content').empty();
			$(this).injectTop(p.getElement('.content'));
			p.getElement('.text').empty();
			if(content){
				$(content).clone().injectTop(p.getElement('.text'));
				p.getElement('.text').setStyle("bottom","-"+p.getElement('.text').getStyle("height"));
				(function(){new Fx.Style(p.getElement('.text'),'bottom',{duration:350}).start(0);}).delay(1000);
			} 
		}});
		
		p.setStyle("display","block");
		p.setStyle("left",(window.getSize().size.x / 2)+"px");
	  	if(overlay) overlay.setStyle("display","block");
	  
		var fintop = ((window.getSize().size.y / 2) + window.getScrollTop());
	  
		if(window.opera){
			fintop = ((window.innerHeight / 2) + window.getScrollTop());
		}  
		
		new Fx.Style(p,'top',{duration:350}).start(fintop+120,fintop);
		
		new Fx.Style(p,'opacity',{duration:350}).start(1);
		if(overlay) new Fx.Style(overlay,'opacity',{duration:350}).start(0.6);

		p.setStyles({
			"overflow":"hidden",
			"width":"40px",
			"height":"40px"
		});
		
		p.getElement('.m').setStyle("display","none");
		
		(function(){
			p.getElement('.m').setStyle("display","block");
			new Fx.Styles(p,{duration:200}).start({
				"width":x+40+"px",
				"height":y+40+"px",
				"left":((window.getSize().size.x-(x-40)) / 2)+"px",
				"top":(fintop-((y+40)/2))+"px"
			});
			new Fx.Style(p.getElement('.t'),'width',{duration:200}).start(0,x);
			new Fx.Style(p.getElement('.b'),'width',{duration:200}).start(0,x);
			new Fx.Style(p.getElement('.m'),'width',{duration:200}).start(0,x);
			
			new Fx.Style(p.getElement('.m'),'height',{duration:200}).start(0,y);
			new Fx.Style(p.getElement('.ml'),'height',{duration:200}).start(0,y);
			new Fx.Style(p.getElement('.mr'),'height',{duration:200}).start(0,y);
			
			p.getElement('.close').setStyle("opacity",0);
			(function(){new Fx.Style(p.getElement('.close'),'opacity',{duration:350}).start(1);}).delay(350);
			
			p.getElement('.padding').setStyle('opacity',0);
			(function(){new Fx.Style(p.getElement('.padding'),'opacity',{duration:350}).start(0,1);}).delay(350);
		}).delay(350);

		p.getElement('.close').addEvent("click", function(){
			new Fx.Style(p.getElement('.close'),'opacity',{duration:350}).start(0);
			new Fx.Style(p.getElement('.padding'),'opacity',{duration:350}).start(0);
			(function(){
				new Fx.Style(p.getElement('.t'),'width',{duration:200}).start(x, 0);
				new Fx.Style(p.getElement('.b'),'width',{duration:200}).start(x, 0);
				new Fx.Style(p.getElement('.m'),'width',{duration:200}).start(x, 0);
				
				new Fx.Style(p.getElement('.m'),'height',{duration:200}).start(y, 0);
				new Fx.Style(p.getElement('.ml'),'height',{duration:200}).start(y, 0);
				new Fx.Style(p.getElement('.mr'),'height',{duration:200}).start(y, 0);
				
				new Fx.Styles(p,{duration:200}).start({
					"left":(window.getSize().size.x/2)+"px",
					"top":fintop+"px"
				}); 
				
				(function(){
					new Fx.Style(p, 'opacity', {duration:350}).start(0);
					if(overlay) new Fx.Style(overlay, 'opacity',{duration:350}).start(0);
				 	new Fx.Styles(p,{duration:350}).start({
						"width":"40px",
						"height":"40px",
						"top":(fintop+120)+"px"
					}); 
					
					(function(){
						p.setStyle("display","none");
						if(overlay) overlay.setStyle("display","none");
					}).delay(350);
				}).delay(200);
			}).delay(200);
		});
	}else{
		new Fx.Style(layer, 'opacity',{duration:350}).start(1);
		(function(){
			var img = new Asset.image(image,{onload:function(){
				new Fx.Style(layer, 'opacity',{duration:350}).start(0);
				p.getElement('.content').empty();
				$(this).injectTop(p.getElement('.content'));
				p.getElement('.text').empty();
				if(content){
					$(content).clone().injectTop(p.getElement('.text'));
					p.getElement('.text').setStyle("bottom","-"+p.getElement('.text').getStyle("height"));
					new Fx.Style(p.getElement('.text'),'bottom',{duration:350}).start(0);
				} 
			}});
		}).delay(350);
	}
}

