function dolistings(){
	var ts = $T('a').filter('/openthumbs/.test(x.className)');
	var is = $T('a').filter('/openinfos/.test(x.className)');

	[ts,is].map(function(lst){
		lst.map(function(x){
		    x.onclick = function(){ 
				var box = x.getAttribute('box');
				var hm = x.getAttribute('hm');
				var boxHeight = $(hm).offsetHeight + 20;
				var other = ($(box).className == 'thumbs') ? oneAfter($(box)) : oneBefore($(box));
				var otherheight = parseInt(other.offsetHeight);
				var dir=(parseInt($(x.getAttribute('box')).style.height) > 0)?true:false;
				anim(box,'style.height',zio,0,boxHeight,2,dir); 
				if(otherheight > 10)
					anim(other.getAttribute('id'),'style.height',zot,0,otherheight,1.5,!dir); 
				return false;
			}
	   	})
	});

	if(!$('formlink')) return false;

	$('formlink').onclick = function(){
		var dir = (parseInt($('searchform').style.height) > 0) ? 1 : 0;
		anim('searchform','style.height',zot,0,160,1.1,dir);
		return false;
	}
}

function showBigImage(img, mouseX, mouseY){
	if($('bigpic'))
		$('bigpic').parentNode.removeChild($('bigpic'));

	var div = document.createElement("div");
	div.setAttribute('id','bigpic');
	$('body').appendChild(div);
	$('bigpic').onclick = function(){this.style.display='none'};
	$('bigpic').style.position='absolute';
	$('bigpic').style.overflow='hidden';
	$('bigpic').style.left=mouseX + 'px';
	$('bigpic').style.top= mouseY + 'px';
	$('bigpic').style.border='10px solid white';
	$('bigpic').innerHTML = "<img id='popup' src='" + img + "' />";
	var tmpimg= new Image();
	tmpimg.src=$('popup').getAttribute('src');
	var pw=(tmpimg.width);
	var ph=(tmpimg.height);
	$('bigpic').style.width=(pw + 'px');
	$('bigpic').style.height=(ph + 'px');
	animclip('bigpic',20);
}

function doBigImages(){
	var ts = $T('img').filter('/thumbimagepic/.test(x.className)');
	ts.map(function(x){ 
		x.onclick = function(e){
			//var mx = mousePos(e).x - 200;
			//var my = mousePos(e).y - 100;
			var mx = getPos(x).x - 200;
			var my = getPos(x).y - 100;
			showBigImage(x.src.replace(/._/,''), mx, my)
		} 
	})
}
function simpleThumbs(){
	if(!($('bigImage'))) return false;
	$('bigImage').onclick = function(){
		var src=$('bigImage').src.replace(/m_/,'');
		window.open("popup.php?img=" +src,"bigImage","width=680,height=560")
		return false;
	};
	var thumbs = $T('img').filter('/thumb/.test(x.className)');
	thumbs.map(function(x){
		x.onclick=function(){$('bigImage').src = x.src.replace(/s_/,'')}
	});
}

function list_headings(fields,shown,x,y){
	var shown = shown.split(' ');
	if(!$('list_headings')) return false;
	var lst = fields.split(' ');
	lst.map(function(x){
		var tick = (shown.member(x)) ? 'checked=1' : '';
		$('items').innerHTML += "<label for='"+x+"'>"+x+"</label>";
		$('items').innerHTML += "<input type='checkbox' value='"+x+"' name='"+x+"' "+tick+" ><br>";
	}); 
	$('contextMenu').style.display = '';
	$('contextMenu').style.left = x + 'px';
	$('contextMenu').style.top =  (y - 100) + 'px';
}

function roundthem(){
	var cs = ['orange','rounded','midTxt','tab'];
	cs.map(function(c){ $C(c).map(function(x){ round(x) }) });
	if($('atencion')) round($('atencion'));
	if($('specOffer')) round($('specOffer'));
}

function showonly(i){
	var lst = $T('div').filter('/formpage/.test(x.className)');
	lst.map("x.style.display='none'");
	lst[i].style.display = '';
}

function formtabs(){
	if(!$('addproperties'))return false;
	var formpages = $T('div').filter('/formpage/.test(x.className)');
	if(formpages.length < 1) return false;
	formpages.iter("x.style.display='none'");
	formpages[0].style.display='';
	var tabsdiv = document.createElement('ul');
	tabsdiv.setAttribute('id','formnav');
	var tabnames = ["Property","Details","Features","Financial","Access","Description"];
	var i = 0;
	tabnames.iter(function(x){
		tabsdiv.innerHTML += "<li><a href='#' index=" +i+ " class=tblink onclick='showonly(" +i+ ");' >" +x+ "</a></li>";
		i++;
	});
	if($('addproperties')) 
		$('addproperties').insertBefore(tabsdiv,$('addproperties').firstChild);	
	var tablinks = $T('a').filter(function(x){return /tablink/.test(x.className)});
}
function alignlist(lst){
	var newlst = lst.sort( function(x,y){
			var a = getPos(x);
			var b = getPos(y);
			var wa = 1000 * Math.floor(parseInt(a.y) / 100) + parseInt(a.x);
			var wb = 1000 * Math.floor(parseInt(b.y) / 100) + parseInt(b.x);
			return wa - wb;
		});

	var imagestring = '';
	var property_id = newlst[0].firstChild.getAttribute('property_id');
	newlst.map(function(x){imagestring += x.firstChild.src.replace(/.*m_/,'') + '|'});
	callserver("resaveimages.php?images="+imagestring+"&property_id="+property_id);
	var amount = newlst.length;
	var width = parseInt(getstyle(newlst[0],'width'));
	var rightMargin = parseInt(getstyle(newlst[0],'margin-right'));
	var i = 0;
	var offx = pagex($('imagelist')) + 15;
	var offy = -150;
	var cols = 6;
	var rows = Math.ceil(amount / cols);
	$('imagelist').style.minHeight = (rows * 150) + "px";
	while(i < amount){
		var modx = ((i+cols) % cols);
		if(modx == 0) offy += 150;
		var posx = modx * (width + rightMargin + 15);
		newlst[i].style.left = posx + "px";
		newlst[i++].style.top = offy + "px";
	}
}

function adminthumbs(){
	var draggable = null;
	document.onmousemove = function(e){
		if(draggable == null) return false;
		draggable.style.position='absolute';
		var px = parseInt(mousePos(e).x);
		var py = parseInt(mousePos(e).y);
		draggable.style.left = (px - 50) - pagex($('imagelist')) + "px";
		draggable.style.top  = (py - 50) - pagey($('imagelist')) + "px";
	}
	document.onmouseup=function(){ 
		if(draggable){
			draggable=null; 
			var thumbs = $C('imagebox');
			alignlist(thumbs);
			thumbs.map("x.style.position='absolute'");
		}
	};
	var ims = $C('adminthumbs');
	var poslst = {};
	ims.map(function(x){
		x.onmousedown=function(e){ 
			draggable = this.parentNode; 
			return false; 
		}
	});
}

addLoadEvent(simpleThumbs);
addLoadEvent(adminthumbs);
addLoadEvent(formtabs);
addLoadEvent(roundthem);
addLoadEvent(dolistings);
addLoadEvent(doBigImages);
addLoadEvent(function(){
	if(!$('listing') || !$('loading'))return false;
	$('listing').style.display='';
	$('loading').style.display='none';
});
addLoadEvent(function(){
	if(!$('list_headings')) return false;
	$('list_headings').onclick = function(e){ 
		var x = mousePos(e).x;
		var y = mousePos(e).y;
		var fields = $('all_fields').innerHTML;
		var shown = $('shown_fields').innerHTML;
		list_headings(fields,shown,x,y); 
	};
});




