/*****************************   anDeRan   ************************************/

var is_ie  = !!document.uniqueID;
var is_ie6 = is_ie && navigator.appVersion.match(/MSIE [56]/) && !navigator.appVersion.match(/MSIE [789]/);

var s_interval, sel_int, lang = document.getElementsByTagName('HTML')[0].lang;

if (is_ie6) {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}

if (is_ie) {
	var XMLHttpRequest = function () {
		return (new ActiveXObject("Msxml2.XMLHTTP")) || (new ActiveXObject("Microsoft.XMLHTTP")) || false;
	};
}

var request = function () {
	try {
		var link = new XMLHttpRequest();
	} catch (e) {
		alert('Your browser doesn\'t support XMLHttpRequest. Please enable XMLHTTPRequest if disabled or update if your browser is old.');
	}

	this.get = function (path, analyze, params) {
		link.onreadystatechange = function () {
			if (link.readyState == 4 && link.status == 200) {
				if (analyze) {
					analyze(link, params);
				}
				
			}
		};
		link.open('GET', path, true);

		if (is_ie6) {
			link.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
		}
		link.send('');
	};
};

var preloadImgs = function () {
	var i = arguments.length;
	while (i-- > 0) {
		(new Image()).src = '/images/' + arguments[i];
	}
};

var scrollTo = function (scrollPos) {
	var startLeft = parseInt(scroll.style.left, 10) || 0;
	var endPos = startLeft + scrollPos;

	var __step = scrollPos;

	currPos = startLeft;

	clearInterval(interval);
	window.interval = setInterval(function () {
		//__step = Math.ceil(scrollPos * (Math.cos(__step / 150) / 15));
		__step /= 2;

		if (scrollPos < 0 && __step > -3) {
			__step = -1;
		}
		if (scrollPos > 0 && __step < 10) {
			__step = 1;
		}

		currPos += Math.round(__step);

		if (scrollPos > 0 && currPos > endPos || scrollPos < 0 && currPos < endPos) {
			currPos = endPos;
			clearInterval(interval);
		}

		if (currPos < 0) {
			currPos = 0;
			clearInterval(interval);
		}

		if (currPos > 559) {
			currPos = 559;
			clearInterval(interval);
		}
		container.style.left = -(Math.round(currPos * scroll_step)) + 'px';
		scroll.style.left = currPos + 'px';
	}, 30);
};

var getScroll = function () {
	var elements = container.getElementsByTagName('P');
	if (is_ie6) {
		var i = elements.length;
		while (i-- > 0) {
			if (elements[i].firstChild.firstChild) {
				elements[i].firstChild.firstChild.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + elements[i].firstChild.firstChild.src + "', sizingMethod='crop')";
				elements[i].firstChild.firstChild.src = '/images/common/blank.png';
			}
		}
	}
	window.scroll_width = 160 * elements.length - 703;
	window.scroll_step = scroll_width / 559;
	collapseProducts('show', elements.length);
};

var makeScroll = function () {
	var div;
	window.products = document.getElementById('products');
	window.scrollbar = document.getElementById('scrollbar');
	window.container = document.getElementById('container');
	window.arr_int = null;

	window.scrollbar = document.createElement('DIV');
	scrollbar.id = 'scrollbar';
	products.parentNode.appendChild(scrollbar);

	window.container = document.createElement('DIV');
	container.innerHTML = products.innerHTML;
	products.innerHTML = '';
	container.id = 'container';
	products.appendChild(container);

	div = document.createElement('DIV');
	//div.className = 'shadow-left';
	div.innerHTML = '<img src="/images/common/shadow_left.png" width="40" height="160" alt="" />';
	if (is_ie6) {
		div.firstChild.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + div.firstChild.src + "', sizingMethod='crop')";
		div.firstChild.src = '/images/common/blank.png';
	}
	div.style.cssText = 'position: absolute; left: 0;';
	products.appendChild(div);
	div = null;
	div = document.createElement('DIV');
	div.innerHTML = '<img src="/images/common/shadow_right.png" width="40" height="160" alt="" />';
	if (is_ie6) {
		div.firstChild.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + div.firstChild.src + "', sizingMethod='crop')";
		div.firstChild.src = '/images/common/blank.png';
	}
	div.style.cssText = 'position: absolute; left: 663px;';
	//div.className = 'shadow-right';
	products.appendChild(div);
	div = null;

	scrollbar.onmousedown = function (e) {
		var e = e || event;
		var obj = e.srcElement || e.target;
		if (obj.id && obj.id == 'scrollbar') {
			var offsetLeft = document.getElementById('header').offsetLeft + 85;
			var startLeft = parseInt(scroll.style.left, 10) || 0;
			var pos = e.clientX - offsetLeft - startLeft;

			if (startLeft > offsetLeft) {
				scrollTo(pos);
			} else {
				scrollTo(pos);
			}
		}
	};

	window.arr_left = document.createElement('DIV');
	arr_left.id = 'arrow-left';
	arr_left.onmouseover = function () {
		this.style.backgroundPosition = '0 0';
	};
	arr_left.onmouseout = function () {
		this.removeAttribute('style');
		clearInterval(arr_int);
	};
	arr_left.onmousedown = function () {
		var startLeft = parseInt(scroll.style.left, 10) || 0;
		var currPos = startLeft;

		var total = container.childNodes.length;
		clearInterval(interval);
		clearInterval(arr_int);
		arr_int = setInterval(function () {
			currPos -= Math.round(scroll_step + (400 / total));

			if (currPos < 0) {
				currPos = 0;
				clearInterval(arr_int);
			}

			container.style.left = -(Math.round(currPos * scroll_step)) + 'px';
			scroll.style.left = currPos + 'px';
		}, 50);
	};
	arr_left.onmouseup = function () {
		clearInterval(arr_int);
	};
	scrollbar.appendChild(arr_left);

	window.arr_right = document.createElement('DIV');
	arr_right.id = 'arrow-right';
	arr_right.onmouseover = function () {
		this.style.backgroundPosition = '0 0';
	};
	arr_right.onmouseout = function () {
		this.removeAttribute('style');
		clearInterval(arr_int);
	};
	arr_right.onmousedown = function () {
		var startLeft = parseInt(scroll.style.left, 10) || 0;
		var currPos = startLeft;

		var total = container.childNodes.length;
		clearInterval(interval);
		clearInterval(arr_int);
		arr_int = setInterval(function () {
			currPos += Math.round(scroll_step + (400 / total));

			if (currPos > 559) {
				currPos = 559;
				clearInterval(arr_int);
			}
			container.style.left = -(Math.round(currPos * scroll_step)) + 'px';
			scroll.style.left = currPos + 'px';
		}, 50);
	};
	arr_right.onmouseup = function () {
		clearInterval(arr_int);
	};
	scrollbar.appendChild(arr_right);

	window.interval = null;
	window.drag = false;
	window.scroll = document.createElement('DIV');
	scroll.id = 'scroll';
	scroll.innerHTML = '<span></span>';
	scrollbar.appendChild(scroll);
	scroll.onmouseover = scroll.onmousemove = function () {
		if (!this.firstChild.style.backgroundPosition) {
			this.firstChild.style.backgroundPosition = '100px 0';
		}
	};
	scroll.onmouseout = function () {
		if (!drag) {
			this.firstChild.removeAttribute('style');
		}
	};
	scroll.onmousedown = function (e) {
		var e = e || event;
		var startX = e.clientX;
		var startLeft = parseInt(scroll.style.left, 10) || 0;

		drag = true;
		clearInterval(interval);

		if (is_ie) {
			document.onselectstart = function () { return false; };
		}
		document.body.unselectable = "on";
		document.body.style.WebkitUserSelect = "none";
		document.body.style.MozUserSelect = "none";
		products.parentNode.style.WebkitUserSelect = "none";
		products.parentNode.style.MozUserSelect = "none";

		document.onmousemove = function (e) {
			if (drag) {
				var e = e || event;
				var currPos = e.clientX - startX + startLeft;

				if (currPos < 0) {
					currPos = 0;
				}

				if (currPos > 559) {
					currPos = 559;
				}
				container.style.left = -(Math.round(currPos * scroll_step)) + 'px';
				scroll.style.left = currPos + 'px';
			}
		};
	};

	document.onmouseup  = function (e) {
		var e = e || event;
		var obj = e.srcElement || e.target;
		drag = false;
		if (obj != scroll.firstChild) {
			scroll.firstChild.removeAttribute('style');
		}
		document.onmousemove = null;
		if (is_ie) {
			document.onselectstart = null;
		}
		document.body.unselectable = "";
		document.body.style.WebkitUserSelect = "";
		document.body.style.MozUserSelect = "";
		products.parentNode.style.WebkitUserSelect = "";
		products.parentNode.style.MozUserSelect = "";
	};

	getScroll();
};

var makeHints = function () {
	var content = document.getElementById('content');
	var divs = content.getElementsByTagName('DIV');
	var col, i = divs.length;
	while (i-- > 0) {
		if (divs[i].className && divs[i].className.indexOf('col-right') != -1) {
			col = divs[i];
			break;
		}
	}
	if (col) {
		var links = col.getElementsByTagName('A');
		i = links.length;
		while (i-- > 0) {
			if (links[i].title) {
				links[i].onmouseover = function () {
					var id = 'hint';
					var div = document.getElementById(id);
					if (!div) {
						div = document.createElement('DIV');
						div.id = id;
						div.className = 'hint-block';
						div.style.visibility = 'hidden';
						document.body.appendChild(div);
					}
					div.innerHTML = this.title;
					this.title = '';
					height = div.offsetHeight * 2;
					div.style.width = 'auto';
					var max = is_ie6 ? 183 : 161;
					if (div.offsetWidth > max) {
						div.style.width = max + 'px';
					}

					this.onmousemove = function (e) {
						e = e || event;
						var id = 'hint';
						var byTop, div = document.getElementById(id);
						if (div) {
							div.style.visibility = 'visible';

							div.style.top = e.clientY + (document.documentElement.scrollTop || window.scrollY || document.body.scrollTop) + 20 + 'px';
							div.style.left = e.clientX + (document.documentElement.scrollLeft || window.scrollX || document.body.scrollLeft) + 'px';
						}
					}
				};

				links[i].onmouseout = function () {
					var id = 'hint';
					var div = document.getElementById(id);
					if (div) {
						this.title = div.innerHTML;
						div.style.visibility = 'hidden';
					}
				};
			}
		}
	}
};

var makeSelects = function () {
	var uls = document.getElementsByTagName('UL');
	var i = uls.length;
	while (i-- > 0) {
		if (uls[i].className && uls[i].className.indexOf('select') != -1) {
			uls[i].onclick = function (e) {
				clearInterval(sel_int);
				if (!this.style.height || this.style.height == '18px') {
//					this.style.overflow = 'visible';
					this.style.zIndex = '1000';
					var max = this.scrollHeight;
					var ul = this;
					var height = this.offsetHeight;
					var step = Math.round(max / 6);
					sel_int = setInterval(function () {
						height += step;
						if (height > max) {
							clearInterval(sel_int);
							height = max;
						}
						ul.style.height = height + 'px';
					}, 50);
					if (is_ie) {
						document.getElementById('crumbs').style.zIndex = -2;
					}
				} else {
//					this.style.overflow = 'hidden';
					this.style.height = '18px';
					this.style.zIndex = '1';
					if (is_ie) {
						document.getElementById('crumbs').style.zIndex = 1;
					}
				}
			};

			var input = document.createElement('INPUT');
			input.type = 'hidden';
			input.name = uls[i].className.replace(/.+ ([a-z_]+)$/, '$1');
			input.id = input.name;
			input.value = '';
			uls[i].appendChild(input);

			var lists = uls[i].getElementsByTagName('LI');
			var j = lists.length;
			while (j-- > 0) {
				if (j) {
					lists[j].onmouseover = function () {
						this.style.backgroundColor = '#EEE';
						this.style.color = '#92A3B5';
					};
					lists[j].onmouseout = function () {
						this.removeAttribute('style');
					};
					lists[j].onclick = function () {
						var value, lists = this.parentNode.getElementsByTagName('LI');
						lists[0].innerHTML = this.innerHTML;
						if (this.className && this.className.indexOf('value-') != -1) {
							value = this.className.substr(6);
						} else {
							value = this.innerHTML;
						}
						if (this.parentNode.lastChild.nodeName == 'INPUT') {
							lists[0].style.color = '#92A3B5';
							this.parentNode.lastChild.value = value;
						}
					};
				} else {
					if (lists[j].className && lists[j].className.indexOf('value-') != -1) {
						lists[j].style.color = '#92A3B5';
						input.value = lists[j].className.replace(/.+value-([0-9\-]+)$/, '$1');
					}
				}
			}
		}
	}
	document.onclick = function (e) {
		var e = e || event;
		var obj = e.srcElement || e.target;
		var parent;
		if (obj.nodeName == 'LI' && obj.parentNode.className && obj.parentNode.className.indexOf('select') != -1) {
			parent = obj.parentNode;
		}

		var uls = document.getElementsByTagName('UL');
		var i = uls.length;
		while (i-- > 0) {
			if (uls[i].className && uls[i].className.indexOf('select') != -1 && parent != uls[i]) {
				uls[i].style.height = '18px';
				uls[i].style.zIndex = '1';
			}
		}
	};
};

var setOpacity = function (obj, num) {
	if (is_ie6) {
		if (num == 1) {
			obj.style.filter = '';
		} else {
			obj.style.filter = 'alpha(opacity=' + (num * 100) + ')';
		}
	} else {
		obj.style.opacity = num;
	}
};

var collapseProducts = function (type, total) {
	clearInterval(s_interval);
	var opacity = 0.5, step = 0.1;
	setOpacity(products, opacity);
	setOpacity(scrollbar, opacity);

	if (type == 'hide') {
		s_interval = setInterval(function () {
			opacity -= step;
			if (opacity < 0) {
				products.style.visibility = 'hidden';
				scrollbar.style.visibility = 'hidden';
				document.getElementById('progress').style.visibility = 'visible';
				clearInterval(s_interval);
			} else {
				setOpacity(products, opacity);
				setOpacity(scrollbar, opacity);
			}
		}, 30);
	} else {
		container.removeAttribute('style');
		scroll.removeAttribute('style');
		document.getElementById('progress').style.visibility = 'hidden';
		products.style.visibility = 'visible';
		if (total > 4) {
			scrollbar.style.visibility = 'visible';
		} else {
			container.style.paddingLeft = (4 - total) * 160 / 2 + 42 + 'px';
			scrollbar.style.visibility = 'hidden';
		}
		s_interval = setInterval(function () {
			opacity += step;
			if (opacity > 1) {
				setOpacity(products, 1);
				setOpacity(scrollbar, 1);
				clearInterval(s_interval);
			} else {
				setOpacity(products, opacity);
				setOpacity(scrollbar, opacity);
			}
		}, 30);
	}
};

var filterProducts = function () {
	this.blur();

	var by_type  = document.getElementById('by_type').value;
	var by_usage = document.getElementById('by_usage').value;

//	if (by_type && by_usage) {
		collapseProducts('hide');

		(new request).get('/scripts/requests.php?action=get_products&by_type=' + by_type + '&by_usage=' + by_usage + '&lang=' + lang, function (req) {
			container.innerHTML = req.responseText;
			getScroll();
		});
//	}
};

var makeDefaults = function () {
	var inputs = document.getElementsByTagName('INPUT');
	var i = inputs.length;
	while (i-- > 0) {
		if (inputs[i].type == 'text' && inputs[i].value && inputs[i].className && inputs[i].className.indexOf('defined') != -1) {
			inputs[i].onfocus = function () {
				if (this.value == this.defaultValue) {
					this.value = '';
				}
			};
			inputs[i].onblur = function () {
				if (!this.value) {
					this.value = this.defaultValue;
				}
			};
		}
	}
};

var initCurrent = function () {
	var num = parseInt(document.location.href.replace(/.+\/products\/(\d+)/, '$1'), 10);
	if (!isNaN(num)) {
		var childs = products.getElementsByTagName('P');
		var total  = childs.length;

		for (var i = 0; i < total; i++) {
			if (childs[i].firstChild.href.indexOf('/' + num + '/') != -1) {
				childs[i].className = 'active';
				if (i < 2) {
					var num = 0;
				} else if (i > total - 3) {
					var num = scroll_width;
				} else {
					var num = (i - 2) * 160 + 35;
				}
				container.style.left = -num + 'px';
				scroll.style.left = Math.abs(num * (559 / scroll_width)) + 'px';
			}
		}
	}
};

window.onload = function () {
	makeScroll();
	initCurrent();
	makeHints();
	makeSelects();
	preloadImgs('nav/' + lang + '/company_over.png', 'nav/' + lang + '/catalog_over.png', 'nav/' + lang + '/services_over.png', 'nav/' + lang + '/propositions_over.png', 'nav/' + lang + '/downloads_over.png', 'nav/' + lang + '/contacts_over.png');
	makeDefaults();

	var refresh = document.getElementById('refresh');
	refresh.onclick = filterProducts;

	/*var ask = document.getElementById('ask');
	if (ask) {
		setTimeout(function () {
			ask.firstChild.onclick();
		}, 15000);
	}*/
};

