var elements = new Array();

$(document).ready(function(){
	$("div.image-block div.item").bind("mouseover", function(){
		$(this).css("border-color", "#fff");
	}).bind("mouseout", function(){
		$(this).css("border-color", "#00b9ff");
	});
	
	$("div.image-block div.item a").bind("click", function(){
		_open($(this));
		return false;
	});

	var i = 0;
	$("div.image-block div.item").each(function() {
		elements[i] = $(this).children();	
		i++;
	});
	
	$("a.aImg, div.bTxt a").bind("click", function(){
		_open_data($(this).parents("div.face"));
		return false;
	});
});

function nextprev ($el) {
		var i = 0;
		
		for (i = 0; i < elements.length; i++ ) {
			if 	($el.attr("href") == elements[i].attr("href")){
				if (i == 0) return {prev:'none', next: elements[i+1]};
				if (i == elements.length-1) return {prev: elements[i-1], next:'none'};
				return {prev: elements[i-1], next: elements[i+1]};
			}
		}
}

function nextprev_data ($el) {
		var i = 0;
		
		for (i = 0; i < $("div.face").length; i++ ) {
			if 	($el.find("img").attr("src") == $("div.face").eq(i).find("img").attr("src")){
				if (i == 0) return {prev:'none', next: $("div.face").eq(i+1)};
				if (i == $("div.face").length-1) return {prev: $("div.face").eq(i-1), next:'none'};
				return {prev: $("div.face").eq(i-1), next: $("div.face").eq(i+1)};
			}
		}
}

function _open($el) {

	var np = nextprev($el);
	$prev = np['prev'];
	$next = np['next'];
	
	var code, width, width_img, height, height_img, rejim, simv = 0, simvol = 0;

	if ($("div.backgroundDiv").length == 0) _build();
	else _show(150, 150, 0);			
	
	imgPreloader = new Image();
	imgPreloader.src = $el.attr("href");
	imgPreloader.onload = function(){
		code = '<div class="margin"><div class="lefthalf">&nbsp;</div><div class="righthalf">&nbsp;</div><img src="' + $el.attr("href") + '" id="main_img"/></div><div class="btm"><a class="prev" href="#">&nbsp;</a><a class="next" href="#">&nbsp;</a><a class="close" href="#">&nbsp;</a></div>';
		width = imgPreloader.width + 20;
		height_img = imgPreloader.height;
		height = imgPreloader.height + 20;
		$("#info").html(code);
		window.setTimeout( function(){
			_show(width, height, 1);
			naves(width - 26, height_img, $next, $prev);
		}, 1000);
	}
	
return false;
};

function _open_data($el) {

	var np = nextprev_data($el);
	$prev = np['prev'];
	$next = np['next'];
	
	var code, width, width_img, height, height_img, rejim, simv = 0, simvol = 0;

	if ($("div.backgroundDiv").length == 0) _build();
	else _show(150, 150, 0);			
	
	imgPreloader = new Image();
	imgPreloader.src = $el.find("div.info img").attr("src");
	imgPreloader.onload = function(){
		code = '<div class="margin">'+ $el.find("div.info").html() + '</div><div class="btm"><a class="prev" href="#">&nbsp;</a><a class="next" href="#">&nbsp;</a><a class="close" href="#">&nbsp;</a></div>';
		width = 620;
		//height_img = imgPreloader.height;
		height = 20 + imgPreloader.height;
		$("#info").html(code);
		window.setTimeout( function(){
			_show(width, height, 1);
			naves_data($next, $prev);
		}, 1000);
	}
	
return false;
};

function naves_data($next, $prev){
	$('a.close').bind('click', function(){ _close(); return false; });
	
	if ($prev == 'none'){
		$('a.prev').hide();
		$('div.lefthalf').css("cursor", "default");
	}
	else $('a.prev, div.lefthalf').bind('click', function(){
		_open_data($prev);
		return false;
	});
	
	if ($next == 'none'){
		$('a.next').hide();
		$('div.righthalf').css("cursor", "default");		
	}
	else $('a.next, div.righthalf').bind('click', function(){
		_open_data($next);
		return false;
	});
}



function _show(width, height, rejim){

	var pos = _getCenterPos(width, height + 36);
	
	if (!rejim) $("div.contentHolder #info").css("display", "none");
	else $("div.contentHolder .loaderIcon").css("display", "none");

	$('div.contentHolder').animate({
				'width': width,
				'height': height,
				'top': pos['top'],
				'left': pos['left']
			}, "normal", function(){
				if (rejim) $("div.contentHolder #info").fadeIn("normal");
				else $("div.contentHolder .loaderIcon").fadeIn("normal");
			});
}

function naves(width, height, $next, $prev){
	$('a.close').bind('click', function(){ _close(); return false; });
	
	$("div.lefthalf, div.righthalf").css({height: height, cursor: "pointer"});
	
	if ($prev == 'none'){
		$('a.prev').hide();
		$('div.lefthalf').css("cursor", "default");
	}
	else $('a.prev, div.lefthalf').bind('click', function(){
		_open($prev);
		return false;
	});
	
	if ($next == 'none'){
		$('a.next').hide();
		$('div.righthalf').css("cursor", "default");		
	}
	else $('a.next, div.righthalf').bind('click', function(){
		_open($next);
		return false;
	});
}


function _build(){

	backgroundDiv = "<div class='backgroundDiv'></div>";
	$('body').append(backgroundDiv);
	$('div.backgroundDiv').css('height',$(document).height());		
	contentHolder = '<div class="contentHolder"><div class="loaderIcon">&nbsp;</div><div id="info"></div></div>';
		
	$('body').append(contentHolder);

	$('.contentHolder').css({'opacity': 0});

	var pos = _getCenterPos($("div.contentHolder").width(), $("div.contentHolder").height() + 36);
	
	$('div.backgroundDiv').css('opacity',0).fadeTo('normal', 0.8, function(){
		$('div.contentHolder').css('opacity',0).fadeTo('normal', 1);
		$('div.contentHolder').css({left: pos['left'], top: pos['top']});	
	});

	window.setTimeout( function(){
		$('div.backgroundDiv').bind('click',function(){
			_close();
		});
	}, 1000);

};
	
function _close(){
	$('div.contentHolder, div.backgroundDiv').fadeOut('normal', function(){
			$('div.backgroundDiv').remove();
			$('div.contentHolder').remove();
	});
};
		
function _getCenterPos(w, h){
	
		var scrollPos = _getScroll();
			
		if($.browser.opera) {
			windowHeight = window.innerHeight;
			windowWidth = window.innerWidth;
		}else{
			windowHeight = $(window).height();
			windowWidth = $(window).width();
		};
			
		projectedTop = (windowHeight/2) + scrollPos['scrollTop'] - h/2;
		if (projectedTop < 0) projectedTop = 25;
		projectedLeft = (windowWidth/2) + scrollPos['scrollLeft'] - w/2;
		
		return {top: projectedTop, left: projectedLeft};
};

$(window).resize(function(){ if($(".contentHolder").size() > 0) _reshow() });
$(window).scroll(function(){ if ($("div.contentHolder").height()+36 < $(window).height()) _reshow(); });

$(document).keyup(function(e){
	switch(e.keyCode){
		case 27:
			_close();
			break;
	};
});

function _reshow() {
	var pos = _getCenterPos($("div.contentHolder").width(), $("div.contentHolder").height() + 36);
	$("div.contentHolder").css("top", pos['top']).css("left", pos['left']);
}

function _getScroll(){
	scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
	scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
	return {scrollTop:scrollTop,scrollLeft:scrollLeft};
};
