// определяем количество элементов.
function getWorksCount() {
    i = 0;
    
    jQuery.each($("#info1BoxContent .item"), function() {
      i = i + 1;
    });

    return i;

}

// определяем позицию текущего элемента.
function getCurrentPosition() {
    i = 0;
    j = 0;
    jQuery.each($("#info1BoxContent .item"), function() {
        i = i + 1;
        if ($(this).css("display") == "block") {
            j = i;
        }
    });

    return j;
}

// скрываем все блоки.
function hiddenAll() {
    jQuery.each($("#info1BoxContent .item"), function() {
        //$(this).animate({ height: "hide"}, "show");
        $(this).animate({ left: "-"+($(this).width()+40)+"px" }, 500)
               .animate({ height: "hide"}, 1);
        //$(this).css("display", "none");
    });
}

// скрываем  блок.
function hidden(j, i) {
    if(i == 1) {
        $("#info1BoxContent .item"+j).animate({ left: "-300px" }, 500)
                                 .animate({ height: "hide"}, 1);
    } else {
        $("#info1BoxContent .item"+j).animate({ left: "300px" }, 500)
                                 .animate({ height: "hide"}, 1);
    }
    $("#info1BoxContent .item"+j).css("left", "0px");
}

// показываем указанный блок.
function show(j, i) {
    if(i == 1) {
        $("#info1BoxContent .item"+j).css("left", "300px");
    } else {
        $("#info1BoxContent .item"+j).css("left", "-300px");
    }

    $("#info1BoxContent .item"+j).css("display","block");
    $("#info1BoxContent .item"+j).animate({ left: "0px" }, 500);
    $("#info1BoxContent .item"+j).css("display","block");
}

function onWorksNext() {
    countAll = getWorksCount();
    currentPosition = getCurrentPosition();
    nextPosition = currentPosition + 1;
    
    if(nextPosition <= countAll) {
        hidden(currentPosition, 1);
        show(nextPosition, 1);
        
        if(nextPosition < countAll) {
            $("#info1BoxRight").css("opacity","1");
        } else {
            $("#info1BoxRight").css("opacity","0.25");
        }
    } else {
        $("#info1BoxRight").css("opacity","0.25");
    }

    if(nextPosition > 1) {
        $("#info1BoxLeft").css("opacity","1");
    }
}

function onWorksPrev() {
    countAll = getWorksCount();
    currentPosition = getCurrentPosition();
    prevPosition = currentPosition - 1;

    if(prevPosition >= 1) {
        hidden(currentPosition, 0);
        show(prevPosition, 0);

        $("#info1BoxRight").css("opacity","1");
    } else {
        
    }

    if(prevPosition <= 1) {
       $("#info1BoxLeft").css("opacity","0.25");
    }
}

$(document).ready(function() {
    countAll = getWorksCount();
    
    $("#info1BoxLeft").css("opacity","0.25");

    if (countAll < 2) {
        $("#info1BoxRight").css("opacity","0.25");
    } else {
        $("#info1BoxRight").css("opacity","1");
    }
});

// закрывает блоки
function closeHoverBlocks() {
    $(".hoveredBlock").animate({ height: "hide"}, 800);
}

// показываем нужный блок
function showHoverBlock(name) {
    $("."+name).animate({ height: "show" }, 800);
}
function showVacBlock(name) {
if( $(".a"+name).find("img").attr("src")=="http://lovedesign.ru/images/v1.gif" ){
    $("."+name).animate({ height: "show" }, 300);
    $(".a"+name).find("img").attr("src", "http://lovedesign.ru/images/v2.gif");
    }
    else{closeVacBlock(name);}
}
function closeVacBlock(name) {
    $("."+name).animate({ height: "hide" }, 300);
    $(".a"+name).find("img").attr("src", "http://lovedesign.ru/images/v1.gif");
}
// показываем нужный блок
function getBlockState(block) {
    srcStr = $(block).find("img").attr("src");
    
    var r = new RegExp(/expand/);
    if (r.test(srcStr)) {
        return 1;
    } else {
        return 0;
    };
}

// меняем картинки
function changeImageBlock(state, block) {

    if (state == 1) { 
        $(block).find("img").attr("src", "../images/collapse.gif");
        $(block).find("h2").attr("class", "h2dash_green");
    } else {
        $(block).find("img").attr("src", "../images/expand.gif");
        $(block).find("h2").attr("class", "h2dash");
    }
    
}

// меняем картинки
function changeImageAllBlock(state) {

    if (state == 1) {
        $(".hoveredImage").find("img").attr("src", "../images/collapse.gif");
        $(".hoveredImage").find("h2").attr("class", "h2dash_green");
    } else {
        $(".hoveredImage").find("img").attr("src", "../images/expand.gif");
         $(".hoveredImage").find("h2").attr("class", "h2dash");
    }

}

// onHoverChange
function onHoverChange(blockName, thisBlock) {
    blockState =  getBlockState(thisBlock);
    

    if(blockState == 1) {
    closeHoverBlocks();
        showHoverBlock(blockName);
        changeImageAllBlock(0);
         changeImageBlock(blockState, thisBlock);
    }

   
}

