function open_send(id) {
	window.open("/callback?id=" + id , "email", "width=350,height=450,top=200,left=200,Resizable=1,status");
}
function open_picture(u,w,h,max_w,max_h) {
	if (window.screen) {
		max_w = window.screen.width - 20;
		max_h = window.screen.height - 53;
	}
	win_w = w + 0;
	win_h = h + 0;
	if (win_w > max_w) win_w = max_w;
        if (win_h > max_h) win_h = max_h;
	win = window.open(u,"NewWin","width=" + win_w + ", height=" + win_h + ",scrollbars=auto,resizable=1,resizable=yes");
        return(win);
}

function open_picture1(u,w,h,max_w,max_h) {
	if (window.screen) {
		max_w = window.screen.width - 20;
		max_h = window.screen.height - 53;
	}
	win_w = w + 0;
	win_h = h + 0;
	if (win_w > max_w) win_w = max_w;
        if (win_h > max_h) win_h = max_h;
	win = window.open('','',"width=" + win_w + ", height=" + win_h + ",scrollbars=auto,resizable=1,resizable=yes");
	win.document.open();
	win.document.write('<body bgcolor=#F1F2F2 leftMargin=0 rightMargin=0 topMargin=0 marginwidth=0 marginheight=0>');
	win.document.write('<img width=' + w + ' height=' + h + ' src="' + u + '">');
	win.document.close();
 	win.focus();
        return(win);
}

function quote(nick,id) {
	var selectedText =  null;
	var quote_mesg = '<Q'+(nick == '' ? '' : ' '+nick)+'>';
	if (document.selection)
		selectedText = document.selection.createRange();
	if (selectedText != null && selectedText.text) {
		mesg = selectedText.text;
	} else {
		eval('mesg = mesg'+id+';');
	}
	quote_mesg += mesg + "</Q>\n";
	if (document.forms['message'].message) {
		//document.forms['message'].message.innerText += quote_mesg;
		document.forms['message'].message.value += quote_mesg;
		document.forms['message'].message.focus();
	} else {
		//document.forms['message'].content.innerText += quote_mesg;
		document.forms['message'].content.value += quote_mesg;
		document.forms['message'].content.focus();
	}
	return true;
}

	
	function show(number) {
		for (var i=1;i<=15;i++) {
			document.getElementById('map_'+i).style.visibility = (i==number)?'visible':'hidden';
		}
		return(false);
	}
	function showpreview(x,y,co) {
		a=parseInt(y)-parseInt(co);
		b=parseInt(x)+parseInt(co);
		c=parseInt(y)+parseInt(co);
		d=parseInt(x)-parseInt(co);
		document.getElementById('scheme_a').style.clip='rect('+a+','+b+','+c+','+d+')';
		
	}
	function hidepreview() {
		document.getElementById('scheme_a').style.clip='rect('+temp+')';
	}

function maxpic(img, w, h){
var win = window.open('','_blank',"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width="+w+",height="+h);
	win.document.open("text/html");
	with (win.document) {
	    write("<html><head><title>Изображение</title></head><script type=text/javascript>self.focus()</script></head>"+
"<body leftmargin=0 topmargin=0 bottommargin=0 rightmargin=0 marginheight=0 marginwidth=0 bgcolor=\"#ffffff\">"+
"<a href='#' onclick='window.close();return false;'><img src='"+img+"' width="+w+
" height="+h+" alt='Закрыть окно' border=0></a>"+
"</body></html>");
	}
}
	
/* */

var count;
var timer;
var nn4 = (document.layers)? true : false;
var dom = (document.getElementById)? true : false;
var ie4 = (document.all && !dom)? true : false;

setOpacity = function(box, value) {
  if (navigator.userAgent.indexOf("Firefox") != -1) {
    if (value == 100) { value = 99.999; }
  }
  box.style.filter = "alpha(opacity=" + value + ")"; // IE/Win
  box.style.KhtmlOpacity = (value / 100);            // Safari 1.1 or lower, Konqueror
  box.style.MozOpacity = (value / 100);              // Older Mozilla+Firefox
  box.style.opacity = (value / 100);                 // Safari 1.2, Firefox+Mozilla
}
/* геттер y-координаты */
objY = function(uid) {
	if (nn4) {return document.layers[uid].pageY;} else {
		var element = (dom)? document.getElementById(uid) : document.all[uid];
		var coordsY = element.offsetTop;
		while (element.offsetParent != null) {
			element = element.offsetParent;
			coordsY += element.offsetTop;
			if (element.tagName == 'BODY') break;
		} return coordsY;
	}
}
/* геттер x-координаты */
objX = function(uid) {
	if (nn4) {
		return document.layers[uid].pageX;
	} else {
		var element = (dom)? document.getElementById(uid) : document.all[uid];
		var coordsX = element.offsetLeft;
		while (element.offsetParent != null) {
			element = element.offsetParent;
			coordsX += element.offsetLeft;
			if (element.tagName == 'BODY') break;
		} return coordsX;
	}
}
/* геттер объекта */
getObj = function(uid) {
	return element = (dom) ? document.getElementById(uid) : document.all[uid];
}

hidebox = function() {
	var box = getObj('tooltip');
	ato = setTimeout(function(){fadeOut(box, 100)}, 350);
	}
	
fadeOut = function(box, count) {
	count = parseFloat(count - 25);
	setOpacity(box, count);
	timer = setTimeout(function(){fadeOut(box, count)}, 1);
	if (count==0) {
		clearTimeout(timer);
		box.style.display = 'none';
		setOpacity(box, 100);
	}
}


showtip = function(id){
	clearAltTimeOut();
	clearAltTimeOut2();
	var len =  mapID.length;
	for (var i = 0; i < len; i++) {
		var ob = mapID[i];
		var box = document.getElementById(ob);
			if (box.style.display == 'block') {
				box.style.display = "none";
				closBox();
				closBox_revers();
			}
		document.getElementById(ob+'link').style.color="";
	}

	var topY = objY('russia') + mapGeoY[id];
	var topX = objX('russia') + mapGeoX[id];
	var box = document.getElementById('tooltip');
	document.getElementById('tool_insert').innerHTML = mapData[id];
	box.style.top = topY + "px";
	box.style.left = topX + "px";
	box.style.display = "block";

	document.getElementById(id).style.display='block';
	/*document.getElementById(id+'link').style.color="#AE5A8C";*/
	document.getElementById(id+'link').style.background = "#FFFFFF";
	
}

showtip_revers = function(id){
	clearAltTimeOut();
	clearAltTimeOut2();
	var len =  mapID.length;
	for (var i = 0; i < len; i++) {
		var ob = mapID[i];
		var box = document.getElementById(ob);
			if (box.style.display == 'block') {
				box.style.display = "none";
				closBox_revers();
				closBox();
			}
		document.getElementById(ob+'link').style.color="";
	}

	var topY = objY('russia') + mapGeoY[id];
	var topX = objX('russia') + mapGeoX[id];
	var box = document.getElementById('tooltip_revers');
	document.getElementById('tool_insert2').innerHTML = mapData[id];
	box.style.top = topY + "px";
	box.style.left = topX + "px";
	box.style.display = "block";

	document.getElementById(id).style.display='block';
	document.getElementById(id+'link').style.background = "#FFFFFF";
	
}

hide = function(div){
	var box = document.getElementById('tooltip');
	box.style.display = "none";
	
	document.getElementById(div).style.display='none'
	document.getElementById(div+'link').style.fontWeight="bold";
	document.getElementById(div+'link').style.color=""; // #2e3193
	document.getElementById(id+'link').style.background = "";
}

var ato = 0;
clearAltTimeOut = function() {
	if (ato != 0 && ato != null) clearTimeout(ato);
	ato = 0;
}

hideAlt = function() {
	ato = setTimeout(function(){closBox()}, 350);
}

closBox = function() {
	var box0 = getObj('tooltip');
	var len =  mapID.length;
	for (var i = 0; i < len; i++) {
		var ob = mapID[i];
		var box = document.getElementById(ob);
			if (box.style.display == 'block') {
				box.style.display = "none";
			}
		document.getElementById(ob+'link').style.color="";
		document.getElementById(ob+'link').style.background = "";
	}
	box0.style.display = 'none';
	}
	
closBox_revers = function() {
	var box0 = getObj('tooltip_revers');
	var len =  mapID.length;
	for (var i = 0; i < len; i++) {
		var ob = mapID[i];
		var box = document.getElementById(ob);
			if (box.style.display == 'block') {
				box.style.display = "none";
			}
		document.getElementById(ob+'link').style.color="";
		document.getElementById(ob+'link').style.background = "";
	}
	box0.style.display = 'none';
	}
	
var atoR = 0;
clearAltTimeOut2 = function() {
	if (atoR != 0 && atoR != null) clearTimeout(atoR);
	atoR = 0;
}

hideAlt2 = function() {
	atoR = setTimeout(function(){closBox_revers()}, 350);
}


show_map = function(id){
	clearAltTimeOut31();
	closBox21();
	/*
	var len =  mapID.length;
	for (var i = 0; i < len; i++) {
		var ob = mapID[i];
		var box = document.getElementById(ob);
			if (box.style.display == 'block') {
				box.style.display = "none";
				//closBox();
			}
		document.getElementById(ob+'link').style.color="";
	}
	*/
	clearAltTimeOut3();
	closBox2();
	var topY = objY('russia') + mapGeoY[id];
	var topX = objX('russia') + mapGeoX[id];
	var box = document.getElementById('tooltip');
	document.getElementById('tool_insert').innerHTML = mapData[id];
	box.style.top = topY + "px";
	box.style.left = topX + "px";
	box.style.display = "block";
	document.getElementById(id+'link').style.background = "#FFFFFF";
	
}

var atoRq = 0;
clearAltTimeOut3 = function() {
	if (atoRq != 0 && atoRq != null) clearTimeout(atoRq);
	atoRq = 0;
}

hideAlt3 = function() {
	atoRq = setTimeout(function(){closBox2()}, 350);
}

closBox2 = function() {
	var box0 = getObj('tooltip');
	
	var len =  mapID.length;
	for (var i = 0; i < len; i++) {
		var ob = mapID[i];
		document.getElementById(ob+'link').style.color="";
		document.getElementById(ob+'link').style.background = "";
	}
	
	box0.style.display = 'none';
}


function show_cataloq(id, img) {
	var box = getObj('c' + id);
	var pic = getObj('pq' + id);
	box.className = "act";
	if(img) {
		pic.src = img;
	}
}

function hide_cataloq(id, img) {
	var box = getObj('c' + id);
	var pic = getObj('pq' + id);
	box.className = "";
	if(img) {
		pic.src = img;
	}
}

function show_cataloq_m(id) {
	var box = getObj('c' + id);
	var pic = getObj('pq' + id);
	box.className = "act";
	pic.src = Pictures[id][3].src;
}

function hide_cataloq_m(id) {
	var box = getObj('c' + id);
	var pic = getObj('pq' + id);
	box.className = "";
	pic.src = Pictures[id][2].src;
}



function show_cataloq_home(id, img, img1) {
	var box = getObj('c' + id);
	var pic = getObj('pq' + id);
	box.className = "act";
	if(img) {
		pic.src = img;
	}
	var myObjColl = getElementsByClassName('ivHome');
	for (var i = 0; i < myObjColl.length; i++) {
		myObjColl[i].style.display = "none";
	}
	var boxIMG = getObj('div_img_past');
	var boxMenu = getObj('dp_' + id);
	if (boxMenu.style.display == "none") {
		boxMenu.style.display = "block";
		if(img) {
			boxIMG.src = img1;
		}
	}
}

function hide_cataloq_home(id, img) {
	var box = getObj('c' + id);
	box.className = "";
	var pic = getObj('pq' + id);
	if(img) {
		pic.src = img;
	}
}


function addEvent(element, type, handler) {
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		if (!element.events) element.events = {};
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		handlers[handler.$$guid] = handler;
		element["on" + type] = handleEvent;
	}
};
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else {
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	}
};

function handleEvent(event) {
	var returnValue = true;
	event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
	var handlers = this.events[event.type];
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};




/* ext dealer */
var atoRq1 = 0;
clearAltTimeOut31 = function() {
	if (atoRq1 != 0 && atoRq1 != null) clearTimeout(atoRq1);
	atoRq1 = 0;
}

hideAlt31 = function() {
	atoRq1 = setTimeout(function(){closBox21()}, 350);
}

closBox21 = function() {
	if (getObj('tooltip_ext')) {
		var box0 = getObj('tooltip_ext');
		box0.style.display = 'none';
	}
}

show_map_ext = function(obj, id, r) {
	clearAltTimeOut3();
	closBox2();
	clearAltTimeOut31();
	//var topY = objY(obj.id);
	//var topX = 0;

	var topY = objY('russia') + mapGeoY[r];
	var topX = objX('russia') + mapGeoX[r];
	//if (r == 0) {topX = objX(obj.id);}
	//else {topX = objX(obj.id) - 130;}
	var box = document.getElementById('tooltip_ext');
	box.style.top = topY + "px";
	
	$('city_dealer').innerHTML = '<b>' + mapData[r] + '</b>';
	$('tool_insert_ext').innerHTML = '<div style="text-align:center;"><img alt="" src="/imgs/ballon/loader.gif" width="32" height="32"></div>';
	$('tool_insert_ext').style.display = '';
	box.style.left = topX + "px";
	box.style.display = "block";

	if (id) {
		var request = 'id=' + id;
		var opt = {
			method: 'post',
			postBody: request,
			onSuccess: function(t) {
				var tmp = t.responseText;
				var rule = tmp.indexOf('dealer_empty');
				if (rule != -1) {
					setTimeout(function() {_text_ext2()}, 1000);
				}
				else {
					$('tool_insert_ext').style.display = '';
					setTimeout(function() {_text_ext(t.responseText)}, 1000);
				}
			},
			onFailure: function(t) {
				alert('Error ' + t.status + ' -- ' + t.statusText);
			}
		}
		new Ajax.Request('/ru/dealer', opt);
	}
}

_text_ext = function(text) {
	$('tool_insert_ext').innerHTML = text;
}
_text_ext2 = function() {
	$('tool_insert_ext').style.display = 'none';
	$('tool_insert_ext').innerHTML = ' ';
}
