/**
 *  PlugIn for JQuery to animate div circle movements
 *  Author: XK @ gcs GmbH Salzburg
 *  contact: xk@gcs-salzburg.at
 *  version: 0.1 ALPHA
 */

(function($)
{
	$.circle=function( element_hoover, elements, params )
	{
		//var locked=false;
		
		try
		{
			//store parameters
			var obj_element=$( element_hoover );
			var obj_elements=$( elements );
			var img="";
			for( var i in params )
			{
				if ( i=="img" )
				{
					this.img = params[ i ];				
				}
				else
				{
					this[ i ] = parseInt( params[ i ] );	
				}
				
			}
			
			if ( this.img!=null )
			{
				img=this.img;
			}
			
			//setup the elements
			obj_elements.css( "opacity","0" );
			obj_elements.css( "position","absolute" );
			obj_elements.css( "z-index", "-5" );
			
			//setup the circle mechanism
			//first two params are the center of the circle (in this case center of the picture)
			var center_x = ( this.center_x==null ? 0 : this.center_x );
			var center_y = ( this.center_y==null ? 0 : this.center_y );
			var radius = ( this.radius==null ? 200 : this.radius ); //the radius of the circle
			var fade_in_start = ( this.fade_in_start==null ? 180 : this.fade_in_start ); //fade IN circle start degree
			var fade_in_end = ( this.fade_in_end==null ? 65 : this.fade_in_end ); //fade IN circle end degree
			var fade_out_start = ( this.fade_out_start==null ? 65 : this.fade_out_start ); //fade OUT circle start degree
			var fade_out_end = ( this.fade_out_end==null ? -110 : this.fade_out_end ); //fade OUT circle end degree
			var fade_in_speed = ( this.fade_in_speed==null ? 1000 : this.fade_in_speed ); //fade IN speed
			var fade_out_speed = ( this.fade_out_speed==null ? 750 : this.fade_out_speed ); //fade OUT speed
			
			/*
			$( document ).ready( function(){
				if ( img != "" )
					$( "#"+img  ).maphilight();
			});
			*/
			
			/*
			function clearLock()
			{
				if( locked == true )
					locked = false;
			}
			
			function getTime()
			{
				var tmp=new Date;
				return tmp.getTime();
			}
			
			var lock_time = getTime();
			
			function setLock()
			{
				lock_time = getTime();
				locked = true;
			}
			
			obj_element.mouseout( function()
			{
				clearLock();
			} );
			*/
			
			obj_element.mouseover( function()
			{
				var index = $( this ).index();
				
				/*
				if ( ( getTime() - lock_time ) > fade_in_speed ) //auto reset lock after fade_in_speed amount of time
				{
					clearLock();
				}
				
				if( locked == true ) 
					return;
				
				setLock();
				*/
			    do_move( obj_elements[ index ] );
			} );
			
			function do_animate( elem, properties, duration, done_event )
			{
				elem.stop(); //stop previous animations (if still running)
				
				if ( done_event=="undefined" )
					elem.animate( properties, duration );
				else
					elem.animate( properties, duration, done_event );
			}
		
			function do_move( elem )
			{
				if ( elem == null ) //check param
					return;
				
			    var req_circle = $( elem ); //the requested circle
			    
			    //if already hoovered, do nothing
			    if( req_circle == null || req_circle.css( "opacity" )==1 )
			        return;
		
			    //first clear each circle
			    obj_elements.each( function( i )
			    {
			    	var sel_circle = $( this );
			    	
			    	//hide all circles
			    	if ( sel_circle.css( "opacity" ) == 1 )
			   		{
			    		do_animate( sel_circle, {
			    			path : new $.path.arc( { //using the path plugin
			    				center	: [ center_x, center_y ],
			                    radius	: radius,
			                    start	: fade_out_start,
			                    end     : fade_out_end,
			                    dir		: -1
			    			} ),
			                opacity : "0"
			            }, fade_out_speed );
			   		}
			    	else
			   		{
			    		do_animate( sel_circle, { "opacity":"0" }, 200 );
			   		}
			    });
		
			    //second show the requested circle
			    do_animate( req_circle, {
			        path : new $.path.arc( {
			            center	: [ center_x, center_y ],
			            radius	: radius,
			            start	: fade_in_start,
			            end		: fade_in_end,
			            dir		: -1
			        } ),
			        opacity : "1"
			    }, fade_in_speed ); //, function(){ clearLock(); } );
			}
		}
		catch( ex )
		{
			//locked=false;
			
			if ( console!=null && console!="undefined" )
			{
				console.log( ex );
			}
		}
	};
})(jQuery);

/*
 * jQuery css bezier animation support -- Jonah Fox
 * version 0.0.1
 * Released under the MIT license.
 */

;(function($){

  $.path = {}

  var V = {
    rotate: function(p, degrees) {
      var radians = degrees * 3.141592654 / 180
      var c = Math.cos(radians), s = Math.sin(radians)
      return [c*p[0] - s*p[1], s*p[0] + c*p[1] ]
    },
    scale: function(p, n) {
      return [n*p[0], n*p[1]]
    },
    add: function(a, b) {
      return [a[0]+b[0], a[1]+b[1]]
    },
    minus: function(a, b) {
      return [a[0]-b[0], a[1]-b[1]]
    }
  }
   
   $.path.bezier = function( params ) { 
     	params.start = $.extend({angle: 0, length: 0.3333}, params.start )
     	params.end   = $.extend({angle: 0, length: 0.3333}, params.end )

     this.p1 = [params.start.x, params.start.y];
     this.p4 = [params.end.x, params.end.y];
     
     var v14 = V.minus(this.p4, this.p1)
     var v12 = V.scale(v14, params.start.length)
     v12 = V.rotate(v12, params.start.angle)
     this.p2 = V.add(this.p1, v12)
      
     var v41 = V.scale(v14, -1)
     var v43 = V.scale(v41, params.end.length)     
     v43 = V.rotate(v43, params.end.angle)
     this.p3 = V.add(this.p4, v43)

     this.f1 = function(t) { return (t*t*t); }
     this.f2 = function(t) { return (3*t*t*(1-t)); } 
     this.f3 = function(t) { return (3*t*(1-t)*(1-t)); }
     this.f4 = function(t) { return ((1-t)*(1-t)*(1-t)); }

     /* p from 0 to 1 */
     this.css = function(p) {
       var f1 = this.f1(p), f2 = this.f2(p), f3 = this.f3(p), f4=this.f4(p)
       var x = this.p1[0]*f1 + this.p2[0]*f2 +this.p3[0]*f3 + this.p4[0]*f4;
       var y = this.p1[1]*f1 + this.p2[1]*f2 +this.p3[1]*f3 + this.p4[1]*f4;
       return {top: y + "px", left: x + "px"}
     }
   }

   $.path.arc = function(params) {
     for(var i in params)
       this[i] = params[i]

     this.dir = this.dir || 1

     while(this.start > this.end && this.dir > 0)
       this.start -= 360

     while(this.start < this.end && this.dir < 0)
       this.start += 360


     this.css = function(p) {
       var a = this.start * (p ) + this.end * (1-(p ))  
       a = a * 3.1415927 / 180 // to radians

       var x = Math.sin(a) * this.radius + this.center[0]
       var y = Math.cos(a) * this.radius + this.center[1]
       return {top: y + "px", left: x + "px"}
     } 

   };
   
       
  $.fx.step.path = function(fx){
    var css = fx.end.css(1 - fx.pos)
    for(var i in css) 
      fx.elem.style[i] = css[i];
  }
})(jQuery);
