var _photos_aw = new Array();
var _photos_aw_name = new Array();
var _photos_aw_podpis = new Array();

var originalWidthAw = 298;
var originalHeightAw = 444;

var old_width_aw = 0;
var old_height_aw = 0;

var cur_width_aw = originalWidthAw;
var cur_height_aw = originalHeightAw;

$(document).ready(
    function() {
        $('#big_foto').width(originalWidthAw);
        $('#big_foto').height(originalHeightAw);
    }
);

function selectClassAwards(a, sel_class) {
    
    var cur_item = 0;
    //! Сохраняем в массив все элементы с классом переданным в переменной sel_class
    var arr = $("."+sel_class).get();
    
    _photos_aw.length = 0;
    //! Пробегаем массив элементов сохраняя в массив имена фоток
    for (var i = 0; i < arr.length; i++) {
        _photos_aw[i] = $(arr[i]).attr("href");
        _photos_aw_name[i] = $(arr[i]).attr("name");
        _photos_aw_podpis[i] = $(arr[i]).attr("podpis");
        if ( _photos_aw[i] == $(a).attr("href") )
            cur_item = i;
    }
    
    showPopup('sertPopup');
    
    showImagesAwards( cur_item );
}

var load_img = new Image();
load_img.src = '/img/loading.gif';

function showImagesAwards(direct) {
    
    old_width_aw = $('#big_foto').width();
    old_height_aw = $('#big_foto').height();
    
    $('#big_foto img').remove();
    
    $('#big_foto').prepend(load_img);
    
    $('#big_foto img').css({paddingTop:parseInt(cur_height_aw/2)});
    
    //$("#podpis_foto").attr("style","display:none");        
    
    /// Добавляем новую
    var img = new Image();
    var show_podpis = false;
    
    img.onload = function() {
        
        $('#big_foto img').remove();
        
        if (  $('#big_foto img').length == 0 ) {
        
            //! Втавляем загруженный рисунок в попап
            $('#big_foto').append(this);
        
            //! Получаем размеры вставленной картинки
            
            width_ = $('#big_foto img').width();
            height_ = $('#big_foto img').height();
            
            $('#big_foto img').hide();
            $('#big_foto img').fadeIn(300);
            
            
            var new_width = 0;
            var new_height = 0;
            
            //diff = (width_ >= originalWidthAw)?(width_/originalWidthAw):( (height_ < width_)?(width_ / originalWidthAw):(height_/originalHeightAw) );
            diff = width_ / originalWidthAw;
            
            new_width = parseInt(width_ / diff);
            new_height = parseInt(height_ / diff);
            
            $('#big_foto img').width(new_width);
            $('#big_foto img').height(new_height);
            
            $('#big_foto b.shTop').css({width:new_width+2});
            $('#big_foto b.shLeft').css({height:new_height-4});
            
            $('#big_foto').width(new_width+'px');
            $('#big_foto').height(new_height+'px');
            
            cur_width_aw = new_width;
            cur_height_aw = new_height;
            
            x = new_width - old_width_aw;
            y = new_height - old_height_aw;
            
            var new_x = $("#sertPopup").offset().left - parseInt(x/2);
            var new_y = $("#sertPopup").offset().top - parseInt(y/2);
            
            $("#sertPopup").css("left",new_x);
            $("#sertPopup").css("top",new_y);
            
        }
    };
    
    /// Колюч текущего фото
    if ( direct != 'next' && direct != 'prev' ){
        photo_key = direct;
    }

    /// Формирование ключей на след. и пред. фото
    if ( photo_key == _photos_aw.length - 1 ){
        photo_next = 0;      
    }
    else{
        photo_next = photo_key + 1;
    }
    if ( photo_key == 0 ){
        photo_prev = _photos_aw.length - 1;
    }
    else{
        photo_prev = photo_key - 1;
    }
    
    /// Вывод фото
    if ( direct == 'next' ){
        img.src = '/img/saved/'+_photos_aw[photo_next];
        photo_key = photo_next;
    }
    else if ( direct == 'prev' ){
        img.src = '/img/saved/'+_photos_aw[photo_prev];
        photo_key = photo_prev;
    }
    else{
        img.src = '/img/saved/'+_photos_aw[photo_key];
    }
    
    $("#name_foto").html(_photos_aw_name[photo_key]);
    $("#sertPopup .podpis_foto").html(_photos_aw_podpis[photo_key]);
    
    if (photo_key == 0) {
        $("#text_prev").text(_photos_aw_name[_photos_aw.length-1]);
        $("#text_next").text(_photos_aw_name[photo_key+1]);
        
    } else if (photo_key == _photos_aw.length - 1) {
        $("#text_prev").text(_photos_aw_name[photo_key-1]);
        $("#text_next").text(_photos_aw_name[0]);
    } else {
        $("#text_prev").text(_photos_aw_name[photo_key-1]);
        $("#text_next").text(_photos_aw_name[photo_key+1]);
    }
    if ( _photos_aw.length == 1 ) $('#sertPopup .controls').css({display:"none"});
      
}
