/*
Developed by Kyle Somogyi
*/
function Shiftr(_ia)
{
	var me = this;

	// Global Configuration Variables
	me.autoScroll = me.setValue(_ia.autoScroll, false);
	me.galleryTitle = me.setValue(_ia.galleryTitle, false);
	me.mainElement = me.setValue(_ia.bindTo, function(){ console.log('You must set an element to bind to for Shiftr...'); });
	me.animation = me.setValue(_ia.animation, function(){ console.log('Shiftr needs an animation to work.'); });
	me.animationSpeed = me.setValue(_ia.animationSpeed, 500);
	me.jumpAmount = me.setValue(_ia.jumpAmount, 1);
	me.modules = me.setValue(_ia.modules, false);

	me.ext = eval('new '+me.animation+'(me)');

	if(me.modules != false)
	{
		me.mod = [];
		for(x in me.modules)
		{
			var tM = eval('new '+x+'(me, me.modules[x].options)');
			me.mod.push(tM);
		}
	}

	if(_ia.disableEvents != true)
		me.bindEvents();
}

Shiftr.prototype.bindEvents = function()
{
	var me = this;
	$(me.mainElement).find('.buttonPrevious, .buttonNext').css(
	{
		'position': 'absolute',
		'z-index': '999'
	});
	$(me.mainElement).find('.buttonPrevious').click(function()
	{
		me.ext.prev();
		for(x in me.mod)
		{
			if(me.mod[x].click)
				me.mod[x].click();
		}
	});
	$(me.mainElement).find('.buttonNext').click(function()
	{
		me.ext.next();
		for(x in me.mod)
		{
			if(me.mod[x].click)
				me.mod[x].click();
		}
	});
	$(me.mainElement).hover(function()
	{
		for(x in me.mod)
		{
			if(me.mod[x].hoverStart)
				me.mod[x].hoverStart();
		}
	}, function()
	{
		for(x in me.mod)
		{
			if(me.mod[x].hoverEnd)
				me.mod[x].hoverEnd();
		}
	});

	if(me.galleryTitle == true)
	{
		if($(me.mainElement).find('.title').length <= 0)
			$(me.mainElement).prepend('<div class="title"></div>');
		
		setTimeout(function()
		{
			$(me.mainElement).find('.title').html($(me.mainElement).children().find(':first').attr('title'));
		}, me.animationSpeed);
	}
};

// Set value to default _dv if _mv is undefined or empty
Shiftr.prototype.setValue = function(_mv, _dv)
{
	if(_mv == undefined || _mv == '' || _mv == null)
	{
		if(typeof _dv == 'function')
			_dv();
		else
			return _dv;
	} else
		return _mv;
};
