PortailUnarec = function()
{
	this.init = function()
	{
	    var oRef = this;
	    
		// grab slides
		this.aoSlide = jQuery('#unarec-carousel-slides div.unarec-carousel-slide').mouseenter(function()
		{
		    oRef.clearInterval();
		}).mouseleave(function()
		{
		    oRef.setInterval();
		});
		
		// thumbs
		this.aoThumb = jQuery('#unarec-carousel-thumbs a').click(function()
		{
		    // grab index
            var iDx = parseInt(jQuery(this).attr('href').match(/\d+/)) - 1;
            return !oRef.tick(iDx);
		}).mouseenter(function()
		{
		    // grab index
            var iDx = parseInt(jQuery(this).attr('href').match(/\d+/)) - 1;
            oRef.clearInterval(); // so it doesn't move when you're hovering
            return !oRef.tick(iDx);
		}).mouseleave(function()
		{
            // reset interval on departure
            oRef.setInterval();
		});
		
		// hook up thumbnail clicking
		jQuery('#unarec-carousel-thumbs li.hasLink span').click(function()
		{
            var iDx = parseInt(jQuery(this).parents('a').attr('href').match(/\d+/)) - 1;
            document.location.href = oRef.aoSlide.eq(iDx).find('a.mainLink').attr('href');
            return false;
		})
		
		// hide everything
		this.aoSlide.hide().eq(0).show();
		this.aoThumb.eq(0).addClass('active');
		this.iCurrent = 0;
		
		this.setInterval();
	}

	this.setInterval = function()
	{
	    return true; // disabled on request from client, 2011-07-22. Remove this line to re-enable auto-scrolling
	    
		var oRef = this;
		clearInterval(this.rInterval);
		this.rInterval = setInterval(function() { oRef.tick(); }, this.iTxDelay);
	}

	this.clearInterval = function()
	{
		clearInterval(this.rInterval);
	}

	this.tick = function()
	{
		var iCurr = this.iCurrent;
		var iNext = (arguments.length == 0) 
					? (((iCurr + 1) == this.aoSlide.length) ? 0 : iCurr + 1)
					: (((arguments[0] > 0) && (arguments[0] <= this.aoSlide.length)) ? arguments[0] : 0);

		// if we're on the current one...
		if (iNext == this.iCurrent)
			return true;


		// slides
		this.aoSlide.eq(iCurr).fadeOut(this.iTxSpeed);
		this.aoSlide.eq(iNext).fadeIn (this.iTxSpeed);
		
		// thumbnail
		this.aoThumb.removeClass('active').eq(iNext).addClass('active');
		
		// update
		this.iCurrent = iNext;
		return true;
	}

	this.rInterval  = null;
	this.iCurrent	= -1
	this.aoSlide	= null;
	this.aoThumb	= null;
	this.iTxSpeed	= 300;
	this.iTxDelay	= 7000;

	this.init();	
}

jQuery(function()
{
    new PortailUnarec();
});
