var sliderClass = "gallery";
var defaultWidth = 1080;
var defaultHeight = 380;
var defaultContinuous = true;
var defaultLoop = true
var defaultSpeed = 1000;
var defaultInterval = 10000;

j(document).ready(function() {
	if(j('.'+sliderClass)) {
		j('.'+sliderClass).appendTo('#internal');
		slider0 = new slider(0, 500, 310, true, true, 900, 6000);
	}
});

function slider(id, boxWidth, boxHeight, continuous, loop, speed, interval ) {
	this.id = id;
	this.current = 0
	this.target = j('.'+sliderClass+':eq('+this.id+')');
	this.next = j(this.target).parent().parent().find('.next');
	this.doubleClicks = 0;
	if(boxWidth) this.boxWidth = boxWidth;
	if(boxHeight) this.boxHeight = boxHeight;
	if(continuous || continuous == false) { this.continuous = continuous }else{ this.continuous = true; }
	if(loop || loop == false) { this.loop = loop }else{ this.loop = true; }
	speed ? this.speed = speed : this.speed = 1000;
	interval ? this.interval = interval : this.interval = 10000;
	j(this.target).find('br').remove();
	j(this.target).wrap('<div class="galleryBg sliderActivated"></div>');
	j(this.target).wrap('<div class="galleryBgIE"></div>');
	j(this.target).wrapInner('<div class="slideContainer"></div>');
	if(j(this.target).find('.gallery-item').length > 1) {
		j(this.target).parent().parent().append('<a href="#" class="prev">Previous</a><a href="#" class="next">Next</a>');
	}
	j(this.target).find('.gallery-caption').append('  |  <a href="/rent-our-space/">Learn about renting the space</a>');
	init(this);
}

function init(c) {
	j(c.target).parent().parent().find('.prev').click(( function( c ) { return function() { clearInterval(goPrev(c)); return false; } } )(c));
	j(c.target).parent().parent().find('.next').click(( function( c ) { return function() { clearInterval(goNext(c)); return false; } } )(c));
	j(c.target).find('.gallery-icon a').click(function(){ return false });
	
	//AUTO MOVE
	var id = c.id
	if(j(this.target).find('.gallery-item').length > 1) {
		c.timer = setInterval(( function( c ) { return function() { goNext(c) } } )(c), c.interval);
	}
	j(c.target).parent().parent().find('.prev').click(( function( timer ) { return function() { clearInterval(timer) } } )(c.timer));
	j(c.target).parent().parent().find('.next').click(( function( timer ) { return function() { clearInterval(timer) } } )(c.timer));
	
	if(c.continuous){
		j(c.target).find('.gallery-item').css('position','absolute');
		for(var i=0; i<j(c.target).find('.gallery-item').length; i++) {
			j(c.target).find('.gallery-item:eq('+i+')').css('left',(c.boxWidth*i))
		}
		j(c.target).find('.gallery-item:last').clone().prependTo(j(c.target).find('.slideContainer'))
		j(c.target).find('.gallery-item:last').remove();
		if(j(this.target).find('.gallery-item').length > 1) {
			j(c.target).find('.gallery-item:first').css('left',-c.boxWidth);
		}
	}
	if(!c.loop){j(c.target).parent().parent().find('.prev').addClass('disabled');}
}

function goPrev(c) {
	target = j(c.target);
	if(!c.loop){ j(target).parent().parent().find('.next').removeClass('disabled'); }
	if(c.current != 0) {
		c.current --;
		if(c.current == 0 && !c.loop) {j(target).parent().parent().find('.prev').addClass('disabled');}
	}else {
		if(c.loop && !c.continuous) {
			c.current = j(target).find('.gallery-item').length - 1;
		}
		if(c.loop && c.continuous) {
			c.current --;
		}
	}
	j(target).find('.slideContainer').stop();
	j(target).find('.slideContainer').animate({left:-(c.current * c.boxWidth) +"px"}, c.speed, ( function( c ) { return function() { clearInterval(callbackPrev(c)) } } )(c));
	if(c.continuous){
		var firstLeft = parseInt(j(target).find('.gallery-item:first').css('left').replace('px','')) - c.boxWidth;
		var myTarget = j(target).find('.gallery-item').length - c.doubleClicks - 1;
		j(target).find('.gallery-item:eq('+myTarget+')').clone().prependTo(j(target).find('.slideContainer'));
		j(target).find('.gallery-item:first').css('left',firstLeft);
	}
	c.doubleClicks++;
}

function goNext(c){
	target = j(c.target);
	if(!c.loop){ j(target).parent().parent().find('.prev').removeClass('disabled'); }
	if(c.current >= j(target).find('.gallery-item').length - 1 ) {
		if(c.loop && !c.continuous) {
			c.current = 0;
		}
		if(c.loop && c.continuous) {
			c.current++;
		}
		if(!c.loop) {
			return false;
		}
	}else {
		c.current++;
		if(c.current >= j(target).find('.gallery-item').length - 1 && !c.loop) {j(target).parent().parent().find('.next').addClass('disabled');}
	}
	j(target).find('.slideContainer').stop();
	j(target).find('.slideContainer').animate({left:-(c.current * c.boxWidth) +"px"}, c.speed, ( function( c ) { return function() { clearInterval(callbackNext(c)) } } )(c));
	if(c.continuous){
		var farthestLeft = parseInt(j(target).find('.gallery-item:last').css('left').replace('px','')) + c.boxWidth;
		j(target).find('.gallery-item:eq('+c.doubleClicks+')').clone().appendTo(j(target).find('.slideContainer'));
		j(target).find('.gallery-item:last').css('left',farthestLeft);
	}
	c.doubleClicks++;
}

function callbackNext(c){
	if(c.continuous) {
		for(var i=0; i<c.doubleClicks; i++) {
			j(c.target).find('.gallery-item:first').remove();
		}
		c.doubleClicks = 0;
	}
}

function callbackPrev(c){
	if(c.continuous) {
		for(var i=0; i<c.doubleClicks; i++) {
			j(c.target).find('.gallery-item:last').remove();
		}
		c.doubleClicks = 0;
	}
}