// JavaScript Document
function ElementsByName(tag, name) {
	var elements = document.getElementsByTagName(tag);
	var arr = [];
	for(var i = 0; i < elements.length; i++) {
		if(elements[i].name == name) arr.push(elements[i]);
	}
	return arr;
}

function SwitchImage(preview, photo, width, height) {
	frame = document.getElementById('photo')
	frame.src = preview;
	frame.setAttribute("onclick", "ShowImage('" + photo + "'," + width + "," + height + ")");
}

function SwitchTab(object, id) {
	document.getElementById(id).className='';
	if (object == 'search') {
		if (id != 'accommodation') document.getElementById('accommodation').className='unactive';
		if (id != 'commercial') document.getElementById('commercial').className='unactive';
		if (id != 'house') document.getElementById('house').className='unactive';
		if (id != 'garage') document.getElementById('garage').className='unactive';
		if (id != 'new-bild') document.getElementById('new-bild').className='unactive';
	}
	else {
		if (id != 'intro') document.getElementById('intro').className='unactive';
		if (id != 'company') document.getElementById('company').className='unactive';
		if (id != 'sale') document.getElementById('sale').className='unactive';
		if (id != 'market') document.getElementById('market').className='unactive';
	}
}

function DistrictSelector (value) {
	if (value) {
		value.parentNode.className = 'districts-show';
	}
	else {
		tabs = ElementsByName('a','tab');
		for (var i = 0; i < tabs.length; i++) tabs[i].removeAttribute('href');
	}
}

function OpenWindow(url, width, height){
	var xPos = Math.floor(screen.width/2-width/2);
	var yPos = Math.floor(screen.height/2-height/2-40);
	window.open(url, '', 'height=' + height + ', width=' + width + ', location=no, scrollbars=no, status=no, menubar=no, toolbar=no, resizable=no, top=' + yPos + ', left=' + xPos );
}

function ShowImage(fullLink, imgWidth, imgHeight){
	if( (imgWidth+30)>screen.width || (imgHeight+30)>screen.height ){
		var ScrollOn = 'yes';
		var windowWidth = Math.floor(screen.width-10);
		var windowHeight = Math.floor(screen.height-100);
		var xPos = 0;
		var yPos = 0;
	}else{
		var ScrollOn = 'no';
		var windowHeight = Math.floor(imgHeight+20);
		var windowWidth = Math.floor(imgWidth+20);
		var xPos = Math.floor(screen.width/2-imgWidth/2);
		var yPos = Math.floor(screen.height/2-imgHeight/2-40);
	}
	big_image=window.open(fullLink+'?isNaked=1', '', 'height=' + windowHeight + ', width=' + windowWidth + ', location=no, scrollbars=' + ScrollOn + ', status=no, menubar=yes, toolbar=no, resizable=yes, top=' + yPos + ', left=' + xPos );
}

function ToggleHeader(header, elementID, changeText){
	var h = header.getAttribute('class');
	
	if (h == 'ohc'){
		header.className = 'ohe';
		if (changeText){
			header.innerHTML = 'Скрыть карту';
		}
	}
	else {
		header.className = 'ohc';
		if (changeText){
			header.innerHTML = 'Показать карту';
		}
	}
	
	ShowEl(elementID);
}

function ShowEl(id){
	var a = document.getElementById(id).style;
	var d = a.display;
	a.display = (!d || d=='none' ? 'block' : 'none');
}

function blinkIt() {
   setInterval(function(){
	   var el = ElementsByName('a','spt');
	   
	   if (el){
		   for(i=0; i<el.length; i++){
			  s=el[i];
			  s.style.visibility=(s.style.visibility=='visible')?'hidden':'visible';
		   }
	   }
   }, 500);
}

function ShowYMaps(params){
	var a = document.getElementById('YMapsWrapper');
	if (!a.innerHTML){
		a.innerHTML = "<iframe id='YMapsFrame' src='/ymaps/?" + params + "' frameborder='0'></iframe><br clear='all' />";
		var ymaps = RUZEE.ShadedBorder.create({ corner:5, border:1 }); 
		ymaps.render('YMapsWrapper');
	}
}

var Calc = {
	element: function(id){
		return document.getElementById(id);
	},
	check: function(id){
		e = Calc.element(id);
		e.value = e.value.replace(/[^0-9\.]/i, "");
	},
	value: function(id){
		return Calc.element(id).value;
	},
	result: function(activeElement, additionalElement, resultElement, method, format, fractionDigits){
		Calc.check(activeElement);
		Calc.check(additionalElement);
		if (Calc.value(activeElement) && Calc.value(additionalElement)){
			switch (method){
				case 'multiply': var result = (Calc.value(activeElement) * Calc.value(additionalElement)).toFixed(fractionDigits); break
				case 'divide': var result = (Calc.value(activeElement) / Calc.value(additionalElement)).toFixed(fractionDigits); break
			}
			Calc.element(resultElement).value = format ? (result * format).toFixed() : result;
		}
	}
}

var tooltip = {
	options: {
		attr_name: "tooltip",
		blank_text: "(откроется в новом окне)",
		newline_entity: "<br>",
		max_width: 300,
		delay: 100,
		processing_tags: ["area","div","a"],
		inner_tags_id: ['ttt','tttl','tttr','ttl','ttr','ttb','ttbl','ttbr']
	},

	t: document.createElement("div"),
	tt: document.createElement("div"),
	c: null,
	g: false,
	canvas: null,

	m: function(e){
		if (tooltip.g){
			var x = window.event ? event.clientX + tooltip.canvas.scrollLeft : e.pageX;
			var y = window.event ? event.clientY + tooltip.canvas.scrollTop : e.pageY;
			tooltip.a(x, y);
		}
	},

	d: function(){
		tooltip.canvas = document.getElementsByTagName(document.compatMode && document.compatMode == "CSS1Compat" ? "HTML" : "BODY")[0];
		tooltip.t.setAttribute("id", "tooltip");
		for (var i=0; i<tooltip.options.inner_tags_id.length; i++){
			var d = document.createElement("div");
			d.setAttribute("id", tooltip.options.inner_tags_id[i]);
			tooltip.t.appendChild(d);
		}
		tooltip.t.appendChild(tooltip.tt);
		document.body.appendChild(tooltip.t);
		if (tooltip.options.max_width) tooltip.t.style.maxWidth = tooltip.options.max_width + "px";
		var a = document.all && !window.opera ? document.all : document.getElementsByTagName("*");
		var l = a.length;
		for (var i = 0; i < l; i++){

			if (!a[i] || !tooltip.options.processing_tags.in_array(a[i].tagName.toLowerCase())) continue;

			var tooltip_title = a[i].getAttribute("title");
			if (tooltip_title && typeof tooltip_title != "string") tooltip_title = "";

			var tooltip_alt = a[i].getAttribute("alt");
			var tooltip_blank = a[i].getAttribute("target") && a[i].getAttribute("target") == "_blank" && tooltip.options.blank_text;
			if (tooltip_title || tooltip_blank){
				a[i].setAttribute(tooltip.options.attr_name, tooltip_blank ? (tooltip_title ? tooltip_title + " " + tooltip.options.blank_text : tooltip.options.blank_text) : tooltip_title);
				if (a[i].getAttribute(tooltip.options.attr_name)){
					a[i].removeAttribute("title");
					if (tooltip_alt && a[i].complete) a[i].removeAttribute("alt");
					tooltip.l(a[i], "mouseover", tooltip.s);
					tooltip.l(a[i], "mouseout", tooltip.h);
				}
			}else if (tooltip_alt && a[i].complete){
				a[i].setAttribute(tooltip.options.attr_name, tooltip_alt);
				if (a[i].getAttribute(tooltip.options.attr_name)){
					a[i].removeAttribute("alt");
					tooltip.l(a[i], "mouseover", tooltip.s);
					tooltip.l(a[i], "mouseout", tooltip.h);
				}
			}
			if (!a[i].getAttribute(tooltip.options.attr_name) && tooltip_blank){
				//
			}
		}
		document.onmousemove = tooltip.m;
		window.onscroll = tooltip.h;
		tooltip.a(-99, -99);
	},
	
	_: function(s){
		s = s.replace(/\&/g,"&amp;");
		s = s.replace(/\</g,"&lt;");
		s = s.replace(/\>/g,"&gt;");
		s = s.replace(/ \//g,"");
		return s;
	},

	s: function(e){
		if (typeof tooltip == "undefined") return;
		var d = window.event ? window.event.srcElement : e.target;
		if (!d.getAttribute(tooltip.options.attr_name)) return;
		var s = d.getAttribute(tooltip.options.attr_name);
		if (tooltip.options.newline_entity){
			var s = tooltip._(s);
			s = s.replace(eval("/" + tooltip._(tooltip.options.newline_entity) + "/g"), "<br>");
			tooltip.tt.innerHTML = s;
		}else{
			if (tooltip.tt.firstChild) tooltip.tt.removeChild(tooltip.tt.firstChild);
			tooltip.tt.appendChild(document.createTextNode(s));
		}
		tooltip.c = setTimeout(function(){
			tooltip.t.style.visibility = 'visible';
		}, tooltip.options.delay);
		tooltip.g = true;
	},

	h: function(e){
		if (typeof tooltip == "undefined") return;
		tooltip.t.style.visibility = "hidden";
		if (!tooltip.options.newline_entity && tooltip.tt.firstChild) tooltip.tt.removeChild(tooltip.tt.firstChild);
		clearTimeout(tooltip.c);
		tooltip.g = false;
		tooltip.a(-99, -99);
	},

	l: function(o, e, a){
		if (o.addEventListener) o.addEventListener(e, a, false);
		else if (o.attachEvent) o.attachEvent("on" + e, a);
			else return null;
	},

	a: function(x, y){
		var w_width = tooltip.canvas.clientWidth ? tooltip.canvas.clientWidth + tooltip.canvas.scrollLeft : window.innerWidth + window.pageXOffset;
		var w_height = window.innerHeight ? window.innerHeight + window.pageYOffset : tooltip.canvas.clientHeight + tooltip.canvas.scrollTop;

		if (document.all && document.all.item && !window.opera) tooltip.t.style.width = tooltip.options.max_width && tooltip.t.offsetWidth > tooltip.options.max_width ? tooltip.options.max_width + "px" : "auto";
		
		var t_width = tooltip.t.offsetWidth;
		var t_height = tooltip.t.offsetHeight;

		tooltip.t.style.left = x + 17 + "px";
		tooltip.t.style.top = y + 17 + "px";
		
		if (x + t_width > w_width) tooltip.t.style.left = w_width - t_width + "px";
		if (y + t_height > w_height) tooltip.t.style.top = w_height - t_height + "px";
	}
}

Array.prototype.in_array = function(value){
	var l = this.length;
	for (var i = 0; i < l; i++)
		if (this[i] === value) return true;
	return false;
};

var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
if (root){
	if (root.addEventListener) root.addEventListener("load", tooltip.d, false);
	else if (root.attachEvent) root.attachEvent("onload", tooltip.d);
}


