// ------------------------------------------------ Скрипт для увеличеного просмотра картинок --------------------------------------------------
// ---------------------------------------------------------------- by Enoth -------------------------------------------------------------------
// ------------------------------------------------------------------ 2009 ---------------------------------------------------------------------
// ------------------------------------------------------------- enothic@gmail.com -------------------------------------------------------------
// Вызов
// enlargePicture(pte, c, psize), где
// pte - ссылка на картинку, pte = '../images/picture.jpg';
// c - название кнопки закрыть, c = 'Close';
// psize - размер картинки в формате psize = [800, 600];
// настройки скрипта
var enlarge_speed = 40; // скорость разворачивания
var reduce_speed = 45; // скорость сворачивания
var max_photo_size = 600; // максимальный размер любой стороны картинки по-умолчанию
var factor = 1;
// ---------------------------------------------------------------------------------------------------------------------------------------------

var s_body_name = 'experiment'; // id тела
var s_blackScreen_name = 'blackScreen'; // id черного экрана
var s_picture_name = 'pictureToEnlarge'; // id картинки
var s_close_link_name = 'pictureClose'; // id кнопки закрытия
var s_table_name = 'experimentTable';
var s_loading_rogress = 'loadingProgress';
var s_previous_picture = 'previousPicture';
var s_next_picture = 'nextPicture';

var s_body;
var s_back;
var s_picture;
var s_close_link;
var s_table;
var s_prog;
var s_next;
var s_prev;
var win_size;
var counter;
var counter2;
var l_counter;
var picture_active = false;
var photo_size = [0, 0];
var body_size;
var enlargePicturesGalleries = null;
var currentPictureSrc = "";
var anotherPircture = false;
var orient = 1;

// Размер рабочей области экрана
function screenWorkspace() {
	var elem = new Array();
	if (window.innerWidth) {
		elem[0] = window.innerWidth;
		elem[1] = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		elem[0] = document.documentElement.clientWidth;
		elem[1] = document.documentElement.clientHeight;
	}
	else if (document.body && document.body.clientWidth) {
		elem[0] = document.body.clientWidth;
		elem[1] = document.body.clientHeight;
	}
	return elem;
}

// Смешение по высоте
function getClientTop() {
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop)
}

function getBtTop() {
	return win_size[1] / 2 - parseInt(s_body.style.height) / 2 + getClientTop();
}

// Смещение по горизонтали
function getClientLeft() {
	return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function getBtLeft() {
	return win_size[0] / 2 - parseInt(s_body.style.width) / 2 + getClientLeft();
}

// картинка
function pictureVisible(vis) {
	if (vis) {
		s_picture.style.width = photo_size[0];
		s_picture.style.height = photo_size[1];
		s_picture.style.left = 6;
		s_picture.style.top = 6;

		s_close_link.style.left = 20;
		s_close_link.style.top = photo_size[1] + 14;
	}
	else {
		var s = document.getElementById(s_picture_name);
		s.parentNode.removeChild(s);

		s = document.getElementById(s_close_link_name);
		s.parentNode.removeChild(s);
	}
}

// фон
function backVisible(vis) {
	if (vis) {
		s_back.style.width = win_size[0];
		s_back.style.height = win_size[1];
		s_back.style.top = getClientTop();
		s_back.style.left = getClientLeft();
	}
	else {
		if (!anotherPircture) {
			var s = document.getElementById(s_blackScreen_name);
			s.parentNode.removeChild(s);
		}
	}
}

function loadingVisible(vis)
 {
	if (vis) {
		s_prog.style.top = win_size[1] / 2  + getClientTop();
		s_prog.style.left = win_size[0] / 2 + getClientLeft();
	}
	else {
		var	s = document.getElementById(s_loading_rogress);
		s.parentNode.removeChild(s);
	}
}

function nextVisible(vis)
 {
	if (vis) {
		s_next.style.top = photo_size[1] + 14;
		s_next.style.left = parseInt(s_body.style.width) / 2 + getClientLeft() + 20;
	}
	else {
		var	s = document.getElementById(s_next_picture);
		s.parentNode.removeChild(s);
	}
}
function prevVisible(vis)
 {
	if (vis) {
		s_prev.style.top = photo_size[1] + 14;
		s_prev.style.left = parseInt(s_body.style.width) / 2 + getClientLeft() - 20;
	}
	else {
		var s = document.getElementById(s_previous_picture);
		s.parentNode.removeChild(s);
	}
}
// тело
function bodyVisible(vis) {
	if (vis) {
		s_body.style.width = s_table.style.width =0;
		s_body.style.height = s_table.style.height = 5;
		s_body.style.left = s_table.style.left = getBtLeft();
		s_body.style.top = s_table.style.top = getBtTop();
	}
	else {
		var s = document.getElementById(s_table_name);
		s.parentNode.removeChild(s);

		s = document.getElementById(s_body_name);
		s.parentNode.removeChild(s);
	}
}

// ----------------------
function preInit() {
	if (!anotherPircture) {
		s_back = document.createElement('div');
		s_back.id = s_blackScreen_name;
		s_back.onclick = reducePicture;
		document.body.appendChild(s_back);
	}

	s_prog = document.createElement('p');
	s_prog.id = s_loading_rogress;
	s_prog.appendChild(document.createTextNode("Loading"));
	document.body.appendChild(s_prog);

	s_body = document.createElement('div');
	s_body.id = s_body_name;

    s_table = document.createElement('table');
    s_table.id = s_table_name;
    var tbody = document.createElement('tbody');
    var tr = document.createElement('tr');
    var td = document.createElement('td');

    s_picture = document.createElement('img');
    s_picture.id = s_picture_name;
    if (!anotherPircture)
	    s_picture.onclick = reducePicture;
	else
		s_picture.onclick = nextPicture;
	td.appendChild(s_picture);

	s_close_link = document.createElement('a');
	s_close_link.id = s_close_link_name;
	s_close_link.className = 'c_pictureClose';
	s_close_link.onclick = reducePicture;
	s_close_link.style.width = 0;
	s_close_link.style.heght = 0;
	s_close_link.appendChild(document.createTextNode(""));
	td.appendChild(s_close_link);

	s_prev = document.createElement('div');
	s_prev.id = s_previous_picture;
	s_prev.onclick = previousPicture;
	td.appendChild(s_prev);

	s_next = document.createElement('div');
	s_next.id = s_next_picture;
	s_next.onclick = nextPicture;
	td.appendChild(s_next);

    tr.appendChild(td);
    tbody.appendChild(tr);
    s_table.appendChild(tbody);
    s_body.appendChild(s_table);
	document.body.appendChild(s_body);
}

function init(pte, c, psize) {
	if (typeof c == 'undefined')
		c='Close';

    win_size = screenWorkspace();

	if (typeof psize != 'undefined')
		max_photo_size = psize;
	else
		max_photo_size = 600;

	scrollVisible(!picture_active);
	backVisible(picture_active);
	loadingVisible(picture_active);
// -----
	if (!anotherPircture) {
		var el = document.getElementsByTagName('img');
		var elName;
		for (var i=0; i<el.length; i++) {
			if (typeof el[i].attributes.onclick != 'undefined')
			if (el[i].className.indexOf('enlargePicture') != -1) {
				if (el[i].attributes.onclick.value.indexOf(pte) != -1) {
					elName = el[i].name;
					break;
				}
			}
		}

		var list = new Array();
		var j = 0;
		for (var i=0; i<el.length; i++) {
			if ((el[i].className.indexOf('enlargePicture') != -1)&&(el[i].name == elName)) {
				list[j] = el[i].attributes.onclick.value;
				j++;
			}
		}
		if (list.length > 1) {
			enlargePicturesGalleries = list;
			s_picture.onclick = nextPicture;
		}
	}
// -----
	l_counter = 1;
	loadingId = window.setTimeout('loadingProgress()', 200);

    s_picture.onload = function s_pictureOnload() {
		if (s_picture.width > s_picture.height) {
			photo_size[0] = max_photo_size;
			var k = photo_size[0] / s_picture.width;
			photo_size[1] = parseInt(s_picture.height * k);
		}
		if (s_picture.width < s_picture.height) {
			photo_size[1] = max_photo_size;
			var k = photo_size[1] / s_picture.height;
			photo_size[0] = parseInt(s_picture.width * k);
		}
		if (s_picture.width == s_picture.height)
			photo_size[0] = photo_size[1] = max_photo_size;

		body_size = [photo_size[0] + 14, photo_size[1] + 34];

		window.clearTimeout(loadingId);

		loadingVisible(!picture_active);
		bodyVisible(picture_active);

		counter = 0;
		counter2 = 0;
		ID = window.setTimeout('heightEnlarge()', 5);
	}

	s_picture.src = pte;
	s_close_link.firstChild.nodeValue = c;
}

function scrollVisible(vis) {
	if (vis)
		document.body.style.overflow = 'scroll';
	else
		document.body.style.overflow = 'hidden';
}

function nextPicture() {
	anotherPircture = true;
	closePicture();
	orient = 1;
}

function previousPicture() {
	anotherPircture = true;
	closePicture();
	orient = -1;
}

// Обрабатываем событие Resize
window.onresize = function resizeWindow() {
	if (picture_active) {
		win_size = screenWorkspace();
		backVisible(picture_active);
		s_body.style.top = s_table.style.top = getBtTop();
		s_body.style.left = s_table.style.left = getBtLeft();
	}
}
// --------------------------------------------------------- Увеличение
function heightEnlarge() {
	s_body.style.height = counter;
	s_table.style.height = counter;
	s_body.style.top = s_table.style.top = getBtTop();
	if (counter == body_size[1]) {
		window.clearTimeout(ID);
		ID2 = window.setTimeout('widthEnlarge()', factor);
	}
	else
		ID = window.setTimeout('heightEnlarge()', factor);
	if (counter + enlarge_speed > body_size[1])
		counter = body_size[1];
	else
		counter += enlarge_speed;
}

function widthEnlarge() {
	s_body.style.width = counter2;
	s_table.style.width = counter2;
	s_body.style.left = s_table.style.left = getBtLeft();
	if (counter2 == body_size[0]) {
		window.clearTimeout(ID2);
		pictureVisible(picture_active);
		if (enlargePicturesGalleries != null) {
			nextVisible(picture_active);
			prevVisible(picture_active);
		}
	}
	else
		ID2 = window.setTimeout('widthEnlarge()', factor);
	if (counter2 + enlarge_speed > body_size[0])
		counter2 = body_size[0];
	else
		counter2 += enlarge_speed;
}

function enlargePicture(pte, c, psize) {
	picture_active = true;
	currentPictureSrc = pte;
	preInit();
	init(pte, c, psize);
}
// --------------------------------------------------------- Уменьшение
function heightReduce() {
	s_body.style.height = counter2;
	s_table.style.height = counter2;
	s_body.style.top = s_table.style.top = getBtTop();
	if (counter2 == 0) {
		window.clearTimeout(ID2);
		bodyVisible(picture_active);
		if (!anotherPircture) {
			backVisible(picture_active);
			scrollVisible(!picture_active);
		}
		else {
			var epgl = enlargePicturesGalleries.length;
			for (var i=0; i<epgl; i++) {
				if (enlargePicturesGalleries[i].indexOf(currentPictureSrc) != -1) {
					var j = i + orient;
					if (j == -1) j = epgl - 1;
					if (j == epgl) j = 0;
					str = enlargePicturesGalleries[j].split(/['");,\s]/);
					if (str.length > 8) {
						if (str[8].length > 0)
							enlargePicture(str[1], str[5], parseInt(str[8]));
						else
							enlargePicture(str[1], str[5]);
					}
					else
						enlargePicture(str[1], str[5]);
					break;
				}
			}
		}
	}
	else
		ID2 = window.setTimeout('heightReduce()', factor);
	if (counter2 - reduce_speed < 0)
		counter2 = 0;
	else
		counter2 -= reduce_speed;
}

function widthReduce() {
	s_body.style.width = counter;
	s_table.style.width = counter;
	s_body.style.left = s_table.style.left = getBtLeft();
	if (counter == 0) {
		window.clearTimeout(ID);
		ID2 = window.setTimeout('heightReduce()', factor);
	}
	else
		ID = window.setTimeout('widthReduce()', factor);
	if (counter - reduce_speed < 0)
		counter = 0;
	else
		counter -= reduce_speed;
}

function reducePicture() {
	anotherPircture = false;
	closePicture();
}

function closePicture() {
	picture_active = false;
	counter = parseInt(s_body.style.width);
	counter2 = parseInt(s_body.style.height);
	pictureVisible(picture_active);
	if (enlargePicturesGalleries != null) {
		nextVisible(picture_active);
		prevVisible(picture_active);
	}
	if (!anotherPircture)
		enlargePicturesGalleries = null;
	ID = window.setTimeout('widthReduce()', factor);
}

// --------------------------------------------------- Loading
function loadingProgress() {
	var text='';
	for (var i = 0; i < l_counter; i++) {
		text += '.';
	}
	s_prog.firstChild.nodeValue = 'Loading' + text;
	loadingId = window.setTimeout('loadingProgress()', 100);
	if (l_counter == 1)
		orient = 1;
	if (l_counter == 6)
		orient = -1;
	l_counter += orient;

}
//=================Slider by BOPOH
var img=2;

function slideshow() {
  	var img_path="url(images/disign/top"+img+".gif)";
  	$("#picrotator").animate({opacity:0.0},700,function(){$("#picrotator").css("background-image",img_path);});
  	$("#picrotator").animate({opacity:1.0},1000);
  	img++;
  	if(img==6)
  	img=1;
	}
$(function()
{
$('div#face_logo a').hover(
function(){ $('div#face_logo a img').attr("src","images/disign/facebook2.png");},function(){
$('div#face_logo a img').attr("src","images/disign/facebook.png");
});


	setInterval(slideshow, 10000);

});