// <script>
//genrics functions on divs
"use strict";

//=====================================================create div====================================
//								input:	{attributes},[styles}
//								convert numeric dimensions/positions proprerties in px
function Bloc(attributes,styles)
{
	
	for (var property in styles)
	{
		if(Object.isNumber(styles[property]))
		switch (property)
		{
			case 'width':
			case 'height':
					if(styles[property]<0)styles[property]=0;
			case 'top':
			case 'left':
			case 'right':
			case 'bottom':
			case 'border':
				styles[property]= styles[property] + "px";
			break;
		}
	}
	//var el=new Element("div",attributes).setStyle({"position":'absolute',"left":'0px',"top":'0px',"width":'0px',"height":'0px'});
	var el=new Element("div",attributes).setStyle({"position":'absolute',"visibility":'visible'});
	el.setStyle(styles);
	return(el);
}
//=======================================================================================
function newId(racine)
{
	if(!$(racine)) return(racine);
	var x=0;
	var r=racine;
	while($(r))r=racine+(x++);
	return(r);
}
function requireScript(scriptName,callback)
{
	var href=location.href.substr(0,location.href.lastIndexOf("/"))+"/";
	var path=location.pathname.substr(0,location.pathname.lastIndexOf("/"))+"/";
	var loaded=new Array;
	var h=$$('head')[0];
	if(scriptName.indexOf("http://")===false)
		scriptName=href+scriptName;
	h.select("SCRIPT").each(function(s,x) { loaded.push(s.src); } );
	if(callback && Object.isString(callback))
	{
		if(!scriptName.endsWith("?")) scriptName+="?";
		else
		if(!scriptName.endsWith("&")) scriptName+="&";
		scriptName=scriptName+"callback="+callback;
	}
	if(!loaded.include(scriptName))
	{
		var script = new Element('script', { type: 'text/javascript', src: scriptName });
		h.appendChild(script);
		script=script;
	}
	else
	if(callback)
	{
		var r=eval(callback+"()");
	}
}

//=============================== preload images with optional callBack:
//									getImages(callBack,img1,img2,....);
//									input : nArguments srcs OR array of srcs
//									output: array of Image
function getImages(callBack)
{
	this.nimages=0;
	this.imgs=new Array;
	this.callBack=Object.isFunction(callBack)?callBack:null;
	if(arguments.length>1 && Object.isArray(arguments[1]) )
	for (var i = 0; i < arguments[1].length; i++)
	{
		this.imgs[i]=new Image();this.imgs[i].src=arguments[1][i];
		if(!this.imgs[i].complete)
		{
			this.nimages++;
			this.imgs[i].onload=function(evt){	
										this.nimages--;
										var n=this.nimages;
										}.bindAsEventListener(this);
			this.imgs[i].onabort=function(evt){	
										this.nimages--;
										}.bindAsEventListener(this);
			this.imgs[i].onerror=function(evt){	
										this.nimages--; 
										}.bindAsEventListener(this);
		}
	}
	else
	for (var i = 1; i < arguments.length; i++)
	{
		this.imgs[i-1]=new Image();this.imgs[i-1].src=arguments[i];
		if(!this.imgs[i-1].complete)
		{
			this.nimages++;
			this.imgs[i-1].onload=function(evt){	
										this.nimages--;
										var n=this.nimages;
										}.bindAsEventListener(this);
			this.imgs[i-1].onabort=function(evt){	
										this.nimages--;
										}.bindAsEventListener(this);
			this.imgs[i-1].onerror=function(evt){	
										this.nimages--; 
										}.bindAsEventListener(this);
		}
	}
	this._wait();
}
getImages.prototype=
{
	_wait:function()
	{
		if(this.nimages>0)
			this._wait.bind(this).delay(0.1);
		else
		{
			if(this.callBack)
				this.callBack(this.imgs);
		}
	}
}

function intersect(div1, div2)
{
	var d1=div1.cumulativeOffset();
	var d2=div2.cumulativeOffset();
	var a={"left":d1.left,"right":d1.left+div1.width(),"top":d1.top,"bottom":d1.top+div1.height()};
	var b={"left":d2.left,"right":d2.left+div2.width(),"top":d2.top,"bottom":d2.top+div2.height()};
	return (	a.left <= b.right &&
				b.left <= a.right &&
				a.top <= b.bottom &&
				b.top <= a.bottom
			)
}
