var Gallery = function (container, increment, delay) {
	this.container = $j(container);
	this.increment = increment;
	
	this.contentsMain = this.container.find('.main .slides');
	this.contentsThumb = this.container.find('.thumbs .slides');
	
	this.slides = this.container.find('.slide');
	
	this.slidesMain = this.contentsMain.find('.slide');
	this.slidesThumb = this.contentsThumb.find('.slide');
	
	this.prevBtn = this.container.find('.prev');
	this.nextBtn = this.container.find('.next');
	
	this.slideCount = this.slides.length / this.increment;
	
	this.offsetMain = this.contentsMain.position()['left'];
	this.offsetThumb = this.contentsThumb.position()['left'];
	
	this.widthMain = $j(this.contentsMain.find('.slide')[0]).width();
	this.widthThumb = $j(this.contentsThumb.find('.slide')[0]).width();
	
	this.current = 0;
	this.interval = 5;
	this.delay = delay | 5;
	this.setup();
};
Gallery.prototype = {
	setup: function () {
		var self = this;
		this.prevBtn.bind('click', function (ev) {
			ev.preventDefault();
			$j(this).addClass('active');
			self.back();
		});
		
		this.nextBtn.bind('click', function (ev) {
			ev.preventDefault();
			$j(this).addClass('active');
			self.forward();
		});
		
		this.slidesThumb.each(function (i) {
			$j(this).bind('click', function () {
				self.goTo(i);
			});
		});
		
		
		$j(self.slidesThumb[0]).addClass('active');
		
		this.goTo(0);
		//this.start();
	},
	goTo: function (idx) {
		var self = this;
		if (this.current === idx) {
			return;
		}
		
		if (idx < 0 || idx >= this.slideCount) {
			return;
		}
		this.current = idx;	
		
		$j(this.contentsMain).animate(
			{ left: (-(idx * self.widthMain) + self.offsetMain) },
			{
				duration: 500,
				easing: 'easeOutExpo'
			}
		);
		
		var x_pos = (-(idx * this.widthThumb) + this.offsetThumb);

		
		//if ( this.current % this.interval == 0) {
			$j(this.contentsThumb).animate(
				{ left: x_pos },
				{
					duration: 500,
					easing: 'easeOutExpo'
				}
			);
		//}
		
		this.slidesThumb.removeClass('active');
		$j(this.slidesThumb[idx]).addClass('active');
	},
	start: function () {
        if (this.timer) {
            return;
        }
        var self = this;
        this.timer = setTimeout(function(){
            self.next();
        }, this.delay * 1000);
	},
	pause: function () {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = false;
        }
        else {
            return;
        }
	},
	back: function () {
		this.pause();
        if (this.current === 0) {
          return; 
        	//this.goTo(this.slides.length - 1);
        }
        else {
            this.goTo(this.current - 1);
        }
        //this.start();	
	},
	forward: function () {
        this.pause();
        this.next();
	},
	next: function () {
        if (this.current+1 === this.slidesMain.length) {
			//this.timer = false;
			//this.goTo(0);
			return;	
        }
        else {
            this.goTo(this.current + 1);
        }
        //this.start();
	}
};

/*
var Gallery = function (main, thumbs) {
	this.main = $j(main);
	this.thumbs = $j(thumbs);
	this.togglers = this.thumbs.find('.slide');
	this.prev_btn_main = this.main.find('.prev');
	this.next_btn_main = this.main.find('.next');
	
	this.current = 0;
	this.setup();	
};
Gallery.prototype = {
	setup: function () {
		var self = this;
		var slider_main = new BasicSlider(this.main, 1);
		var slider_thumbs = new BasicSlider(this.thumbs, 1);
		
		$j(this.togglers[this.current]).addClass('active');
		
		this.togglers.each(function (i) {
			$j(this).bind('click', function () {
				self.togglers.removeClass('active');
				$j(this).addClass('active');
				self.current = slider_main.goTo(i);
			});
		});
		
		this.next_btn_main.bind('click', function () {
			if (self.current == self.togglers.length - 1) { 
				return; 
			}
			else {
				self.current += 1;
				self.togglers.removeClass('active');
				$j(self.togglers[self.current]).addClass('active');
			}
		});
		
		this.prev_btn_main.bind('click', function () {
			self.current -= 1;
			self.togglers.removeClass('active');
			$j(self.togglers[self.current]).addClass('active');
		});
		
		this.togglers.hoverIntent({
			sensitivity: 1,
			interval: 10,
			over: function () {
				$j(this).animate(
					{ opacity: 1.0 },
					{ duration: 300 }
				);	
			},
			out: function () {
				$j(this).animate(
					{ opacity: 0.6 },
					{ duration: 300 }
				);
			}
		});
	}
};
*/

/*--------------------------------------------------------------------------*/
/* DOM Ready */
$j(document).ready(function () {
	//new Gallery($j('.gallery .main'), $j('.gallery .thumbs'));
	
	if ($j('.breadcrumbs li').length > 3) {
		$j('.breadcrumbs li a, .breadcrumbs li:last-child').each(function () { 
			new Truncator($j(this), 60); 
		});
	}	
	$j('.breadcrumbs').addClass('active');	
	
	if ($j('.gallery').length) {
		new Gallery($j('.gallery'), 1, 10);
	}
	
	if ($j('.breadcrumbs li').length > 3) {
		$j('.breadcrumbs li a, .breadcrumbs li:last-child').each(function () { 
			new Truncator($j(this), 50); 
		});
		$j('.breadcrumbs').addClass('active');
	}

	$j('iframe').each(function () {
		$j(this).attr('allowTransparency', true);
	});	
	
	$j('.module_recent .scroll_pane').jScrollPane({
		scrollbarWidth: 27,
		verticalDragMinHeight: 41,
		verticalDragMaxHeight: 41,
		horizontalDragMinWidth: 27,
		horizontalDragMaxWidth: 27
	});		
});


