GTECH_IMAGE_GALLERY = new Class({
	options: {
		strImage      : "Resim",
		resizeDuration: 400,
		resizeTransition: false,	// default transition
		initialWidth: 250,
		initialHeight: 250,
		minWidth : 250,
		minHeight:0,
		zIndex: 10,
		galleryType : 'image',
		iframeW     : 600,
		iframeH     : 300,
		animateCaption: true,
		showCounter: true,
		autoScanLinks: true,
		relString: 'gtechimggallery',
		useDefaultCss: true,
		assetBaseUrl: GTECH_ASSET_DIR+'assets/global/slimbox/',
		cssFileName : 'slimbox.css',
		onImageShow: Class.empty,
		onDisplay: Class.empty,
		onHide: Class.empty,
		clickPosY : 0,
		enableNavigation:true,
		autoLoadCSS : false,
		overlayStyles: {}		
	},

	initialize: function(options, anchors){			
		this.setOptions(options);			    
		this.anchors = anchors || [];		
		if (this.options.autoScanLinks) {
			$$('a').each(function(el){
				if (el.getProperty('rel') && el.getProperty('rel').test("^"+this.options.relString,'i')){
					if(!el.getProperty('lightboxed')) this.anchors.push(el);
				}	
			}, this);
		}
		if(!$$(this.anchors).length) return; //no links!
		if(this.options.useDefaultCss) this.addCss();
		$$(this.anchors).each(function(el){			
			if(!el.getProperty('lightboxed')) {
				el.setProperty('lightboxed', true);
				el.addEvent('click', function(e){
					new Event(e).stop();
					this.click(el);
				}.bind(this));				
			}			
		}.bind(this));
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);
		window.addEvent('domready', this.addHtmlElements.bind(this));
	},

	addHtmlElements: function(){		
		this.overlay = new Element('div', {
			'class': 'lbOverlay',
			styles: {
				'z-index':this.options.zIndex				
			}
		}).injectInside(document.body).setStyles(this.options.overlayStyles);		
		this.center = new Element('div', {
			styles: {	
				width: this.options.initialWidth+'px', 
				height: this.options.initialHeight+'px', 
				'margin-left': (-(this.options.initialWidth/2))+'px', 'display': 'none',
				'z-index':this.options.zIndex+1
			}
		}).injectInside(document.body).addClass('lbCenter');
		this.image = new Element('div', {
			'class': 'lbImage'
		}).injectInside(this.center);
		
		this.prevLink = new Element('a', {'class': 'lbPrevLink', 'href': 'javascript:void(0);', 'styles': {'display': 'none'}}).injectInside(this.image);
		this.nextLink = this.prevLink.clone().removeClass('lbPrevLink').addClass('lbNextLink').injectInside(this.image);
		this.prevLink.addEvent('click', this.previous.bind(this));
		this.nextLink.addEvent('click', this.next.bind(this));

		this.bottomContainer = new Element('div', {'class': 'lbBottomContainer', 'styles': {'display': 'none', 'z-index':this.options.zIndex+1}}).injectInside(document.body);
		this.bottom = new Element('div', {'class': 'lbBottom'}).injectInside(this.bottomContainer);
		new Element('a', {'class': 'lbCloseLink'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.caption = new Element('div', {'class': 'lbCaption'}).injectInside(this.bottom);
		this.number = new Element('div', {'class': 'lbNumber'}).injectInside(this.bottom);
		new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.bottom);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			resize: this.center.effects($extend({duration: this.options.resizeDuration, onComplete: nextEffect}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
			image: this.image.effect('opacity', {duration: 500, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 400, onComplete: nextEffect})
		};        		
		this.preloadPrev = new Element('img');
		this.preloadNext = new Element('img');
	},
	
	addCss: function(){
		window.addEvent('domready', function(){
			if(!$('SlimboxCss')) new Asset.css(this.options.assetBaseUrl + this.options.cssFileName, {id: 'SlimboxCss'});
		}.bind(this));
	},

	click: function(link){				
		link = $(link);
	    this.clickPosY = 0;
		if (link){
			var tmpCoor = link.getCoordinates();
			if (tmpCoor){
				this.clickPosY = tmpCoor['top'];
				window.scrollTo(0,tmpCoor['top']);
			}			 
		}
		
		var rel = link.getProperty('rel')||this.options.relString;	
			
		//if (rel == this.options.relString) return this.show(link.href, link.title);
        if (!this.options.enableNavigation)  return this.show(link.href, link.title);
		var j, imageNum, images = [];				
		this.anchors.each(function(el){		    		    
			if (el.getProperty('rel') == link.getProperty('rel')){
				for (j = 0; j < images.length; j++) if(images[j][0] == el.href) break;
				if (j == images.length){
					images.push([el.href, el.title]);
					if (el.href == link.href) imageNum = j;
				}
			}
		}, this);
		return this.open(images, imageNum);
	},

	show: function(url, title){
		return this.open([[url, title]], 0);
	},

	open: function(images, imageNum){			
		this.fireEvent('onDisplay');		
		this.images = images;
		this.position();				
		this.setup(true);			
		this.top = (window.getScrollTop() + 60);														
		if (this.top==60 && this.images[imageNum]){
		    this.top = this.clickPosY;
		}
		this.center.setStyles({
			top: this.top+'px', 
			display: ''
		});
		this.fx.overlay.start(0.8);			
		return this.changeImage(imageNum);
	},

	position: function(){				
		var wh = window.getHeight();		
		if (wh<900){ wh = 2900;}		
		this.overlay.setStyles({
			'top': window.getScrollTop()+'px', 
			'height': wh+'px'
		});
	},
	setup: function(open){		
		if (open==undefined || open==null){ open = false; }						
		var elements = $$('object, iframe');		
		elements.extend($$(window.ie ? 'select' : 'embed'));
		elements.each(function(el){			
			if (open){
				if (el.style.visibility==null || el.style.visibility==""){
					el.lbBackupStyle = 'visible';
				}else{
					el.lbBackupStyle = el.style.visibility;
				} 				
			}						
			//if (open) el.lbBackupStyle = el.getStyle('visibility');
			var vis = (open ? 'hidden' : el.lbBackupStyle);			
			var divObj = $(el.id+"_flashdiv");
			if (divObj){				
				divObj.setStyle('visibility', vis);
			}		
			if (el.style.visibility!=""){
				el.setStyle('visibility', vis);
			}else{								 			
			}											
		});		
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
	},
	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: case 88: case 67: this.close(); break;
			case 37: case 80: this.previous(); break;	
			case 39: case 78: this.next();
		}
	},

	previous: function(){	
		return this.changeImage(this.activeImage-1);
	},

	next: function(){
		return this.changeImage(this.activeImage+1);
	},

	changeImage: function(imageNum){			
		if (this.options.galleryType=="iframe"){
			this.activeImage = imageNum;   
			this.preload = new Element('iframe'); 		
    		this.step = 1;
	    	this.preload.width  = this.options.iframeW+50;
	    	this.preload.height = this.options.iframeH;	    	    	
    		this.nextEffect();
			return false;
		}
		this.fireEvent('onImageShow', imageNum);
		if (this.step || (imageNum < 0) || (imageNum >= this.images.length)) return false;
		this.step = 1;
		this.activeImage = imageNum;
		this.center.setStyle('backgroundColor', '');
		this.bottomContainer.setStyle('display', 'none');
		this.prevLink.setStyle('display', 'none');
		this.nextLink.setStyle('display', 'none');
		this.fx.image.hide();
		this.center.addClass('lbLoading');
		this.preload = new Element('img');
		this.preload.addEvent('load', this.nextEffect.bind(this)).setProperty('src', this.images[imageNum][0]);								
		return false;
	},

	nextEffect: function(){		
		switch (this.step++){
		case 1:
		    this.image.setStyle('backgroundColor', '#FFFFFF');
		    if (this.options.galleryType=="iframe"){
		    	this.image.setStyle('backgroundImage', 'url('+this.images[this.activeImage][0]+')');
		    	this.image.setHTML("<IFRAME src='"+this.images[this.activeImage][0]+"' border=0 style='width:"+this.options.iframeW+"px;height:"+this.options.iframeH+"px;' ></IFRAME>");
		    }else{
		    	this.image.setStyle('backgroundImage', 'url('+this.images[this.activeImage][0]+')');
		    }	    		    		   		  		    
											
			//this.image.setStyle('backgroundAttachment', 'fixed');			
			//this.image.setStyle('backgroundRepeat', 'no-repeat');
			this.image.setStyle('backgroundPosition', 'center');
			if (this.options.minWidth>0 && this.preload.width<this.options.minWidth){
				this.image.setStyle('width', this.options.minWidth+'px');
				this.bottom.setStyle('width',this.options.minWidth+'px');
			}else{
				this.image.setStyle('width', this.preload.width+'px');
				this.bottom.setStyle('width',this.preload.width+'px');
			}						
			this.image.setStyle('height', this.preload.height+'px');
			this.prevLink.setStyle('height', this.preload.height+'px');
			this.nextLink.setStyle('height', this.preload.height+'px');

			this.caption.setHTML(this.images[this.activeImage][1] || '');
			this.number.setHTML((!this.options.showCounter || (this.images.length == 1)) ? '' : this.options.strImage+' : '+(this.activeImage+1)+' / '+this.images.length);
            
			if (this.activeImage) $(this.preloadPrev).setProperty('src', this.images[this.activeImage-1][0]);
			if (this.activeImage != (this.images.length - 1)) 
				$(this.preloadNext).setProperty('src',  this.images[this.activeImage+1][0]);
			if (this.center.clientHeight != this.image.offsetHeight){
				this.fx.resize.start({height: this.image.offsetHeight});
				break;
			}			
			this.step++;
		case 2:
			if (this.center.clientWidth != this.image.offsetWidth){
				this.fx.resize.start({width: this.image.offsetWidth, marginLeft: -this.image.offsetWidth/2});
				break;
			}
			this.step++;
		case 3:
			this.bottomContainer.setStyles({
				top: (this.top + this.center.getSize().size.y)+'px', 
				height: '0px', 
				marginLeft: this.center.getStyle('margin-left'), 
				display: ''
			});
			this.fx.image.start(1);
			break;
		case 4:
			this.center.style.backgroundColor = '#000';
			if (this.options.animateCaption){
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.setStyle('height', '');
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.style.height = '';
		case 5:
			if (this.activeImage) this.prevLink.setStyle('display', '');
			if (this.activeImage != (this.images.length - 1)) this.nextLink.setStyle('display', '');
			this.step = 0;
		}		
		if (this.options.galleryType=="iframe"){
			if (this.step>5 || this.step==0){
				return;
			}else{
				//this.nextEffect();				
			}
		}		
	},
	close: function(){
		this.fireEvent('onHide');
		if (this.step < 0) return;
		this.step = -1;
		if (this.preload){
			this.preload.onload = Class.empty;
			this.preload = null;
		}
		for (var f in this.fx) this.fx[f].stop();
		this.center.setStyle('display', 'none');
		this.bottomContainer.setStyle('display', 'none');
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return;
	}
});
GTECH_IMAGE_GALLERY.implement(new Options);
GTECH_IMAGE_GALLERY.implement(new Events);
function GTECH_IMAGE_GALLERY_GO(url,pageGetParam,targetPageNum){
	window.location = url+"&"+pageGetParam+"="+targetPageNum;
}
function GTECH_IMAGE_GALLERY_ACTION(actionSender,actionName,pageGetParam,currentPage,totalPage,url){
	if (actionName=="gofirst"){
		if (currentPage!=1){GTECH_IMAGE_GALLERY_GO(url,pageGetParam,0);	}
	}else if (actionName=="prev"){
		if (currentPage>1){GTECH_IMAGE_GALLERY_GO(url,pageGetParam,currentPage-2);	}
	}else if (actionName=="next"){
		if (currentPage<totalPage){GTECH_IMAGE_GALLERY_GO(url,pageGetParam,currentPage); }
	}else if (actionName=="golast"){
		if (currentPage!=totalPage){GTECH_IMAGE_GALLERY_GO(url,pageGetParam,totalPage-1);}
	}else if (actionName=="selectpage"){
		if (actionSender!=null && actionSender.value>=0){
			GTECH_IMAGE_GALLERY_GO(url,pageGetParam,actionSender.value);
		}	
	}
	//alert(actionSender+" - "+actionName+" - "+pageGetParam+" - "+currentPage+" - "+totalPage);
}
