supportAttr = function(el,attr) {
	if(attr in document.createElement(el))return true;return false
};

stopBubble = function(oEvent){
	if(oEvent && oEvent.stopPropagation){
		oEvent.stopPropagation()// для DOM-совместимых браузеров
	}
	else
	{
		window.event.cancelBubble = true;//для IE
	}
}

if(document.getElementsByClassName == undefined) { 
   document.getElementsByClassName = function(cl) { 
      var retnode = [],
					myclass = new RegExp('\\b'+cl+'\\b'),
					elem = this.getElementsByTagName('*'); 
      for (var i = 0; i < elem.length; i++) { 
         var classes = elem[i].className; 
         if (myclass.test(classes)) { 
            retnode.push(elem[i]); 
         } 
      } 
      return retnode; 
   } 
};

var body_tag;


(function(){
for(var i=0,ie_elem=["header","footer","nav","section","article","aside","time"], ie_elem_length=ie_elem.length;i<ie_elem_length;i++){
		document.createElement(ie_elem[i])
}

var facebox_expr = /\bfacebox\b/, img_expr = /\.(gif|png|jpe?g|bmp)$/i, local_url = location.href.replace(location.hash,"");
$.extend($.expr[':'],
{
	facebox: function(el) {
			var href = el.getAttribute("href") || ""
			if (facebox_expr.test(el.className) && href.length > 0){
			return true
			} else {
				return false;
			}
	}
},
 {
	anchor: function(el) {
		var href=el.href
		if(href.indexOf("#") < 0)return false
		href = href.replace(local_url,"")
		if(href.length > 1 && href.charAt(0)== "#"){
			return true
		}
		return false
		}
	}, 

{
		image_link: function(el) {
			var href=el.getAttribute('href') || "";
			if (img_expr.test(href)){
					return true
			} else {return false}

		} 
	}
)
})()

//Раскраска для таблицы с регионами и датами
function regions_table_colors(table){
	//Если id таблицы не задан, то берем дефолтную таблицу
	if(!table)return
	var tbody = table.getElementsByTagName("tbody") || [table], tbody_length = tbody.length;
	for(var i=0;i<tbody_length;i++){
		var tr = tbody[i].getElementsByTagName("tr"), tr_length = tr.length
		//Если tbody в таблице не один, то красим четырмя цветами
		if(tbody_length>1){
			if(i%2!=0)tbody[i].className = "regions_grey";
			for(var j=0;j<tr_length;j++)
				if(j%2!=0)tr[j].className="dark"
		} else {
			for(var j=0;j<tr_length;j++)
				if(j%2!=0)tr[j].className="regions_grey"
		}
	}
}




function show_tips(el, id){
		tips_mes = document.getElementById(id+"_mes")
		if(tips_mes){
			$(tips_mes).stop().fadeOut("fast", function(){
				$(this).remove()
			})
			return
		}
		if(tip_block = document.getElementById(id)){
			var elPos = $(el).offset(), id_mes = id+"_mes"
			with(obj = document.createElement("div")){
				id=id_mes
				style.left = elPos.left+20+"px"
				style.top = elPos.top+"px"
				innerHTML = tip_block.innerHTML
				className="tips_mes"
				onclick = function(event){
					stopBubble(event)
				}
			}
			body_tag.appendChild(obj)
			$(obj).fadeIn("fast")
		}
	}


$(function(){
	body_tag = document.body;
	//Всплывающие подсказки
		$("#application_form_block").find(".app_form_tip[data-tips]").click(function(event){
			stopBubble(event)
			show_tips(this, this.getAttribute("data-tips"))
		});
	$(document).click(function(){
		$(".tips_mes").fadeOut("fast", function(){
			$(this).remove()
		})
	})
	;(function(cont, animEl){
		if((cont = document.getElementById("welcome_anim")) && (animEl = cont.getElementsByTagName("div"))){
			setInterval(function(){
				$(animEl[0]).css({"opacity":0}).animate({"opacity":1}, 2000)
				cont.appendChild(animEl[0])
			},7000)
		}
	})();

	//Слайдеры заголовки
	$("#content_layout").find(".slider_title").click(function(){
		$(this).toggleClass("slider_up").parent().next("div").toggle("slow")
		return false
	})
	var inside_container =  document.getElementById("inside_container")
	if(inside_container){
		inside_container.style.minHeight = document.getElementById("right_block").offsetHeight-40+"px"
	}
	$("#content_text :submit").mousedown(function(){
		this.className+=" push"}
		).on('click mouseleave', function(){
		this.className = this.className.replace(" push","")
		this.blur()
		})
	$("#feedback_form").submit(function(){
		if ($("#feedback_surname").val() == ""){
			alert("Введите имя");
			$("#feedback_surname").focus();
			return false;
			} else 
			if ($("#feedback_email").val() == ""){
			alert("Введите e-mail");
			$("#feedback_email").focus();
			return false;}
			 else if ($("#feedback_subj").val() == ""){
			alert("Введите сообщение");
			$("#feedback_subj").focus();
			return false;
		};
	});
	if(!supportAttr("input","placeholder")){
		$("input,textarea").each(function(){
			var placeholder = this.getAttribute("placeholder")
			if(placeholder){
				if(this.value == "")$(this).val(placeholder).addClass("placeholder")
				$(this).focus(function(){
					if(this.value == placeholder)$(this).val("").removeClass("placeholder")
				}).blur(function(){
					if(this.value == ""){
						this.value=placeholder
						this.className+=" placeholder"
					}
				})
			}
		})
	}

	for(var i=0, table=document.getElementsByClassName("color_table");i<table.length;i++){
		regions_table_colors(table[i])
	}
	$(".link_blank").click(function(){
	return !window.open(this.href);
	});
});
