$(document).ready(function(){
//    console.log('$IS_ROOT_PAGE ' + $IS_ROOT_PAGE);
//    console.log('$IS_PRODUCT_PAGE ' + $IS_PRODUCT_PAGE);

    var $container = $("#FishEyeContainer");
		if($IS_ROOT_PAGE || $IS_PRODUCT_PAGE)
			var maxScalePct = 60;
		else
			var maxScalePct = 40;
    $container.FishEye({
        fishEyeItemName: ".FishEye",
        maxScalePct: maxScalePct,
        scaleStepPct: 30
    });

    // выравниваем элементы
    function update_position($items){
        var items_length = $items.size();
        var offset = 100;
		if($IS_ROOT_PAGE || $IS_PRODUCT_PAGE)
	        var step = 190;
		else
		    var step = 200;

        for(var i=0; i<items_length; i++){
            var position = $($items[i]).attr('position');
            if(!position){
                position = i - Math.floor(items_length/2);
            }
            var left = position * step + offset;
            var $item = $($items[i]);
            $item.attr('position', String(position));
            //$item.css('left', left);

            $item.animate({
                "left": left+"px"
            }, 1000);

            /*
            console.log(String($item.css('left')).replace(/\D/g, ''));

            left = left - parseInt(String($item.css('left')).replace(/\D/g, ''));

            $item.animate(
                    {
                        _left: (left>0?'+=':'-=') + Math.abs(left) + 'px',
                        left: left + 'px'
                    }
            );
*/

        }
    }

    var $items = $(".FishEye");
    update_position($items);

    if($IS_ROOT_PAGE)
    	$items.each(function(index){
        $(this).bind(
            "click",
            function(event){
                event.preventDefault();


                $('.product_carusel').hide();

                var $item = $($items.get(index));
                var product_info = $item.next();
                $('.current_product').attr('href', $item.parent('a').attr('href'))
                var title = $('.title', product_info).text();
                var slogan = $('.slogan', product_info).text();
                var product_id = product_info.attr('_id');
                $('#carusel_' + product_id).show();
                $('.current_product .info .title').text(title);
                $('.current_product .info .slogan').text(slogan);
                //console.log(title, slogan);
                $('.current_product .image').css('background-image', 'url('+$item.attr('src')+')');

                return false;
            }
        );

    });

    $arrow_right = $("#body_trade #arrow-right");
    $arrow_right.click(function(event){
        if($IS_ROOT_PAGE || $IS_PRODUCT_PAGE)
		    return;
        var items_length = $items.size();
        var first_position = -Math.floor(items_length/2);
        var end_position = (items_length - 1) - Math.floor(items_length/2);

        for(var i=0; i<items_length; i++){
            var $item = $($items[i]);
            var current_position = +$item.attr('position');
            var next_position = current_position + 1;

            if(end_position == current_position){
                next_position = first_position;
            }

            //console.log(current_position, "->", next_position);

            $item.attr('position', next_position);

        }

        update_position($items);

    });

    $arrow_left = $("#body_trade #arrow-left");
    $arrow_left.click(function(event){
		if($IS_ROOT_PAGE || $IS_PRODUCT_PAGE)
			return;

        var items_length = $items.size();
        var first_position =  - Math.floor(items_length/2);
        var end_position = (items_length - 1) - Math.floor(items_length/2);

        for(var i=0; i<items_length; i++){
            var $item = $($items[i]);
            var current_position = +$item.attr('position');
            var next_position = current_position - 1;

            if(first_position == current_position){
                next_position = end_position;
            }

            //console.log(current_position, "->", next_position);

            $item.attr('position', next_position);

        }

        update_position($items);

    });
	if($IS_ROOT_PAGE)
	    $(".FishEye:first").trigger('click');

    $('.product_carusel').CloudCarousel({
            xPos: 290,
            yPos: 40,
            buttonLeft: $("#arrow-left"),
			buttonRight: $("#arrow-right"),
	        reflHeight: 40,
	        yRadius: 1,
	        mouseWheel: true
        });
   
});

