
$(document).ready(function(){

	$(".it a img").css("visibility", "hidden");

	$(".it a img").each(function(e) {
		if (this.complete) {
			$(this).css("top", (146-$(this).attr("height"))/2);
			$(this).css("visibility", "visible");
		} else {
			$(this).bind("load", function(e) {
				var el=$(e.target);
				$(el).css("top", (146-$(el).attr("height"))/2);
				$(el).css("visibility", "visible");
				$(el).unbind("load");
			});
		}
	});

	$("div.it a").bind("click", function(){
		open($(this));
		return false;
	});

	hint();

});

function open($el) {

	var code, width, height, rejim, size;
	
		_build();
		imgPreloader = new Image();
		imgPreloader.src = $el.attr("href");
		imgPreloader.onload = function(){
			
			size = _fitToViewport(imgPreloader.width, imgPreloader.height);
			
			width = size['contentWidth'];
			height = size['contentHeight'];
			
			code = '<div class="margin"><div class="clear"><a href="#" class="close"><img src="/bitrix/templates/main/images/prettyPhoto/btnClose.gif"/></a><a href="#" class="btnexpand">&nbsp;</a><a href="#" class="btncontract">&nbsp;</a></div><img src="' + $el.attr("href") + '" id="main_img" width="'+ size['width'] +'" height="'+ size['height'] +'"/><div class="navig_info"></div></div>';
			$("#info").html(code);
			$("#info div.navig_info").html($el.parent().children().eq(3).html());
			
			$("a.btnexpand").bind("mouseover", function(){$(this).css("background-position", "0px -21px");});
			$("a.btnexpand").bind("mouseout", function(){$(this).css("background-position", "0px 0px");});
			$("a.btnexpand").bind("mouseover", function(){$(this).css("background-position", "0px -21px");});
			$("a.btnexpand").bind("mouseout", function(){$(this).css("background-position", "0px 0px");});
			
			
			if (size['resized']){
				
				$("a.btnexpand").css("display", "inline");
			
				$("a.btnexpand").attr("href", $el.attr("href")).attr("target", "_blank");
			
				/*$("a.btnexpand").bind("click", function(){
					$("#main_img").width(imgPreloader.width).height(imgPreloader.height);
					$("#info").css("display", "none");
					_show(imgPreloader.width + 20, imgPreloader.height + 67, 1);
					$("a.btnexpand").css("display", "none");
					$("a.btncontract").css("display", "inline");
					return false;
				});
				
				$("a.btncontract").bind("click", function(){
					$("#main_img").width(size['width']).height(size['height']);
					$("#info").css("display", "none");
					_show(size['contentWidth'], size['contentHeight'], 1);
					$("a.btncontract").css("display", "none");
					$("a.btnexpand").css("display", "inline");
					return false;
				});*/
			}
			
			window.setTimeout( function(){
				_show(width, height, 1);
				$("#info div.navig_info").fadeIn("normal");		
				$("#info div.navig_info a").bind("click", function(){
					re_open($(this));
					return false;
				});					
			}, 1000);
			$('a.close').bind('click', function(){ close(); return false; });
		}

	return false;
};

function re_open ($el){
	
	$("a.btnexpand").css("display", "none");
	$("a.btncontract").css("display", "none");	
	
	$("#info div.navig_info a").unbind("click");
	$("a.btnexpand").unbind("click");
	$("a.btncontract").unbind("click");	
	imgPreloader2 = new Image();
	imgPreloader2.src = $el.attr("href");
	$("div.contentHolder .loaderIcon").css("display", "block");
	$("div.contentHolder .loaderIcon").css("left", $("div.contentHolder").width()/2-50).css("top", $("div.contentHolder").height()/2-50);
	$("#info").css("display", "none");
	imgPreloader2.onload = function(){

		size = _fitToViewport(imgPreloader2.width, imgPreloader2.height);
			
		width = size['contentWidth'];
		height = size['contentHeight'];
		
		$("a.btnexpand").bind("mouseover", function(){$(this).css("background-position", "0px -21px");});
		$("a.btnexpand").bind("mouseout", function(){$(this).css("background-position", "0px 0px");});
		$("a.btncontract").bind("mouseover", function(){$(this).css("background-position", "0px -21px");});
		$("a.btncontract").bind("mouseout", function(){$(this).css("background-position", "0px 0px");});		
			
		if (size['resized']){
				
			$("a.btnexpand").css("display", "inline");
		
			$("a.btnexpand").bind("click", function(){
				$("#main_img").width(imgPreloader2.width).height(imgPreloader2.height);
				$("#info").css("display", "none");
				_show(imgPreloader2.width + 20, imgPreloader2.height + 67, 1);
				$("a.btnexpand").css("display", "none");
				$("a.btncontract").css("display", "inline");
				return false;
			});
				
			$("a.btncontract").bind("click", function(){
				$("#main_img").width(size['width']).height(size['height']);
				$("#info").css("display", "none");
				_show(size['contentWidth'], size['contentHeight'], 1);
				$("a.btncontract").css("display", "none");
				$("a.btnexpand").css("display", "inline");
				return false;
			});
		}

		_show(width, height, 1);
		$(".negative").removeClass("negative");
		$el.addClass("negative");			
		$("#info img#main_img").attr("src", $el.attr("href")).width(size['width']).height(size['height']);			
		window.setTimeout( function(){$("#info").fadeIn("normal");}, 1000);
		$("#info div.navig_info a").bind("click", function(){
			re_open($(this));
			return false;
		});		
		$('a.close').bind('click', function(){ close(); return false; });
		$('a#negative').bind("click", function (){ return false});
	}
}

function _show(width, height, rejim){
	
	var pos = _getCenterPos(width, height);
	
	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 _build(){

	if($.browser.msie && $.browser.version == 6){
		$('select').css('visibility','hidden');
	};

	backgroundDiv = "<div class='backgroundDiv'></div>";
	$('body').append(backgroundDiv);
	$('div.backgroundDiv').css('height',$(document).height());		
	contentHolder = '<div class="contentHolder"><div class="loaderIcon"></div><div id="info"></div></div>';
		
	$('body').append(contentHolder);

	$('.contentHolder').css({'opacity': 0});

	var pos = _getCenterPos($("div.contentHolder").width(), $("div.contentHolder").height());
	
	$('div.backgroundDiv').css('opacity',0).fadeTo('normal', 0.7, 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();
			$("select").css("display", "inline");
	});
	
};
		
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;
		if (projectedTop + h > $(document).height()) projectedTop = $(document).height() - h - 50;		
		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() < $(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());
	$("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};
};


function _fitToViewport(width,height){
	hasBeenResized = false;
		
	contentHeight = height + 67;
	contentWidth = width + 20;
	
	imageWidth = width;
	imageHeight = height;

	if($.browser.opera) {
		windowHeight = window.innerHeight;
		windowWidth = window.innerWidth;
	}else{
		windowHeight = $(window).height();
		windowWidth = $(window).width();
	};
		
	if ((contentWidth > windowWidth) || (contentHeight > windowHeight)) {
		hasBeenResized = true;

		var xscale = (contentWidth + 100) / windowWidth;
		var yscale = (contentHeight + 100) / windowHeight;

		if (yscale>xscale){
			imageWidth = Math.round(width * (1/yscale));
			imageHeight = Math.round(height * (1/yscale));
		} else {
			imageWidth = Math.round(width * (1/xscale));
			imageHeight = Math.round(height * (1/xscale));
		};
		
		contentHeight = imageHeight + 67;
		contentWidth = imageWidth + 20;
	};

	return {
		width:imageWidth,
		height:imageHeight,
		contentHeight:contentHeight,
		contentWidth:contentWidth,
		resized:hasBeenResized
	};
};

function callei() {
    thisMovie("map").print_map();
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}

function hint(){
	$("div.services div.sblock").each(function(){
		hint_target($(this), $(this));												   
	});
	
	$("a.legal").each(function(){
		hint_target($(this), $("div.services div.sblock").eq(0));
	});
	
	$("a.consulting").each(function(){
		hint_target($(this), $("div.services div.sblock").eq(1));
	});
	
	$("a.accountancy").each(function(){
		hint_target($(this), $("div.services div.sblock").eq(2));
	});
	
	$("a.initiative").each(function(){
		hint_target($(this), $("div.services div.sblock").eq(3));
	});	
		
}

function hint_target(el_hover, el_holder){
	el_hover.bind("mouseover", function(){
		var hint = el_holder.find("div.hint").show();
		el_hover.mousemove(function(e){
			hint.css({"left":e.pageX-$("div.main").offset().left+9, "top":e.pageY+15});
		});
	}).bind("mouseout", function(){
		el_holder.find("div.hint").hide();
	});
}

