// -----------------------------------------------------------------------------
// proton JavaScript Library (inspired by the prototype.js framework [grate job
// Sam!], moo.fx [nice one Valerio!] and lots other grate code for which I will
// give all the necesary credit when due...).
//
// Code freely distributable under the terms of the GNU GPL.
//
// 01.11.2005
//  - creation
// -----------------------------------------------------------------------------

var agt = navigator.userAgent.toLowerCase();

var is_dom     = (document.getElementById ? true:false);
var is_ie      = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_opera   = (agt.indexOf('opera') != -1);
var is_gecko   = (agt.indexOf('gecko') != -1);
var is_firefox = (agt.indexOf('firefox') != -1);

// -----------------------------------------------------------------------------

var Proton = new Object();

// -----------------------------------------------------------------------------

Proton.Cookie = Class.create();
Proton.Cookie = {
		get_cookie: function( name ) {
			arg  = name+"=";
			alen = arg.length;
			clen = document.cookie.length;
			i=0;
			while( i < clen ) {
				j=i+alen;
				if( document.cookie.substring(i,j) == arg ) {
					return this.get_value(j);
				}
				i=document.cookie.indexOf(" ",i) + 1;
				if( i === 0 ) {
					break;
				}
			}
		},
		
		get_value: function( offset ) {
			endstr=document.cookie.indexOf(";", offset);
			
			if( endstr == -1 ) {
				endstr = document.cookie.length;
			}
			
			return unescape( document.cookie.substring(offset,endstr) );
		},
		
		set_value: function( name,value ) {
			argv=arguments;
			argc=arguments.length;
			expires=(argc>2) ? argv[2] : null;
			path=(argc>3) ? argv[3] : null;
			domain=(argc>4) ? argv[4] : null;
			secure=(argc>5) ? argv[5] : false;
			document.cookie=name+"="+escape(value) +
				((expires === null) ? "" : ("; expires="+expires.toUTCString())) +
				((path === null) ? "" : ("; path="+path)) +
				((domain === null) ? "" : ("; domain="+domain)) +
				((secure === true) ? "; secure" : "");
		}
};

// -----------------------------------------------------------------------------

Proton.DomObj = Class.create();
Proton.DomObj.prototype = {	
	initialize: function( obj_id ) {
		with( this ) {
			this.ref = $( obj_id );
		    this.sty = this.ref.style;
		}
	},
	
	x: function() {
   		if( isNaN(arguments[0]) ) {
   			return parseInt( this.sty.left );
   		} else {
   			this.sty.left = arguments[0] + 'px';
   		}
	},
	
	y: function() {
		if( isNaN(arguments[0]) ) {
			return parseInt( this.sty.top );
		} else {
			this.sty.top = arguments[0] + 'px';
		}
    },
	
	w: function() {
   		if( isNaN(arguments[0]) ) {
   			return this.ref.offsetWidth;
   		} else {
   			this.sty.width = arguments[0] + 'px';
   		}
    },
	
	h: function() {
   		if( isNaN(arguments[0]) ) {
   			return this.ref.offsetHeight;
   		} else {
   			this.sty.height = arguments[0] + 'px';
   		}
    },
	
	alpha: function() {
   		if( is_ie ) {
   			this.sty.filter = ' Alpha(Opacity=' + arguments[0] + ')';
  		} else if( is_dom ) {
   			this.sty.MozOpacity = arguments[0]/100+';';
  		}
    },
	
	class_name: function() {
		if( isNaN(arguments[0]) ) {
			return this.ref.className;
		} else {
			this.ref.className = arguments[0];
		}
    }
};

// -----------------------------------------------------------------------------

Proton.AnimationBase = Class.create();
Proton.AnimationBase.prototype = {

	init: function( obj, options ) {
		this.obj = new Proton.DomObj( obj );
		this.options = {
			duration: 300,
			is_hidden: false,
			on_complete: ''
		}
		Object.extend( this.options, options || {} );
	},
	
	custom: function( start_value, end_value ) {
		if( this.timer != null ) {
			return;
		} else {
			this.start_value = start_value;
			this.end_value   = end_value;
			this.go();
		}
	},

	go: function() {
		this.start_time = (new Date).getTime();
		this.timer     = setInterval( this.generator.bind(this), 13 );
	},

	generator: function() {
		var time = (new Date).getTime();
		var now  = ( time - this.start_time ) / this.options.duration;
		if( time >= this.options.duration + this.start_time ) {
			this.now = this.end_value;
			clearInterval( this.timer );
			this.timer = null;
			if( this.options.on_complete ) {
				setTimeout(this.options.on_complete.bind(this), 10);
			}
		} else {
			this.now = ((-Math.cos(now*Math.PI)/2) + 0.5) * (this.end_value - this.start_value) + this.start_value;
			//this position generation equation is from script.aculo.us
		}
		this.set(); // set() is ment to be overriden by the extensions of the class
	}
	
};

Proton.AnimateHeight = Class.create();
Object.extend( Object.extend(Proton.AnimateHeight.prototype, Proton.AnimationBase.prototype), {

	initialize: function( obj, options ) {
		this.init( obj, options );
		this.obj.sty.overflow   = 'hidden';
		this.obj.initial_height = this.obj.h();
	},

	set: function() {
		this.obj.h( this.now );
	},
	
	toggle: function() {
		if( this.obj.h() > 0 ) {
			this.custom( this.obj.h(), 0 );
		} else {
			this.custom( 0, this.options.is_hidden ? this.obj.ref.scrollHeight:this.obj.initial_height );
		}
	}
	
});

Proton.AnimateWidth = Class.create();
Object.extend( Object.extend(Proton.AnimateWidth.prototype, Proton.AnimationBase.prototype), {

	initialize: function( obj, options ) {
		this.init( obj, options );
		this.obj.sty.overflow = 'hidden';
		this.obj.initial_width = this.obj.w();
	},

	set: function() {
		this.obj.w( this.now );
	},
	
	toggle: function() {
		if( this.obj.w() > 0 ) {
			this.custom( this.obj.w(), 0 );
		} else {
			this.custom( 0, this.obj.initial_width );
		}
	}
	
});

Proton.AnimateAlpha = Class.create();
Object.extend( Object.extend(Proton.AnimateAlpha.prototype, Proton.AnimationBase.prototype), {

	initialize: function( obj, options ) {
		this.init( obj, options );
		this.now = 1;
	},
	
	set: function() {
		this.obj.alpha( this.now );
		if( this.now > 0 ) {
			this.obj.sty.visibility = 'visible';
		} else {
			this.obj.sty.visibility = 'hidden';
		}
	},
	
	toggle: function() {
		if( this.now > 0 && !this.options.is_hidden ) {
			this.custom( 100, 0 );

		} else {
			this.custom( 0, 100 );
		}
	}

});

Proton.AnimateHeightAlpha = Class.create();
Object.extend( Object.extend( Proton.AnimateHeightAlpha.prototype, Proton.AnimationBase.prototype), {
	
		initialize: function( obj, options ) {
			this.init( obj, options );
			this.animate_height = new Proton.AnimateHeight( obj, options );
			this.animate_alpha  = new Proton.AnimateAlpha( obj, options );
		},
		
		toggle: function() {
			this.animate_height.toggle();
			this.animate_alpha.toggle();
		},
		
		custom: function( start_value, end_value ) {
			this.animate_height.custom( start_value, end_value );
			this.animate_alpha.custom( start_value, end_value);
		}
		
});

//:~(w)ritten by Silviu Lucian <silviulucian@gmail.com>
