
function gebi(id){
	return document.getElementById(id)
}
function newImage(s){
	var r= new Image()
	r.src = s
	return r
}


function changetab(tab) {
	activetab = tab
	var table = gebi('tab')
	var tabs = (table.rows[0].cells.length - 2) / 2
	for (var i=0; i<tabs; i++) {
		table.rows[0].cells[2 * i + 1].className = ( i + 1 == activetab ? 't-tab t-tab-h' : 't-tab')
		table.rows[0].cells[2 * i + 2].firstChild.src = 
			'images/tab/b' + (i + 1 == activetab ? '2' : '1') + 
			( i + 1 == tabs ? '0' : (i + 2 == activetab ? '2' : '1')) + ".gif"
		gebi('tb' + (i + 1)).style.display = ( i + 1 == activetab ? 'block' : 'none')
	}
	if (gebi('crop').style.display!='none'){
		gebi('crop').style.display = 'none'
		crop.active=false;
		reposition();
		gebi('act-3').sl = gebi('act-3').bu;
		gebi('act-3').src = gebi('act-3').sl.src;
	}
}

var processing = false
function processingStart(id, add){
	if (id && !session_prefix) {
		alert('Изображение не загружено')
	} else {
		processing = setTimeout("alert('Операция не удалась\\nПопробуйте повторить'); processingEnd()", 300000)
		gebi('loading').style.display = 'block'
		if (id) {
			gebi('fprocessing').src = 'func.php?land='+land+'&id=' + id + (add || '')
		}
	}
}
function processingEnd(w, h){
	if (processing) {
		try{clearTimeout(processing)} catch(e) {}
		processing = false
		gebi('loading').style.display = 'none'
		if (session_prefix) { 
			last_image.src = session_prefix + 'preview.jpg' + '?x=' + Math.random()
			//last_image.src = 'session/' + 'preview.jpg' + '?x=' + Math.random()
			gebi('mainpic').src = last_image.src
			//alert(gebi('mainpic').src);
			if (w && h) {
				last_img_width = gebi('mainpic').width = w
				last_img_height = gebi('mainpic').height = h
			}
		}
	}
}
function uploadEnd(s_prefix, w, h){
	processing = processing || true
	if (s_prefix) { 
		session_prefix = s_prefix
		original_image = newImage(session_prefix + 'preview_original.jpg')
		historyClear()
	}
	processingEnd(w, h)
}

function help_msg(err){
	if (!err) {
		gebi("help").style.display='none'
		return
	}
	gebi("help_msg").innerHTML=err
	gebi("help").style.left=document.body.clientWidth/2 - 200
	gebi("help").style.display='block'
}

function confirm_msg(err){
	if (!err) {
		gebi("confirm").style.display='none'
		return
	}
	gebi("confirm_msg").innerHTML=err
	gebi("confirm").style.left=document.body.clientWidth/2 - 200
	gebi("confirm").style.display='block'
}

function gradient_msg(err){
	if (gebi("gradient").style.display=='block'){
		gebi("gradient").style.display='none';
		return 1;
	}
	gebi("gradient").style.left=document.body.clientWidth/2 - 200
	gebi("gradient").style.display='block'
	return 0;
}

function stran_msg(err){
	if (!err) {
		gebi("stran").style.display='none'
		return
	}
	gebi("stran_msg").innerHTML=err
	gebi("stran").style.left=document.body.clientWidth/2 - 200
	gebi("stran").style.display='block'
}

function startupmenu(tab) {
	activetab = tab;
	if (activetab == 0)
		window.open = ("help/bistr_start.html", "_blank");
	else if (activetab == 1)
		window.location.href = "help/index.html";
	else if (activetab == 2)
		window.location.href = "/forum";
}

var change_wh = true;
function recalc_w(new_w){
   if (!change_wh) return true;
        new_w = gebi('size_w').value;
	w = gebi('mainpic').width;
	h = gebi('mainpic').height;
	if (w){
		new_h = parseInt(new_w) * h / w;
  		change_wh = false;
		gebi('size_h').value = Math.round(new_h);
		if (parseInt(new_h)+parseInt(new_w)>20000){
  		   alert("Сумма разрешений по ширине и высоте не должна превышать 20000");
		   gebi('size_w').value = Math.round(20000/(1+h/w));
		   gebi('size_h').value = Math.round(20000/(1+h/w)*h/w);
		}
  	        change_wh = true;
	}
}

function recalc_h(new_h){
   if (!change_wh) return true;
        new_h = gebi('size_h').value;
	w = gebi('mainpic').width;
	h = gebi('mainpic').height;
	if (h){
		new_w = parseInt(new_h) * w / h;
  		change_wh = false;
		gebi('size_w').value = Math.round(new_w);
		if (parseInt(new_h)+parseInt(new_w)>20000){
  		   alert("Сумма разрешений по ширине и высоте не должна превышать 20000");
		   gebi('size_h').value = Math.round(20000/(1+w/h));
		   gebi('size_w').value = Math.round(20000/(1+w/h)*w/h);
		}
  	        change_wh = true;
	}
}

function fs(obj){
  obj.focus();
  obj.select();
}
