/**
 * @author jman
 */

/* Функция которая убирает обводку ссылок и кнопок */
function shitfocus() {
	var arrLinks = document.getElementsByTagName('a');
	for (var i = 0; i < arrLinks.length; i++) {
		arrLinks[i].hideFocus = true;
	}
	var arrInputs = document.getElementsByTagName('input');
	for (i = 0; i < arrInputs.length; i++) {
		arrInputs[i].hideFocus = true;
	}
}

function preloadImages(elemSrc) {
	if (!document.getElementById("ImagePreloader")) {
		var preDiv = document.createElement('div');
		document.body.appendChild(preDiv);
		preDiv.setAttribute("id", "ImagePreloader");
	}
	var preDiv = document.getElementById("ImagePreloader");
	var preImg = document.createElement('img');
	preDiv.style.position = 'absolute';
	preDiv.style.visibility = 'hidden';
	preDiv.style.height = '1px';
	preDiv.style.overflow = 'hidden';
	preImg.src = elemSrc;
	preDiv.appendChild(preImg);
}

/* ползучая галерея */
var left = 0;
var photo;
var IntervalId;
var galContainer;
var TimeoutId = new Array;
var globalImg;

function galery() {
	galContainer = document.getElementById("galeryContainer");
	if (!galContainer) {
		return
	}
	galContainer.style.marginLeft = left + "px";
	var childrens = galContainer.childNodes;
	for (var i = 0; i < childrens.length; i++) {
		if (childrens[i].nodeType != 1) {
			galContainer.removeChild(childrens[i]);
		}
	}
	for (var i = 0; i < childrens.length; i++) {
		preloadImages(childrens[i].getElementsByTagName("img")[0].src);
		preloadImages(childrens[i].getElementsByTagName("a")[0].name);
		photo = childrens[i].getElementsByTagName("img");
		photo[0].onmouseover = onmouseHandler;
	}
	IntervalId = setInterval("setLeft()", 45);
}

function setLeft() {
	left -= 1;
	galContainer.style.marginLeft = left + "px";
	if (left == -104) {
		left = 0;
		replaceChild();
	}
}

function replaceChild() {
	var fChild = galContainer.firstChild;
	galContainer.style.marginLeft = left + "px";
	galContainer.removeChild(fChild);
	galContainer.appendChild(fChild);
}

function onmouseHandler() {
	clearInterval(IntervalId);
	zoomPhoto(this.parentNode.parentNode, this, true);
	this.onmouseout = function() {
		for (var i = 10; i <= 50; i += 10) {
			clearTimeout(TimeoutId[i]);
		}
		zoomPhoto(this.parentNode.parentNode, this, false);
		IntervalId = setInterval("setLeft()", 45);
	}
}

function zoomPhoto(li, image, dest) {
	if (dest) {
		var tmpsrc = image.src;
		image.src = image.parentNode.name;
		image.parentNode.name = tmpsrc;

		slideZoom(image);
		li.style.position = "relative";
	} else {
		var tmpsrc = image.src;
		image.src = image.parentNode.name;
		image.parentNode.name = tmpsrc;

		image.style.width = "";
		image.style.height = "";
		image.style.top = "";
		image.style.left = "";
		image.style.position = "";
		li.style.position = "";
	}
}

function slideZoom(image) {
	globalImg = image;
	for (var i = 10; i <= 50; i += 10) {
		TimeoutId[i] = setTimeout('animationZoom(' + i + ',globalImg)', i * 5);
	}
}

function animationZoom(pos, image) {
	var width = pos + 92;
	var height = pos + 92;
	var top = -(pos / 2);
	var left = -(pos / 2);
	image.style.width = width + "px";
	image.style.height = width + "px";
	image.style.top = top + "px";
	image.style.left = left + "px";
	image.style.position = "absolute";
	image.style.zIndex = "100";
}

/* ТУЛТИПЫ ДЛЯ ФОТОК "ЛИЦА" */

function showFace() {
	var cont = document.getElementById('content');
	for (var i = 0; i < cont.getElementsByTagName('div').length; i++) {
		if (cont.getElementsByTagName('div')[i].className.indexOf('faces') != -1) {
			var faces = cont.getElementsByTagName('div')[i]
					.getElementsByTagName('ul')[0];
		}
	}
	if (!faces) {
		return
	}
	for (var i = 0, fcLength = faces.getElementsByTagName('li').length; i < fcLength; i++) {
		var face = faces.getElementsByTagName('li')[i];
		var faceImg = face.getElementsByTagName('img');
		faceImg[0].onmouseover = showAbout;
		faceImg[0].onmouseout = hideAbout;
	}
}
function showAbout() {
	this.parentNode.parentNode.className = "show";
}
function hideAbout() {
	this.parentNode.parentNode.className = "";
}
/*
/* Маленькая галерея 
var bigPhoto;

function photoGalery() {
	if (!document.getElementById("photoGalery")) {
		return
	}
	var mainObject = document.getElementById("photoGalery");
	for (var i = 0, elemLenght = mainObject.getElementsByTagName("div").length; i < elemLenght; i++) {
		if (mainObject.getElementsByTagName("div")[i].className == "photoBig") {
			bigPhoto = mainObject.getElementsByTagName("div")[i];
			bigPhoto.getElementsByTagName('a')[0].onclick = function() {
				smallZoomPhoto(this);
				return false;
			}
		}
	}
	var previewContainer = mainObject.getElementsByTagName("ul")[0]
	for (var i = 0, elemLenght = previewContainer.getElementsByTagName("li").length; i < elemLenght; i++) {
		previewPhoto = previewContainer.getElementsByTagName("li")[i];
		previewPhoto.getElementsByTagName('a')[0].onclick = switchPhotos;
	}
}
function switchPhotos() {
	var tmpPhoto = bigPhoto.getElementsByTagName("img")[0].src;
	var tmpTitle = bigPhoto.getElementsByTagName("a")[0].title;
	var tmpPreview = bigPhoto.getElementsByTagName("a")[0].href;
	bigPhoto.getElementsByTagName("img")[0].src = this.href;
	bigPhoto.getElementsByTagName("a")[0].href = this
			.getElementsByTagName("img")[0].src;
	bigPhoto.getElementsByTagName("a")[0].title = this.title
	this.href = tmpPhoto;
	this.title = tmpTitle;
	this.getElementsByTagName("img")[0].src = tmpPreview;
	return false;
}

function smallZoomPhoto(elem){
	var image = new Image();
	image.src = elem.title;
	var interID = setInterval( function(){
		if (image.complete) {
			clearInterval(interID);
			var win = {};
			win.height = document.documentElement.clientHeight - 100;
			win.width = document.documentElement.clientWidth - 100;
			win.k = 1;
			if (win.height < image.height) {
				win.k = win.height / image.height;
			}
			image.height *= win.k;
			image.width *= win.k;
			var cont = document.createElement("div");
			var htm = "<div class='popupImg'>";
			htm += "<a href='#' id='zoomPhClose'></a>";
			htm += "<img src='" + image.src + "'";
			htm += "height='" + image.height + "'";
			htm += "width='" + image.width + "' alt='' />";
			htm += "<br class='clear'/></div>";
			with (cont) {
				id = "zoomPh";
				className = "popupContainer";
				innerHTML = htm;
			}
			document.body.appendChild(cont);
			modalWindow('zoomPh');
			document.getElementById('zoomPhClose').onclick = function() {
				modalWindow('zoomPh', 'hide');
				cont.parentNode.removeChild(cont);
				return false;
			}
		}
	},100);

}*/

//* ==== Открытие под разделов ====== 
function showMore() {
	for (var i = 0, alength = document.links.length; i < alength; i++) {
		if (document.links[i].parentNode.className == "show_more") {
			document.links[i].onclick = openSubsection;
		}
	}
}
function morePhoto() {
	for (var i = 0, alength = document.links.length; i < alength; i++) {
		if (document.links[i].parentNode.className == "more_photo") {
			document.links[i].onclick = openPhotoSubsection;
		}
	}
}
function openPhotoSubsection() {
	var elem = this.parentNode.parentNode
	var openText = "Все фото";
	var closeText = "Скрыть";
	if (elem.className.indexOf("open") != -1) {
		elem.className = elem.className.replace(/open/g, '');
		this.innerHTML = openText;
	} else {
		elem.className = (elem.className) ? elem.className += " open" : "open";
		this.innerHTML = closeText;
	}
	return false;
}
function openSubsection() {
	var elem = this.parentNode.parentNode
	var openText = "Раскрыть подраздел";
	var closeText = "Скрыть подраздел";
	if (elem.className.indexOf("open") != -1) {
		elem.className = elem.className.replace(/open/g, '');
		this.innerHTML = openText;
	} else {
		elem.className = (elem.className) ? elem.className += " open" : "open";
		this.innerHTML = closeText;
	}
	return false;
}

function modalWindow(elem, flag) {
	var obj = document.getElementById(elem);
	if (flag != "hide") {
		runSpalshScreen("show" ,1 , "000", false);
		obj.parentNode.removeChild(obj);
		document.body.appendChild(obj);
		obj.style.display = "block";
		obj.style.position = "fixed";
		obj.style.zIndex = '600';
		obj.top = 0;
		if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
			obj.top = parseInt(document.documentElement.scrollTop, 10);
			obj.style.position = "absolute";
		}
		obj.height = obj.clientHeight;
		obj.width = obj.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var windowWidth = document.documentElement.clientWidth;
		obj.style.top = ((windowHeight / 2) - (obj.height / 2)) + obj.top
				+ "px";
		obj.style.left = ((windowWidth / 2) - (obj.width / 2)) + "px";
	} else {
		obj.style.display = "none";
		runSpalshScreen("hide");
	}
}

function runSpalshScreen(flag, opacity, bg, slow ) {
	if (flag != "hide") {
		var shadow = document.createElement('div');
		with(shadow){
			id = ('tShadow');
			style.width = '100%';
			style.top = 0;
			style.left = 0;
			style.position = 'fixed';
			style.zIndex = '400';
			style.opacity = '0';
			style.filter = ('alpha(opacity=0)');
		}
		document.body.appendChild(shadow);
		if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
			document.body.style.height = '100%';
			shadow.style.position = 'absolute';
			shadow.style.setExpression("top", "parseInt(document.documentElement.scrollTop, 10) + 'px'");
			var fr = document.createElement("iframe");
			with (fr) {
				id = ('fShadow');
				border = 0;
				src = "about:blank";
				style.width = '100%';
				style.height = '100%';
				style.left = 0;
				style.setExpression("top", "parseInt(document.documentElement.scrollTop, 10) + 'px'");
				style.position = "absolute";
				style.zIndex = 100;
				style.filter = ('alpha(opacity=0)');
				style.backgroundImage = 'url(x.gif)';
				style.backgroundAttachment = 'fixed';
			}
			document.body.appendChild(fr);
		}
		if (bg){
			shadow.style.backgroundColor = "#" + bg;
		}
		if (opacity) {
			var op = opacity
			shadow.style.opacity = op/10;
			shadow.style.filter = ('alpha(opacity='+op*10+')');
		}
		if (slow) {
			for (var i = 0; i <= 100; i += 5) {
				(function(){
					var pos = i;
					setTimeout(function(){
						shadow.style.height = pos + "%";
					}, (pos + 1) * 2);
				})();
			}
		}
		else{
			shadow.style.height = "100%";
		}
	}
	else {
		var shadow = document.getElementById('tShadow');
		shadow.parentNode.removeChild(shadow);
		if(document.getElementById('fShadow')){
			var elem = document.getElementById('fShadow');
			elem.parentNode.removeChild(elem);
		}
	}
}
// Ajax
function getXmlHttp() {
	var xmlhttp;
	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}


// Обработка формы

function selectForm() {
	var container = false;
	if (container = document.getElementById("mainSearch")){
		var selector = document.getElementById("categorySelector");
		for (var i = 0, l = selector.length; i < l; i++){
			if (selector.options[i].defaultSelected){
				selector.selectedIndex = i;
			}
		}
		var req = getXmlHttp();
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status == 200) {
					container.style.height = "";
					container.innerHTML = req.responseText;
					selectForm();
				} else {
					alert("Возникла ошибка: ответ сервера -" + req.statusText);
				}
			}

		}
		selector.onchange = function (){
			var val = this.value || selector.value;
			container.style.height = getHeight(container) + "px";
			container.innerHTML = '<span class="ajaxLoaderBar"></span>';
			req.open('GET', 'ajax/formselector.php?selForm='+ val +'&_='+ Math.random() , true);
			req.send(null);
		}
	}
}

// Высота элемента
function getHeight(elem){
	with (elem) {
		style.position = "absolute";
		style.visibility = "hidden";
	}
	var height = elem.clientHeight;
	with (elem) {
		style.position = "";
		style.visibility = "";
	}
	return height;
}

// Загрузка старницы
window.onload = function() {
	shitfocus();
	showFace()
	galery();
	//photoGalery();
	showMore();
	morePhoto();
	selectForm();
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
		document.execCommand("BackgroundImageCache", false, true);
	}
}

		/*gallery*/
$(function() {
        $('#photoGalery .photoPreview a').lightBox();
    });
