var left_pos,	top_pos, el_focus, destination, destination_left, window_pos, posminus, anim_obj;

if ($.browser.webkit){
	anim_obj = "body"
} else {
	anim_obj = "html"
}



(function(){
var facebox_expr = /\bfacebox\b/, link_check_terms = /\bglossary-term\b/i, img_expr = /\.(gif|png|jpg|jpeg|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) || link_check_terms.test(el.href)){
			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}

		} 
	},

//Аудио mp3
{
		audio_mp3: function(element) {
				var link_check = /\.mp3$/i, href=element.getAttribute('href') || "";
				if (element.tagName.toUpperCase() == "A" && link_check.test(href)){
					return true;
				} else {return false}
		
		} 
	}
)
})()



//
// Всплывающие окошки
//


//Закрытие окошка
function facebox_close(close_time)
{
	var face_time = close_time || 1;
	 setTimeout(function(){
		 $("#facebox").animate({"opacity":0,"top":"-=200"}, 700, function(){
			$("#facebox_overlay").animate({"opacity":0}, function(){
			facebox_focus();
			$("#facebox, #facebox_overlay").remove();
			facebox_active = 1;
			});
		});
	}, face_time)
}


//Загрузка окошка

function facebox_load()
{

	$("#facebox, #facebox_overlay").remove();
	$("body").append('\
			<div id="facebox_overlay" onclick="facebox_close();">&nbsp;</div>\
			<div id="facebox">\
			<!--[if lte IE 7]><table><tr><td><![endif]--> \
				<div id="facebox_block_top"><div><div></div></div></div>\
					<div id="facebox_center">\
						<div>\
							<div id="facebox_content">\
								<div id="facebox_close">\
									<div id="facebox_title"></div>\
									<img src="/images/close.gif" alt="" title="Закрыть" onclick="facebox_close();">\
								</div>\
								<div id="facebox_content_i"></div>\
								<input type="button" value="&nbsp;" id="alert_ok" onclick="facebox_close();">\
							</div>\
						</div>\
					</div>\
					<div id="facebox_block_bottom"><div><div></div></div></div>\
			<!--[if lte IE 7]></td></tr></table><![endif]-->\
			</div>\
		');
	$("#facebox_overlay").animate({"opacity":0.3}, function(){
		$("#facebox").animate({"top":top_pos,"opacity":1}, 1000, "easeOutBack");
		$("#facebox_loading").remove();
	});
}

//Берем позицию у элемента, для фокуса и стрелки.
function get_position()
{
	destination = $(el_focus).offset().top;
	destination_left = $(el_focus).offset().left;
	window_pos = $(window).scrollTop();
	posminus = window_pos - destination;
}

var arrow_num = 0;
function arrow(counter, obj){
	if(!counter || counter == 0){
		counter = 0;
		arrow_num++;
		$("body").append('<div class="alert_arrow" id="alert_arrow'+arrow_num+'"></div>');
		obj = $("#alert_arrow"+arrow_num).css({"left":destination_left-40,"top":destination-5});
	}
	$(obj).animate({"left":"-=30"}, "linear", function(){
		$(this).animate({"left":"+=30"}, "linear", function(){
		 counter++;
		 if (counter < 5){
		 arrow(counter, obj);
		 } else{
			$(this).animate({"opacity":0}, "slow", function(){
				$(this).remove();
				});
			}
			return;
		});
	}); 
}

//Фокус
function facebox_focus(element_focus)
{
	if(element_focus){
	el_focus = element_focus
	}
	if(el_focus != null){
		get_position();
		if ((window_pos > destination) || ((window_pos < destination) && posminus < -400)){
				$(anim_obj).animate({scrollTop: destination-250}, 900, function(){
				$(el_focus).focus();
			});
		}else{
		$(el_focus).focus();
		}
		arrow();
	}
}

$.fn.facebox_focus = function(){
facebox_focus(this)
}


//Установка позиционирования окна
function facebox_position()
{
	left_pos = ($(window).width() - $("#facebox").width())/2,
	top_pos = ($(window).height() - $('#facebox').height()) / 2 - 50;
	$("#facebox").css({"left":left_pos, "top": top_pos-300});
}

function facebox_resize(){
	if($("#facebox").length > 0){
			left_pos = ($(window).width() - $("#facebox").width())/2,
			top_pos = ($(window).height() - $('#facebox').height()) / 2 - 50;
			$("#facebox").dequeue().animate({"left":left_pos, "top": top_pos}, "slow", "easeOutBack");
		}
}

//Красивый Алерт
function facebox_alert(el_text, end_check)
{
	if (end_check == "end" || end_check == null){
	el_focus = null
	} else {
	el_focus = end_check
	};
	facebox_load();
	$("#facebox").addClass("alert_mes");
	$("#facebox_content_i").html("<h3 class='alert'>"+el_text+"</h3>");
	facebox_position();
	window.setTimeout(function() {
		$("#alert_ok").focus();}, 1500);
}
//Загрузка Аяксом
$.fn.facebox = function()
{
	//Проверяем, что у элемента есть атрибут href и он хоть что-то содержит
		$(this).click(function(){
			el_focus = null;
			var url_load = $(this).attr("href");
			//Если контент находиться на странице href="#id"
			if ($(this).is("[href^='#'][href!='#']")){
				facebox_load();
				$("#facebox_content_i").html($(url_load).html());
				facebox_position();
			}//Если ссылка на страницу, грузим аяксом
			else {
			$.ajax({
				url: url_load,
				beforeSend: function(){$("body").append('<img id="facebox_loading" src="/images/lightbox_green/loading.gif" alt="">')},
				error:function(){
					facebox_alert("Ошибка, не удается загрузить страницу");
				},
				success: function(html){
					facebox_load();
					$("#facebox_content_i").html(html);
					facebox_position();
					}
				});
			}
			return false;
		})
}

$(function(){
	
	$(window).resize(function(){
		facebox_resize()
	})
	$(document).keydown(function(e){
		 if(e.keyCode == 27){
						facebox_close();
					}
	 });
	$("a:facebox").facebox();
	$("a:anchor").click(function(){
    el_focus = $(this).attr("href");
    if($(el_focus)[0]){
			get_position();
			$(anim_obj).animate({scrollTop: destination-100}, 1500 );
			arrow();
		}
    return false;
  });
	var search_form = document.getElementById("search")
	if(search_form){
		search_form.onfocus = function(){
			if(this.value == "Поиск")
			{this.value=""}
			}
		search_form.onblur = function(){
			if(this.value == "")
			{this.value="Поиск"}
			}
	}
	
	$("#searchform").submit(function(){
		if(search_form.value=="Поиск" || search_form.value == ""){
			facebox_alert("Необходимо заполнить поле для поиска", search_form)
			return false
		}
	})
	//mp3 проигрыватель
	if($.browser.flash){
		$("#content_inside").find("a:audio_mp3").each(function(){
		var mp3_url = encodeURIComponent(this.href);
		$(this).before("<embed src='/swf/own_flashplayer_plc.swf?file="+mp3_url+"' type='application/x-shockwave-flash' wmode='transparent' allowScriptAccess='sameDomain' class='mp3_embed'>")
		$(this).hide();
	 })
	}else if($.browser.webkit || ($.browser.msie && $.browser.version > 8)){
		$("#content_inside").find("a:audio_mp3").each(function(){
			var mp3_url = this.href;
			$(this).before("<audio src='"+mp3_url+"' controls='controls'></audio>");
			$(this).hide();
		 })
	}
	$("#hide_email").append("<a href='mailto:shailendra"+"@bk.ru'>Связаться с нами</a>")
});



if($.browser.msie && $.browser.version < 9){
		document.createElement('header');
		document.createElement('nav');
		document.createElement('section');
		document.createElement('article');
		document.createElement('aside');
		document.createElement('footer');
		document.createElement('time');
	}
