function getStyle(el, style) {
   if(!document.getElementById) return;

     var value = el.style[toCamelCase(style)];

    if(!value)
        if(document.defaultView)
            value = document.defaultView.getComputedStyle(el, null).getPropertyValue(style);
        else if(el.currentStyle)
            value = el.currentStyle[toCamelCase(style)];
     return value;
}

function setStyle(objId, style, value) {
    document.getElementById(objId).style[style] = value;
}

function toCamelCase( sInput ) {
    var oStringList = sInput.split('-');
    if(oStringList.length == 1)
        return oStringList[0];
    var ret = sInput.indexOf("-") == 0 ?
       oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
    for(var i = 1, len = oStringList.length; i < len; i++){
        var s = oStringList[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1)
    }
    return ret;
}

function increaseWidth(addToWidth, whichDiv){
    var theDiv = document.getElementById(whichDiv);
    var currWidth = parseInt(getStyle(theDiv, "width"));
    var newWidth = currWidth + parseInt(addToWidth);
    setStyle(whichDiv, "width", newWidth + "px");
}

var cities = new Array();
var cities_point = new Array();
var city_num;
var cross;

function crossWith(idx) {
	if(cities[idx][4]==9) 
		xxx=1;
	var pos = "01234567";
	var cx = cities[idx][2];
	var cy = cities[idx][3];
	var cw = cities[idx][0];
	var ch = cities[idx][1];
	var qx1 = cx - cw;
	var qy1 = cy - ch;
	var qx2 = cx + cw + 6;
	var qy2 = cy + ch + ch;


	for(i=0;i<city_num;i++) {
		if(idx==i) continue;
		var ii = cities[i][4];
		var ix = cities[i][2];
		var iy = cities[i][3];
		var iw = cities[i][0];
		var ih = cities[i][1];
		var ip = cities[i][5];
		if(ip==0) {				// right-top
			var tx1 = ix + 6;
			var ty1 = iy - ih;
			var tx2 = ix + iw + 6;
			var ty2 = iy;
		} else
		if(ip==1) {				// right
			var tx1 = ix + 6;
			var ty1 = iy;
			var tx2 = ix + iw + 6;
			var ty2 = iy + ih;
		} else
		if(ip==2) {				// right-bottom
			var tx1 = ix + 6;
			var ty1 = iy + ih;
			var tx2 = ix + iw + 6;
			var ty2 = iy + ih + ih;
		} else
		if(ip==3) {				// center-bottom
			var tx1 = ix - parseInt((iw -6)/2);
			var ty1 = iy + ih;
			var tx2 = ix + parseInt((iw -6)/2) + 6;
			var ty2 = iy + ih + ih;
		} else
		if(ip==4) {				// left-bottom
			var tx1 = ix - iw;
			var ty1 = iy + ih;
			var tx2 = ix;
			var ty2 = iy + ih + ih;
		} else
		if(ip==5) {				// left
			var tx1 = ix - iw;
			var ty1 = iy;
			var tx2 = ix;
			var ty2 = iy + ih;
		} else
		if(ip==6) {				// left-top
			var tx1 = ix - iw;
			var ty1 = iy - ih;
			var tx2 = ix;
			var ty2 = iy;
		} else
		if(ip==7) {				// center-top
			var tx1 = ix - parseInt((iw -6)/2);
			var ty1 = iy - ih;
			var tx2 = ix + parseInt((iw -6)/2) + 6;
			var ty2 = iy;
		} else {
			tx1 = -2; tx2 = -1;
			ty1 = -2; ty2 = -1;
		}


		// remove places where is the tx object ( top-left in the area )

		if(tx1>qx1 && ty1>qy1 && tx1<qx2 && ty1<qy2) {
			if(tx1>qx1 && tx1<cx) {
				if(ty1<cy) { 
					pos=pos.replace(/6/,""); 
					if(tx2>(cx-parseInt((cw-6)/2))) 
						pos=pos.replace(/7/,""); 
					if(tx2>cx+6) pos=pos.replace(/0/,"");
				}
				if(ty1<(cy+ch) && ty1>=cy) pos=pos.replace(/5/,"");
				if(ty1>=(cy+ch)) {
					pos=pos.replace(/4/,"");
					if(tx2>(cx-parseInt((cw-6)/2))) 
						pos=pos.replace(/3/,""); 
					if(tx2>cx+6) pos=pos.replace(/2/,"");
				}
				if(ty2<=cy) {
					 pos=pos.replace(/6/,"");
					if(tx2>(cx-parseInt((cw-6)/2))) 
						pos=pos.replace(/7/,""); 
					if(tx2>cx+6) pos=pos.replace(/0/,"");
				}
				if(ty2<=(cy+ch) && ty2>cy) pos=pos.replace(/5/,"");
				if(ty2>=(cy+ch)) {
					pos=pos.replace(/4/,"");
					if(tx2>(cx-parseInt((cw-6)/2))) 
						pos=pos.replace(/3/,""); 
					if(tx2>cx+6) pos=pos.replace(/2/,"");
				}
			}
			if(tx1>(cx-parseInt((cw-6)/2)) && tx1<(cx+parseInt((cw-6)/2)+6)) {
				if(ty1<cy) { 
					pos=pos.replace(/7/,"");
					if(tx2>cx+6) pos=pos.replace(/0/,"");
				}
				if(ty1>=(cy+ch)) {
					pos=pos.replace(/3/,"");
					if(tx2>cx+6) pos=pos.replace(/2/,"");
				}
				if(ty2<=cy) { 
					pos=pos.replace(/7/,"");
					if(tx2>cx+6) pos=pos.replace(/0/,"");
				}
				if(ty2>=(cy+ch)) {
					pos=pos.replace(/3/,"");
					if(tx2>cx+6) pos=pos.replace(/2/,"");
				}
			}
			if(tx1>(cx+6) && tx1<qx2) {
				if(ty1<cy) pos=pos.replace(/0/,"");
				if(ty1<(cy+ch) && ty1>=cy) pos=pos.replace(/1/,"");
				if(ty1>=(cy+ch)) pos=pos.replace(/2/,"");
				if(ty2<=cy) pos=pos.replace(/0/,"");
				if(ty2<=(cy+ch) && ty2>cy) pos=pos.replace(/1/,"");
				if(ty2>=(cy+ch)) { 
					pos=pos.replace(/2/,""); 
				}
			}
		}

		// remove places where is the tx object ( bottom-right in the area )
		
//		if((ty2<qy2 && ty2>qy1 && tx2>qx2 && tx1<qx2) ||
		if((tx2<qx2 && ty2<qy2 && tx2>qx1 && ty2>qy1)) {
			if(tx2<qx2 && tx2>(cx+6)) {
				if(ty2<=cy) { 
					pos=pos.replace(/0/,""); 
					if(tx1<(cx+parseInt((cw-6)/2)+6)) 
						pos=pos.replace(/7/,""); 
					if(tx1<cx) pos=pos.replace(/6/,"");
				}
				if(ty2<=(cy+ch) && ty2>cy) pos=pos.replace(/1/,"");
				if(ty2>(cy+ch)) {
					pos=pos.replace(/2/,"");
					if(tx1<(cx+parseInt((cw-6)/2)+6)) 
						pos=pos.replace(/3/,""); 
					if(tx1<cx) pos=pos.replace(/4/,"");
				}
				if(ty1<cy) {
					pos=pos.replace(/0/,"");
					if(tx1<(cx+parseInt((cw-6)/2)+6)) 
						pos=pos.replace(/7/,""); 
					if(tx1<cx) pos=pos.replace(/6/,"");
				}
				if(ty2<(cy+ch) && ty2>=cy) pos=pos.replace(/5/,"");
				if(ty2>(cy+ch)) {
					pos=pos.replace(/2/,"");
					if(tx1<(cx+parseInt((cw-6)/2)+6)) 
						pos=pos.replace(/3/,""); 
					if(tx1<cx) pos=pos.replace(/4/,"");
				}
			}
			if(tx2<(cx+parseInt((cw-6)/2)+6) && tx2>(cx-parseInt((cw-6)/2))) {
				if(ty2<=cy) { 
					pos=pos.replace(/7/,"");
					if(tx1<cx) pos=pos.replace(/6/,"");
				}
				if(ty2>(cy+ch)) {
					pos=pos.replace(/3/,"");
					if(tx1<cx) pos=pos.replace(/4/,"");
				}
				if(ty1<cy) { 
					pos=pos.replace(/7/,"");
					if(tx1<cx) pos=pos.replace(/6/,"");
				}
				if(ty1<=(cy+ch)) {
					pos=pos.replace(/3/,"");
					if(tx1<cx) pos=pos.replace(/4/,"");
				}
			}
			if(tx2<cx && tx2>qx1) {
				if(ty1<cy) pos=pos.replace(/6/,"");
				if(ty1<(cy+ch) && ty1>=cy) pos=pos.replace(/5/,"");
				if(ty1>=(cy+ch)) pos=pos.replace(/4/,"");
				if(ty2<=cy) pos=pos.replace(/6/,"");
				if(ty2<=(cy+ch) && ty2>cy) pos=pos.replace(/5/,"");
				if(ty2>=(cy+ch)) pos=pos.replace(/4/,"");
			}
		}


		tx1 = ix;
		ty1 = iy;
		tx2 = ix+6;
		ty2 = iy+6;


		// remove places where is the tx object ( top-left in the area )

		if(tx1>qx1 && ty1>qy1 && tx1<qx2 && ty1<qy2) {
			if(tx1>qx1 && tx1<cx) {
				if(ty1<cy) { 
					pos=pos.replace(/6/,""); 
					if(tx2>(cx-parseInt((cw-6)/2))) 
						pos=pos.replace(/7/,""); 
					if(tx2>cx+6) pos=pos.replace(/0/,"");
				}
				if(ty1<(cy+ch) && ty1>=cy) pos=pos.replace(/5/,"");
				if(ty1>=(cy+ch)) {
					pos=pos.replace(/4/,"");
					if(tx2>(cx-parseInt((cw-6)/2))) 
						pos=pos.replace(/3/,""); 
					if(tx2>cx+6) pos=pos.replace(/2/,"");
				}
				if(ty2<=cy) {
					 pos=pos.replace(/6/,"");
					if(tx2>(cx-parseInt((cw-6)/2))) 
						pos=pos.replace(/7/,""); 
					if(tx2>cx+6) pos=pos.replace(/0/,"");
				}
				if(ty2<=(cy+ch) && ty2>cy) pos=pos.replace(/5/,"");
				if(ty2>=(cy+ch)) {
					pos=pos.replace(/4/,"");
					if(tx2>(cx-parseInt((cw-6)/2))) 
						pos=pos.replace(/3/,""); 
					if(tx2>cx+6) pos=pos.replace(/2/,"");
				}
			}
			if(tx1>(cx-parseInt((cw-6)/2)) && tx1<(cx+parseInt((cw-6)/2)+6)) {
				if(ty1<cy) { 
					pos=pos.replace(/7/,"");
					if(tx2>cx+6) pos=pos.replace(/0/,"");
				}
				if(ty1>=(cy+ch)) {
					pos=pos.replace(/3/,"");
					if(tx2>cx+6) pos=pos.replace(/2/,"");
				}
				if(ty2<=cy) { 
					pos=pos.replace(/7/,"");
					if(tx2>cx+6) pos=pos.replace(/0/,"");
				}
				if(ty2>=(cy+ch)) {
					pos=pos.replace(/3/,"");
					if(tx2>cx+6) pos=pos.replace(/2/,"");
				}
			}
			if(tx1>(cx+6) && tx1<qx2) {
				if(ty1<cy) pos=pos.replace(/0/,"");
				if(ty1<(cy+ch) && ty1>=cy) pos=pos.replace(/1/,"");
				if(ty1>=(cy+ch)) pos=pos.replace(/2/,"");
				if(ty2<=cy) pos=pos.replace(/0/,"");
				if(ty2<=(cy+ch) && ty2>cy) pos=pos.replace(/1/,"");
				if(ty2>=(cy+ch)) pos=pos.replace(/2/,"");
			}
		}

		// remove places where is the tx object ( bottom-right in the area )
		
		if(tx2<qx2 && ty2<qy2 && tx2>qx1 && ty2>qy1) {
			if(tx2<qx2 && tx2>(cx+6)) {
				if(ty2<=cy) { 
					pos=pos.replace(/0/,""); 
					if(tx1<(cx+parseInt((cw-6)/2)+6)) 
						pos=pos.replace(/7/,""); 
					if(tx1<cx) pos=pos.replace(/6/,"");
				}
				if(ty2<=(cy+ch) && ty2>cy) pos=pos.replace(/1/,"");
				if(ty2>(cy+ch)) {
					pos=pos.replace(/2/,"");
					if(tx1<(cx+parseInt((cw-6)/2)+6)) 
						pos=pos.replace(/3/,""); 
					if(tx1<cx) pos=pos.replace(/4/,"");
				}
				if(ty1<cy) {
					pos=pos.replace(/0/,"");
					if(tx1<(cx+parseInt((cw-6)/2)+6)) 
						pos=pos.replace(/7/,""); 
					if(tx1<cx) pos=pos.replace(/6/,"");
				}
				if(ty2<(cy+ch) && ty2>=cy) pos=pos.replace(/5/,"");
				if(ty2>(cy+ch)) {
					pos=pos.replace(/2/,"");
					if(tx1<(cx+parseInt((cw-6)/2)+6)) 
						pos=pos.replace(/3/,""); 
					if(tx1<cx) pos=pos.replace(/4/,"");
				}
			}
			if(tx2<(cx+parseInt((cw-6)/2)+6) && tx2>(cx-parseInt((cw-6)/2))) {
				if(ty2<=cy) { 
					pos=pos.replace(/7/,"");
					if(tx1<cx) pos=pos.replace(/6/,"");
				}
				if(ty2>(cy+ch)) {
					pos=pos.replace(/3/,"");
					if(tx1<cx) pos=pos.replace(/4/,"");
				}
				if(ty1<cy) { 
					pos=pos.replace(/7/,"");
					if(tx1<cx) pos=pos.replace(/6/,"");
				}
				if(ty1<=(cy+ch)) {
					pos=pos.replace(/3/,"");
					if(tx1<cx) pos=pos.replace(/4/,"");
				}
			}
			if(tx2<cx && tx2>qx1) {
				if(ty1<cy) pos=pos.replace(/6/,"");
				if(ty1<(cy+ch) && ty1>=cy) pos=pos.replace(/5/,"");
				if(ty1>=(cy+ch)) pos=pos.replace(/4/,"");
				if(ty2<=cy) pos=pos.replace(/6/,"");
				if(ty2<=(cy+ch) && ty2>cy) pos=pos.replace(/5/,"");
				if(ty2>=(cy+ch)) pos=pos.replace(/4/,"");
			}
		}




		
	}

//	if(pos.length>0 && pos.search(/1/)==-1) cities[idx][5]=pos.charat(0);
	if(pos.length>0 && pos.search(/1/)==-1) cities[idx][5]=pos.substr(0,1);
	else cities[idx][5]=1;
//	alert("bla"+pos);


}

function sortByY(a, b) {
	return b[2]-a[2];
}

function order_cities() {
	var tmp;
	var outfit;
	var elem;

	city_num = parseInt(document.getElementById('city_num').value);
//	alert(document.getElementById('city_1').offsetWidth);

	for(i=0;i<city_num;i++) {
		//tmp = 'city_' + (i+1);
		tmp1 = 'cityid_' + (i+1);
		tmp = 'city_' + document.getElementById(tmp1).value;
		elem = document.getElementById(tmp);
		var attr = new Array(7);
//		attr[0] = getStyle(elem, "width");
		attr[0] = elem.offsetWidth;
//		attr[0] = parseInt(getStyle(elem, "width"));
//		attr[1] = parseInt(getStyle(elem, "line-height"))-5; 
		attr[1] = elem.offsetHeight-5;
		attr[2] = parseInt(getStyle(elem, "left")) - 6;
		attr[3] = parseInt(getStyle(elem, "top"));
		attr[4] = document.getElementById(tmp1).value;//i;
		attr[5] = 8;
		attr[6] = elem.innerHTML;
//		if(i==0)
//			alert(attr);
//		attr[1] = elem.offsetWidth;
		cities[i] = new Array(7);
		cities_point[i] = new Array(2);
		cities[i] = attr;
		cities_point[i][0] = attr[2];
		cities_point[i][1] = attr[3];
//		outfit += "\n" + attr.toString();
//		document.write(attr.toString()+'<br/>');
	} 

	cities.sort(sortByY);
//	debugger;
	for(k=0;k<city_num;k++) {
		crossWith(k);
	}

	for(k=0;k<city_num;k++) {
		//tmp = 'city_' + (cities[k][4]+1);
		tmp = 'city_' + (cities[k][4]);
		//tmp = 'city_' + document.cities[i].value;
		var newtop;
		var newleft;
		var ix = cities[k][2];
		var iy = cities[k][3];
		var iw = cities[k][0];
		var ih = cities[k][1];
		var ip = cities[k][5];
		if(ip==0) {				// right-top
			var tx1 = ix + 6;
			var ty1 = iy - ih;
			var tx2 = ix + iw + 6;
			var ty2 = iy;
		} else
		if(ip==1) {				// right
			var tx1 = ix + 6;
			var ty1 = iy;
			var tx2 = ix + iw + 6;
			var ty2 = iy + ih;
		} else
		if(ip==2) {				// right-bottom
			var tx1 = ix + 6;
			var ty1 = iy + ih;
			var tx2 = ix + iw + 6;
			var ty2 = iy + ih + ih;
		} else
		if(ip==3) {				// center-bottom
			var tx1 = ix - parseInt((iw -6)/2);
			var ty1 = iy + ih;
			var tx2 = ix + parseInt((iw -6)/2) + 6;
			var ty2 = iy + ih + ih;
		} else
		if(ip==4) {				// left-bottom
			var tx1 = ix - iw;
			var ty1 = iy + ih;
			var tx2 = ix;
			var ty2 = iy + ih + ih;
		} else
		if(ip==5) {				// left
			var tx1 = ix - iw;
			var ty1 = iy;
			var tx2 = ix;
			var ty2 = iy + ih;
		} else
		if(ip==6) {				// left-top
			var tx1 = ix - iw;
			var ty1 = iy - ih;
			var tx2 = ix;
			var ty2 = iy;
		} else
		if(ip==7) {				// center-top
			var tx1 = ix - parseInt((iw -6)/2);
			var ty1 = iy - ih;
			var tx2 = ix + parseInt((iw -6)/2) + 6;
			var ty2 = iy;
		} else {
			tx1 = -2; tx2 = -1;
			ty1 = -2; ty2 = -1;
		}

		setStyle(tmp, "left", tx1 + "px");
		setStyle(tmp, "top", ty1 + "px");


	}

//	var cities_ordered = order(cities,1);

//	alert(outfit + "Done!");
//	alert(cities + "Done!");
}

function order(pole,idx) {
	if(idx>city_num) return 0;
/*
	if(crossed(pole, idx)) {
		cities[idx][2] 

	}

	else return cities[idx][0] + order(idx+1);
*/
}

