$(document).ready(function(){
	InitLightBox();
});
function InitLightBox(){
	var _light = $('.popup-block');
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		_light.hide();
	}else{
		_light.css({opacity:0});
	}
	var _lay = $('<div class="overlay"></div>').css({opacity:0,height:$('#wrapper').height()}).appendTo($('body')).hide();
	var _close=$('a.close');
	var _view = $('.block-logos ul>li');
	var _next = $('.popup-block a.link-next');
	var _prev = $('.popup-block a.link-prev');
	var i,_i;
	var _f = true;
	_view.each(function(i){
		var _hold = $(this);
		function ShowPopup(i){
			if(jQuery.browser.msie && jQuery.browser.version < 7){
				_f = true;
				_light.each(function(){
					if ($(this).is(':visible')){
						$(this).hide();
					}
				});
				_light.eq(i).appendTo('body').show();
				LightPlace();
				if (!_lay.is(':visible')){_lay.css({opacity:0.7}).show();}
			}else{
				_light.each(function(){
					if ($(this).is(':visible')){
						$(this).animate({opacity:0}, {queue:false, duration:600, complete:function(){$(this).hide();_f = true;}})
					}
				});
				_light.eq(i).appendTo('body').show().animate({opacity:1}, {queue:false, duration:600});
				LightPlace();
				if (!_lay.is(':visible')){_lay.show().animate({opacity:0.7}, {queue:false, duration:600});}
			}
			_i = i;
		};
		function LightPlace(){
			var _w = _light.width();
			var _h = _light.height(); 
			if (window.innerHeight){var _wx = window.innerWidth; var _wy = window.innerHeight;}
			else{_wx = document.documentElement.clientWidth; _wy = document.documentElement.clientHeight;};
			if ($('body > div').eq(0).height()<_wy){
				_lay.css({height:_wy})
			}else{
				_lay.css({height:($('body > div').eq(0).height())});
			};
			_light.css({left:(_wx-_w)/2, top:(_wy-_h)/2+$(document).scrollTop()});
		};
		
		_lay.click(function(){
			if(jQuery.browser.msie && jQuery.browser.version < 8){
				_light.hide();
				_lay.hide();
			}else{
				_light.animate({opacity:0}, {queue:false, duration:600, complete:function(){$(this).hide();}});
				_lay.animate({opacity:0}, {queue:false, duration:600, complete:function(){$(this).hide();}});
			}
			return false;
		});
		_close.click(function(){
			if(jQuery.browser.msie && jQuery.browser.version < 8){
				_light.hide();
				_lay.hide();
			}else{
				_light.animate({opacity:0}, {queue:false, duration:600, complete:function(){$(this).hide();}});
				_lay.animate({opacity:0}, {queue:false, duration:600, complete:function(){$(this).hide();}});
			}
			return false;
		});
		_hold.click(function(){
			ShowPopup(i);
			_next.unbind('click');
			_prev.unbind('click');
			_next.click(function(){
			if (_f){
				_f = false;
				_i++; if(_i>=_light.length){_i=0}
				ShowPopup(_i);
			}	
			return false;
		});
		_prev.click(function(){
			if (_f){
				_f = false;
				_i--; if(_i<=-1){_i=_light.length-1}
				ShowPopup(_i);
			}
			return false;
		});
			
			return false;
		});
	});
};



