var Slideshow = {
	
	currentIndex : 0,
	/* timers */
	transitionEffectDelay:400,
	slideChangeDelay:5000,
	/* opts */
	transitionAllowed:true,
	onPause:false,
	allowPauseOnHover : false,
	autostart:false,
	showControls:true,
	showAdditionalInfo:false,
	/* class names */
	dotsActiveClassName : 'active',
	slideshowId : 'photo_cont',
	slideshowControlsId : 'controls',
	slideshowControlsPrevId : 'prev',
	slideshowControlsNextId : 'next',
	imageContainerClassname_1 : 'image_1',
	imageContainerClassname_2 : 'image_2',
	
	init:function() {
		var _this=this;
		/* Creating the slideshow blocks */
		jQuery('#'+_this.slideshowId).append('<div class="'+this.imageContainerClassname_1+'"></div>');
		jQuery('#'+_this.slideshowId).append('<div class="'+this.imageContainerClassname_2+'"></div>');
		
		if (this.showControls) {
			jQuery('#'+_this.slideshowId).append('<div class="'+this.slideshowControlsId+'"><div class="'+this.slideshowControlsPrevId+'"><a href="#">&nbsp;</a></div><div class="'+this.slideshowControlsNextId+'"><a href="#">&nbsp;</a></div><div class="dots"></div></div>');
		}
		jQuery('#'+_this.slideshowId+'>img').remove();
		
		jQuery.each(jQuery('#'+_this.slideshowId+' .blocks a'), function() {
			jQuery(this).bind('click', function() {
				clearTimeout(_this.autostartSlideshowTimeoutId);
				return _this.setData(jQuery(this)[0]);
			});
			var className = '';
			if (jQuery(this).hasClass(_this.dotsActiveClassName))
				className = _this.dotsActiveClassName;
			jQuery('.dots').append('<em class="'+ className +'">&nbsp;</em>');
		});
		jQuery('.dots').css('margin-left', -Math.round(jQuery('.dots').width()/2));
		
		/* !!!CUSTOM ISSUES!!! */
		jQuery.each(jQuery('.boxes .box'), function() {
			jQuery(this).bind('click', function() {
				jQuery('#'+_this.slideshowId+' .blocks a:eq('+jQuery(this).index()+')').click();
			});
		});
		/* ------------ */
		
		this.go(jQuery('#'+_this.slideshowId+' .blocks a').index(jQuery('#'+_this.slideshowId+' .blocks a.active')));
		
		this.bindPrevButton();
		this.bindNextButton();
		
		if (this.allowPauseOnHover) {
			jQuery('.'+_this.imageContainerClassname_1).hoverIntent({
				sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
				interval: 400,   // number = milliseconds of polling interval
				over: function(){ _this.pause(); },  // function = onMouseOver callback (required)
				timeout: 400,   // number = milliseconds delay before onMouseOut function call
				out: function(){ setTimeout(function(){ if (!_this.onPause) { _this.transitionAllowed = true; _this.go(_this.nextIndex);} },1000); }    // function = onMouseOut callback (required)
			});
		}
	},
	
	go:function(index) {
		_this = this;
		if (this.transitionAllowed) {
			jQuery('#'+_this.slideshowId+' .blocks a:eq('+index+')').click();
			if (this.autostart)
				this.autostartSlideshowTimeoutId = setTimeout(function(){_this.go(_this.nextIndex)},_this.slideChangeDelay);
		}
	},
	
	setActiveDot:function(index) {
		jQuery.each(jQuery('.dots em'), function() {
				jQuery(this).removeClass(_this.dotsActiveClassName);
			});
			jQuery('.dots em:eq('+index+')').addClass(_this.dotsActiveClassName);
			jQuery('.dots em:eq('+this.prevIndex+')').removeClass(_this.dotsActiveClassName);
	},
	
	pause:function (index) {
		this.transitionAllowed = false;
	},
	
	/* get src of next/prev image, allow transition after image loading */
	setData:function(object) {
		_this = this;
		this.transitionAllowed = false;
		var new_img_src = (jQuery(object).find('img')).attr('longdesc');
		jQuery.imgpreload(new_img_src, function(){
			/* set data after image loading */
			jQuery('#'+_this.slideshowId+' .'+_this.imageContainerClassname_1).css('background', 'url("'+new_img_src+'") no-repeat center 0px');
			jQuery('#'+_this.slideshowId+' .'+_this.imageContainerClassname_2).animate({opacity: 0}, _this.transitionEffectDelay, function(){
				jQuery(this).css('opacity', 1);
				jQuery('#'+_this.slideshowId+' .'+_this.imageContainerClassname_1).css('background', 'none');
				jQuery(this).css('background', 'url("'+new_img_src+'") no-repeat center 0px');
				_this.transitionAllowed = true;
				_this.setActive(object);
				_this.setActiveDot(_this.currentIndex);
				
				/* !!!CUSTOM ISSUES!!! */
				jQuery('.slider_points .item.active').removeClass('active');
				jQuery('.slider_points .item:eq('+_this.currentIndex+')').addClass('active');
				jQuery('.boxes .box.active').removeClass('active');
				jQuery('.boxes .box:eq('+_this.currentIndex+')').addClass('active');
				/* ----------------- */
				
				/* Set additional info data here */
				if (_this.showAdditionalInfo) {
					jQuery('#'+_this.slideshowId+' .info h1').html(jQuery(object).find('span.h1').html());
					jQuery('#'+_this.slideshowId+' .info h2').html(jQuery(object).find('span.h2').html());
					jQuery('#'+_this.slideshowId+' .info .more').attr('href', jQuery(object).attr('href'));
				}
			});
		});

		return false;
	},
	
	setActive:function(object) {
		var _this=this;
		if (this.currentIndex>=0) {
			jQuery('.blocks .active').removeClass('active');
		}
		jQuery(object).addClass('active');
		this.currentIndex =  jQuery('#'+_this.slideshowId+' .blocks a').index(jQuery('#'+_this.slideshowId+' .blocks a.active'));
		this.setPrevIndex();
		this.setNextIndex();
	},
	
	setNextIndex:function() {
		var _this=this;
		if (this.currentIndex == (jQuery('#'+_this.slideshowId+' .blocks a').length-1)) {
			this.nextIndex = 0; /* if last */
		}
		else
			this.nextIndex = this.currentIndex + 1;
	}, 
	
	setPrevIndex:function() {
		var _this=this;
		if (this.currentIndex == 0) {
			this.prevIndex = jQuery('#'+_this.slideshowId+' .blocks a').length-1; /* if first */
		}
		else
			this.prevIndex = this.currentIndex - 1;
	},
	
	bindPrevButton:function() {
		jQuery('#'+_this.slideshowId+' .prev a').bind('click', function() {
			//_this.transitionAllowed = true;
			_this.go(_this.prevIndex);
			return false;
		});
	},
	
	bindNextButton:function() {
		jQuery('#'+_this.slideshowId+' .next a').bind('click', function() {
			//_this.transitionAllowed = true;
			_this.go(_this.nextIndex);
			return false;
		});
	},
	
	unbindPrevButton:function() {
		jQuery('#'+_this.slideshowId+' .prev a').unbind('click');
	},
	
	unbindNextButton:function() {
		jQuery('#'+_this.slideshowId+' .next a').unbind('click');
	}
	
};
