/*
FUNZIONI E PROPRIETA' GLOBALI
*/


var IS_IPHONE = navigator.userAgent.indexOf("iPhone") != -1 ;
var IS_IPOD = navigator.userAgent.indexOf("iPod") != -1 ;
var IS_IPAD = navigator.userAgent.indexOf("iPad") != -1 ;
var IS_IOS = IS_IPHONE || IS_IPOD || IS_IPAD ;





	Element.prototype.insertProgressState = function ()
	{		
		var size = this.getSize();
		if(size.x==0)size.x=100;
		if(size.y==0)size.y=100; 
		this.innerHTML = '<div style="width:'+size.x+'px; height:'+size.y+'px;" id="PROGRESS_STATE"></div>';
	}


	Element.prototype.setVisibility = function  ( _value )
	{
	 	this.style.visibility = _value;
	}
	
	
	Element.prototype.getVisibility = function  ()
	{
	 	return this.style.visibility ;	
	}
	
	
	Element.prototype.getPosition = function  ()
	{
	 return {x:this.offsetLeft,y:this.offsetTop};
	}
	
	Element.prototype.setPosition = function  ( _x , _y )
	{
	 	this.style.left = _x;
		this.style.top = _y;
	}
	
	
	
	Element.prototype.getSize = function  ()
	{
	 return {x:this.offsetWidth,y:this.offsetHeight};
	}
	
	
	Element.prototype.setSize = function  ( _x , _y )
	{
	 	this.style.width = _x;
		this.style.height = _y;
	}
	
	
	Element.prototype.getGlobalPosition = function  ()
	{
	 var  x = this.offsetLeft;
	 var  y = this.offsetTop;
	 
	 var currentElement = this;
	 
	 if (currentElement.offsetParent)
	 {
	  while (currentElement = currentElement.offsetParent)
	  {
	   	x += currentElement.offsetLeft;
	   	y += currentElement.offsetTop;
	  }
	 }
	 return {x:x,y:y};
	}
	
	
	Element.prototype.getChildById = function ( _id )
	{
		for( var id = 0; id<this.childNodes.length ; id++){
			if( this.childNodes[id].id == _id ) return this.childNodes[id];
		}
		
		return null;
	}
	
	

	
	Element.prototype.hasClass = function (cls)
	{
		return this.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
	}
	Element.prototype.addClass = function (cls)
	{
		if (!this.hasClass(cls)) this.className += " "+cls;
	}
	Element.prototype.removeClass = function (cls)
	{
		if (this.hasClass(cls)) {
			var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
			this.className=this.className.replace(reg,' ');
		}
	}







	
	
	/*

	Element.prototype.hasClassName = function(name) {
	  return new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)").test(this.className);
	};
	
	Element.prototype.addClassName = function(name) {
	  if (!this.hasClassName(name)) {
	    this.className = this.className ? [this.className, name].join(' ') : name;
	  }
	};
	
	Element.prototype.removeClassName = function(name) {
	  if (this.hasClassName(name)) {
	    var c = this.className;
	    this.className = c.replace(new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)", "g"), "");
	  }
	};
*/


/*
String.prototype.extractNumber = function ( id )
{		
	var numStr = '';
	while( this[id] )
	{		
		switch (this[id])
		{
			case '-':case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.':
				numStr+=this[id];
				id++;                          			
				while( this[id] )
				{		
					switch (this[id])
					{
						case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.':
							numStr+=this[id];
						break;
						case 'e':
							numStr+=this[id];
							id++;
							numStr+=this[id]; // aggiungo il -
						break;
						default:
							//alert(numStr);
					     	return {number:Number(numStr), nextId:id}; 
					    break;
					}
					id++;
				}
				//alert('end '+numStr);
				return {number:Number(numStr), nextId:id}; 			
			break;
		}
		id++;
	}
	return null; 
}
*/




function BREAK_EVENT(){
	return false;
}

function HTML ( _htmlElement ){
	return document.all ? document.all[ _htmlElement ] : document.getElementById( _htmlElement );
}

/*
function HTML_getChildById( _container, _id)
{
	for( var id = 0; id<_container.childNodes.length ; id++){
		if( _container.childNodes[id].id == _id ) return _container.childNodes[id];
	}
	
	return null;
}
*/

/*
function HTML_getGlobalPosition ( _htmlElement )
{
 var  x = _htmlElement.offsetLeft;
 var  y = _htmlElement.offsetTop;
 
 if (_htmlElement.offsetParent)
 {
  while (_htmlElement = _htmlElement.offsetParent)
  {
   	x += _htmlElement.offsetLeft;
   	y += _htmlElement.offsetTop;
  }
 }
 return {x:x,y:y};
}
*/


function ColorHexToRgb ( _hexColor , _opacity  )
{
	var r;
    var g;
    var b;
    
    if (_hexColor.substr(0, 1) == '#')
    {
        _hexColor = _hexColor.substr(1);
    }



    if (_hexColor.length == 3)
    {
        r = _hexColor.substr(0, 1);
        r += r;
        g = _hexColor.substr(1, 1);
        g += g;
        b = _hexColor.substr(2, 1);
        b += b;
    }
    else if (_hexColor.length == 6)
    {
        r = _hexColor.substr(0, 2);
        g = _hexColor.substr(2, 2);
        b = _hexColor.substr(4, 2);
    }
    
    
    r = parseInt(r, 16);
    g = parseInt(g, 16);
    b = parseInt(b, 16);
    
    if( _opacity != undefined )
    {
    	return 'rgba('+r+','+g+','+b+','+_opacity+')';
    }else{    	
    	return 'rgba('+r+','+g+','+b+')';    
    }
    	
}		 



function PointToLineDist ( x1, y1, x2, y2, x3, y3 )
{
	var dx=x2-x1;
	var dy=y2-y1;
	if (dx==0&&dy==0)
	{
		x2+=1;
		y2+=1;
		dx=dy=1;
	}
	var u = ((x3 - x1) * dx + (y3 - y1) * dy) / (dx * dx + dy * dy);
	var closestX;
	var closestY;
	if (u<0)
	{
		closestX=x1;
		closestY=y1;
	} else if (u> 1) {
		closestX=x2;
		closestY=y2;
	} else {
		closestX=x1+u*dx;
		closestY=y1+u*dy;
	}
	dx=closestX-x3;
	dy=closestY-y3;
	return Math.sqrt(dx * dx +dy * dy);
}


/*

function TextToClipboard(s) {
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData('Text', s);
    }
}

function TextFromClipboard() {
    if (window.clipboardData && clipboardData.setData) {
        return clipboardData.getData('Text');
    }
}


TextToClipboard( 'ciccio');
alert(TextFromClipboard());
*/


function WindowSize()
{	
	var winW = 980;
	var winH = 560;
	
	if (document.body && document.body.offsetWidth)
	{
	 winW = document.body.offsetWidth;
	 winH = document.body.offsetHeight;
	}
	else if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth )
	{
	 winW = document.documentElement.offsetWidth;
	 winH = document.documentElement.offsetHeight;
	}
	else if (window.innerWidth && window.innerHeight)
	{
	 winW = window.innerWidth;
	 winH = window.innerHeight;
	}

	return{x:winW, y:winH}    		
}



//* @author Kevin Lindsey
function ExtendClass(subClass, baseClass) {
   function inheritance() {}
   inheritance.prototype = baseClass.prototype;
   subClass.prototype = new inheritance();
   subClass.prototype.constructor = subClass;
   subClass.baseConstructor = baseClass;
   subClass.superClass = baseClass.prototype;
}


function IncludeJavascript( _url , _onLoadFunction )
{		
	var head = document.getElementsByTagName('head')[0];
	var script = document.createElement('script');
	script.src = _url;
	script.type = "text/javascript";
	
	if( _onLoadFunction != undefined && _onLoadFunction != null )
	{
		if (script.readyState){  //IE
	        script.onreadystatechange = function()
	        {
	            if (script.readyState == "loaded" || script.readyState == "complete")
	            {
	                script.onreadystatechange = null;
	                _onLoadFunction();
	            }
	        };
	    } else {  //Others
	        script.onload = function(){
	            _onLoadFunction();
	        };
	    }
    }
		
	//head.insertBefore( script, head.firstChild );		
	head.appendChild( script);
}


function IncludeCSS( _url )
{		
	var head = document.getElementsByTagName('head')[0];
	var css=document.createElement("link");
  	css.setAttribute("rel", "stylesheet");
  	css.setAttribute("type", "text/css");
  	css.setAttribute("href", _url);
  	head.appendChild( css);
}


function Bind(scope, fn ) {
    return function () { fn.apply(scope, arguments); }
}


function EventPreventDefault( e  ) {
    if (e.preventDefault) e.preventDefault();
    if (e.stopPropagation) e.stopPropagation();
    return false;
}
	
       



