$(document).ready(function(){

    
    var phr = {}
    phr.arrayImg = []
    phr.strImg = '';
    phr.strImg1 = '';
    phr.strHead = '<div style="width:650px"><div id="phr" class="phr"><div class="phr_tabs"><div style="color: rgb(113, 113, 113);float:left;margin-right:50px;margin-bottom: 5px;" class="phr_prev">&lt; Попередня</div><div style="position:relative; width:342px;float:left;">';
    phr.strEnd1 = '</div><div class="phr_next" style="float:left; margin-left:50px;">Наступна &gt;</div></div><div class="clear"></div><div class="phr_frame" style="border:1px solid #D3D2CF;"><div class="old_image nofloat"><img alt="" src="';
    phr.strEnd2 = '"/><div class="clear"></div></div><span class="img_author">'+''+'</span></div></div></div>';

    phr.result = '';


    phr.strText = $('.article_content').html();
    phr.naiden = phr.strText.search(/<table[^>]+phr_separator/i);
    phr.strText = phr.strText.substr(0,phr.naiden);

    phr.source = $(".article_content img").each(function(i){
        phr.arrayImg[i] = this.src;
        phr.num = 1+i;
        var suffix = '';
        if(i==0){
            suffix = 'phr_active';
            phr.result = phr.strHead;
            phr.strImg1 = this.src;
        } else {
            suffix = '';
        }

        phr.result += '<div class="phr_navcell '+suffix+'">'+phr.num+'</div>';

        phr.strImg += '<div class="old_image"><img  src="'+ this.src+'" alt=""/></div>';
    });
    
    $(".article_content").html(phr.strText+phr.result+phr.strEnd1+phr.strImg1+phr.strEnd2).show().disableSelection();
    
    
    $(".phr_navcell").live('click', function(){
        $('.old_image img').attr('src',phr.arrayImg[$(this).text()-1]);
        $('.phr_active').removeClass('phr_active');
        $(this).addClass('phr_active');
    })


    $(".phr_prev").live('click', function(){
        var current = $('.phr_active').text();
        if(current==1){
            $('.phr_navcell').eq($('.phr_navcell').size()-1).click();
        } else {
            $('.phr_navcell').eq(current-2).click();
        }
    })

    $(".phr_next").live('click', function(){
        var current = $('.phr_active').text();

        if(current == $('.phr_navcell').size()){
            $('.phr_navcell').eq(0).click();
        } else {
            $('.phr_navcell').eq(current).click();
        }
    })

})
