var HlPositions = new Array;
var StoredElements = new Array;
var data = {1:{name:"Aunt Sally I am trapped in my marriage - Times Online.htm",type:"Entertainment",icon:"doc",link:"01_Entertainment_Aunt Sally I am trapped in my marriage - Times Online.htm"},
	2:{name:"Lindsay Lohan talks about her troubled life - Times Online.htm",type:"Entertainment",icon:"doc",link:"02_Entertainment_Lindsay Lohan talks about her troubled life - Times Online.htm"},
	3:{name:"The Writer-Director Dilemma  Newsweek_com.htm",type:"Entertainment",icon:"rtf",link:"03_Entertainment_The Writer-Director Dilemma  Newsweek_com.htm"},4:{name:"The Sound of Dictatorship - WSJ_com.htm",type:"Entertainment",icon:"pdf"},5:{name:"Vanity Fair Portraits at the National Portrait Gallery - Times Online.htm",type:"Entertainment",icon:"doc"},6:{name:"Wachovia's big mortgage buy - Feb_ 20, 2008.htm",type:"Economics",icon:"doc"},7:{name:"VCA Antech Shares Dip on Revenue Concern Financial News - Yahoo! Finance.htm",type:"Economics",icon:"doc"},8:{name:"U.S. Home Foreclosures Jump 90% as Mortgages Reset.htm",type:"Economics",icon:"doc"},9:{name:"Wall Street lifts on IBM stock buyback Associated Press Business News - MSN Money.htm",type:"Economics",icon:"doc"},10:{name:"Why insurers may rebuff Buffett - Feb_ 12, 2008.htm",type:"Economics",icon:"doc"},11:{name:"Wake up and smell the smoke of disaster  - Times Online.htm",type:"Environment",icon:"doc"},12:{name:"Life feels the effects of a changing climate - climate-change - 14 May 2008 - New Scientist Environment.htm",type:"Environment",icon:"doc"},13:{name:"Western U_S_ Faces Drought Crisis, Warming Study Says.htm",type:"Environment",icon:"doc"},14:{name:"Which countries would you pick for your climate team - climate-change - 02 July 2008 - New Scientist Environment.htm",type:"Environment",icon:"doc"},15:{name:"Winds of climate change - Environment - News - Belfast Telegraph.htm",type:"Environment",icon:"doc"},16:{name:"The Sports Network - National Basketball Association.htm",type:"Sports",icon:"doc"},17:{name:"The Sports Network - International Soccer.htm",type:"Sports",icon:"doc"},18:{name:"The Sports Network - National Hockey League.htm",type:"Sports",icon:"doc"},19:{name:"Woods, Stricker only top seeds remaining - Yahoo! News.htm",type:"Sports",icon:"doc"},20:{name:"Lack of reliability dogs Ferrari's Formula One promise - Motorsport - Telegraph.htm",type:"Sports",icon:"doc"}};
var CompTable = {1:[100,45.88,32.99,29.01,33.24,26.39,18.97,25.07,15.60,26.75,47.68,37.63,25.05,38.56,32.00,23.90,18.39,18.89,31.75,32.66],2:[46.80,100.00,44.92,35.87,48.36,36.60,26.75,36.30,26.10,37.15,55.76,37.61,33.14,42.25,39.67,26.33,20.35,21.88,39.13,44.57],3:[32.18,44.11,100.00,28.18,35.53,28.19,24.30,23.73,20.51,26.05,45.17,32.87,26.93,37.59,30.86,22.75,23.51,20.27,31.30,29.35],4:[27.79,34.87,28.26,100.00,29.15,24.24,22.84,27.46,23.97,27.82,37.76,32.26,27.90,32.07,29.87,24.17,19.97,17.41,29.25,28.15],5:[33.24,48.38,35.78,29.51,100.00,31.01,22.80,26.35,19.68,28.70,48.89,32.09,27.39,32.54,30.75,30.85,16.03,20.34,29.05,33.05],6:[23.77,36.15,28.10,24.33,31.20,100.00,38.02,35.43,31.37,49.90,40.03,30.97,28.01,35.33,28.29,21.29,18.97,21.38,28.99,26.44],7:[19.17,26.08,24.77,23.22,22.98,39.02,100.00,35.05,55.81,37.46,30.90,27.05,31.74,27.16,28.57,17.89,11.33,17.50,38.97,23.82],8:[24.23,35.25,23.61,27.11,26.01,34.84,35.56,100.00,48.25,31.15,33.36,26.23,28.84,27.84,30.94,19.01,13.99,22.93,24.75,30.10],9:[15.60,26.18,23.02,24.36,20.22,33.10,56.24,48.26,100.00,33.50,29.98,24.81,36.19,25.41,32.40,20.28,16.43,18.17,25.72,28.42],10:[26.42,37.11,26.72,27.71,28.97,49.26,38.15,30.80,33.29,100.00,36.38,25.27,21.82,26.47,30.48,21.79,15.56,21.91,26.76,30.39],11:[48.34,56.14,46.58,38.76,48.91,40.86,31.65,34.40,30.27,37.36,100.00,61.18,38.31,68.16,54.19,25.87,23.42,24.97,35.33,42.40],12:[38.07,38.10,33.96,33.20,32.94,31.69,27.41,26.43,24.81,26.46,61.12,100.00,43.78,73.23,47.17,18.69,15.70,20.59,27.21,32.39],13:[24.35,32.83,25.49,27.07,27.59,27.48,31.57,28.46,35.68,21.99,37.20,42.46,100.00,40.91,38.05,21.82,17.24,21.76,35.08,37.14],14:[38.56,42.31,39.21,32.71,33.05,35.84,27.66,28.30,25.41,28.44,67.90,72.68,41.96,100.00,53.53,21.15,18.20,22.47,33.14,36.05],15:[31.46,38.76,30.96,29.78,30.16,27.92,27.91,31.32,31.99,31.19,52.98,45.26,37.05,52.33,100.00,23.92,13.91,21.97,31.16,37.22],16:[23.04,26.07,22.75,23.88,30.14,21.60,19.04,19.00,20.43,22.03,25.21,17.46,21.35,20.02,23.60,100.00,32.93,36.80,36.11,29.08],17:[20.46,21.93,23.85,20.26,16.90,19.43,12.33,14.34,16.79,15.78,23.46,15.70,17.44,18.46,14.97,33.67,100.00,38.36,40.75,28.98],18:[20.27,23.24,20.27,17.40,21.30,20.30,18.03,22.92,18.40,22.06,24.74,19.76,21.11,22.14,22.66,36.53,36.87,100.00,30.28,27.81],19:[31.35,39.09,31.37,29.40,29.51,29.63,38.97,25.13,26.17,26.95,34.69,26.75,35.37,32.77,31.78,36.81,40.68,31.20,100.00,37.23],20:[30.77,44.13,29.91,28.44,32.68,26.32,25.16,30.21,28.41,30.63,41.01,31.67,37.93,35.30,37.70,30.45,29.07,28.79,37.00,100.00]};
var Sel = {id:0,color:"",left:0,top:0};
var Options = new Array;
var Step = 0;
var Left = new Array;

//Next button clicked
function CalculateAll() {
	if(Options.CatClickRedraw==null) {
		return false;
	}
	if(Options.ElementsReloaded==0 && Options.CatClickRedraw==1) {
		return false;
	}
	DeleteInput(1);
	DeleteInput(4);
	DeleteInput(7);
	ReloadTexts();
	CheckForEmptyElements();
	RedrawRight(Options.LastHighlighted);
	document.getElementById("t_button").className = "updateinactive";
	$(".rightMenu").css("display","none");
	$(".glright").css("display","block");
	Options.CatClickRedraw = 1;
	Options.ElementsReloaded = 0;
}

function ReloadTexts() {
	$("#t_todo").html("The source documents links can be wrong as these documents may be deleted or removed. We used cashed data to show Categorizer Demo work.");
	$("#t_notes").html("1.&nbsp;You can change the fingerprint of any category and click Update to see new results.<br />2.&nbsp;By clicking the diagram you can see all  the information about the document and the link to it.<br />3.&nbsp;By clicking any category title you can get the list of the documents sorted by relevancy.");
	$("#t_comml").html("One and the same document can be relevant to different categories. Categorizer work quality directly depends on the quality of the etalon documents for the fingerprints.");
	$("#t_commr").html("Here you can see the categorized documents. The color shows the relevance of the document to any category. The diagram shows how relevant can be the document to the category.");
}

function CatHandler(id) {
	if(Options.CatClickRedraw && Options.CatClickRedraw==1) {
		id = HighlightLeftBox(id,0);
		if(Options.Clicked==null || Options.Clicked!=id) {
			if(Options.Clicked!=null && Options.Clicked!=0) document.getElementById("cat"+Options.Clicked).className = "category"+GetIdName(Options.Clicked);
			if(document.getElementById("cat"+id))
				document.getElementById("cat"+id).className = "category"+GetIdName(id)+"ArrSel";
			Options.Clicked = id;
		} else {
			Options.Clicked = null;
		}
		CheckForEmptyElements();
		RedrawRight(id);
	}
}

function DeleteInput(id) {
	if(document.getElementById("inp"+id)) {
		document.getElementById("cat"+id).innerHTML = document.getElementById("inp"+id).value;
	}
	$("#cat"+id).css("cursor","pointer");
	addEventListener(document.getElementById("cat"+id),"mouseover",function(){ShowHighlight(id,1);});
	addEventListener(document.getElementById("cat"+id),"mouseout",function(){HideHighlight(id,1);});
}

function ShowFull(id) {
	var Height = Math.max(document.documentElement.clientHeight,document.body.clientHeight)+"px";
	$("#hidden").click(HideFull).css("display","block").css("height",Height);
	var top = (document.documentElement.scrollTop!=0) ? document.documentElement.scrollTop : document.body.scrollTop;
	var left = Math.round(document.body.clientWidth/2) - 232;
	$("#floatContent").css({top:top+300+"px",left:left+"px",display:"block"});
	SetBox(1,id);
	SetBox(4,id);
	SetBox(7,id);
	return false;
}

function HideFull() {
	$("#hidden").css("display","none");;
	document.getElementById("abs").onclick = null;
	$("#floatContent").css({display:"none"});
	return false;
}

function SetBox(i,id) {
	var comp = Left[i];
	var s = 0;
	for(var j in comp) {
		s+=CompTable[comp[j]][id-1];
	}
	s = Math.round(s/comp.length);
	document.getElementById("text"+i).innerHTML = s+"% ("+document.getElementById("cat"+i).innerHTML+")";
	document.getElementById("img"+i).alt = s+"%";
	document.getElementById("img"+i).title = s+"%";
	document.getElementById("img"+i).style.width = s+"%";
	document.getElementById("cname"+i).innerHTML = document.getElementById("cat"+i).innerHTML;
	document.getElementById("floatName").innerHTML = data[id].name;
	var name = (id<10?"0"+id:id)+"_"+data[id].type+"_"+data[id].name;
	var link = document.getElementById("clink");
	link.innerHTML = name;
	link.href="sources/"+name;
	link.title = name;
}

//Redraw Right menu to show diagrams
function RedrawRight(type) {
	Left = {1:new Array,4:new Array,7:new Array};
	var els = $("#tblLeft div:has(strong)");
	var cc, el;
	for (var i =0;i<els.length;i++) {
		cc = parseInt(els[i].id.substr(2));
		el = parseInt(els[i].value.substr(2));
		switch(cc) {
			case 1:case 2:case 3:Left[1].push(el);break;
			case 4:case 5:case 6:Left[4].push(el);break;
			case 7:case 8:case 9:Left[7].push(el);break;
		}
	}
	var els = $("#tblRight div:has(strong)");
	var text = "";
	if(type==0) {
		var Comp = new GetCompSimple();
		for (var i=0;i<els.length;i++) {
			if(els[i].id.indexOf("el")==-1) continue;
			el = parseInt(els[i].id.substr(2));
			Comp.GetVar(el);
		}
		text = '<div class="cblockheader">Categorized Documents</div>'+Comp.GetText();
	} else {
		var id = GetIdName(type);
		var Comp = new GetCompComplex();
		var color = (type==1?"c1":(type==4?"c2":"c3"));
		for (var i=0;i<els.length;i++) {
			if(els[i].id.indexOf("el")==-1) continue;
			el = parseInt(els[i].id.substr(2));
			Comp.GetVar(el,Left[type],color,id);
		}
		text = Comp.GetText(color);
		text = '<div class="cblocknorm head'+color+'">Selected Category: <b>'+document.getElementById("cat"+type).innerHTML+"</b></div>" + text;
	}
	document.getElementById("tblRight").innerHTML = text;
	$("#tblRight .containerleft").tooltip();
}

//Returning text for graph in right menu - simple style (3 colors, 1 for each box)
function GetCompSimple() {
	var C1 = new Array;
	var C2 = new Array;
	var C3 = new Array;
	var Color = "c1";
	var OldW = 0;
	this.GetWidth=function(el,comp){var i=0;for(var j in comp){i+=CompTable[comp[j]][el];};return i/comp.length;}
	this.GetLine=function(el,comp,color){var Width=this.GetWidth(el,comp);if(Width>OldW){Color=color;OldW=Width};if(Width>50)Width=50;
		return'<div class="graph '+color+'" style="width:'+Math.round(Width)+'px;margin-left:'+(50-Math.round(Width))+'px;"></div>';
	}
	this.GetColor = function(){return Color;}
	this.GetVar = function(el) {
		OldW = 0;
		var subtext = "";
				subtext += this.GetLine(el-1,Left[1],"c1");
				subtext += this.GetLine(el-1,Left[4],"c2");
				subtext += this.GetLine(el-1,Left[7],"c3");
			subtext +='</div>';
			subtext +='<div class="containerright">';
				subtext +='<div class="cshort" onmousedown=\'SetMovable(this,event);\' id="el'+el+'">'+document.getElementById("el"+el).innerHTML+'</div>';
			subtext +='</div>';
		var color = this.GetColor();
		subtext ='<div class="containerleft" id="cl'+el+'" title="Click here to see full information about the document" onclick="ShowFull('+el+');" onmouseover="$(\'#pcol'+el+'\').removeClass(\''+color+'\').addClass(\'c4\');" onmouseout="$(\'#pcol'+el+'\').addClass(\''+color+'\').removeClass(\'c4\');">'+subtext;
		subtext ='<div value="w'+Math.round(OldW)+'" class="containergraph '+color+'" id="pcol'+el+'">' + subtext + '</div>';
		if(color=="c1"){C1.push(subtext);}else if(color=="c2"){C2.push(subtext);}else{C3.push(subtext);}
	}
	this.GetText = function() {
		var text = "";
		if(C1.length>0){C1.sort(this.sortNumber);text+='<div class="cblockheader cblc1">'+document.getElementById("cat1").innerHTML+'</div>'+C1.join(" ");}
		if(C2.length>0){C2.sort(this.sortNumber);text+='<div class="cblockheader cblc2">'+document.getElementById("cat4").innerHTML+'</div>'+C2.join(" ");}
		if(C3.length>0){C3.sort(this.sortNumber);text+='<div class="cblockheader cblc3">'+document.getElementById("cat7").innerHTML+'</div>'+C3.join(" ");}
		return text;
	}
	this.sortNumber = function(a,b) {
		return parseInt(b.substr(13,2)) - parseInt(a.substr(13,2));
	}
}


function GetCompComplex() {
	var High = new Array;
	var Low = new Array;
	var None = new Array;
	this.GetWidth=function(el,comp){var i=0;for(var j in comp){i+=CompTable[comp[j]][el];};return Math.round(i/comp.length);}
	this.GetVar = function(el,comp,color,id) {
		var Width = this.GetWidth(el-1,comp);
		if(Width>=40) {
			High.push(this.GenerateBlock(el,color,Width,id+"High"));
		} else if (Width>20) {
			Low.push(this.GenerateBlock(el,color,Width,id+"Low"));
		} else {
			None.push(this.GenerateBlock(el,color,Width,id+"Irr"));
		}
	}
	this.GenerateBlock = function(el,color,Width,bgcol) {
		var text = '<div value="w'+Width+'" class="containergraph c'+bgcol+'" id="pcol'+el+'">';
			text += '<div class="containerleft" id="cl'+el+'" title="Click here to see full information about the document" onclick="ShowFull('+el+');" onmouseover="$(\'#pcol'+el+'\').removeClass(\'c'+bgcol+'\').addClass(\'c4\');" onmouseout="$(\'#pcol'+el+'\').addClass(\'c'+bgcol+'\').removeClass(\'c4\');">';
				text += '<div class="singlegraph '+color+'" style="width:'+(Width>50?50:Width)+'px;margin-left:'+(50-(Width>50?50:Width))+'px;"></div>';
				text += '<div class="singletext">'+Width+'%</div>';
			text += '</div>';
			text += '<div class="containerright">';
				text += '<div class="cshort" onmousedown=\'SetMovable(this,event);\' id="el'+(el)+'">'+document.getElementById("el"+el).innerHTML+'</div>';
			text += '</div>';
		text += '</div>';
		return text;
	}
	this.GetText = function(color) {
		var text = "";
		if(High.length>0) {
			High.sort(this.sortNumber);
			text += '<div class="cblockheader cbl'+color+'">High relevant documents</div>'+High.join(" ");
		}
		if(Low.length>0) {
			Low.sort(this.sortNumber);
			text += '<div class="cblockheader cbl'+color+'">Low relevant documents</div>'+Low.join(" ");
		}
		if(None.length>0) {
			None.sort(this.sortNumber);
			text += '<div class="cblockheader cbl'+color+'">Irrelevant documents</div>'+None.join(" ");
		}
		return text;
	}
	this.sortNumber = function(a,b) {
		return parseInt(b.substr(13,2)) - parseInt(a.substr(13,2));
	}
}

function HighlightLeftBox(id) {
	if(Options.LastHighlighted!=null && Options.LastHighlighted>0) {
		HideHighlight(Options.LastHighlighted,0);
	}
	if(Options.LastHighlighted==id) {
		Options.LastHighlighted = 0;
		return 0;
	}
	Options.LastHighlighted = id;
	return id;
}

function HideHighlight(old_id,type) {
	if(type==1 && Options.LastHighlighted!=null && Options.LastHighlighted==old_id) return false;
	old_name = GetIdName(old_id);
	if(Options.Clicked!=old_id)
		document.getElementById("cat"+old_id).className = "category"+old_name;
	document.getElementById("cc"+old_id).className = "cat"+old_name;
	old_id++;
	document.getElementById("cc"+old_id).className = "cat"+old_name;
	old_id++;
	document.getElementById("cc"+old_id).className = "cat"+old_name+"e";
}

function ShowHighlight(id,type) {
	var name = GetIdName(id);
	if(Options.Clicked!=id)
		document.getElementById("cat"+id).className = "category"+name+"Sel";
	document.getElementById("cc"+id).className = "catBold"+name;
	id++;
	document.getElementById("cc"+id).className = "catBold"+name;
	id++;
	document.getElementById("cc"+id).className = "catBold"+name+"e";
}

function CheckForEmptyElements() {
	Options.lastsearch = "";
	var amount = 0;
	var free_array = new Array;
	if(StoredElements[1]==undefined || StoredElements[1]==0) {
		free_array.push(1);
	} else {
		amount += StoredElements[1];
	}
	if(StoredElements[4]==undefined || StoredElements[4]==0) {
		free_array.push(4);
	} else {
		amount += StoredElements[4];
	}
	if(StoredElements[7]==undefined || StoredElements[7]==0) {
		free_array.push(7);
	} else {
		amount += StoredElements[7];
	}
	amount = (6-amount)>0?(6-amount):1;
	Options.ElementsReloaded += amount;
	for (var i in free_array) {
		amount = SetArrByRandom(free_array[i],amount,free_array.length);
	}
}

function SetArrByRandom(tid,amount,len) {
	var rand = 0;
	if(amount>3) {
		rand = 3;
	} else if (amount==3 && len==1) {
		rand = 3;
	} else if (amount>=2) {
		rand = 2;
	} else {
		rand = 1;
	}
	amount = amount-rand;
	var el, id;
	var type = "";
	var amounts = 0;
	while(rand>0) {
		if(type=="")
			if(Options.lastsearch=="")
				el = $("#tblRight div:has(strong):not(:has(div))");
			else {
				el = $("#tblRight div"+Options.lastsearch+":has(strong):not(:has(div))");
			}
		else {
			el = $("#tblRight div:has(strong:contains("+type+")):not(:has(div))");
		}
		if(el.length==0) break;
		id = Math.round(Math.random()*el.length);
		while(!el[id]) {
			id = Math.round(Math.random()*el.length);
		}
		el = el[id];
		type = data[el.id.substr(2)].type;
		rand--;
		MoveLR(tid,el.id);
		tid++;
		amounts++;
	}
	Options.lastsearch+=":not(:contains('"+type+"'))";
	tid -= amounts;
	StoredElements[tid] = amounts;
	return amount;
}

function MoveLR(tid,fromid) {
	var To = document.getElementById("cc"+tid);//target
	var From = document.getElementById(fromid);
	To.innerHTML = From.innerHTML;
	To.value = fromid;
	From.innerHTML = "";
	From.onmousedown = null;
	To.onmousedown = function(e) {SetMovable(this,e);};
}

function SetMovable(el,e) {
	if (!e) e = window.event;
	if (e.preventDefault) e.preventDefault();
	var div = $("#abs");
	div.removeClass();
	if(el) {
		var c = el.className.toString().split(" ");
		var Height = Math.max(document.documentElement.clientHeight,document.body.clientHeight)+"px";
		$("#cover").css({display:"block",height:Height});
		div.addClass("abs");
		Sel.from = el.id;
		if(c.length>1) {
			div.addClass(c[1]);
		} else {
			div.addClass("cw");
		}
		div.html(el.innerHTML);
		el.innerHTML = "";
		addEventListener(document,"mousemove",MoveHandler);
		addEventListener(document,"mouseup",RemoveMovable);
		div.css({display:"block",top:e.clientY+((document.documentElement.scrollTop!=0) ? document.documentElement.scrollTop : document.body.scrollTop)-1 +"px",left:e.clientX-((document.documentElement.scrollLeft!=0) ? document.documentElement.scrollLeft : document.body.scrollLeft)-1+"px"});
	}
}

function RemoveMovable() {
	removeEventListener(document,"mousemove",MoveHandler);
	removeEventListener(document,"mouseup",RemoveMovable);
	var div = $("#abs");
	var update_btn = true;
	if(Sel.id>0) {
		//moving element to left column
		var To = document.getElementById("cc"+Sel.id);//target
		//if target value is set and there was element from right and this is not this element (target not moved to it's own position)
		if(To.value && To.id!=Sel.from && To.value.indexOf("el")!=-1) {
			//move element to it's right panel and make it movable
			var ToRight = document.getElementById(To.value);
			if(ToRight) {
				ToRight.innerHTML = To.innerHTML;
				ToRight.onmousedown = function(e) {SetMovable(this,e);};
				if ($("#"+To.value).hasClass("cshort")) {
					$("#cl"+To.value.substr(2)).css("visibility","visible");
				}
			} else {
				var elNew = document.createElement("div");
				elNew.id = To.value;
				elNew.innerHTML = div.html();
				elNew.className = "cnormal";
				elNew.onmousedown = function(e) {SetMovable(this,e);};
				document.getElementById("tblRight").appendChild(elNew);
			}
		} else {
			//To value free. Checking if this element was get from this category
			if(Sel.from.substr(0,2)=="cc") {
				var from_id = Sel.from.substr(2);
				if((from_id>=1 && from_id<=3 && Sel.id>=1 && Sel.id<=3) || (from_id>=4 && from_id<=6 && Sel.id>=4 && Sel.id<=6) || (from_id>=7 && from_id<=9 && Sel.id>=7 && Sel.id<=9))
					update_btn = false;
			}
		}
		if ($("#"+Sel.from).hasClass("cshort")) {
			$("#cl"+Sel.from.substr(2)).css("visibility","hidden");
		}
		To.innerHTML = div.html();
		var select = "";

		var jbold = "";
		if(Options.LastHighlighted!=null) {
			if((Sel.id>=1 && Sel.id<=3 && Options.LastHighlighted==1) || (Sel.id>=4 && Sel.id<=6 && Options.LastHighlighted==4) || (Sel.id>=7 && Sel.id<=9 && Options.LastHighlighted==7))
				jbold = "Bold";
		}

		To.className = "cat"+jbold+GetIdName(Sel.id);
		if(Sel.from.indexOf("cc")==-1) {
			//if element was moved from right - save it's ID
			To.value = Sel.from;
		} else if (To.id!=Sel.from) {
			//else save id from previous left column
			To.value = document.getElementById(Sel.from).value;
			document.getElementById(Sel.from).value = 0;
            var FromType = Sel.from.replace("cc", "");
            CheckTypes(FromType,0);
		}
		document.getElementById(Sel.from).onmousedown = null;
		To.onmousedown = function(e) {SetMovable(this,e);};
		CheckTypes(Sel.id,0);
		Sel.id = 0;
		if(update_btn) UpdateButtonActive();
	} else if (Sel.from!=0) {
		//if id of left column isn't set
		if(Sel.from.indexOf("cc")==-1) {
			//if element was taken from right - move it back here
				document.getElementById(Sel.from).innerHTML = div.html();
//				document.getElementById(Sel.from).onmousedown = null;
		} else {
			//if from left - check if the right div exists
			var Toid = document.getElementById(Sel.from).value;
			var To2 = document.getElementById(Toid);
			if(To2) {
				To2.innerHTML = div.html();
				To2.onmousedown = function(e) {SetMovable(this,e);};
				if ($("#"+Toid).hasClass("cshort")) {
					$("#cl"+Toid.substr(2)).css("visibility","visible");
				}
				//if this element exits - move it there
			} else {
				//else create it
				var elNew = document.createElement("div");
				elNew.id = document.getElementById(Sel.from).value;
				elNew.innerHTML = div.html();
				elNew.className = "cnormal";
				elNew.onmousedown = function(e) {SetMovable(this,e);};
				document.getElementById("tblRight").appendChild(elNew);
			}
			document.getElementById(Sel.from).onmousedown = null;
			document.getElementById(Sel.from).value = null;
			CheckTypes(Sel.from,1);
			if(update_btn) UpdateButtonActive();
		}
		Sel.from = 0;
	}
	$("#cover").css({display:"none",height:"0px"});
	div.removeClass();
	div.css({display:"none"});
}

function UpdateButtonActive() {
	if(document.getElementById("t_button").className=="updateinactive") document.getElementById("t_button").className = "update";
	Options.ElementsReloaded++;
}

function CheckTypes(id,type){
	if(type==1) id = id.substr(2);
	id = parseInt(id);
	var st, end, el;
	if(id>=1 && id<=3){st=1;end=3;}else if(id>=4 && id<=6){st=4;end=6}else{st = 7; end = 9;};
	var amount = 0;
	var temp = new Array;
	for(var i=st;i<=end;i++){
		el = document.getElementById("cc"+i);
		if(el.value!=null && el.value!=0) {
			amount++;
			temp[data[el.value.substr(2)].type] = el.value.substr(2);
		}
	}
	if(HashLen(temp)>1) {
		document.getElementById("err"+st).style.display = "block";
	} else {
		document.getElementById("err"+st).style.display = "none";
	}
	StoredElements[st] = amount;
	CalcLeftDivPosition();
}

function HashLen(arr) {
	var len = 0;
	for(var i in arr) {
		len++;
	}
	return len;
}

function Manage() {
	if(document.execCommand) {
		try {
		document.execCommand("BackgroundImageCache", false, true);
		} catch (e) {

		}
	}
	BuildRightColumn();
	CalcLeftDivPosition();
	$("#t_button").removeClass("nextinactive");
	Options.CatClickRedraw = 0;
	Options.ElementsReloaded = 0;
	Options.LastHighlighted = 0;
}

function CalcLeftDivPosition(){
	var el, top, left;
	for(var i=1;i<=9;i++) {
		top = 0;
		left = 0;
		el = document.getElementById("cc"+i);
		while(el.offsetParent) {
			top += el.offsetTop;
			left += el.offsetLeft;
			el = el.offsetParent;
		}
		HlPositions[i] = {top:top,left:left};
	}

}

function BuildRightColumn() {
	var td = document.getElementById("tblRight");
	var div;
	for(var i in data) {
		div = document.createElement("div");
		div.id = "el"+i;
		div.className = "cnormal";
		div.innerHTML = (data[i].icon?"<img src=\"images/icon_"+data[i].icon+".gif\" alt=\"icon\" align=\"absmiddle\" /> ":"")+"<strong>"+data[i].type+"</strong> "+data[i].name;
		div.onmousedown = function(e) {SetMovable(this,e);};
		td.appendChild(div);
	}
}

function CheckMousePosition(top,left) {
	var j = CheckPosition(top,left);
	var sbold = "";
	if(Sel.id!=0 && Options.LastHighlighted!=null) {
		if((Sel.id>=1 && Sel.id<=3 && Options.LastHighlighted==1) ||
		(Sel.id>=4 && Sel.id<=6 && Options.LastHighlighted==4) ||
		(Sel.id>=7 && Sel.id<=9 && Options.LastHighlighted==7)) {
			sbold = "Bold";
		}
	}
	if(j==0) {
		if(Sel.id!=0) {
			document.getElementById("cc"+Sel.id).className = "cat"+sbold+GetIdName(Sel.id);
			Sel.id = 0;
		}
	} else if (j!=Sel.id) {
		var jbold = "";
		if(Options.LastHighlighted!=null) {
		if((j>=1 && j<=3 && Options.LastHighlighted==1) || (j>=4 && j<=6 && Options.LastHighlighted==4) || (j>=7 && j<=9 && Options.LastHighlighted==7))
			jbold = "Bold";
		}
		if(Sel.id!=0) document.getElementById("cc"+Sel.id).className = "cat"+sbold+GetIdName(Sel.id);
		Sel.id = j;
		document.getElementById("cc"+j).className = 'cat'+jbold+GetIdName(j)+'Sel';
	}
}

function CheckPosition(top,left) {
	var j = 0;
	for (var i in HlPositions) {
		if (top>HlPositions[i].top && top<HlPositions[i].top+19) {
			if (left>HlPositions[i].left && left<HlPositions[i].left+266) {
				j = i;
				break;
			}
		}
	}
	return j;
}

function GetIdName(id){
	var name;
	if(id>=1 && id<=3){name="One";}else if(id>=4 && id<=6){name="Two";}else{name="Three";}
	if(id==3 || id==6 || id==9)name+="e";
	return name;
}

function MoveHandler(e) {
	if (!e) e = window.event;
	var top =(document.documentElement.scrollTop!=0)?document.documentElement.scrollTop:document.body.scrollTop;
	var left=(document.documentElement.scrollLeft!=0)?document.documentElement.scrollLeft:document.body.scrollLeft;
	Sel.top = e.clientY+top;
	Sel.left = e.clientX-left;
	document.getElementById("abs").style.top = e.clientY+top-10+"px";
	document.getElementById("abs").style.left = e.clientX+left-10+"px";
	CheckMousePosition(e.clientY+top,e.clientX+left);
	if (e.preventDefault) e.preventDefault();
}

function Show(el) {
	var w = document.getElementById("www");
	w.innerHTML = "";
	for (i in el) {
		if (i.indexOf("HTML")!=-1 || i.indexOf("Text")!=-1) {
			continue;
		}
		w.innerHTML += i+"=>"+el[i]+"<br>";
	}
}

function addEventListener(el,event,func){
	try{el.addEventListener(event,func,false);}catch(e){
		try{el.detachEvent('on'+event,func);el.attachEvent('on'+event,func);}catch(e){el['on'+event]=func;}
	}
}

function removeEventListener(el,event,func){
	try{el.removeEventListener(event,func,false);}catch(e){
		try{el.detachEvent('on'+event,func);}catch(e){el['on'+event]=null;}
	}
}
