/* ATTACH LENT */
    function CLent( _prm )
    {
        var $lent  = _prm.lent;
        var $pages = _prm.pages;

        var $next = _prm.next;
        var $prev = _prm.prev;

        var visibleBlocks = _prm.visibleBlocks || 2;
        var blockQty = _prm.blockQty;
        var step     = _prm.step;
        var curPage  = 1;

/**/
        this.SetBlockQty = function( _qty ){ blockQty = _qty; } 
        this.SetStep = function( _step ){ step = _step; }
/**/
        this.Go2CurPage = function(){
			this.ScrollCurPage();
			this.RefreshView();
        }
/**/
        this.ScrollCurPage = function()
        { $lent.animate( { left : -step*(curPage-1) }, 'fast' ); }
/**/
        this.RefreshView = function(){
			if($pages)
				$pages.find('li').eq(curPage-1).addClass('cur').siblings().removeClass('cur');

            if( $next != null )
                $next.toggleClass( 'hide', curPage+visibleBlocks-1 == blockQty );
            if( $prev != null )
                $prev.toggleClass( 'hide', curPage == 1 );

			  console.log( blockQty )
			  console.log( curPage+visibleBlocks )
        }
/**/
        this.BindControl = function(){
            this.BindPages();
            this.BindBtn();
        }
/**/
        this.BindPages = function(){ var isThis = this;
			if($pages)
				$pages.find('li').each( function(i){
					$(this).click( function(){
						curPage = i+1; // Set cur Page
						isThis.Go2CurPage();
					});
				});
        }
/**/
        this.BindBtn = function(){ var isThis = this;
            if( $next != null )
                $next.click( function(){ if( curPage<(blockQty-visibleBlocks+1) ){ curPage++; isThis.Go2CurPage(); } } );

            if( $prev != null )
                $prev.click( function(){ if(curPage>1){ curPage--; isThis.Go2CurPage(); } } );
        }
/**/
		  this.BuildNavi = function(){
            var html = "";
            var pagesQty = Math.ceil( blockQty/visibleBlocks );
            var i;


            if( blockQty > 0 ){
                html += "<li class='cur'>1</li>"

                for(i=2; i <= pagesQty; i++)
                    html += "<li>"+i+"</li>";
            }

            $pages.html( html );
		  };
/**/
        $lent.css( {'position' : 'relative', 'left' : 0 } );
/**/
    }



$( function() {
	// Биндимся
	$('div.servicesDoor').click(function(){
		$('div.darkness').attr( 'style', 'display:block;' );
		$('div.servicesPopup').attr( 'style', 'display:block;' );
	});

   $('div.close').click(function(){
		$('div.servicesPopup').attr( 'style', 'display:none;' );
		$('div.darkness').attr( 'style', 'display:none;' );
	});

   $('div.mapSiteDoor').click(function(){
		$('div.mapSite').attr( 'style', 'display:block;' );
	});

	$('#closeMap').click(function(){
		$('div.mapSite').attr( 'style', 'display:none;' );
		window.location.hash='';
	});

	$('div.showAll').bind('click',function(){
		if( $('div.priceBlock').is(':hidden') ) {
			$('div.priceBlock').attr( 'style', 'display:block' );
			$('span.nameDoor').parent('li').attr('class','cur');
			$(this).html('<span>Свернуть все</span>');
		}else{
			$('div.priceBlock').toggle();
			$(this).html('<span>Развернуть все</span>');
			$('span.nameDoor').parent('li').attr('class','');
		}
	});

	$('span.nameDoor').bind('click',function(){
		$(this).siblings('div.priceBlock').toggle('slow');
		$(this).parent('li').toggleClass('cur');
	});

	if( window.location.hash=='#sitemap' ){ 
		$('div.mapSite').attr( 'style', 'display:block;' );
	}


	// lents
	(function(){

		$('dl.mastersList dt ~ dd div.photoLenta').each(function(){
			var $div = $(this);

			var $prev  = $div.find('div.prev');
			var $next  = $div.find('div.next');
			var $lenta = $div.find('div.lenta');

			var $blocks = $lenta.find('div.block')

			if( $blocks.length > 0 ){
			var lent = new CLent( {
				lent     : $lenta,
				next     : $next,
				prev     : $prev,

				visibleBlocks : 3,
				blockQty : $blocks.length,
				step     : $blocks.outerWidth()
			});

			lent.BindControl();
			} else {
				$prev.hide();
				$next.hide();
			}
		});

	})();


	$("a.zoom").fancybox({/*
		  'scrolling'   : 'no'
      , 'zoomSpeedIn' : 500
      , 'zoomSpeedOut': 500
      , 'hideOnContentClick': false
		, onComplete : function(){ debugger; }*/
	});

	$('.iframe').removeClass('iframe').each(function(){ $(this).attr('src', $(this).attr('src')+'#'  ) } );
});

