var MenuAct;  // ID активной закладки
var SkinsAct;
var noAct = 0;
var url;
var linkaGo;

$(document).ready(function () {
	// Страница
	if(linkaGo == 1) {
		MenuAct = linkaHost;
	} else {
		thispage=document.location.hash;
		pages=thispage.substring(thispage.lastIndexOf("#") + 1,thispage.length);

		var n = pages;
		if(n == 'about') n = 0;
		else if(n == 'news') n = 1;
		else if(n == 'work') n = 2;
		else if(n == 'miscellaneous') n = 3;
		else if(n == 'www') n = 4;
		else if(n == 'contact') n = 5;
		
		if(pages != thispage) MenuAct = n;
		if(pages == thispage) MenuAct = 0;
	}

	// skin
	if($.cookie("skins")) {
		SkinsAct = $.cookie("skins");
	} else {
		SkinsAct = 1;
	}
	$(".main").css("background", "url(/_img/fon"+SkinsAct+".jpg)");
	
	// Стартуем
	$("nav ul li b em:eq("+MenuAct+")").addClass("act");
	$("header ul.skins").hide();
	
	// ПЕЧАТАЕМ КОД СТРАНИЦЫ :)) (ТИПА ПРЕЛОАДЕР)
	$('<div id="download"><p></p></div>').prependTo("body");
	var letters = {1:'&lt;', 2:'!', 3:'D', 4:'O', 5:'C', 6:'T', 7:'Y', 8:'P', 9:'E', 10:'&nbsp;', 11:'h', 12:'t', 13:'m', 14:'l', 15:'&gt;', 16:'&nbsp;', 17:'<br/>', 18:'&lt;', 19:'h', 20:'t', 21:'m', 22:'l', 23:'&gt;', 24:'&nbsp;', 25:'<br/>', 26:'&lt;', 27:'h', 28:'e', 29:'a', 30:'d', 31:'&gt;', 32:'&nbsp;', 33:'<br/>', 34:'&lt;', 35:'t', 36:'i', 37:'t', 38:'l', 39:'e', 40:'&gt;', 41:'Ш', 42:'У', 43:'С', 44:'Т', 45:'Р', 46:'О', 47:'В', 48:'&nbsp;', 49:'А', 50:'Н', 51:'Д', 52:'Р', 53:'Е', 54:'Й', 55:'&nbsp;', 56:'-', 57:'&nbsp;', 58:'П', 59:'О', 60:'Р', 61:'Т', 62:'Ф', 63:'О', 64:'Л', 65:'И', 66:'О', 67:'&lt;', 68:'/', 69:'t', 70:'i', 71:'t', 72:'l', 73:'e', 74:'&gt;', 75:'<br/>', 76:'&lt;', 77:'m', 78:'e', 79:'t', 80:'a', 81:'&nbsp;', 82:'c', 83:'h', 84:'a', 85:'r', 86:'s', 87:'e', 88:'t', 89:'=', 90:'"', 91:'w', 92:'i', 93:'n', 94:'d', 95:'o', 96:'w', 97:'s', 98:'-', 99:'1', 100:'2', 101:'5', 102:'1', 103:'"', 104:'&nbsp;', 105:'/', 106:'&gt;', 107:'&nbsp;', 108:'<br/>', 109:'&lt;', 110:'m', 111:'e', 112:'t', 113:'a', 114:'&nbsp;', 115:'n', 116:'a', 117:'m', 118:'e', 119:'=', 120:'"', 121:'d', 122:'e', 123:'s', 124:'c', 125:'r', 126:'i', 127:'p', 128:'t', 129:'i', 130:'o', 131:'n', 132:'"', 133:'&nbsp;', 134:'c', 135:'o', 136:'n', 137:'t', 138:'e', 139:'n', 140:'t', 141:'=', 142:'"', 143:'"', 144:'&nbsp;', 145:'/', 146:'&gt;', 147:'&nbsp;', 148:'<br/>', 149:'&lt;', 150:'l', 151:'i', 152:'n', 153:'k', 154:'&nbsp;', 155:'r', 156:'e', 157:'l', 158:'=', 159:'"', 160:'i', 161:'c', 162:'o', 163:'n', 164:'"', 165:'&nbsp;', 166:'h', 167:'r', 168:'e', 169:'f', 170:'=', 171:'"', 172:'/', 173:'f', 174:'a', 175:'v', 176:'i', 177:'c', 178:'o', 179:'n', 180:'.', 181:'i', 182:'c', 183:'o', 184:'&nbsp;', 185:'/'};
	var men = 30;
	setTimeout(function() {
		for (i=1; i<186; i++) {
			men = men + 30;
			$("#download p").append('<span id="t'+i+'">'+letters[i]+'</span>');
			$("#t"+i).animate({opacity: "hide"}, 0).delay(men).animate({opacity: "show"}, 50);
		}
	}, 500);
	$.preloadImages(['http://www.lonwan.ru/_img/fon'+SkinsAct+'.jpg','http://www.lonwan.ru/_img/img.gif','http://www.lonwan.ru/_img/fastener.png','http://www.lonwan.ru/_img/handle.png','http://www.lonwan.ru/_img/menu.png','http://www.lonwan.ru/_img/fon.png','http://www.lonwan.ru/_img/ramka.png','http://www.lonwan.ru/_img/link.gif','http://www.lonwan.ru/_img/link2.gif'], function () {
		setTimeout(function() {
			ajax(MenuAct);
			$("nav ul li b em").css("left", "-165px");
			$("nav ul li b em.act").css("left", "-55px");
			$("#download, noscript").remove();
			$(".main").show().animate({opacity: "hide"}, 0).animate({opacity: "show"}, 1000);
			$('<div id="download"></div>').prependTo("body");
			CenterBox();
			setTimeout(function() {
			
				$(".handle").rotate({
					duration:1500,
					angle:360, 
					animateTo:0
				});
				
				AnimeMenu();
				
				setTimeout(function() {
					mydetect();
					
					$.preloadImages(['http://www.lonwan.ru/_img/fon1.jpg','http://www.lonwan.ru/_img/fon2.jpg','http://www.lonwan.ru/_img/fon3.jpg','http://www.lonwan.ru/_img/fon4.jpg'], function () {
						setTimeout(function() {
							$("header ul.skins").animate({opacity: "show"}, 1000);
							$("header ul.skins li:eq("+(SkinsAct - 1)+")").css("border", "1px #FFF solid");
						}, 2000);
					});
					
				}, 1500);
			}, 1500);
		}, 3000);
	});

	// НАВЕДЕНИЕ МЫШКИ НА ЗАКЛАДКУ
	var menuClick=$("nav ul li b em");
	menuClick.hover(function() {
		if(noAct == 0) {
			if($(this).hasClass("act"))
				$(this).stop().animate({left:"0"}, 200);
			else
				$(this).stop().animate({left:"-115"}, 200);
		}
	},
	function() {
		if(noAct == 0) {
			if($(this).hasClass("act"))
				$(this).stop().animate({left:"-15px"}, 200);
			else
				$(this).stop().animate({left:"-130px"}, 200);
		}
	});

	// КЛИК ПО ЗАКЛАДКЕ МЕНЮ
	menuClick.click( function(e) {
		MenuAct = menuClick.index(e.target); // ID активной закладки
		if($(this).hasClass("act"))
			$(this).stop().animate({left:"-15px"}, 200).animate({left:"0"}, 200);
		else 
			ActMenu(MenuAct);
	});
	
	// КЛИК ПО ССЫЛКАМ НА САЙТЕ (class="link")
	$(".link").live("click", function(e) {
		link = $(this).attr("href");
		dop_link = $(this).attr("id");
		
		var nym;
		var n = link;
		if(n == 'about' || n == 0) nym = 0;
		else if(n == 'news' || n == 1) nym = 1;
		else if(n == 'work' || n == 2) nym = 2;
		else if(n == 'miscellaneous' || n == 3) nym = 3;
		else if(n == 'www' || n == 4) nym = 4;
		else if(n == 'contact' || n == 5) nym = 5;
			
		if(!dop_link && pages == link || dop_link == pages) {
			$("nav ul li b em:eq("+nym+")").stop().animate({left:"0"}, 300).animate({left:"-15px"}, 300);
		} else {
			if(!dop_link && pages != link) {
				pages = link
				ActMenu(pages);
			} else if(dop_link && dop_link != link || dop_link && dop_link == link && pages != link) {
				pages = link
				ActMenu(pages,dop_link);
			} else {
				ActMenu(pages);
			}
		}
		
		// Скрываем ссылку
		if(dop_link == 'email') $(this).hide();
		
		// Убираем focus
		$(".link").blur();
		return false;
	});
	
	// КЛИК ПО ССЫЛКАМ
	$("a.potrfolio").live("click", function(e) {
		window.open(this.href, "", "fullscreen=yes, scrollbars=yes");
		return false;
	});
	
	// Ошибка при выполнении ajax-запроса
	$("body").ajaxError(
	  function(e, xhr, settings, exept){
		ajax(settings.url)
	  }
	);
	
	// ФИНИШ AJAX
	$("body").ajaxComplete(function(){
		setTimeout(function() {
			box_anime_l();
			box_anime_r();
		}, 1000);
	});
		
	// Ручка 
	/*
	$(".handle").rotate({ 
	   bind: {
			mouseover : function() {
				$(".fastener").hide();
				$(this).rotate({
					duration:300,
					animateTo:5
				})
			},
			mouseout : function() {
				$(".fastener").show();
				$(this).rotate({
					duration:300,
					animateTo:0
				})
			}
		 } 
	});
	*/
	$(".handle").rotate({bind:{
		  click: function(e){
			  $(".fastener").hide();
			  $(this).rotate({
				duration:3000,
				angle: 0, 
				animateTo:1080,
				callback: function(){
					$(".fastener").show();
				}
			  })
		  }
	  }
	});
	
	// АНИМАЦИЯ БЛОКОВ. (ЛИСТАЛКА ПО СТРАНИЦАМ)
	var box_coll_l=9; // КОЛЛИЧЕСТВО БЛОКОВ
	var box_coll_r=9; // КОЛЛИЧЕСТВО БЛОКОВ	
	var box_pokaz_l;
	var box_pokaz_r;	
	var box_all_l;
	var box_all_r;	
	var tbox;
	
	// ЛЕВАЯ СТРАНИЦА
	function box_anime_l() {
	
		box_pokaz_l = box_coll_l;
		box_all_l = ($(".box_l li").length)-1;
		
		if(box_coll_l > box_all_l) $(".box_link2_l").hide();
		else $(".box_link2_l").show();
		
		$(".box_l").show();
		$(".box_link1_l").hide();
		$(".box_l li").hide();

		for (i = box_pokaz_l - box_coll_l; i < box_pokaz_l; i++) {
			$(".box_l li:eq("+i+")").show();
		}
		return false;
	}

	// ПРЕДЫДУЩИЕ
	$("article .box_link2_l").live("click", function(e) {
		$("article:eq(0)").fadeOut(400, function(){

			if(box_pokaz_l <= box_all_l) {
				box_pokaz_l = box_pokaz_l + box_coll_l;
				if(box_pokaz_l >= box_all_l) $(".box_link2_l").hide();
				$(".box_link1_l").show();
				$(".box_l li").hide();
				for (i = box_pokaz_l - box_coll_l; i < box_pokaz_l; i++) {
					$(".box_l li:eq("+i+")").show();
				}
			} else { $(".box_link2_l").hide(); }
			
			$("article:eq(0)").fadeIn(400);
		});
		return false;
	});

	// СЛЕДУЮЩИЕ
	$("article .box_link1_l").live("click", function(e) {
		$("article:eq(0)").fadeOut(400, function(){
	
			if(box_pokaz_l > box_coll_l) {
				box_pokaz_l = box_pokaz_l - box_coll_l;
				if(box_pokaz_l <= box_coll_l) $(".box_link1_l").hide();
				$(".box_link2_l").show();
				$(".box_l li").hide();
				for (i = box_pokaz_l - box_coll_l; i < box_pokaz_l; i++) {
					$(".box_l li:eq("+i+")").show();
				}
			} else { $(".box_link1_l").hide(); }
			
			$("article:eq(0)").fadeIn(400);
		});
		return false;
	});

	// ПРАВАЯ СТРАНИЦА
	function box_anime_r() {
	
		box_pokaz_r = box_coll_r;
		box_all_r = ($(".box_r li").length)-1;
		
		if(box_coll_r > box_all_r) $(".box_link2_r").hide();
		else $(".box_link2_r").show();
		
		$(".box_r").show();
		$(".box_link1_r").hide();
		$(".box_r li").hide();

		for (i = box_pokaz_r - box_coll_r; i < box_pokaz_r; i++) {
			$(".box_r li:eq("+i+")").show();
		}
		return false;
	}

	// ПРЕДЫДУЩИЕ
	$("article.right .box_link2_r").live("click", function(e) {
		$("article:eq(1)").fadeOut(400, function(){
	
			if(box_pokaz_r <= box_all_r) {
				box_pokaz_r = box_pokaz_r + box_coll_r;
				if(box_pokaz_r >= box_all_r) $(".box_link2_r").hide();
				$(".box_link1_r").show();
				$(".box_r li").hide();
				for (i = box_pokaz_r - box_coll_r; i < box_pokaz_r; i++) {
					$(".box_r li:eq("+i+")").show();
				}
			} else { $(".box_link2_r").hide(); }
			
			$("article:eq(1)").fadeIn(400);
		});
		return false;
	});

	// СЛЕДУЮЩИЕ
	$("article.right .box_link1_r").live("click", function(e) {
		$("article:eq(1)").fadeOut(400, function(){
	
			if(box_pokaz_r > box_coll_r) {
				box_pokaz_r = box_pokaz_r - box_coll_r;
				if(box_pokaz_r <= box_coll_r) $(".box_link1_r").hide();
				$(".box_link2_r").show();
				$(".box_r li").hide();
				for (i = box_pokaz_r - box_coll_r; i < box_pokaz_r; i++) {
					$(".box_r li:eq("+i+")").show();
				}
			} else { $(".box_link1_r").hide(); }
			
			$("article:eq(1)").fadeIn(400);
		});
		return false;
	});
	
	// SKINS
	var block = 1;
	var SkinsClick = $("ul.skins li");
	SkinsClick.click( function(e) {
		if(block == 1){
			block = 0;

			SkinsAct = SkinsClick.index(e.target);
			SkinsAct = SkinsAct + 1;
			$.cookie("skins", SkinsAct);
			
			$(this).css("border", "1px #FFF solid");
			SkinsClick.not(this).css("border", "1px #B0B0B0 solid").animate({opacity:'0.2'}, 300);
			 
			var MenuAct = SkinsClick.index(e.target);
			MenuAct = MenuAct + 1;
			$('<div id="fon"></div>').animate({opacity:'0.0'},0).css("background", "url(/_img/fon"+MenuAct+".jpg)").prependTo(".main").animate({opacity:'1.0'},2500, function(){
				$(".main").css("background", "url(/_img/fon"+MenuAct+".jpg)");
				$("#fon").remove();
				
				SkinsClick.animate({opacity:'1.0'}, 300);
				
				block = 1;
			});
			AnimeMenu();
		}
		return false;
	});	
	
	// ICONS
	var IconsClick = $("ul.icons li");
	IconsClick.click( function(e) {

		var MenuAct = IconsClick.index(e.target);
		if(MenuAct == 0){window.open("http://www.foto.lonwan.ru/");}
		if(MenuAct == 1){bookmark("http://www.lonwan.ru/");}
		if(MenuAct == 2){
		/*
			if($("nav ul li b em:eq(5)").hasClass("act")) {
				$("nav ul li b em:eq(5)").stop().animate({left:"0"}, 300).animate({left:"-15px"}, 300);
			} else {
				ActMenu("contact");
			}
		*/
			ActMenu("contact_email");
		}
		if(MenuAct == 3){ActMenu("maps");}
	});
	
	IconsClick.hover(function() {
		IconsClick.not(this).stop().animate({opacity:'0.5'}, 100);
	},
	function() {
		IconsClick.stop().animate({opacity:'1.0'}, 100);
	});
	
	// ФОТКИ
	$("#foto_box").live("mouseover mouseout", function() {
		$(this).toggleClass("open");
	});

	$('.foto_next_1').live("click", function(e) {
		SlaidFoto(1);	
	});	
	
	$('.foto_next_2').live("click", function(e) {
		SlaidFoto();	
	});
	
	$('body *').disableSelection();

});

$(window).resize(function() {
	CenterBox();
});

// ЦЕНТРУЕМ  - - - - - - - - - 
function CenterBox() {
	heightAll = $(window).height();
	widthAll = $(window).width();

	heightMain = $("section:eq(0)").height(); 
	widthMain = $("section:eq(0)").width(); 
	
	centerMainH = (heightAll - heightMain)/2;
	centerMainW = (widthAll - widthMain)/2;
	
	if(heightMain < heightAll) {
		$("section:eq(0)").css("top", centerMainH);
		$("body").css("overflow-y", "hidden");
		$(".main").css({"width": widthAll, "height": heightAll});
	} else {
		$("section:eq(0)").css("top", 0);
		$("body").css("overflow-y", "auto");
		$(".main").css({"width": widthAll, "height": heightMain});
	}
	
	if(775 < widthAll) {
		$("section:eq(0)").css("left", centerMainW);
		$("body").css("overflow-x", "hidden");
	} else {
		$("section:eq(0)").css("left", 0);
		$("body").css("overflow-x", "auto");
	}
}

function ActMenu(n,dop) {
	noAct = 1;
		if(!dop){
			ajax(n);
		} else {
			if(n == dop){
				ajax(n);
			} else {
				ajax(n,dop);
				n = n+'_'+dop;
			}
		}
		
		var nym;
		if(n == 'about' || n == 0) nym = 0;
		else if(n == 'news' || n == 1) nym = 1;
		else if(n == 'work' || n == 2) nym = 2;
		else if(n == 'miscellaneous' || n == 3) nym = 3;
		else if(n == 'www' || n == 4) nym = 4;
		else if(n == 'contact' || n == 5) nym = 5;
		else nym = "";
	
		$('<div id="download"></div>').prependTo("body");
		
		$("nav ul li b em.act").stop().animate({left:"-55px"}, 500, function(){
			$("nav ul li b em.act").css("left", "-165px").animate({left:"-130px"}, 200, function(){
				$("nav ul li b em").removeClass("act");
			});
		});
		
		$("nav ul li b em:eq("+nym+")").stop().animate({left:"-165px"}, 500, function(){
			$("nav ul li b em:eq("+nym+")").addClass("act2").css("left", "-55px").animate({left:"0"}, 200).animate({left:"-15px"}, 300);
		});
		
		setTimeout(function() {
			$("nav ul li b em:eq("+nym+")").toggleClass("act2 act");
			noAct = 0;
			$("#download").remove();
		}, 1000);
}

function AnimeMenu() {
	if(noAct == 0){
		noAct = 1;
		$(".handle").animate({"left": "29px", "top": "70px"}, 500, function(){
			var men=0;
			for (i = 0; i < 6; i++) {
				men = men + 300;
				if($("nav ul li:eq("+i+") b em").hasClass("act"))
					$("nav ul li:eq("+i+") b em").delay(men).animate({left:"0"}, 300).animate({left:"-30px"}, 300).animate({left:"-15px"}, 300);
				else
					$("nav ul li:eq("+i+") b em").delay(men).animate({left:"-115px"}, 300).animate({left:"-145px"}, 300).animate({left:"-130px"}, 300);
			}
		});

		setTimeout(function() {
			$("#download").remove();
			$(".fastener").show();
			noAct = 0;
		}, 3000);
	}
}

// ALAX
function ajax(ur,pa) {
	if(!pa) {
		url = "p.php?id=" + ur;
	} else {
		url = "p.php?id=" + ur + "&in=" + pa;
	}
	
	$.ajax({
		url:url,
		type: "POST",
		callback: true,
		cache: false, 
		async: false,
		success:function(code){
			$(code).each(function(){
				if ($(this).is("#left")) {
					var text_left = $(this).html();
					$("article:eq(0)").fadeOut(400, function(){
						$("article:eq(0)").html(text_left).fadeIn(400);
					});
				}
				if ($(this).is("#right")) {
					var text_right = $(this).html();
					$("article:eq(1)").fadeOut(400, function(){
						$("article:eq(1)").html(text_right).fadeIn(400);
					});
				}
				if ($(this).is("#ho")) {
					pages = $(this).html();
					document.location.hash = pages;
					
					$('ul.dopmenu li').removeClass('on');
					setTimeout(function() {
						$('ul.dopmenu li#'+ pages).addClass('on');
					}, 500);
				}
				if ($(this).is("#title")) {
					titl = $(this).html();
					document.title = "АНДРЕЙ ШУСТРОВ - " + titl; 
				}
			});
		}
	});
}

// МОНИТОР АДРЕСНОЙ СТРОКИ
function detect(suf) {
	thispage=document.location.hash;
	pages=thispage.substring(thispage.lastIndexOf("#") + 1,thispage.length);
	if(pages != suf && pages != thispage) {ajax(pages); ActMenu(pages);}
	if(pages != suf && pages == thispage) {ajax("main"); ActMenu("main");}
	return false;
}
function mydetect() {
	detect(pages);
	setTimeout(function() {mydetect();}, 1000);
}

// ПРЕДВАРИТЕЛЬНО ЗАГРУЖАЕМ КАРТИНКИ
$.preloadImages = function () {
	if (typeof arguments[arguments.length - 1] == 'function') {
		var callback = arguments[arguments.length - 1];
	} else {
		var callback = false;
	}
	if (typeof arguments[0] == 'object') {
		var images = arguments[0];
		var n = images.length;
	} else {
		var images = arguments;
		var n = images.length - 1;
	}
	var not_loaded = n;
	for (var i = 0; i < n; i++) {
		$(new Image()).load(function() {
			if (--not_loaded < 1 && typeof callback == 'function') {
				callback();
			}
		}).attr('src', images[i]);
	}
}

// popup
function openWindow(anchor, options) {
 
	var args = '';
 
	if (typeof(options) == 'undefined') { var options = new Object(); }
	if (typeof(options.name) == 'undefined') { options.name = 'win' + Math.round(Math.random()*100000); }
 
	if (typeof(options.height) != 'undefined' && typeof(options.fullscreen) == 'undefined') {
		args += "height=" + options.height + ",";
	}
 
	if (typeof(options.width) != 'undefined' && typeof(options.fullscreen) == 'undefined') {
		args += "width=" + options.width + ",";
	}
 
	if (typeof(options.fullscreen) != 'undefined') {
		args += "width=" + screen.availWidth + ",";
		args += "height=" + screen.availHeight + ",";
	}
 
	if (typeof(options.center) == 'undefined') {
		options.x = 0;
		options.y = 0;
		args += "screenx=" + options.x + ",";
		args += "screeny=" + options.y + ",";
		args += "left=" + options.x + ",";
		args += "top=" + options.y + ",";
	}
 
	if (typeof(options.center) != 'undefined' && typeof(options.fullscreen) == 'undefined') {
		options.y=Math.floor((screen.availHeight-(options.height || screen.height))/2)-(screen.height-screen.availHeight);
		options.x=Math.floor((screen.availWidth-(options.width || screen.width))/2)-(screen.width-screen.availWidth);
		args += "screenx=" + options.x + ",";
		args += "screeny=" + options.y + ",";
		args += "left=" + options.x + ",";
		args += "top=" + options.y + ",";
	}
 
	if (typeof(options.scrollbars) != 'undefined') { args += "scrollbars=1,"; }
	if (typeof(options.menubar) != 'undefined') { args += "menubar=1,"; }
	if (typeof(options.locationbar) != 'undefined') { args += "location=1,"; }
	if (typeof(options.resizable) != 'undefined') { args += "resizable=1,"; }
 
	var win = window.open(anchor, options.name, args);
	return false;
}

// Закладки
function getBrowserInfo() { 
  var t,v = undefined; 
 
  if (window.chrome) t = 'Chrome'; 
  else if (window.opera) t = 'Opera'; 
  else if (document.all) { 
  t = 'IE'; 
  var nv = navigator.appVersion; 
  var s = nv.indexOf('MSIE')+5; 
  v = nv.substring(s,s+1); 
  } 
  else if (navigator.appName) t = 'Netscape'; 
 
  return {type:t,version:v}; 
 } 
 
 function bookmark(a){ 
  var url = window.document.location; 
  var title = window.document.title; 
  var b = getBrowserInfo(); 
 
  if (b.type == 'IE' && 8 >= b.version && b.version >= 4) window.external.AddFavorite(url,title); 
  else if (b.type == 'Opera') { 
  a.href = url; 
  a.rel = "sidebar"; 
  a.title = url+','+title; 
  return true; 
  } 
  else if (b.type == "Netscape") window.sidebar.addPanel(title,url,""); 
  else alert("Нажмите CTRL-D, чтобы добавить страницу в закладки."); 
  return false; 
 }
 
 var sf;
 var block_foto = 1;
 
 function SlaidFoto(next){
	if(block_foto == 1) {
		block_foto = 0;

		var elems = $("#foto");
		var Lenta = [
			['_img/foto21.jpg','<b>Coffee Bean</b> 2012 год.'],
			['_img/foto.jpg','<b>Венеция</b> 2010 год.'],
			['_img/foto2.jpg','<b>Леванто</b> 2010 год.'],
			['_img/foto3.jpg','<b>Москва</b> 2010 год.'],
			['_img/foto7.jpg','<b>Суздаль - Масленица</b> 2011 год.'],
			['_img/foto5.jpg','<b>На катке</b> 2010 год.'],
			['_img/foto6.jpg','<b>Корпоратив Chevron</b> 2010 год.'],
			['_img/foto8.jpg','<b>Озеро Изео</b> 2010 год.'],
			['_img/foto9.jpg','<b>На балконе Джульетты</b> 2010 год.'],
			['_img/foto10.jpg','<b>Конфетка</b> 2010 год.'],
			['_img/foto11.jpg','<b>Живописный мост</b> 2010 год.'],
			['_img/foto12.jpg','2009 год.'],
			['_img/foto13.jpg','<b>Петергоф</b> 2009 год.'],
			['_img/foto14.jpg','<b>Петергоф</b> 2009 год.'],
			['_img/foto15.jpg','<b>Екатерининский дворец</b> 2009 год.'],
			['_img/foto16.jpg','<b>Пётр &#8544;</b> 2009 год.'],
			['_img/foto17.jpg','<b>Кубок Легенд</b> 2009 год.'],
			['_img/foto18.jpg','<b>Корпоратив Chevron</b> 2008 год.'],
			['_img/foto19.jpg','<b>Боулинг</b> 2008 год.'],
			['_img/foto20.jpg','<b>Автосалон</b> 2008 год.'],
			['_img/foto4.jpg','<b>Mail.Ru</b> 21.12.2009 год.']
		];

		if(elems.length > 0) {
			var q = Lenta.length;
			for (s=0; s < q; s++) {
				var FotoSrc = $('#foto img').attr('src');
				FotoSrc = FotoSrc.replace(/^http:\/\/www.lonwan.ru\/(.*?)/gim, '$1');
				if(Lenta[s][0] == FotoSrc) {
					if(!next){
						sf = (s)+1;
					} else {
						sf = (s)-1;
					}
					if(sf >= q) sf = 0;
					if(sf < 0) sf = (q)-1;
					
					var fotoNext = Lenta[sf][0];
					var fotoNextName = Lenta[sf][1];

					$("#foto_name").html('З А Г Р У З К А . . .');
					$.preloadImages('http://www.lonwan.ru/'+fotoNext, function () {
		
						setTimeout(function() {
							$("#foto").after('<div id="foto"><img alt="'+fotoNextName+'" src="'+fotoNext+'"></div>');
							$("#foto_name").html(fotoNextName);
							$("#foto:first").css('zIndex', 2).animate({opacity:'0'}, 500, function () {
								$("#foto:first").remove();
								block_foto = 1;
							});
						}, 500);
					
					});
				}
			}
		}
	}
 }
 
// Отпрака письма
function text (str) { return /[0-9_;:'!~?=+<|>]/g.test(str); }
//function numeric (str) { return /^[0-9-\+\(\)\s]+z/.test(str + "z"); }
function mail (str) { return /^[a-z0-9_\.-]+@[a-z0-9_\.-]+.[a-z]{2,3}$/.test(str); }

function checkForm(){
  var title;
  var elem;
  var dutyField = "Не заполнено поле ";
  var wrongField = "Неверное значение поля ";
  var check = true;

  function checkError (field, str)
	 {
	 document.getElementById('alert_name').className = ('ddd');
	 document.getElementById('alert_email').className = ('ddd');
	 document.getElementById('alert_message').className = ('ddd');
	 
	   $("#alert_message_pole").after('<p class="red" id="alert">'+str+'</p>');
	   check = false;
	 
	 document.getElementById('alert_' + field).className = ('ddd_error');
	 document.getElementById('alert_' + field + '_pole').focus();
	 
	 }
	 $("#alert").remove();
	 
  if (check)
	 {
	 title = '"Имя"';
	 elem = document.preview.name.value;
	 if (elem.length == 0) checkError('name', dutyField + title);
	 else if (text(elem)) checkError('name', wrongField + title);
	 }
	 
  if (check)
	 {
	 title = '"E-mail"';
	 elem = document.preview.email.value;
	 if (elem.length == 0) checkError('email', dutyField + title);
	 else if (!mail(elem)) checkError('email', wrongField + title);
	 }
	 
  if (check)
	 {
	 title = '"Сообщение"';
	 elem = document.preview.message.value;
	 if (elem.length == 0) checkError('message', dutyField + title);
	 }

  if (check){
	var ajx = typeof window.external=='object' && typeof document.all=='object'
	? new ActiveXObject("Microsoft.XMLHTTP")
	: new XMLHttpRequest();
	var q1 = "name="+document.getElementById('alert_name_pole').value;
	var q2 = "email="+document.getElementById('alert_email_pole').value;
	var q3 = "message="+document.getElementById('alert_message_pole').value;
	var q = q1 + '&' + q2 + '&' + q3;
	ajx.open('post','/email/ajx.php',true);
	ajx.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajx.setRequestHeader('Accept-Charset', 'windows-1251');
	ajx.send(q);
	
	$('.email').html('<p>Спасибо,<br /> Ваше сообщение отправлено.<p>');
	}

return check;
}

// admin
function checkForm2(){
  var title;
  var elem;
  var dutyField = "Не заполнено поле ";
  var check = true;

  function checkError (field, str)
	 {
	 document.getElementById('alert_user_name').className = ('ddd');
	 document.getElementById('alert_user_pass').className = ('ddd');
	 
	   document.getElementById("alert").innerHTML = str;
	   check = false;
	 
	 document.getElementById('alert_' + field).className = ('ddd_error');
	 document.getElementById('alert_' + field + '_pole').focus();
	 
	 }
	 document.getElementById("alert").innerHTML = "";

  if (check)
	 {
	 title = '"Логин"';
	 elem = document.preview.user_name.value;
	 if (elem.length == 0) checkError('user_name', dutyField + title);
	 }
	 
  if (check)
	 {
	 title = '"Пароль"';
	 elem = document.preview.user_pass.value;
	 if (elem.length == 0) checkError('user_pass', dutyField + title);
	 }
			
  if (check)  { 

	var ajx = typeof window.external=='object' && typeof document.all=='object'
	? new ActiveXObject("Microsoft.XMLHTTP")
	: new XMLHttpRequest();
	var q1 = "user_name="+document.getElementById('alert_user_name_pole').value;
	var q2 = "user_pass="+document.getElementById('alert_user_pass_pole').value;
	var q = q1 + '&' + q2;
	ajx.open('post','/admin/authorize.php',true);
	ajx.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajx.setRequestHeader('Accept-Charset', 'windows-1251');
	ajx.send(q);
	setTimeout(function() {
		ActMenu('authorization');
	}, 1000);
	}
return check;
}

function checkAdmin(){
	var ajx = typeof window.external=='object' && typeof document.all=='object'
	? new ActiveXObject("Microsoft.XMLHTTP")
	: new XMLHttpRequest();
	var q = 'close=1';
	ajx.open('post','/admin/authorize.php',true);
	ajx.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajx.setRequestHeader('Accept-Charset', 'windows-1251');
	ajx.send(q);
	ActMenu('admin');
}

// end. admin


jQuery.fn.extend({
    disableSelection : function() {
            this.each(function() {
                    this.onselectstart = function() { return false; };
                    this.unselectable = "on";
                    jQuery(this).css('-moz-user-select', 'none');
            });
    },
    enableSelection : function() {
            this.each(function() {
                    this.onselectstart = function() {};
                    this.unselectable = "off";
                    jQuery(this).css('-moz-user-select', 'auto');
            });
    }
});
