if (self.parent.frames.length != 0)
	self.parent.location.replace(document.location.href);

function em(box, server, zone) {
	var at = String.fromCharCode(64);
	document.write('<a href="'+'mai'+'lto'+':'+box+at+server+'.'+zone+'">'+box+at+server+'.'+zone+'<'+'/a>');
}

function popup_window(window_url, width, height) {
	var left = Math.round((screen.width-width)/2), top = Math.round((screen.height-height)/2)-30
	var window_param =	'ontop=yes,status=yes,menubar=no,toolbar=no,directories=no,resizable=yes,scrollbars=yes,location=no,'+
						'width='+width+',height='+height+','+
						'left='+left+',top='+top;
	var window_name = 'win_'+Math.floor(Math.random()*1000);
	var win = window.open(window_url, window_name, window_param);
	win.focus();
	return false;
}


/*** REQUECT STUFF ***********************************************************/
document.onclick = function(e) {
	var target, external;
	target = e ? e.target : window.event.srcElement;

	while (target && !/^(body|a)$/i.test(target.nodeName))
		target = target.parentNode;

	if (target && typeof(target.rel)!='undefined' && target.rel== 'external') {
		external = window.open(target.href);
		return external.closed;
	}

	return true;
}

function open_request_window() {
	return popup_window("./request.html?template_id=request", 750, 700);
}

function request_submit(form) {
	message_present = false;
	var controls = new Array("name", "company", "phone", "addr");
	reset_highlight(form, controls);
	return process_check(form, controls);
}

function request_any_submit(form) {
	message_present = false;
	var controls = new Array("name", "phone", "addr", "equipment");
	reset_highlight(form, controls);
	return process_check(form, controls);
}

function check_name(form) {
	return check_length(form, "name", 3, -1, "Представьтесь, пожалуйста!");
}

function check_company(form) {
	return check_length(form, "company", 3, -1, "Не указано название Вашей организации!");
}

function check_equipment(form) {
	return check_length(form, "equipment", 5, -1, "Укажите оборудование!");
}

function check_phone(form) {
	if (valid_email(form.addr.value))
		return true;
	return check_length(form, "phone", 5, -1, "Укажите контактный номер телефона или адрес электронной почты!");
}

function check_addr(form) {
	if (form.phone.value.length >= 5)
		return true;

	if (!valid_email(form.addr.value)) 	{
		if (!message_present) {
			alert("Некорректный адрес электронной почты");
			message_present = true;
		}
		highlight_control(form.addr, !message_present);
		return false;
	}
	return true;
}

function valid_email(addr) {
	if (addr.length==0)
		return false;
	return addr.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
}

/*** OVERLABELS **************************************************************/
function initOverLabels() {
	if (!document.getElementById)
		return;
	var labels, id, field;
	// Set focus and blur handlers to hide and show 
	// LABELs with 'overlabel' class names.
	labels = document.getElementsByTagName('label');
	for (var i=0; i<labels.length; i++) {
		if (labels[i].className == 'overlabel') {
			// Skip labels that do not have a named association
			// with another field.
			id = labels[i].htmlFor || labels[i].getAttribute('for');
			if (!id || !(field = document.getElementById(id)))
				continue;
 
			// Change the applied class to hover the label 
			// over the form field.
			labels[i].className = 'overlabel-apply';
 
			// Hide any fields having an initial value.
			if (field.value !== '')
				hideLabel(field.getAttribute('id'), true);
 
			// Set handlers to show and hide labels.
			field.onfocus = function() {
				hideLabel(this.getAttribute('id'), true);
			};
			field.onblur = function() {
				if (this.value === '')
					hideLabel(this.getAttribute('id'), false);
			};
 
			// Handle clicks to LABEL elements (for Safari).
			labels[i].onclick = function() {
				var id, field;
				id = this.getAttribute('for');
				if (id && (field = document.getElementById(id)))
					field.focus();
			};
		}
	}
}
 
function hideLabel(field_id, hide) {
	var field_for, labels = document.getElementsByTagName("label");
	for (var i=0; i<labels.length; i++) {
		field_for = labels[i].htmlFor || labels[i].getAttribute('for');
		if (field_for == field_id) {
			labels[i].style.textIndent = (hide) ? '-9999px' : '0px';
			return true;
		}
	}
}

function initTabsCatalogue() {
	$("ul.catalogue-tabs li a").bind("click", function(){
		var obj = $(this);
		obj.parent().parent().find("li").removeClass("selected");
		obj.parent().addClass("selected");
		var alias = obj.attr("href");
		var res = /\?tab=(.*)/.exec(obj.attr("href"));
		alias = res ? res[1] : '';

		var tabs = $("div#catalogue-tabs");
		tabs.find("div.catalogue-tab").removeClass("catalogue-tab-selected");
		tabs.find("div#catalogue-tab-"+alias).addClass("catalogue-tab-selected");

		obj.parent().parent().find("li").removeClass("selected");
		obj.parent().addClass("selected");
      	return false;
	});
}

function initHomepageCatalogue() {
	$("#catalogue_folders_list h2 a").bind("click", function(){
		var obj = $(this).parent().parent().parent().toggleClass("selected").find("ul");
		if (!obj.length)
	      	return true;
		obj.slideToggle("fast");
      	return false;
	});
}

function closePopupCatalogue() {
	$("#popupshadow").remove();
}

function initPopupCatalogue() {
	$("#popup-catalogue-a").bind("click", function(){
		var rel_list = $(this).attr("rel").split("|");
		$.get('/ajax/catalogue/', {folder_id:rel_list[0], folder_url:rel_list[1], folder_name:rel_list[2]}, function(data){
			var res = /\/\/[^\/]+(\/.*)/.exec(window.location.href);
			var current_uri = res[1];
			if (data.length > 0) {
				var pair, list, folder_started, popup_html;

				folder_started = null;
				popup_html = "";
				list = data.split("\n");

				for (var i=0, il=list.length; i<il; i++) {
					pair = list[i].split("|");
					if (pair.length<2)
						continue;
					if (pair[0].length==0) {
						if (folder_started==null) {
							popup_html += '<ul class="columns">';
							folder_started = '';
						}
						var url = rel_list[1]+(folder_started.length?folder_started+'/':'')+pair[1]+"/";
						popup_html += '<li'+(current_uri.substr(0, url.length)==url?' class="selected"':'')+'><a href="'+url+'">'+pair[2]+'</a></li>';
					} else {
						if (folder_started!=null) {
							popup_html += '</ul>';
							folder_started = null;
						}
						var url = rel_list[1]+pair[0]+"/";
						popup_html += '<h3'+(current_uri.substr(0, url.length)==url?' class="selected"':'')+'><a href="'+rel_list[1]+pair[0]+'/">'+pair[1]+'</a></h3>';
						if (!folder_started) {
							popup_html += '<ul class="columns">';
							folder_started = pair[0];
						}
					}
				}
				if (folder_started!=null)
					popup_html += '</ul>';
				popup_html =
					'<div id="popupshadow" style="left:10%; top:250px"><div class="conWrp">'+
						'<div class="title"><h2><a href="'+rel_list[1]+'">'+rel_list[2]+'</a></h2><div class="close"><a href="javascript:void()" onclick="javascript:closePopupCatalogue()"><img src="/i/close.gif" width="20" height="20" alt="Закрыть"></div></div>'+
						popup_html +
					'</div><span class="shadowBox"><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i></span></div>';
			    $("body").append($(popup_html));
			}
		}, "text");
		return false;
	});
}

$(document).ready(function() {
	initOverLabels();
	initHomepageCatalogue();
	initPopupCatalogue();
	initTabsCatalogue();
});

