/* Begin: TragicMedia.com - jQuery Fade Script
/* created 1/10 by Rich Rudzinski
/*------------------------------------------*/
	// opacitySpeed is how fast the item will fade in/out, the intervalSpeed is how often the items will change (in milliseconds), opacitySpeed is in reverse, the lower the number the slower the fade
	// var constants = {container:"fadeCont", itemClass:"item", opacitySpeed:1000, intervalSpeed:3000, pageDots:true, arrowLeft:"#id", arrowRight"#id"};
	// var fade = new jFade(constants);
	
function jFade(values) {
	var obj = this;
	this.animate = false;
	this.values = values;
	// set default values if not filled in
	this.container = $('#'+values.container);					 
	if(this.values.opacitySpeed == undefined) this.values.opacitySpeed = 60;
	if(this.values.intervalSpeed == undefined) this.values.intervalSpeed = 90000;
	this.values.pageDots = (values.pageDots == undefined) ? false : values.pageDots;
	this.values.arrows = (this.values.arrowLeft == undefined || this.values.arrowLeft == '') ? false : true;
	if(this.values.arrows) this.addArrowEvent();
	this.fadeItems = $('#'+this.container.attr('id')+ ' .'+values.itemClass);
	if(this.values.pageDots) {
		this.makePageDots();
	}
	this.currItem = 0;
	this.initCss();
	this.orderItems();
	this.fadeInterval = setInterval(function() { 
		obj.animate = true;
		$(obj.fadeItems[0]).animate({
			opacity: 0
		}, obj.values.opacitySpeed, function() {
			$(obj.fadeItems[0]).insertAfter($(obj.fadeItems[obj.fadeItems.length - 1]));
			obj.orderItems();
			obj.animate = false;
		});
		if(obj.currItem + 1 == obj.fadeItems.length) obj.currItem = 0;
		else obj.currItem++;
		if(obj.values.pageDots) { 
			obj.changePageDot(obj.currItem);
		}
	}, this.values.intervalSpeed);
}	
// initial CSS settings
jFade.prototype.initCss = function() {
	this.container.css({
		'width':$(this.fadeItems[0]).width() + 'px',
		'height':$(this.fadeItems[0]).height() + 'px',
		'overflow':'hidden',
		'position':'relative'
	});
	this.fadeItems.css({
		'width':$(this.fadeItems[0]).width() + 'px',
		'height':$(this.fadeItems[0]).height() + 'px',
		'overflow':'hidden',
		'position':'absolute',
		'top':this.container.css('padding-top'),
		'left':0,
		'opacity':1,
		'filter':'alpha(opacity=100)'
	});
}
// create new page element for page dots
jFade.prototype.makePageDots = function() {
	var obj = this;
	this.container.prepend('<div class="pageDots"></div>');
	this.dotContainer = $('#'+this.container.attr('id')+' .pageDots');
	for(i=0; i<this.fadeItems.length; i++) {
		this.dotContainer.append('<a href="#" class="imgDots" rel="'+i+'">'+i+'</a>');
	}
	this.dotLinks = $('#'+this.container.attr('id')+' .imgDots');
	this.dotLinks.click(function() {
		if(!obj.animate) obj.dotSkip($(this).attr('rel'));
		return false;
	});
	// define css
	this.dotLinks.css('float', 'left');
	this.dotContainer.css({
		'width':this.dotLinks[0].offsetWidth*this.fadeItems.length+'px',
		'position':'absolute',
		'top':0,
		'left':Math.round(($(this.fadeItems[0]).width() - this.dotLinks[0].offsetWidth*this.fadeItems.length)/2) + 'px'
	});
	this.container.css('padding-top', this.dotLinks[0].offsetHeight + 5 + 'px');
	$($('#'+this.container.attr('id')+' .imgDots')[0]).addClass('active');
}
// Add onclicks for side arrows
jFade.prototype.addArrowEvent = function() {
	var obj = this;
	this.arrowLeft = $('#'+this.values.arrowLeft);
	this.arrowRight = $('#'+this.values.arrowRight);
	this.arrowLeft.click(function() {
		if(!obj.animate) obj.dotSkip(parseInt(obj.currItem)-1);
		return false;
	});
	this.arrowRight.click(function() {
		if(!obj.animate) obj.dotSkip(parseInt(obj.currItem)+1);
		return false;
	});
}
// Sets container width, shifts elements if startPosition != 1			
jFade.prototype.orderItems = function() {
	this.fadeItems = $('#'+this.container.attr('id')+ ' .'+this.values.itemClass);
	for(var i=0; i<this.fadeItems.length; i++) {
		$(this.fadeItems[i]).css({
			'zIndex':this.fadeItems.length - i,
			'opacity':1
		});
	}
}
// handle dot click
jFade.prototype.dotSkip = function(num) {
	if(num>=this.fadeItems.length) num = num - this.fadeItems.length;
	this.animate = true;
	var obj = this;
	currentItem = $(this.fadeItems[0]);
	var nextNum = ((num-this.currItem)>0) ? num-this.currItem : num-this.currItem+this.fadeItems.length;
	nextItem = $(this.fadeItems[nextNum]);
	if(num != this.currItem) {
		clearTimeout(this.fadeInterval);		
		for(i=0; i<this.fadeItems.length; i++) {
			if(i==0 || i==nextNum) {
				continue;
			} else {
				$(this.fadeItems[i]).css('opacity', 0);
			}
		}
		var skip = (num - this.currItem > 0) ? num - this.currItem : parseInt(this.fadeItems.length) - this.currItem + parseInt(num);
		currentItem.animate({
			opacity:0
		}, this.values.opacitySpeed, function() {
			for(var n=0; n<skip; n++) {
				$(obj.fadeItems[0]).insertAfter($(obj.fadeItems[obj.fadeItems.length - 1]));
				obj.orderItems();
			}
			obj.animate = false;
		});
		
		this.currItem = (num >= 0) ? num : parseInt(this.fadeItems.length) - this.currItem + parseInt(num);
		if(this.values.pageDots) { 
			this.changePageDot(this.currItem);
		}
	}
}
// adjust dots according to scroll position
jFade.prototype.changePageDot = function(num) {
	this.dotLinks.removeClass('active');
	$(this.dotLinks[num]).addClass('active');
}
	
//------------------------------- End Rich's jQuery Fade Script --------------------------------------->
