/**
 * NyaToggleText jQuery plugin for text input or area elements.
 * If default text is displayed then element will get class 'toggle-no-entry', so you can style it.
 * Note that before sending form you should strip values of all elements with class 'toggle-no-entry', or otherwise that default text will be sent to server like it was entered by user.
 * @author Ivan Hušnjak
 * @param string defaultText Text that will be toggled - removed on focus, and reapear on empty text field - if omitted plugin will use current value of element
 */
$.fn.nyaToggleText = function(defaultText){
	var isToggle = false;
    if(defaultText){
		if(!$(this).val()){ // if not already set to some value!
			isToggle = true;
			$(this).val(defaultText);
		}
    }
    else {
        defaultText = $(this).val();
    }
    if(defaultText){
		if(isToggle){
			$(this).addClass('toggle-no-entry');
		}
	    $(this).focus(function(){
		    if( $(this).val() == defaultText ){
			    $(this).val('');
			    $(this).removeClass('toggle-no-entry');
		    }
	    });
	    $(this).blur(function(){
		    if( $(this).val() == '' ){
			    $(this).val(defaultText);
			    $(this).addClass('toggle-no-entry');
		    }
	    });
	}
	return this;
}
/**
 * NyaPause jQuery plugin
 * @author Ivan Hušnjak
 * @param int duration If > 0 it will delay execution of animations/events for this object
 */
$.fn.nyaPause = function(duration) {
	if(duration > 0){
		$(this).animate({ dummy: 1 }, duration);
	}
    return this;
};
/**
 * NyaBounce jQuery plugin
 * @author Ivan Hušnjak
 * @param object options
 * option name: default value - description
 * 		delay: 3000				- miliseconds to delay bouncing (it will delay it each time a bounce is started)
 * 		speed: 25				- speed of bouncing effect in px/sec
 * 		bounce: 'vertical'		- bouncing axis, either 'vertical' or 'horizontal'
 * 		bounceFromEnd: false 	- bouncing will start from the other end of axis
 * 		pausing: true			- wether or not to pause bouncing on mousein, and continue on mouseleave
 * 		delayAfterPause: 50 	- miliseconds to delay bouncing after mouseleave event
 * 		container: 'div'		- inner container of bouncing element, a jQuery|string selector|DOM
 * 		css: {}					- if you need to set somekind of custom css on bounceing start
 * 		force: false			- if set true, then it will force bouncing even if inner container is not larger than parent bouncer element
 */
$.fn.nyaBounce = function(options){
	// is bouncing paused
	this._paused = false;
	// default & custom options
	this.nyaBounceOptions = {delay: 3000, speed: 25, bounce: 'vertical', bounceFromEnd: false, pausing: true, afterPauseDelay: 50, container: 'div', css: {}, force: false };
	var obj = this;
	if(options){
		$.each(options, function(key, option){
			obj.nyaBounceOptions[key] = option;
		});
	}
	this._nyaInnerBounceOptions = { }; // internal options, not visible
	// recalc some inner thingies ;-)
	this._nyaInnerBounceOptions.speed = 1000 / this.nyaBounceOptions.speed;
	
	if( this.nyaBounceOptions.bounce == 'vertical' ){
		nextMethod = '_nyaBounceDown';
		if( this.nyaBounceOptions.bounceFromEnd){
			nextMethod = '_nyaBounceUp';
		}
	}
	else {
		nextMethod = '_nyaBounceLeft';
		if( this.nyaBounceOptions.bounceFromEnd){
			nextMethod = '_nyaBounceRight';
		}
	}
	this._nyaInnerBounceOptions.nextMethod = nextMethod;
	this._nyaInnerBounceOptions.container = $(this.nyaBounceOptions.container, this);
	
	
	this._nyaBounceTo = function(fetchMethod, scrollMethod, thisMethod, nextMethod, type){
		var box = eval('$(obj).'+ fetchMethod + '()');
		var inBox = eval('$(obj._nyaInnerBounceOptions.container).'+ fetchMethod + '()');
		var delay = obj.nyaBounceOptions.delay;
		var speed = obj._nyaInnerBounceOptions.start_speed;
		if( obj.nyaBounceOptions.pausing && obj._paused ){
			delay = obj.nyaBounceOptions.afterPauseDelay;
			if(type){
				speed = eval('$(obj).' + scrollMethod + '()')  * obj._nyaInnerBounceOptions.speed;
			}
			else {
				speed = (inBox - box - eval('$(obj).' + scrollMethod + '()') ) * obj._nyaInnerBounceOptions.speed;
			}
		}
		obj._nyaInnerBounceOptions.nextMethod = thisMethod;
		options = {};
		options[scrollMethod] = type? 0: inBox - box;
		$(obj).nyaPause(delay).animate(options, speed, function(){ eval('obj.' + nextMethod + '()'); });
	}
	
	// bounce downwards 
	this._nyaBounceDown = function(){
		obj._nyaBounceTo('height', 'scrollTop', '_nyaBounceDown', '_nyaBounceUp');
	};
	// bounce upwards
	this._nyaBounceUp = function(){
		obj._nyaBounceTo('height', 'scrollTop', '_nyaBounceUp', '_nyaBounceDown', true);
	};
	// bounce left
	this._nyaBounceLeft = function(){
		obj._nyaBounceTo('width', 'scrollLeft', '_nyaBounceLeft', '_nyaBounceRight');
	};
	// bounce right
	this._nyaBounceRight = function(){
		obj._nyaBounceTo('width', 'scrollLeft', '_nyaBounceRight', '_nyaBounceLeft', true);
	};
	// pause bouncing
	this._nyaBouncePause = function(){
		$(obj).stop(true, false);
	}
	// continue bouncing
	this._nyaBounceContinue = function(){
		obj._paused = true;
		eval( 'obj.' + obj._nyaInnerBounceOptions.nextMethod + '();');
		obj._paused = false;
	}
	
	// init
	if( this.nyaBounceOptions.bounce == 'vertical' ){
		var box = $(this).height();
		var inBox = $(this._nyaInnerBounceOptions.container).height();
	}
	else {
		var box = $(this).width();
		var inBox = $(this._nyaInnerBounceOptions.container).width();
	}
	
	if ( inBox > box || this.nyaBounceOptions.force) {
		// apply custom css
		if(this.nyaBounceOptions.css){
			$(this).css(this.nyaBounceOptions.css);
		}
		if( this.nyaBounceOptions.bounceFromEnd ){
			if( this.nyaBounceOptions.bounce == 'vertical'){
				$(this).scrollTop(inBox - box);
			}
			else {
				$(this).scrollLeft(inBox - box);
			}
		}
		this._nyaInnerBounceOptions.start_speed = (inBox - box) * this._nyaInnerBounceOptions.speed; // last thing to setup is the starting speed (for maintaining constant bounce speed)
		// execute first bounce
		eval( 'this.' + this._nyaInnerBounceOptions.nextMethod + '();');
		// attach pausing
		if(this.nyaBounceOptions.pausing){
			$(this).mouseenter(function(){ obj._nyaBouncePause() });
			$(this).mouseleave(function(){ obj._nyaBounceContinue() });
		}
	}
	
	return this;
}

