/*
 * Used for creating the list of available objects in DTM
 * */
function _listObjects(){// constructor for the main class
	this.name='_listObjects';

	this.region= new Region();
	this.region.attributes.morphDuration=0.75;
	this.region.attributes.zIndex=150;

	this.eventScheduler=new _eventScheduler();

	this.objects=new Array();
	this.heap=new Array();

	this.heap['events']=new Array();

	this.hiddenTypes={};
	this.hiddenLayers={};
	this.hiddenObjectTypes={};
	this.hiddenPositionings={};
}

/* destroy this object */
_listObjects.prototype.destroy=function (){
	if(this.objects && this.objects.controls){
		for(var i=0;i<this.objects.length;i++){
			var control=this.objects.controls[i];

			if(control){
				object=$('listObjects_item_'+(control._controlsID)+'_');

				if(object){	// if this object is already created, but is inexistant in the Controls array, then remove it from the DOM
					var id=object.identify();

					Event.stopObserving($(object),'mouseover',this.heap['events']['mouseover'+id]);
					Event.stopObserving($(object),'mouseout',this.heap['events']['mouseout'+id]);
					Event.stopObserving($(object),'click',this.heap['events']['click'+id]);

					$(object).remove();
					this.objects[attr.identifier]=false;
				}
			}
		}
	}


	this.hiddenTypes=null;
	this.hiddenLayers=null;
	this.hiddenObjectTypes=null;

	this.hiddenPositionings=null;

	this.eventScheduler.destroy();

	this.objects=null;
}

// updates the random key (generationKey)
_listObjects.prototype.update=function (){
	this.hiddenTypes.text=$('containerObjects_quickFilters_text').checked;
	this.hiddenTypes.image=$('containerObjects_quickFilters_image').checked;
	this.hiddenTypes.group=$('containerObjects_quickFilters_group').checked;
	this.hiddenTypes.rectangle=$('containerObjects_quickFilters_rectangle').checked;
	this.hiddenTypes.horizontalLine=$('containerObjects_quickFilters_horizontalLine').checked;
	this.hiddenTypes.verticalLine=$('containerObjects_quickFilters_verticalLine').checked;

	this.hiddenLayers[0]=$('containerObjects_quickFilters_layer_0').checked;
	this.hiddenLayers[1]=$('containerObjects_quickFilters_layer_1').checked;
	this.hiddenLayers[2]=$('containerObjects_quickFilters_layer_2').checked;
	this.hiddenLayers[3]=$('containerObjects_quickFilters_layer_3').checked;
	this.hiddenLayers[4]=$('containerObjects_quickFilters_layer_4').checked;

	this.hiddenObjectTypes.text=$('containerObjects_quickObjectFilters_text').checked;
	this.hiddenObjectTypes.image=$('containerObjects_quickObjectFilters_image').checked;
	this.hiddenObjectTypes.group=$('containerObjects_quickObjectFilters_group').checked;
	this.hiddenObjectTypes.rectangle=$('containerObjects_quickObjectFilters_rectangle').checked;
	this.hiddenObjectTypes.horizontalLine=$('containerObjects_quickObjectFilters_horizontalLine').checked;
	this.hiddenObjectTypes.verticalLine=$('containerObjects_quickObjectFilters_verticalLine').checked;

	this.hiddenPositionings.fixed=		$('containerObjects_quickFilters_positioning_fixed').checked;
	this.hiddenPositionings.absolute=	$('containerObjects_quickFilters_positioning_absolute').checked;
	this.hiddenPositionings.repeatable=	$F('repeatable_edit_');

	var attr={};
	var control=null;
	var child;

	var controls=new Array();
	for(var i=0;i<Controls.controlList.length;i++){
		if(Controls.controlList[i])controls.push(Controls.controlList[i]);
	}

	// split the controls array to an multi-depth array, then we will sort each level, and flatten the array
	var groupedControls=new Array();

	var pushes=0;
	for(var i=0;i<controls.length;i++){
		var control=controls[i];

		var l1,l2;

		if(control.child.attributes.fixed){
			var off=control.child.object.cumulativeOffset();

			l1=px2mm(Number(off['top']));
			l2=px2mm(Number(off['left']));

		}else{
			l1=Number(1000*Number(control.child.attributes.Ymm).toFixed(0));
			l2=Number(1000*Number(control.child.attributes.Xmm).toFixed(0));
		}

		if(groupedControls[l1]==null)groupedControls[l1]=new Array();
		if(groupedControls[l1][l2]==null)groupedControls[l1][l2]=new Array();

		pushes++;
		groupedControls[l1][l2].push(control);
	}

	var pushes=0;
	controls=new Array();

	for(var i=0;i<groupedControls.length;i++){
		var controlArr=groupedControls[i];
		if(controlArr)
			for(var j=0;j<controlArr.length;j++){
				if(controlArr[j])
					for(var k=0;k<controlArr[j].length;k++){
						pushes++;
						controls.push(controlArr[j][k]);
					}
			}
	}
	groupedControls=null;


	for(var i=0;i<controls.length;i++){
		control=controls[i];

		attr={};

		if(!control._controlsID) continue;

		attr.identifier=control._controlsID;

		if(control){
			attr.name=control.getDescription();
			attr.name=String(attr.name).truncate(23);
			attr.type=String(control.child.name).replace(/^_control_/,'');
		}

		var object=$('listObjects_item_'+ attr.identifier +'_');

		if(control && object==null){		// if this object is dows not exists in the DOM, then we must create it now
			clone=CloneTemplates['listObjects_item_'].createClone($('listObjects_item_container_'),attr);

			var id=clone.identify();
			this.heap['events']['mouseover'+id]	= this.mouseOver.bindAsEventListener(this,clone,control);
			this.heap['events']['mouseout'+id]	= this.mouseOut.bindAsEventListener(this,clone,control);
			this.heap['events']['click'+id]		= this.click.bindAsEventListener(this,clone,control);

			Event.observe($(clone),'mouseover',this.heap['events']['mouseover'+id]);
			Event.observe($(clone),'mouseout',this.heap['events']['mouseout'+id]);
			Event.observe($(clone),'click',this.heap['events']['click'+id]);

			$(clone).addClassName('create_buttonSmall');
			$(clone).addClassName('create_button_normal');

			this.objects[attr.identifier]=true;
		}else if(control==null && object){	// if this object is already created, but is inexistant in the Controls array, then remove it from the DOM
			var id=object.identify();

			Event.stopObserving($(object),'mouseover',this.heap['events']['mouseover'+id]);
			Event.stopObserving($(object),'mouseout',this.heap['events']['mouseout'+id]);
			Event.stopObserving($(object),'click',this.heap['events']['click'+id]);

			$(object).remove();
			this.objects[attr.identifier]=false;
		}
	}

	var lastParams={};

	for(var i=0;i<controls.length;i++){
		control=controls[i];

		attr={};
		attr.identifier=control._controlsID;

		if(control){
			attr.type=String(control.child.name).replace(/^_control_/,'');
			attr.layer=control.child.attributes.layer;
			attr.fixed=control.child.attributes.fixed;
			attr.repeatable=control.child.attributes.repeatable;

			object=$('listObjects_item_'+(attr.identifier)+'_');
			icon=$('listObjects_item_'+(attr.identifier)+'_icon');

			if(object){
				var visibleInList=true;
				var visibleInPage=true;

				if(this.hiddenTypes[attr.type]){
					visibleInList&=false;
				}else{
					visibleInList&=true;
				}

				if(this.hiddenLayers[attr.layer]){
					visibleInList&=false;
					visibleInPage&=false;
				}else{
					visibleInList&=true;
					visibleInPage&=true;
				}

				// hide hidden objects
				if(this.hiddenObjectTypes[attr.type]){
					visibleInPage&=false;
				}else{
					visibleInPage&=true;
				}

				if(this.hiddenPositionings.fixed && attr.fixed){
					visibleInPage&=false;
					visibleInList&=false;
				}else if(this.hiddenPositionings.absolute && !attr.fixed){
					visibleInPage&=false;
					visibleInList&=false;
				}

				if(this.hiddenPositionings.repeatable){
					if(!attr.repeatable){
						visibleInPage&=false;
						visibleInList&=false;
					}
				}else{
					if(attr.repeatable){
						visibleInPage&=false;
						visibleInList&=false;
					}
				}

				/*
				 * actually hide objects
				 * 	visibleInList determines the displayin the tab labelled "list"
				 * 	visibleInPage hides, or shows the object in the design page
				 * */
				if(visibleInList){
					object.show();

					if(control.child.attributes.fixed){
						icon.update(control.child.attributes.orderIndex);
					}else{
						icon.update("");
					}

				}else{
					object.hide();
				}

				if(control.child){
					child=control.child.preview;

					if(visibleInPage){
						child.show();
					}else{
						child.hide();
					}
				}
			}
		}
	}

	return true;
}

_listObjects.prototype.scrollTo=function (X,Y,speed){
	if(speed==null)speed=1;
	if(this['heap']['scrollX']==null)this['heap']['scrollX']=$('pageContainer').scrollLeft;
	if(this['heap']['scrollY']==null)this['heap']['scrollY']=$('pageContainer').scrollTop;

	var y=Y;
	y=Math.max(0,y);

	speed=Math.cos(speed);

	var x=X;
	x=Math.max(0,x);

	if(Math.random()<0.1)
		if(Math.abs(this['heap']['scrollX']-x)<15 && Math.abs(this['heap']['scrollY']-y)<15) return;

	this['heap']['scrollX']+=Math.round((x-this['heap']['scrollX'])/(5*speed));
	this['heap']['scrollY']+=Math.round((y-this['heap']['scrollY'])/(5*speed));

	$('pageContainer').scrollLeft = this['heap']['scrollX'];
	$('pageContainer').scrollTop  = this['heap']['scrollY'];

	this.eventScheduler.schedule('scrollTo',this.scrollTo.bind(this,X,Y,speed),0.1);
}

_listObjects.prototype.mouseOver=function (event,clone,control){
	var childDimensions=control.child.getDimensions();

	$(clone).addClassName('create_button_hover');
	$(clone).removeClassName('create_button_normal');

	if(control.child.attributes.fixed){
		var off=control.child.object.cumulativeOffset();
		var offParent=control.page.cumulativeOffset();

		childDimensions.X=off['left']-offParent['left'];
		childDimensions.Y=off['top']-offParent['top'];
	}else{
		childDimensions.X=mm2px(control.child.attributes.Xmm);
		childDimensions.Y=mm2px(control.child.attributes.Ymm);
	}

	if(event.ctrlKey)
		this.scrollTo(childDimensions.X,childDimensions.Y);

	if(control.child.attributes.color)
		this.region.attributes.backgroundColor=control.child.attributes.color;
	else
		this.region.attributes.backgroundColor='#fff';

	this.region.setup({	X:childDimensions.X,
						Y:childDimensions.Y,
						width:childDimensions.width,
						height:childDimensions.height,
						morph:true
						});
	this.region.show();

	this.eventScheduler.cancel('hide');
}

_listObjects.prototype.mouseOut=function (event,clone,control){
	this.eventScheduler.schedule('hide',this.hide.bind(this,control,false),0.5);
	$(clone).removeClassName('create_button_hover');
	$(clone).addClassName('create_button_normal');
}

_listObjects.prototype.click=function (event,clone,control){
	if(control)
		control.setFocus();
	this.hide(control,true)
}

_listObjects.prototype.hide=function (control,implode){
	if(implode){
		var childDimensions=control.child.getDimensions();

		if(control.child.attributes.fixed){
			var off=control.child.object.cumulativeOffset();
			var offParent=control.page.cumulativeOffset();

			childDimensions.X=off['left']-offParent['left'];
			childDimensions.Y=off['top']-offParent['top'];
		}else{
			childDimensions.X=control.child.attributes.X;
			childDimensions.Y=control.child.attributes.Y;
		}

		this.region.attributes.backgroundColor='#fff';

		this.region.setup({	X:parseInt(childDimensions.X)+parseInt(childDimensions.width/2),
							Y:parseInt(childDimensions.Y)+parseInt(childDimensions.height/2),
							width:1,
							height:1,
							morph:true
							});
		this.region.show();

		this.eventScheduler.schedule('hide',this.hide.bind(this),0.75);
	}else{
		this.region.hide();
	}
}

_listObjects.prototype.refresh=function (){
	$('containerObjects_list').childElements().each(function(element){if(!Object.isFunction(element))element.remove();});
	this.update();
	return true;
}

function refreshObjects(realCall){
	if(realCall)return objectList.refresh();
	eventScheduler.schedule('refreshObjects',refreshObjects.bind(this,true),0.5);
}

function listObjects(realCall){
	if(realCall)return objectList.update();
	eventScheduler.schedule('listObjects',listObjects.bind(this,true),0.5);

}


Event.observe(window, 'load', function() {
	objectList=new _listObjects();
});