////////////////////////////////////////////////////////////////////////////////////////////
//
//	File:		smarts_fx.js
//	Author:		Alexander Jung
//	Copyright:	Copyright (c) 2007 Zebworx Interactive All Rights Reserved.
//	Datum:		1.11.2006
//	Notes:
//
////////////////////////////////////////////////////////////////////////////////////////////

var smarts_fx=function(){
	SmartS.fx=this;
};

new smarts_fx();

var smarts_fx_aufklapper=function(target,attr){
	this.element=(typeof(target)=='string')?document.getElementById(arguments[0]):arguments[0];

	if(typeof this.element._lock!='undefined'&&this.element._lock==true){
		this.element._toggle=true;
		return;
	}

	this.element._lock=true;

    if(!this.element.style.display||this.element.style.display=='none'){
        if(!this.element._init){
            this.element._visibility=this.element.style.visibility;
            this.element._overflow=this.element.style.overflow;
            this.element.style.visibility='hidden';
            this.element.style.display='block';
            this.element._geometry={
                width:this.element.offsetWidth,
                height:this.element.offsetHeight
                };
            this.element.style.display='none';
            this.element.style.height='0px';
            this.element.style.visibility=this.element._visibility;
            this.element._duration=(typeof attr.duration!='number')?1:attr.duration;
            this.element._steps=Math.ceil(this.element._geometry.height/((this.element._duration*1000)/5));

            this.element._lock=true;
            this.element._toggle=false;
            this.element._init=true;
        }

        this.element.style.overflow='hidden';
		this.element.style.display='block';

		this.element._innerHeight=(typeof(window.innerHeight)=='undefined')?document.documentElement.clientHeight:window.innerHeight;
		this.element._offset=SmartS_DOM.offsetTop(this.element)+this.element._geometry.height-this.element._innerHeight;

		if(this.element._offset>0){
		//	window.scrollBy(0,this.element._geometry.height);
		}
		
		this.open();
	}else{
		this.element.style.overflow='hidden';

		this.close();
	}
}

smarts_fx_aufklapper.prototype.open=function(){
	var newHeight=this.element.clientHeight+this.element._steps;

	if(this.element._duration==0){
		this.element.style.overflow=this.element._overflow;
		this.element.style.height=this.element._geometry.height+'px';
		this.element._lock=false;

		return;
	}

	if(newHeight>this.element._geometry.height){
		this.element.style.overflow=this.element._overflow;
		this.element.style.height=this.element._geometry.height+'px';
		this.element._lock=false;
	}else{
		this.element.style.height=newHeight+'px';

        if(this.element._toggle==true){
            this.element._toggle=false;
            SmartS.bind=this;
            setTimeout('SmartS.bind.close()',this.element._duration);
        }else{
            SmartS.bind=this;
            setTimeout('SmartS.bind.open()',this.element._duration);
        }
    }
}

smarts_fx_aufklapper.prototype.close=function(){
    var newHeight=this.element.clientHeight-this.element._steps;

    if(this.element._duration==0){
        this.element.style.display='none';
        this.element.style.overflow=this.element._overflow;
        this.element._lock=false;
        return;
    }

    if(newHeight<=0){
        this.element.style.height='0px';
        this.element.style.display='none';
        this.element.style.overflow=this.element._overflow;
        this.element._lock=false;
    }else{
        this.element.style.height=newHeight+'px';

        if(this.element._toggle==true){
            this.element._toggle=false;
            SmartS.bind=this;
            setTimeout('SmartS.bind.open()',this.element._duration);
        }else{
            SmartS.bind=this;
            setTimeout('SmartS.bind.close()',this.element._duration);
        }
    }
}

smarts_fx.prototype.aufklapper=function(target,attr){
	new smarts_fx_aufklapper(target,attr);
}

var smarts_fx_fade=function(target,attr){
	this.element=(typeof(target)=='string')?document.getElementById(arguments[0]):arguments[0];

	if(typeof this.element._lock!='undefined'&&this.element._lock==true){
		this.element._toggle=true;
		return;
	}

	this.element._lock=true;

	if(!this.element.style.display||this.element.style.display=='none'){
		if(!this.element._init){
			this.element.style.opacity=0;

			this.element._duration=(typeof attr.duration!='number')?1:attr.duration;
			this.element._steps=100/(this.element._duration*(1000/40));
			this.element._currentOpacity=0;
			this.element._lock=true;
			this.element._toggle=false;
			this.element._init=true;
		}

		this.element.style.display='block';

		this.fadein();
	}else{
		this.fadeout();
	}
}

smarts_fx_fade.prototype.fadein=function(){
	this.element._currentOpacity+=this.element._steps;

	if(this.element._duration==0){
		SmartS_DOM.setOpacity(this.element,100);
		// TODO: alex;;;this.element.style.filter=null; wird im moment gebraucht damit transparente png's die eingeblendet werden im ie7 richtig dargestellt werden
		this.element.style.filter=null;

		this.element._lock=false;

		return;
	}

	if(this.element._currentOpacity>=100){
		SmartS_DOM.setOpacity(this.element,100);
		// TODO: alex;;;this.element.style.filter=null; wird im moment gebraucht damit transparente png's die eingeblendet werden im ie7 richtig dargestellt werden
		this.element.style.filter=null;

		this.element._lock=false;
	}else{
		SmartS_DOM.setOpacity(this.element,this.element._currentOpacity);

		if(this.element._toggle==true){
			this.element._toggle=false;
			SmartS.bind=this;
			setTimeout('SmartS.bind.fadeout()',20);
		}else{
			SmartS.bind=this;
			setTimeout('SmartS.bind.fadein()',20);
		}
	}
};

smarts_fx_fade.prototype.fadeout=function(){
	this.element._currentOpacity-=this.element._steps;

	if(this.element._duration==0){
		SmartS_DOM.setOpacity(this.element,0);
		this.element.style.display='none';
		this.element._lock=false;
		return;
	}

	if(this.element._currentOpacity<=0){
		SmartS_DOM.setOpacity(this.element,0);
		this.element.style.display='none';
		this.element._lock=false;
	}else{
		SmartS_DOM.setOpacity(this.element,this.element._currentOpacity);

		if(this.element._toggle==true){
			this.element._toggle=false;
			SmartS.bind=this;
			setTimeout('SmartS.bind.fadein()',20);
		}else{
			SmartS.bind=this;
			setTimeout('SmartS.bind.fadeout()',20);
		}
	}
};

smarts_fx.prototype.fade=function(target,attr){
	new smarts_fx_fade(target,attr);
}

var smarts_fx_scroll=function(element,target,attr){
	this.element=(typeof(target)=='string')?document.getElementById(target):target;

	if(typeof this.element._lock!='undefined'&&this.element._lock==true){
		return;
	}

	this.element._lock=true;

	this.element._scrollOffset=(typeof attr.offset!='number')?100:attr.offset;
	this.element._scrollPos=(window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0);

	this.element._offset=SmartS_DOM.offsetTop(this.element)-this.element._scrollOffset;
	this.element._delta=this.element._offset-this.element._scrollPos;

	this.element._scrolldown=(this.element._delta>0)?true:false;
	
	this.element._duration=(typeof attr.duration!='number')?1:attr.duration;
	this.element._steps=Math.ceil(this.element._delta/(this.element._duration*(1000/40)));

	this.scrollto();
}

smarts_fx_scroll.prototype.scrollto=function(){
	var newOffset=(window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0)+this.element._steps;

	if(this.element._duration==0){
		window.scrollTo(0,this.element._offset);
		
		this.element._lock=false;

		return;
	}

	if((this.element._delta>0&&newOffset>this.element._offset)||((this.element._delta<0&&newOffset<this.element._offset)||this.element._currentOffset==newOffset)){
		window.scrollTo(0,this.element._offset);

		this.element._lock=false;
	}else{
		window.scrollBy(0,this.element._steps);

		this.element._currentOffset=newOffset;
		
		SmartS.bind=this;
		setTimeout('SmartS.bind.scrollto()',20);
	}
};

smarts_fx.prototype.scroll=function(element,target,attr){
	new smarts_fx_scroll(element,target,attr);
}
