//version 1.0
//by: eBizzSol
//url: www.ebizzsol.com
/*
container:'#jContainer'
slide:'.slide'
paginContainer:'#paging'
paginElem:'li'
fadeTime:1.0
waitTime:5.0
next:'#next'
previous:'#prev'
pauseToggle:'#playPause'
isRandom:true
topBottom:true
diagonal:true
transition:'slide'
noAutoSlide:true/false //do not autometic slide
example: new fadeAppear({container:'#jContainer',slide:'.slide',paginContainer:'#paging',paginElem:'li',fadeTime:0.5,waitTime:5.0,next:'#next',previous:'#prev',isRandom:true,transition:'slide'});
*/
var fadeAppear = function(o){
	var ths = new Object;
	ths.waitTime = (o.waitTime?o.waitTime:5.0)*1000;
	ths.fadeTime = (o.fadeTime?o.fadeTime:0.5)*1000;
	ths.isRandom = o.isRandom?o.isRandom:false;
	ths.transition = o.transition?o.transition:'fade';
	ths.elems = $(o.container + ' ' + o.slide);
	ths.paging = $(o.paginContainer + ' ' + o.paginElem);
	$(o.paginContainer + ' a').attr('href','javascript:');
	ths.topBottom = o.topBottom?o.topBottom:false;
	ths.diagonal = o.diagonal?o.diagonal:false;
	ths.nextId = o.next?o.next:'';
	ths.noAutoSlide = o.noAutoSlide?o.noAutoSlide:false;
	ths.previousId = o.previous?o.previous:'';
	ths.pauseToggle = o.pauseToggle?o.pauseToggle:'';
	if( ths.transition == 'slide' ){
		ths.eachWidth = $(o.container).width();
		ths.eachHeight = $(o.container).height();
		}
	if( ths.isRandom ) ths.hide = Math.round(Math.random() * (ths.elems.length-1));
	else ths.hide = ths.elems.length-1;
	ths.show = ths.hide+1;
	if( ths.show > ths.elems.length-1 ) ths.show = 0;
	if( ths.hide > ths.elems.length-1 ) ths.hide = 0;
	ths.interval = null;
	ths.isPrev = false;
	ths.transitionInterval = null;
	ths.swap = function(){
		ths.tmp = ths.hide;
		ths.hide = ths.show;
		ths.show = ths.tmp;
		}
	ths.fade = function(){
		if( ths.interval )
			clearInterval(ths.interval);
		if( ths.isPrev && !ths.goingDirect ){
			if( ths.transition == 'fade' ) ths.swap();
			else if( ths.transition == 'slide' ) ths.show-=2;
			}
		if( ths.show > ths.elems.length-1 ) ths.show = 0;
		else if( ths.show < 0 ) ths.show = ths.elems.length-1;
		if( ths.hide > ths.elems.length-1 ) ths.hide = 0;
		else if( ths.hide < 0 ) ths.hide = ths.elems.length-1;
		if( ths.transition == 'fade' ){
			$(ths.elems[ths.hide]).fadeOut(ths.fadeTime);
			$(ths.elems[ths.show]).fadeIn(ths.fadeTime);
			}
		else{
			if( ths.topBottom ){
				if( ths.isPrev ){
					$(ths.elems[ths.show]).css({top:'-'+ths.eachHeight+'px'});
					$(ths.elems[ths.hide]).css({top:'0px'});
					$(ths.elems[ths.hide]).animate({top:'+'+ths.eachHeight+'px'},ths.fadeTime);
					$(ths.elems[ths.show]).animate({top:'0px'},ths.fadeTime);
					}
				else{
					$(ths.elems[ths.show]).css({top:'+'+ths.eachHeight+'px'});
					$(ths.elems[ths.hide]).css({top:'0px'});
					$(ths.elems[ths.hide]).animate({top:'-'+(ths.eachHeight+1)+'px'},ths.fadeTime);
					$(ths.elems[ths.show]).animate({top:'0px'},ths.fadeTime);
					}
				}
			else if( ths.diagonal ){
				if( ths.isPrev ){
					$(ths.elems[ths.show]).css({left:'+'+ths.eachWidth+'px',top:'-'+ths.eachHeight+'px'});
					$(ths.elems[ths.hide]).css({left:'0px',top:'0px'});
					$(ths.elems[ths.hide]).animate({left:'-'+ths.eachWidth+'px',top:'-'+ths.eachHeight+'px'},ths.fadeTime);
					$(ths.elems[ths.show]).animate({left:'0px',top:'0px'},ths.fadeTime);
					}
				else{
					$(ths.elems[ths.show]).css({left:'+'+ths.eachWidth+'px',top:'-'+ths.eachHeight+'px'});
					$(ths.elems[ths.hide]).css({left:'0px',top:'0px'});
					$(ths.elems[ths.hide]).animate({left:'-'+(ths.eachWidth+1)+'px',top:'-'+ths.eachHeight+'px'},ths.fadeTime);
					$(ths.elems[ths.show]).animate({left:'0px',top:'0px'},ths.fadeTime);
					}
				}
			else{
				if( ths.isPrev ){
					$(ths.elems[ths.show]).css({left:'-'+ths.eachWidth+'px'});
					$(ths.elems[ths.hide]).css({left:'0px'});
					$(ths.elems[ths.hide]).animate({left:'+'+ths.eachWidth+'px'},ths.fadeTime);
					$(ths.elems[ths.show]).animate({left:'0px'},ths.fadeTime);
					}
				else{
					$(ths.elems[ths.show]).css({left:'+'+ths.eachWidth+'px'});
					$(ths.elems[ths.hide]).css({left:'0px'});
					$(ths.elems[ths.hide]).animate({left:'-'+(ths.eachWidth+1)+'px'},ths.fadeTime);
					$(ths.elems[ths.show]).animate({left:'0px'},ths.fadeTime);
					}
				}
			}
		
		$(ths.paging[ths.hide]).removeClass('active');
		$(ths.paging[ths.show]).addClass('active');
		
		if( ths.isPrev && !ths.goingDirect ){
			if( ths.transition == 'slide' ){
				ths.show++;
				ths.hide = ths.show-1;
				}
			else{
				ths.swap();
				ths.show = ths.hide--;
				}
			}
		else{
			ths.hide = ths.show++;
			}
		ths.isPrev = false;
		ths.goingDirect = false;
		if( !ths.noAutoSlide ) ths.interval = setInterval(ths.fade,ths.waitTime);
		}
	ths.pause = function(){
		clearInterval(ths.interval);
		}
	ths.play = function(){
		clearInterval(ths.interval);
		if( !ths.noAutoSlide ) ths.interval = setInterval(ths.fade,ths.waitTime);
		}
	ths.clearTransition = function(){
		clearInterval(ths.transitionInterval);
		ths.transitionInterval = null;
		}
	ths.next = function(){
		if( ths.transitionInterval ) return;
		ths.transitionInterval = setInterval(ths.clearTransition,ths.fadeTime);
		clearInterval(ths.interval);
		ths.fade();
		}
	ths.prev = function(){
		if( ths.transitionInterval ) return;
		ths.transitionInterval = setInterval(ths.clearTransition,ths.fadeTime);
		clearInterval(ths.interval);
		ths.isPrev = true;
		ths.fade();
		}
	this.directGo2 = function(index){
		if( ths.transitionInterval ) return;
		ths.havToGo = index;
		if( ths.havToGo == ths.hide ) return; // same slide don't need to go
		ths.goingDirect = true;
		if( ths.havToGo < ths.show ) ths.isPrev = true;
		ths.show = ths.havToGo;
		ths.transitionInterval = setInterval(ths.clearTransition,ths.fadeTime);
		ths.fade();
		}
	ths.directGo = function(){
		if( ths.transitionInterval ) return;
		ths.havToGo = $(this.tagName,$(this).parent()).index(this,$(this).parent());
		if( ths.havToGo == ths.hide ) return; // same slide don't need to go
		ths.goingDirect = true;
		if( ths.havToGo < ths.show ) ths.isPrev = true;
		ths.show = ths.havToGo;
		ths.transitionInterval = setInterval(ths.clearTransition,ths.fadeTime);
		ths.fade();
		}
	if( ths.elems.length > 1 ){
		//initialize controllers
		if( ths.nextId ) $(ths.nextId).click(ths.next).attr('href','javascript:');
		if( ths.previousId ) $(ths.previousId).click(ths.prev).attr('href','javascript:');
		if( ths.pauseToggle ) $(ths.pauseToggle).toggle(
			function(){
				$(ths.pauseToggle).addClass('pause');
				$(ths.pauseToggle).removeClass('play');
				ths.pause();
				},
			function(){
				$(ths.pauseToggle).addClass('play');
				$(ths.pauseToggle).removeClass('pause');
				ths.play();
				}).attr('href','javascript:');
		for( i = 0; i < ths.elems.length; i++ ){
			e = ths.elems[i];
			$(e).mouseover(ths.pause);
			$(e).mouseout(ths.play);
			if(ths.paging.length) $(ths.paging[i]).click(ths.directGo)
			if( ths.transition == 'slide' ){
				if( ths.topBottom ) $(e).css({height:ths.eachHeight+'px'});
				else $(e).css({width:ths.eachWidth+'px'});
				}
			if( ths.hide != i ){
				if( ths.transition == 'fade' ) $(e).fadeOut(20);
				else if( ths.topBottom ) $(e).css({top:'-'+ths.eachHeight+'px'});
				else $(e).css({left:'-'+ths.eachWidth+'px'});
				}
			}
		$(ths.paging[ths.hide]).addClass('active');
		ths.fade();
		}
	else{
		//hide controllers
		//$(ths.paging).css({visibility:'hidden'});
		if( ths.nextId ) $(ths.nextId).css({visibility:'hidden'});
		$(o.previous).css({visibility:'hidden'});
		$(ths.pauseToggle).css({visibility:'hidden'});
		}
	}