
(function($){
/*----------------------------------------------------------------------------------
Class: FloatObject
-------------------------------------------------------------------------------------*/
	function FloatObject(jqObj, params)
	{
		this.jqObj = jqObj;
		
		switch(params.speed)
		{
			case 'fast': this.steps = 2; break;
			case 'normal': this.steps = 10; break;
			case 'slow': this.steps = 20; break;
			default: this.steps = 10;
		};
		
		var offset = this.jqObj.offset();
		
		//this.currentX = offset.left;
		this.currentX = $("#maincontainer").offset().left;
		this.currentY = offset.top;
		
		
		this.origX = typeof(params.x) == "string" ?  this.currentX : params.x;
		this.origY = typeof(params.y) == "string" ?  this.currentY : params.y;
		//if( params.y) this.origY = params.y;
		
		
		//now we make sure the object is in absolute positions.
		this.jqObj.css({'position':'absolute' , 'top':this.currentY ,'left':this.currentX});
	}
	
	FloatObject.prototype.updateLocation = function()
	{
		this.updatedX = /*$(window).scrollLeft() + */$("#maincontainer").offset().left;
		this.updatedY = $(window).scrollTop()+ this.origY;
		
		this.dx = Math.abs(this.updatedX - this.currentX );
		this.dy = Math.abs(this.updatedY - this.currentY );
		
		return this.dx || this.dy;
	}
	
	FloatObject.prototype.move = function()
	{
		if( this.jqObj.css("position") != "absolute" ) return;
		var cx = 0;
		var cy = 0;
		
		if( this.dx > 0 )
		{			
			if( this.dx < this.steps / 2 )
				cx = (this.dx >= 1) ? 1 : 0;
			else
				cx = Math.round(this.dx/this.steps);
			
			if( this.currentX < this.updatedX )
				this.currentX += this.dx;//cx;
			else
				this.currentX -= this.dx;//cx;
		}
		
		if( this.dy > 0 )
		{
			if( this.dy < this.steps / 2 )
				cy = (this.dy >= 1) ? 1 : 0;
			else
				cy = Math.round(this.dy/this.steps);
			
			if( this.currentY < this.updatedY )
				this.currentY += cy;
			else
				this.currentY -= cy;
		}
		
		//alert("update top: " + this.currentY);
		this.jqObj.css({'left':this.currentX, 'top': this.currentY });
	}

	
	
/*----------------------------------------------------------------------------------
Object: floatMgr
-------------------------------------------------------------------------------------*/
	$.floatMgr = {
		
		FOArray: new Array() ,
		
		timer: null ,
		
		active: false,
		
		initializeFO: function(jqObj,params) 
		{
			var settings =  $.extend({
				x: 0 ,
				y: 0 ,
				speed: 'normal'	},params||{});
			var newFO = new FloatObject(jqObj,settings);
			
			$.floatMgr.FOArray.push(newFO);
			
			if( !$.floatMgr.timer ) $.floatMgr.adjustFO();
			
			//now making sure we are registered to all required window events
			if( !$.floatMgr.registeredEvents ) 
			{
				$(window).bind("resize", $.floatMgr.onChange);
				$(window).bind("scroll", $.floatMgr.onChange);
				$.floatMgr.registeredEvents = true;
			}		
		} ,
		
		adjustFO: function() 
		{
			$.floatMgr.timer = null;
			
			var moveFO = false;
			
			for( var i = 0 ; i < $.floatMgr.FOArray.length ; i++ )
			{
				 FO = $.floatMgr.FOArray[i];
				 if( FO.updateLocation() )  moveFO = true;
			}
				
			if( moveFO )
			{
				$("#header").css("display","none");
				for( var i = 0 ; i < $.floatMgr.FOArray.length ; i++ )
				{
					
					FO = $.floatMgr.FOArray[i];
					FO.move();
				}
				
				if( !$.floatMgr.timer ) 
				{	
					$.floatMgr.timer = setTimeout($.floatMgr.adjustFO,50);
				}
			} else
			{
				if( $.floatMgr.active && $("#header").css("display") == "none")
					$("#header").fadeIn();
			}
		}	,
		
		checkModality: function()
		{
			var returnVariable = false;
			
			if($(window).width() < $("#totalcontainer").innerWidth() || 
			   ($.browser.msie && parseInt(jQuery.browser.version) <= 6) ||
			   DetectAndroid() || navigator.platform.indexOf("iPad") != -1)
			{
				$("#header").css("position","absolute");
				$("#headercontainer").css("position","static");
				$.floatMgr.active = true;
				returnVariable = true;
			} else
			{
				$.floatMgr.active = false;
				$("#header").css("position","static");
				$("#headercontainer").css("position","fixed");
			}
			return returnVariable;
		},
		
		onChange: function(e)
		{	
			if($.floatMgr.checkModality())
			{
				/* ho la barra di scorrimento orizzontale in questo caso
				   attivo la modalita' floating DIV */
				   				   
				if( !$.floatMgr.timer )
				{				
					if(e.type == "scroll")
					{
						
						$.floatMgr.adjustFO();
					} else (e.type == "resize")
					{
						for( var i = 0 ; i < $.floatMgr.FOArray.length ; i++ )
						{
				 			FO = $.floatMgr.FOArray[i];
							FO.currentY = $(window).scrollTop() + FO.origY;
							if(navigator.platform.indexOf("iPad") == -1)
							{
								var newTop = FO.currentY;
								if(jQuery("#videodetailcontainer").length > 0 && $("#videopreview").css('display') == 'none')
								{
									newTop = -jQuery("#headercontainer").height() - 10;
									if(jQuery.trim(jQuery("#header").css('position')) != 'absolute')
									{
										if(jQuery.trim(jQuery("#headercontainer").css('position')) != 'fixed')
											newTop = jQuery(window).scrollTop() + newTop;
											
										jQuery("#headercontainer").animate({top: newTop+"px", left: $("#maincontainer").offset().left}, 250, "linear", function() { $(".btn_reopen_menu").css('visibility', 'visible'); jQuery("#header").hide(); });
									}else
									{
										if(jQuery.trim(jQuery("#header").css('position')) != 'fixed')
											newTop = jQuery(window).scrollTop() + newTop;
											
										jQuery("#header").animate({top: newTop+"px", left: $("#maincontainer").offset().left}, 250, "linear", function() { $(".btn_reopen_menu").css('visibility', 'visible'); jQuery("#header").hide(); });
									}
								}else
								{
									$("#header").css({'top': FO.currentY, 'left': $("#maincontainer").offset().left});
								}
							}else 	
							{
								$("#header").css({'top': FO.currentY});
							}
						}
					}
				}
			} else 
			{
				/* non ho la barra di scorrimento orizzontale 
				   la gestione torna con header FIXED*/
				clearTimeout($.floatMgr.timer);
				$.floatMgr.timer = null;
				$("#header").css("display","block");
			}
		}
	};
	
/*----------------------------------------------------------------------------------
Function: makeFloat
-------------------------------------------------------------------------------------*/		
	$.fn.makeFloat = function(params) {
		var obj = this.eq(0); //we only operate on the first selected object;
		$.floatMgr.initializeFO(obj,params);
		if($.floatMgr.checkModality()) $("#header").css("display","none");
		if( $.floatMgr.timer == null ) {
			$.floatMgr.adjustFO();
		}
		return obj;
	};
})(jQuery);
