

function pBW(id){
	id=document.getElementById(id);
	if (id) {
		var state=id.style.filter=="Gray";
		id.style.filter="Gray";
		state=state?!id.filters["Gray"].enabled:true;
		id.filters["Gray"].enabled = state;
	}
}


function pPropadd(pic) {
	var o=document.getElementsByTagName('textarea');
 	for(var i=0;i<o.length;i++) {
		if (o[i].id.indexOf('c_contmainmess')!=-1) {
			if (confirm(G_lng[13])) {
				if (document.getElementById(o[i].id).style.display!='none') {
					pCrop('pic_id');
					showCrop();
					var c=findPos('pic_id');
					var w=parseInt(document.getElementById('pic_id').offsetWidth);
					var h=parseInt(document.getElementById('pic_id').offsetHeight);
					var p=findPos('crop_up');
					var u=p[1]+parseInt(document.getElementById('crop_up').offsetHeight)-c[1];
					p=findPos('crop_down');
					var d=c[1]+h-p[1];
					p=findPos('crop_left');
					var l=p[0]+parseInt(document.getElementById('crop_left').offsetWidth)-c[0];
					p=findPos('crop_right');
					var r=c[0]+w-p[0];
					var v='[prop:'+u+','+r+','+d+','+l+','+pic+']';
					insertAtCursor(o[i].id, v);
				}
			}
			i=o.length;
		}
	}
}


function pGold(ids,redraw) {
	var id=document.getElementById(ids);
	if (id) {
		var g=document.getElementById('golden');
		if (g.style.display=='none'||redraw==1) {

			if (ISshowDIV('crop_up')) {
				var u=findPos('crop_up');
				var y=u[1]+parseInt(document.getElementById('crop_up').offsetHeight);

				var l=findPos('crop_left');
				var x=l[0]+parseInt(document.getElementById('crop_left').offsetWidth);

				var t=findPos('crop_right');
				var w=t[0]-x;

				t=findPos('crop_down');
				var h=t[1]-y;

				g.style.width=w+'px';
				g.style.height=h+'px';

				setDIVxy('golden',x,y);

			} else {
				var c=findPos(ids);

				g.style.width=parseInt(id.offsetWidth)+'px';
				g.style.height=parseInt(id.offsetHeight)+'px';

				setDIVxy('golden',c[0],c[1]);
			}
var s='<table width="100%" height="100%" border="1" cellspacing="0" cellpadding="0">'
+'<tr><td height="38%" width="38%">&nbsp;</td><td width="24%">&nbsp;</td><td width="38%">&nbsp;</td></tr>'
+'<tr><td height="24%" width="38%">&nbsp;</td><td width="24%">&nbsp;</td><td width="38%">&nbsp;</td></tr>'
+'<tr><td height="38%" width="38%">&nbsp;</td><td width="24%">&nbsp;</td><td width="38%">&nbsp;</td></tr>'
+'</table>';
			showDIV('golden',s);
		} else { hideDIV('golden'); }
	}
}

cropInit=false;
function pCrop(ids) {
	var id=document.getElementById(ids);
	var o='';
	if (!id) return;
	if (!cropInit) {
		var c=findPos(ids);
		//---up;
		o=document.getElementById('crop_up');
		o.style.width=parseInt(id.offsetWidth)+'px';
		o.style.height=8+'px';
		setDIVxy('crop_up',c[0],c[1]-7);
		//---down;
		o=document.getElementById('crop_down');
		o.style.width=parseInt(id.offsetWidth)+'px';
		o.style.height=8+'px';
		setDIVxy('crop_down',c[0],c[1]+parseInt(id.offsetHeight)-1);
		//---left;
		o=document.getElementById('crop_left');
		o.style.height=parseInt(id.offsetHeight)+'px';
		o.style.width=8+'px';
		setDIVxy('crop_left',c[0]-7,c[1]);
		//---right;
		o=document.getElementById('crop_right');
		o.style.height=parseInt(id.offsetHeight)+'px';
		o.style.width=8+'px';
		setDIVxy('crop_right',c[0]+parseInt(id.offsetWidth)-1,c[1]);

		showCrop();
		cropInit=true;
	} else {
		var b=document.getElementById('back_c').style.backgroundColor;
		changeCBC(b);
		switchDIV('crop_up');
		switchDIV('crop_down');
		switchDIV('crop_left');
		switchDIV('crop_right');
		if (ISshowDIV('golden')) { pGold('pic_id',1); }
	}
}

function pCropadd(id) {
	if (document.getElementById(id) && document.getElementById(id).style.display!='none') {
		pCrop('pic_id');
		showCrop();
		var c=findPos('pic_id');
		var w=parseInt(document.getElementById('pic_id').offsetWidth);
		var h=parseInt(document.getElementById('pic_id').offsetHeight);

		var p=findPos('crop_up');
		var u=p[1]+parseInt(document.getElementById('crop_up').offsetHeight)-c[1];

		p=findPos('crop_down');
		var d=c[1]+h-p[1];

		p=findPos('crop_left');
		var l=p[0]+parseInt(document.getElementById('crop_left').offsetWidth)-c[0];

		p=findPos('crop_right');
		var r=c[0]+w-p[0];

		var v='[crop:'+u+','+r+','+d+','+l+']';
		if (confirm(G_lng[7]+' '+v+' ?')) {insertAtCursor(getCCont(), v);}
	}
}

function pCropset(u,r,d,l) {
	recalcAll();
	pCrop('pic_id');
	var c=findPos('pic_id');
	var w=parseInt(document.getElementById('pic_id').offsetWidth);
	var h=parseInt(document.getElementById('pic_id').offsetHeight);
	p=findPos('crop_up'); p[1]=c[1]+u-8; setDIVxy('crop_up',p[0],p[1]); calcCrop(1);
	p=findPos('crop_left'); p[0]=c[0]+l-8; setDIVxy('crop_left',p[0],p[1]); calcCrop(2);
	p=findPos('crop_right'); p[0]=c[0]+w-r; setDIVxy('crop_right',p[0],p[1]); calcCrop(3);
	p=findPos('crop_down'); p[1]=c[1]+h-d; setDIVxy('crop_down',p[0],p[1]); calcCrop(4);
	showCrop();
	location.href='#pic_top';
}

function getCCont(){
	var s='';
	var e=document.getElementsByTagName('textarea');
	if (e.length>0) {
		for(var i=0;i<e.length;i++) {
			if (e[i].id.indexOf('c_cont')!=-1) {s=e[i].id;i=e.length;}
		}
	}
	return s;
}

function getCTab(){
	var s='';
	var e=document.getElementsByTagName('div');
	if (e.length>0) {
		for(var i=0;i<e.length;i++) {
			if (e[i].id.indexOf('tab_')!=-1) {s=e[i].id;i=e.length;}
		}
	}
	return s;
}

function showCrop() {
	var b=document.getElementById('back_c').style.backgroundColor;
	changeCBC(b);
	showDIV('crop_up');
	showDIV('crop_down');
	showDIV('crop_left');
	showDIV('crop_right');
	if (ISshowDIV('golden')) { pGold('pic_id',1); }
}

function hideCrop() {
	hideDIV('crop_up');
	hideDIV('crop_down');
	hideDIV('crop_left');
	hideDIV('crop_right');
}

function recalcAll() {
	cropInit=false;
	hideCrop();
	hideDIV('golden');
}

function calcCrop(obj) {
	var ids='pic_id';
	var id=document.getElementById(ids);
	var c=findPos(ids);
	switch (obj) {
	case 1:
		o=document.getElementById('crop_up');
		var p=findPos('crop_up');
		if (p[1]>c[1]-7) {
			o.style.height=(p[1]-c[1]+8)+'px';
			o.style.top=c[1]+'px';
		} else {
			p[1]=c[1]-7;
			setDIVxy('crop_up',p[0],p[1]);
		}
		break;
	case 4:
		o=document.getElementById('crop_down');
		var p=findPos('crop_down');
		if (p[1]<c[1]+parseInt(id.offsetHeight)) {
			o.style.height=(c[1]+parseInt(id.offsetHeight)-p[1]+8)+'px';
		} else {
			p[1]=c[1]+parseInt(id.offsetHeight)-1;
			setDIVxy('crop_down',p[0],p[1]);
		}
		break;
	case 2:
		o=document.getElementById('crop_left');
		var p=findPos('crop_left');
		if (p[0]>c[0]-7) {
			o.style.width=(p[0]-c[0]+8)+'px';
			o.style.left=c[0]+'px';
		} else {
			p[0]=c[0]-7;
			setDIVxy('crop_left',p[0],p[1]);
		}
		break;
	case 3:
		o=document.getElementById('crop_right');
		var p=findPos('crop_right');
		if (p[0]<c[0]+parseInt(id.offsetWidth)) {
			o.style.width=(c[0]+parseInt(id.offsetWidth)-p[0]+8)+'px';
		} else {
			p[0]=c[0]+parseInt(id.offsetWidth)-1;
			setDIVxy('crop_right',p[0],p[1]);
		}
		break;
	}
	if (ISshowDIV('golden')) { pGold('pic_id',1); }
}

function preCrop(id) {
	switch (id) {
	case 'crop_up':
		o=document.getElementById('crop_up');
		var p=findPos('crop_up');
		var h=parseInt(o.offsetHeight);
		o.style.height=8+'px';
		o.style.top=(p[1]+h-8)+'px';
		break;
	case 'crop_down':
		o=document.getElementById('crop_down');
		o.style.height=8+'px';
		break;
	case 'crop_left':
		o=document.getElementById('crop_left');
		var p=findPos('crop_left');
		var w=parseInt(o.offsetWidth);
		o.style.width=8+'px';
		o.style.left=(p[0]+w-8)+'px';
		break;
	case 'crop_right':
		o=document.getElementById('crop_right');
		o.style.width=8+'px';
		break;
	}
	var c=findPos('pic_id');
	var o=document.getElementById('pic_id');
	G_move.slimX1=c[0]-7;
	G_move.slimX2=c[0]+parseInt(o.offsetWidth);
	G_move.slimY1=c[1]-7;
	G_move.slimY2=c[1]+parseInt(o.offsetHeight);

	switch (id) {
	case 'crop_up':
		var pd=findPos('crop_down');
		G_move.slimY2=pd[1]-18;
		break;
	case 'crop_down':
		var pu=findPos('crop_up');
		var cu=document.getElementById('crop_up');
		G_move.slimY1=pu[1]+parseInt(cu.offsetHeight)+18;
		break;
	case 'crop_left':
		var pr=findPos('crop_right');
		G_move.slimX2=pr[0]-18;
		break;
	case 'crop_right':
		var pl=findPos('crop_left');
		var cl=document.getElementById('crop_left');
		G_move.slimX1=pl[0]+parseInt(cl.offsetWidth)+18;
		break;
	}

}

function changeCBC(c){
	document.getElementById('crop_up').style.backgroundColor=c;
	document.getElementById('crop_down').style.backgroundColor=c;
	document.getElementById('crop_left').style.backgroundColor=c;
	document.getElementById('crop_right').style.backgroundColor=c;
}


