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

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

var 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.');
	}

	document.onmousemove = function (e) {
		e = e || event;

		if (loading) {
			loading.style.display = 'block';
			loading.style.top = (e.clientY + (document.documentElement.scrollTop || window.scrollY || document.body.scrollTop) - 18) + 'px';
			loading.style.left = (e.clientX + (document.documentElement.scrollLeft || window.scrollX || document.body.scrollLeft) - 22) + 'px';
		}
	};

	this.get = function (path, analyze, params, no_loading) {
		if (no_loading) {
			loading.style.display = 'none';
		}
		link.onreadystatechange = function () {
			if (link.readyState == 4 && link.status == 200) {
				if (loading) {
					loading.style.display = 'none';
				}
				document.onmousemove = null;
				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 initLoading = function () {
	window.loading = addChild(document.body, 'DIV');
	loading.id = 'ajax-loading';
	loading.style.display = 'none';
};

var addChild = function (parent, name, className) {
	var child = document.createElement(name);
	if (className) {
		child.className = className;
	}
	parent.appendChild(child);
	return child;
};

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

var updateTime = function () {
	(new request).get('/scripts/requests.php?action=names&lang=' + lang, function (req) {
		var node = document.getElementById('expired').getElementsByTagName('STRONG')[0];
		var hash = req.responseText.split('|||');
		var periods = hash[0].split('||');
		periods[0] = periods[0].split('|');
		periods[1] = periods[1].split('|');
		if (!isNaN(hash[1])) {
			node.parentNode.style.visibility = 'visible';
			var offset_def = (new Date()).getTimezoneOffset() * 60;
			setInterval(function () {
				var time          = Math.floor(new Date().getTime() / 1000) - offset_def;
				var remains       = parseInt(hash[1], 10) - time;
				var days          = Math.floor(remains / 86400).toString();
				var days_stamp    = days * 86400;
				var days_last     = days.substr(days.length-1);
				var hours         = Math.floor((remains - days_stamp) / 3600).toString();
				var hours_stamp   = hours * 3600;
				var hours_last    = hours.substr(hours.length-1);
				var minutes       = Math.floor((remains - (days_stamp + hours_stamp)) / 60).toString();
				var minutes_stamp = minutes * 60;
				var seconds       = remains - (days_stamp + hours_stamp + minutes_stamp);
				node.innerHTML = days + ' ' + (days_last == 1 ? periods[0][1] : (days_last > 1 && days_last < 5 ? periods[0][2] : periods[0][0])) + ' ' + hours + ' ' + (hours_last == 1 && hours != 11 ? periods[1][1] : (hours_last > 1 && hours_last < 5 && (hours < 11 && hours > 15) ? periods[1][2] : periods[1][0])) + ' ' + minutes + ' ' + periods[2] + ' ' + seconds + ' ' + periods[3];
			}, 1000);
		}
	});
};

var makeHints = function () {
	var links = document.getElementsByTagName('A');
	i = links.length;
	while (i-- > 0) {
		if (links[i].title && links[i].className.indexOf('hint') != -1) {
			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 ? 203 : 181;
				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 makeMaps = function () {
	var map = document.getElementById('map');
	if (map) {
		(new request).get('/scripts/requests.php?action=values&lang=' + lang, function (req) {
			window.values = req.responseText.split('|');
		});
		var imgs = ['s', 'b2', 'p'];
		var places = map.getElementsByTagName('SPAN');
		var i = places.length;
		while (i-- > 0) {
			if (places[i].className.indexOf('status-') == -1) {
				places[i].onclick = function () {
					var mode = 1;
					var pos = this.className.replace(/.*\-([xy2]+).*/, '$1');
					if (this.className.indexOf('added') == -1) {
						this.className += ' added-' + pos;
					} else {
						this.className = this.className.replace(/ added\-[xy2]+/, '');
						mode = 0;
					}
					(new request).get('/scripts/requests.php?action=places&place=' + this.id.replace('place', '') + '&mode=' + mode + '&lang=' + lang, function (req) {
						var cart = document.getElementById('cart');
						cart.innerHTML = '';

						if (req.responseText.indexOf('|') != -1) {
							var levels = req.responseText.split('|');
							var p, i = levels.length;
							while (i-- > 0) {
								if (levels[i]) {
									p = document.createElement('P');
									p.innerHTML = '<img src="/images/common/place_' + imgs[i] + '_x.png" width="15" height="13" alt="" /> ' + levels[i];
									cart.appendChild(p);
								}
							}
							p = document.createElement('P');
							p.innerHTML = values[0];
							cart.appendChild(p);
						} else {
							var p = document.createElement('P');
							p.innerHTML = req.responseText;
							cart.appendChild(p);
						}
					});
					(new request).get('/scripts/requests.php?action=places_all&lang=' + lang, function (req) {
						var cart = document.getElementById('cart-copy');
						cart.innerHTML = req.responseText;
					});
				};
			}

			places[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';
					}
				}
			};

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

var sendOrder = function (link) {
	var form = link.parentNode.parentNode.parentNode.parentNode;
	if (form.nodeName.toUpperCase() != 'FORM') {
		form = link.parentNode.parentNode.parentNode;
	}
	var errors = false;
	if (form.elements && form.elements['payment']) {
		var select = form.elements['payment'];
		if (select.options[select.selectedIndex].value == 3) {
			var total = parseFloat(document.getElementById('total').innerHTML);
			if (total > 18000) {
				alert('Сумма заказа не должна превышать 18000 грн при оплате карточкой Visa / Mastercard');
				errors = true;
			}
		}
	}
	if (!errors) {
		form.submit();
	}
	return false;
};

var changePrices = function (select) {
	var procent = select.options[select.selectedIndex].value;
	var tr = select.parentNode.parentNode;
	var table = tr.offsetParent;
	var price = tr.cells[3].lastChild.value;
	var sum = parseFloat(price) - (parseFloat(price) / 100 * procent);
	tr.cells[3].firstChild.innerHTML = sum.toFixed(2);

	var total, abs_total = 0;
	var tables = table.parentNode.getElementsByTagName('TABLE');
	var j, i = tables.length;
	while (i-- > 0) {
		total = 0;
		j = tables[i].rows.length;
		while (j-- > 0) {
			if (tables[i].rows[j].cells.length == 5) {
				total += parseFloat(tables[i].rows[j].cells[3].firstChild.innerHTML);
			}
		}

		procent, j = tables[i].rows.length;
		if (tables[i].rows[j-2].cells.length == 3) {
			procent = total / 100 * parseInt(tables[i].rows[j-2].cells[2].lastChild.value, 10);
			total -= procent;
			tables[i].rows[j-2].cells[2].firstChild.innerHTML = '-' + procent.toFixed(2);
		}
		tables[i].rows[j-1].cells[2].firstChild.innerHTML = total.toFixed(2);
		abs_total += total;
	}
	document.getElementById('total').innerHTML = abs_total.toFixed(2);
};

var setBlock = function (radio) {
	var current = radio.value;
	if (current == 0) {
		document.getElementById('part-0').style.display = 'block';
		document.getElementById('part-1').style.display = 'none';
	} else {
		document.getElementById('part-0').style.display = 'none';
		document.getElementById('part-1').style.display = 'block';
	}
	radio.blur();
};

var fixIEnav = function () {
	if (is_ie6) {
		var nav = document.getElementById('nav');
		var links = nav.getElementsByTagName('A');
		var i = links.length;
		while (i-- > 0) {
			links[i].onmouseover = function () {
				this.className += ' hover';
			};
			links[i].onmouseout = function () {
				this.className = this.className.replace(' hover', '');
			};
		}
	}
};

var setLang = function (select) {
	var lang = select.options[select.selectedIndex].value;
	window.location.href = '/' +lang + '/';
};

window.onload = function () {
	initLoading();
	updateTime();
	makeHints();
	makeMaps();
	fixIEnav();
};

