new Namespace("at.steiermark.gallery_slider.slider");
at.steiermark.gallery_slider.slider = Class.create({

	initialize : function (element) {
		this.sliderbox = element;
		
		this.captiontext = this.sliderbox.getElementsBySelector(".captiontext")[0];
		this.panelButtons = this.sliderbox.getElementsBySelector(".panel")[0];
		this.totalImages = this.sliderbox.getElementsBySelector(".total")[0];
		this.actualImage = this.sliderbox.getElementsBySelector(".actual")[0];
		this.itemsContainer = this.sliderbox.getElementsBySelector(".all_images")[0];
		this.buttonLeft = this.sliderbox.getElementsBySelector(".gallery_slide_left")[0];
		this.buttonRight = this.sliderbox.getElementsBySelector(".gallery_slide_right")[0];
		
		this.galleryOverview = this.sliderbox.getElementsBySelector(".g_overview")[0];
		this.overItemsContainer = this.sliderbox.getElementsBySelector(".g_all")[0];
		this.overpanelButtons = this.sliderbox.getElementsBySelector(".overview_panel")[0];
		this.buttonOverview = this.sliderbox.getElementsBySelector(".gallery_button")[0];
		this.closeButton = this.sliderbox.getElementsBySelector(".close")[0];
		this.overbuttonLeft = this.sliderbox.getElementsBySelector(".overview_slide_left")[0];
		this.overbuttonRight = this.sliderbox.getElementsBySelector(".overview_slide_right")[0];
		
		this.posCounter = 0;
		this.posOverCounter = 0;
		
		this.doubleClickRight = false;
		this.doubleClickLeft = false;		
		
		// HACK image width 
		this.itemWidth = 460;
		this.overitemWidth = 450;
		
		this.items = this.sliderbox.getElementsBySelector('.image_entry');		
		this.itemsCount = this.items.length;
		
		if (this.overItemsContainer != null) {
			this.overitems = this.sliderbox.getElementsBySelector('.over_image');		
			this.overitemsCount = this.overitems.length;
			this.overItemsContainer.setStyle({ width: ((this.overitemsCount+1)*150)/2 + 'px' });
			this.overContainerWidth = ((this.overitemsCount+1)*150)/2;
			
			this.slideContainerWidth = this.itemsCount * this.itemWidth;		
			this.itemsContainer.style.width = this.slideContainerWidth+'px';
			
			this.writeEvents();
			this.totalImages.innerHTML = this.itemsCount;
		}
		
		this.updateFields();
	},
	
	writeEvents : function () {
		this.buttonOverview.observe('click', this.showOverview);
		this.buttonOverview.myClass = this;
		
		this.closeButton.observe('click', this.closeOverview);
		this.closeButton.myClass = this;
		
		this.buttonLeft.observe('click', this.toLeft);
		this.buttonLeft.myClass = this;
	
		this.buttonRight.observe('click', this.toRight);
		this.buttonRight.myClass = this;
		
		this.overbuttonLeft.observe('click', this.toOverLeft);
		this.overbuttonLeft.myClass = this;
	
		this.overbuttonRight.observe('click', this.toOverRight);
		this.overbuttonRight.myClass = this;
	},
	
	showOverview : function () {
		this.myClass.itemsContainer.setStyle({ display: 'none' });
		this.myClass.panelButtons.setStyle({ display: 'none' });
		this.myClass.galleryOverview.setStyle({ display: 'block' });
		this.myClass.overpanelButtons.setStyle({ display: 'block' });
	},
	
	closeOverview : function () {
		this.myClass.itemsContainer.setStyle({ display: 'block' });
		this.myClass.panelButtons.setStyle({ display: 'block' });
		this.myClass.galleryOverview.setStyle({ display: 'none' });
		this.myClass.overpanelButtons.setStyle({ display: 'none' });
	},
	
	updateFields : function () {
		//actual Image Display
		if (this.actualImage != null) {
			this.actualImage.innerHTML = this.posCounter + 1;
		}

		//actual Caption text
		this.captiontext.innerHTML = '';
		this.getAltText = this.items[this.posCounter].getElementsByTagName('img')[0].getAttribute("alt");
		
		this.captiontext.innerHTML = this.getAltText;
	
	},
	
	removeDoubleclickleft : function () {
		this.myClass.doubleClickLeft = false;
		clearInterval(this.removeleft);
	},
	
	toLeft : function () {
		if(this.myClass.doubleClickLeft == false) {
			this.myClass.doubleClickLeft = true;
			this.removeleft = setInterval(this.myClass.removeDoubleclickleft.bind(this),600);
			var position = parseInt(this.myClass.itemsContainer.getStyle('left'));
			
			if(position < 0) {
				this.myClass.posCounter = this.myClass.posCounter - 1;
				newPosition = this.myClass.itemWidth;
				end = position + newPosition;
				
				ex9 = new Animator({transition: Animator.makeEaseOut(4),duration: 600});
				ex9.addSubject(new NumericalStyleSubject(this.myClass.itemsContainer, 'left', position, end));
				ex9.play();
			
				this.myClass.updateFields();
			}
		}
	},
	
	toOverLeft : function () {
		if(this.myClass.doubleClickLeft == false) {
			this.myClass.doubleClickLeft = true;
			this.removeleft = setInterval(this.myClass.removeDoubleclickleft.bind(this),600);
			var position = parseInt(this.myClass.overItemsContainer.getStyle('left'));
			
			if(position < 0) {
				this.myClass.posOverCounter = this.myClass.posOverCounter - 1;
				newPosition = this.myClass.overitemWidth;
				end = position + newPosition;
				
				ex9 = new Animator({transition: Animator.makeEaseOut(4),duration: 600});
				ex9.addSubject(new NumericalStyleSubject(this.myClass.overItemsContainer, 'left', position, end));
				ex9.play();
			}
		}
	},
	
	removeDoubleclickright : function () {
		this.myClass.doubleClickRight = false;
		clearInterval(this.remove);
	},
	
	toRight : function () {
		if(this.myClass.doubleClickRight == false) {
			this.myClass.doubleClickRight = true;
			this.remove = setInterval(this.myClass.removeDoubleclickright.bind(this),600);
			var position = parseInt(this.myClass.itemsContainer.getStyle('left'));
			temp = (this.myClass.slideContainerWidth - this.myClass.itemWidth) + position;
			
			if(temp >= 95) {
				this.myClass.posCounter = this.myClass.posCounter + 1;
				newPosition = this.myClass.itemWidth*-1;
				end = position + newPosition;
				
				ex9 = new Animator({transition: Animator.makeEaseOut(4),duration: 600});
				ex9.addSubject(new NumericalStyleSubject(this.myClass.itemsContainer, 'left', position, end));
				ex9.play();
			
				this.myClass.updateFields();
			}
		}
	},
	
	toOverRight : function () {
		if(this.myClass.doubleClickRight == false) {
			this.myClass.doubleClickRight = true;
			this.remove = setInterval(this.myClass.removeDoubleclickright.bind(this),600);
			var position = parseInt(this.myClass.overItemsContainer.getStyle('left'));
			temp = (this.myClass.overContainerWidth - this.myClass.overitemWidth) + position;
			
			if(temp >= 95) {
				this.myClass.posOverCounter = this.myClass.posOverCounter + 1;
				newPosition = this.myClass.overitemWidth*-1;
				end = position + newPosition;
				
				ex9 = new Animator({transition: Animator.makeEaseOut(4),duration: 600});
				ex9.addSubject(new NumericalStyleSubject(this.myClass.overItemsContainer, 'left', position, end));
				ex9.play();
			}
		}
	}



});



