function gebId(searchId) {
	if( document.getElementById ) return document.getElementById(searchId);
	else return false;
}
if( String && String.prototype ) {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g,"");
	}
}

var functionsOnload = new Array();
function onloadFunctionStarter() {
	var n = functionsOnload.length;
	if( n > 0 ) for( var i = 0; i < n; i++ ) functionsOnload[i]();
	functionsOnload = new Array();
}

function ispr_text(text) {
	text = text.replace("&", "&amp;");
	text = text.replace("\"", "&quot;");
	text = text.replace("'", "&#039;");
	text = text.replace("<", "&lt;");
	text = text.replace(">", "&gt;");
	text = text.replace(/([\s])+/, "$1");
	text = text.replace("ё", "е");
	return text;
}
document.onkeydown = startNavigate;
function startNavigate (event) {
	if (document.getElementById) {
		if (window.event) event = window.event;
		if (event.ctrlKey) {
			var keyCode = null;
			if( event.keyCode )
				keyCode = event.keyCode;
			else if( event.which )
				keyCode = event.which;
			if( keyCode == 0x27 && gebId("nextPage") && gebId("nextPage").href )
				location.href = gebId("nextPage").href;
			else if( keyCode == 0x25 && gebId("prevPage") && gebId("prevPage").href )
				location.href = gebId("prevPage").href;
		}
	}
}

var maxWidthBody = 1600;
var minWidthBody = 995;
var load = false;
function positionLayersOnResize() {
	if(gebId("NewLayerPage")) stretchElement(gebId("NewLayerPage"));
	if(gebId("layerPage")) positionLayerPage(gebId("layerPage"));
}
window.onresize = positionLayersOnResize;
if(window.attachEvent) window.attachEvent("onresize", positionLayersOnResize);
functionsOnload[functionsOnload.length] = positionLayersOnResize;
function stretchElement(obj) {
	var b = (document.getElementsByTagName("body")[0] || document.body);
	var bWidth = b.offsetWidth > minWidthBody ? b.offsetWidth : minWidthBody;
	if( b.offsetWidth >= maxWidthBody && b.scrollWidth > maxWidthBody ) bWidth = b.scrollWidth;
	var bHeight = b.scrollHeight > b.offsetHeight ? b.scrollHeight : b.offsetHeight;
	if( window.innerHeight && window.innerHeight > bHeight ) bHeight = window.innerHeight;
	if( document.documentElement && document.documentElement.clientHeight && document.documentElement.clientHeight > bHeight ) bHeight = document.documentElement.clientHeight;
	if( b.clientHeight && b.clientHeight > bHeight ) bHeight = b.clientHeight;
	obj.style.width = bWidth - 2 + "px";
	obj.style.height = bHeight - 2 + "px";
}
function positionLayerPage(obj) {
	var width = parseInt(obj.style.width.replace("px", ""));
	var height = parseInt(obj.style.height.replace("px", ""));
	var left = (parseInt(gebId("NewLayerPage").style.width.replace("px", "")) - 6 - width)/2;
	if(left<10) left = 10;
	obj.style.left = left + "px";
	var b = parseInt(gebId("NewLayerPage").style.height.replace("px", ""));
	if( (b-height-50) < 0 ) {
		gebId("NewLayerPage").style.height = String(height+50) + "px";
		b = (document.getElementsByTagName("body")[0] || document.body);
		var bWidth = b.offsetWidth > minWidthBody ? b.offsetWidth : minWidthBody;
		if( b.offsetWidth >= maxWidthBody && b.scrollWidth > maxWidthBody ) bWidth = b.scrollWidth;
		gebId("NewLayerPage").style.width = bWidth - 2 + "px";
	}
	var top = ((window.innerHeight || document.documentElement.clientHeight) - 6 - height)/2;
	if(top<40) top = 40;
	obj.style.top = top + "px";
}
function loadLine(width) {
	var obj = gebId("loadLine");
	if(!load && gebId("layerPage")) gebId("loadLine").style.display = "none";
	else if(load) {
		if( gebId("loadLine").style.display == "none" ) gebId("loadLine").style.display = "";
		if( !obj.style.width ) obj.style.width = "10px";
		else {
			var value = parseInt(obj.style.width.replace("px", ""));
			if(value==width) obj.style.width = "10px";
			else if(value+10>width) obj.style.width = width + "px";
				else obj.style.width = value + 10 + "px";
		}
		setTimeout("loadLine(" + width + ")", 100);
	}
}
var uSLP = 0;
function showNewLayerPage(width, height) {
	if((document.getElementById) && width<=(minWidthBody-20) && !gebId("layerPage")) {
		uSLP = window.pageYOffset || (document.body && document.body.scrollTop) || document.documentElement.scrollTop || 0;
		window.scrollTo(0,0);
		var b = (document.getElementsByTagName("body")[0] || document.body);
		var sels = document.getElementsByTagName("select");
		for(var i = 0; i < sels.length; i++) {
			sels[i].style.visibility = "hidden";
		}
		var l = document.createElement("div");
		b.appendChild(l);
		l.id = "NewLayerPage";
		var p = document.createElement("div");
		b.appendChild(p);
		p.id = "layerPage";
		p.style.width = width + "px";
		p.style.height = height + "px";
		p.innerHTML = '<div id="loadLine"></div>';
		load = true;
		loadLine(width);
		l.innerHTML = '<div style="padding: 5px 0px; border: 1px solid #cccccc; width: 100px; cursor: pointer; cursor: hand; font-size: 130%; background-color: white; text-align: center; color: red;" onClick="destroyLayerPage();" onMouseOver="this.style.color=\'\';" onMouseOut="this.style.color=\'red\';"><b>Закрыть</b></div>';
		stretchElement(l);
		positionLayerPage(p);
		return false;
	}
	else return true;
}
function showImage(obj) {
	var url;
	if( !obj || typeof(obj.href) == "undefined" ) {
		obj = window.event ? window.event.srcElement : obj.target;
		url = String(obj.parentNode.href);
	}
	else url = String(obj.href);
	var type = url.replace("tyres.ru", "");
	if( type.replace("tyres", "") != type ) type = "tyre";
	else if( type.replace("wheels", "") != type ) type = "wheel";
	if(obj && url.length && document.getElementById) {
		var image = document.createElement("img");
		if( type == "tyre" ) {
			if( showNewLayerPage(304,414) ) return true;
			image.width = "300";
			image.height = "410";
		}
		else if(type == "wheel") {
			if( showNewLayerPage(404,404) ) return true;
			image.width = "400";
			image.height = "400";
		}
		else return true;
		image.src = url;
		gebId("layerPage").appendChild(image);
		image.onload = new function() { load = false; };
		image.onclick = destroyLayerPage;
		if( window.event )
			window.event.returnValue = false;
		return false;
	}
	return true;
}
function destroyLayerPage() {
	load = false;
	var b = (document.getElementsByTagName("body")[0] || document.body);
	if( gebId("NewLayerPage") ) {
		gebId("NewLayerPage").innerHTML = "";
		b.removeChild(gebId("NewLayerPage"));
	}
	if( gebId("layerPage") ) {
		gebId("layerPage").innerHTML = "";
		b.removeChild(gebId("layerPage"));
	}
	var sels = document.getElementsByTagName("select");
	for(var i = 0; i < sels.length; i++) {
		sels[i].style.visibility = "";
	}
	window.scrollTo(0, uSLP);
	if( location.href.indexOf("#avtosize") !== -1 )
		location.replace(location.href.replace(/#.*/, "") + "#");
	var s = b.getElementsByTagName("script");
	if(s.length>0) for(var i in s) if( s[i].comment == "request" ) {
		b.removeChild(s[i]);
		destroyLayerPage();
		return;
	}
}
function requestScript(url, width, height) {
	if(width && height) if(showNewLayerPage(width, height)) return true;
	var s = document.createElement("script");
	s.type="text/JavaScript";
	s.language="JavaScript";
	s.src = url;
	s.comment = "request";
	(document.getElementsByTagName("body")[0] || document.body).appendChild(s);
	return false;
}
function refreshSession() {
	requestScript("/?id=refresher&" + Math.random(), 0, 0);
	setTimeout("refreshSession()", 600000);
}
setTimeout("refreshSession()", 600000);//10 минут
function cursorHand(obj) {
	if( obj.style ) { obj.style.cursor = 'pointer'; obj.style.cursor = "hand"; }
}
function arraySort(a, b) {
	if(String(a)<String(b)) return 1;
	else if(String(b)>String(a)) return -1;
	return 0;
}
function getNodeForm(name) {
	if(document.forms[name]) return document.forms[name];
	var form = document.getElementsByTagName("form");
	for(var i in form) if(form[i].name == name) return form[i];
	return false;
}
function addOptionIntoObj(select, option) {
	try {
		select.add(option, null);
	}
	catch( ex ) {
		try {
			select.add(option);
		}
		catch( ex ) {
			select.appendChild(option);
		}
	}
}

var catalogTyres = null;
function realizeSeasonSelecterForTyres(event) {
	var ul = gebId("catalog");
	var e;
	if( typeof(event) !== "boolean" ) {
		e = window.event ? window.event.srcElement : event.target;
		e.onmouseover = new function() {};
		e.onmouseout = new function() {};
		e.style.backgroundColor = "";
		e.className = "seasonActiv";
		var tdArray = e.parentNode.getElementsByTagName("TD");
		for( var i in tdArray )
			if( tdArray[i].innerHTML != e.innerHTML && tdArray[i].className == "seasonActiv" ) {
				tdArray[i].className = "";
				tdArray[i].removeAttribute("class");
				tdArray[i].style.border = "";
				tdArray[i].onmouseover = seasonBackgroundChanger;
				tdArray[i].onmouseout = seasonBackgroundChanger;
			}
		e = e.innerHTML;
		if( e == "всё" ) e = false;
	}
	else e = false;
	if( seasonArray.length > 1 ) {
		var liAr = ul.getElementsByTagName("LI");
		var a;
		for( var i in liAr ) {
			if( !liAr[i].getElementsByTagName )
				continue;
			a = liAr[i].getElementsByTagName("A");
			a = a[0];
			if( (e === false || a.title == e) && liAr[i].style )
				liAr[i].style.display = "";
			else if( liAr[i].style )
				liAr[i].style.display = "none";
		}
	}
	return false;
}
function seasonBackgroundChanger(event) {
	var e = window.event ? window.event.srcElement : event.target;
	if( e.className != "seasonActiv" ) {
		var s = e.style.backgroundColor || "";
		if( s == "" || s == "transparent" ) {
			e.style.backgroundColor = "#fff";
			e.style.border = "1px dotted #ccc";
		}
		else {
			e.style.backgroundColor = "transparent";
			e.style.border = "";
		}
	}
}
function createSeasonSelecterForTyres() {
	if( seasonArray.length >= 1 ) return false;
	var ul = gebId("catalog");
	if( ul && ul.tagName == "UL" ) {
		var liAr = ul.getElementsByTagName("LI");
		for( var i in liAr )
			if( liAr[i].tagName == "LI" && liAr[i].childNodes.length >= 1 ) {
				var a = liAr[i].getElementsByTagName("A");
				a = a[0];
				if( a.title && seasonArray.toString().indexOf(a.title) == -1 )
					seasonArray[seasonArray.length] = a.title;
			}
	}
	if( seasonArray.length > 1 ) {
		tbody = document.createElement("tbody");
		var table = document.createElement("table");
		table.className = "seasonTyres";
		var tr = document.createElement("tr");
		var td = document.createElement("td");
		td.innerHTML = "Отобразить по сезону";
		tr.appendChild(td);
		td = document.createElement("td");
		td.className = "seasonActiv";
		td.innerHTML = "всё";
		td.style.fontWeight = "bold";
		td.onclick = realizeSeasonSelecterForTyres;
		td.onmouseover = seasonBackgroundChanger;
		td.onmouseout = seasonBackgroundChanger;
		cursorHand(td);
		tr.appendChild(td);
		for( var i in seasonArray ) {
			var td = document.createElement("td");
			td.innerHTML = seasonArray[i];
			td.onclick = realizeSeasonSelecterForTyres;
			cursorHand(td);
			tr.appendChild(td);
			td.onmouseover = seasonBackgroundChanger;
			td.onmouseout = seasonBackgroundChanger;
			td.style.fontWeight = "bold";
		}
		tbody.appendChild(tr);
		table.appendChild(tbody);
		ul.parentNode.insertBefore(table, ul);
		return realizeSeasonSelecterForTyres(false);
	}
}
function setImagePop() {
	var cat = gebId("catalog") || gebId("tyrePreview") || gebId("wheelPreview") || gebId("content").getElementsByTagName("form");
	if( cat ) {
		if( cat.length ) {
			var f = cat;
			cat = new Array();
			var l = f.length;
			for( var i = 0; i < l; i++ )
				if( f[i].tagName == "FORM" ) {
					var img = f[i].getElementsByTagName("img");
					for( var j = 0; j < img.length; j++ )
						if( img[j].parentNode.tagName == "A" && img[j].parentNode.className == "thmb" )
							cat[cat.length] = img[j];
				}
		}
		else if( cat.tagName && cat.tagName == "IMG" )
			cat = new Array(cat);
		else if( cat.tagName && cat.tagName == "UL" )
			cat = cat.getElementsByTagName("img");
		else
			return;
		if( cat && cat.length ) {
			var l = cat.length;
			for( var i = 0; i < l; i++ ) {
				cat[i].parentNode.onclick = showImage;
			}
		}
	}
}
if( location.pathname.match(/^\/?tyres\.cat\/[0-9]+_[a-z_0-9]+\/?$/) ) {
	functionsOnload[functionsOnload.length] = createSeasonSelecterForTyres;
	seasonArray = new Array();
	functionsOnload[functionsOnload.length] = setImagePop;
}
if( location.pathname.match(/^\/?tyres\.cat\/[0-9]+_[a-z_0-9]+\/[a-z0-9_]+\.html$/) || location.pathname.match(/^\/?wheels\.cat\/[a-z_0-9]+\/?$/) || location.pathname.match(/^\/?wheels\.cat\/[a-z_0-9]+\/[a-z_0-9]+\/[a-z0-9_]+\.html$/) || location.href.match(/(&id=search_tyres|&id=search_wheels|id=search_tyres&|id=search_wheels&)/) ) {
	functionsOnload[functionsOnload.length] = setImagePop;
}
function ifAvtoSizes() {
	if( location.href.indexOf("#avtosize") !== -1 && location.hash != "" ) {
		var hash = location.hash.replace("avtosize", "").replace("#", "");
		if( hash.indexOf("") !== -1 && hash.replace(".", "").match(/^[0-9]+$/) ) {
			hash = hash.split(".");
			if( !requestScript("/by_avto.cat/?id=.&script=1", 760, 500) )
				setTimeout('requestScript("/by_avto.cat/?id=" + ' + hash[0] + ' + "." + ' + hash[1] + ' + "&script=true", 0, 0)', 500);
		}
	}
}
functionsOnload[functionsOnload.length] = ifAvtoSizes;
function isEmptyRightBlock() {
	var obj = gebId("rightBlock");
	if( obj && obj.childNodes.length == 0 ) {
		obj.parentNode.removeChild(obj);
		gebId("content").style.marginRight = "20px";
	}
}
functionsOnload[functionsOnload.length] = isEmptyRightBlock;
function createAvtoSize() {
	if( window.event )
		window.event.returnValue = false;
	return requestScript("/by_avto.cat/?script=true", 760, 500);
}
/**
 * Убрать после продумывания безопасного метода показа подбора
 * 15.10.2008
 */
function activateByAvtoElement() {
	if( document.getElementById && document.getElementsByTagName ) {
		var leftBlock = gebId("leftBlock");
		if( leftBlock ) {
			var h2 = leftBlock.getElementsByTagName("h2");
			var l = h2.length;
			for( var i = 0; i < l; i++ )
				if( h2[i].innerHTML == "Подбор" ) {
					var ul = h2[i].nextSibling;
					while(!ul.tagName || ul.tagName != "UL") {
						if( !ul.nextSibling )
							return;
						ul = ul.nextSibling;
					}
					var li = document.createElement("li");
						li.onclick = createAvtoSize;
					var a = document.createElement("a");
						a.setAttribute("href", "#avtosize.");
						a.innerHTML = "По марке авто";
					li.appendChild(a);
					ul.appendChild(li);
				}
		}
	}
}
functionsOnload[functionsOnload.length] = activateByAvtoElement;

//end
window.onload = onloadFunctionStarter;
if(window.attachEvent) window.attachEvent("onload", onloadFunctionStarter);