var currentPage = 1;

function nextPage(){

    $("#fileslist_" + currentPage).hide();
    currentPage++;
    if(currentPage > pages){
        currentPage = 1;
    }
    $("#fileslist_" + currentPage).show();

    $("#currentpage").html(currentPage + '/' + pages);

}

function prevPage(){

    $("#fileslist_" + currentPage).hide();
    currentPage--;
    if(currentPage < 1){
        currentPage = pages;
    }
    $("#fileslist_" + currentPage).show();

    $("#currentpage").html(currentPage + '/' + pages);
    
}

function changePage(id){
    $("#fileslist_" + currentPage).hide();
    currentPage = id;
    $("#fileslist_" + currentPage).show();
    $("#currentpage").html(currentPage + '/' + pages);
}

var currentEventFile = 0;
var imgload = new Image();

function changeEventFile(id){
    var eventimg = $("#eventImage");
    currentEventFile = id;
    src = files[id][0];
    desc = files[id][1];
    if( $('.selectedefh').hasClass('buyit') ){
        $('.selectedefh').removeClass('selectedefh').addClass('buyefh');
    }else{
        $('.selectedefh').removeClass('selectedefh').addClass('efh');
    }
    $("#eventfileholder_" + id).removeClass('buyefh').addClass('selectedefh');
    $("#eventImageDesc").html(desc);
    imgload.src = src;
    eventimg.fadeOut(100, function(){
        $(this).attr('src', src).load(function(){
            $(this)
            .fadeIn(500);
        });
    });
}


function vKosarico( idf ){

    if(idf == null){
        idf = files[currentEventFile][2];
    }
    
    $.ajax({
      type: 'POST',
      url: URL_BASE + "Medifoto/VKosarico/",
      data: {
          idf: idf
      },
      success: function(data){
          if(data.status >= 1){
            alert('Datoteka je bila dodana v košarico.');
            $("#eventfileholder_" + currentEventFile).addClass('buyit');
          }else if(data.status == -1){
              alert('To datoteko ste že kupili.');
          }
        },
      dataType: 'json'
    });

}


function preloadFiles(){
    for(var i=0; i<files.length-1; i++){
        imgload.src = files[i][0];
    }
}

function prevEventFile(){
    currentEventFile--;
    if(currentEventFile < 0){
        currentEventFile = files.length-1;
    }
    changeEventFile( currentEventFile );
    properPage = Math.ceil((currentEventFile+1)/8);
    if(properPage != currentPage){
        changePage(properPage);
    }
}

function nextEventFile(){
    currentEventFile++;
    if(currentEventFile >= files.length){
        currentEventFile = 0;
    }
    changeEventFile( currentEventFile );
    properPage = Math.ceil((currentEventFile+1)/8);
    if(properPage != currentPage){
        changePage(properPage);
    }
}

$(function(){
    $(".reflectside").reflect({
        height: 0.2
    });
});

function getCalendarInfo(id){
    
    $.ajax({
      type: 'POST',
      url: URL_BASE + "Koledar/Podatki",
      data: {
          id: id
      },
      success: function(data){
        if(data.id != -1){
            $('#eventDesc').html('<h1 style="margin-bottom: 5px;">' + data.name + '</h1><h5 style="margin-bottom: 5px;">' + data.date +  '</h5><h3 style="margin-bottom: 5px;">' + data.description)
            .slideDown();
        }
      },
      dataType: 'json'
    });
        
}

function getCalendar( m, y ){
    
    $.ajax({
      type: 'POST',
      url: URL_BASE + "Koledar/Koledar",
      data: {
          m: m,
          y: y
      },
      success: function(data){
        var kt = $('#koledarTabela');
        var tmp = '';
        kt.fadeOut(200, function(){

            kt.html("");
            var divCount = 1;
            for(var i=1; i<=data.startDay; i++){
                kt.append('<div style="float: left; width: 110px; background-color: #ffffff; border: 1px solid #ffffff; margin: 0px 10px 0px 0px; padding: 5px;">&nbsp;</div>');
                divCount++;
            }
            for(i=1; i<=data.monthDays; i++){
                tmp = '';
                if(data.events[i + '-' + m + '-' + y] != null){
                    for(var j = 0; j<data.events[i + '-' + m + '-' + y].length; j++){
                        //' . $e->name . '</a><br />
                        tmp += '<a onclick="getCalendarInfo(' + data.events[i + '-' + m + '-' + y][j].id + ');" style="font-size: 0.8em; cursor: pointer;">' + data.events[i + '-' + m + '-' + y][j].name + '</a><br />';
                    }
                }
                kt.append('<div style="float: left; width: 110px; border: 1px dotted #e0e0e0; margin: 0px 10px 0px 0px; padding: 5px;">' +
                    '<b style="font-size: 1.5em;">' + i + '</b><br />' + tmp + '</div>');

                if(divCount%7==0){
                    kt.append('<div class="clear" style="height: 15px;"></div>');
                }

                divCount++;
            }

            kt.fadeIn();

        });
      },
      dataType: 'json'
    });
    
}

function updateCalendar(){
    getCalendar( $('#calendarMonth').val(), $('#calendarYear').val() );
}

function _calculateMFT(ide, idm){
    
    if(timerCMFT != null){
        clearTimeout(timerCMFT);
    }
    
    $.ajax({
      type: 'POST',
      url: URL_BASE + "Medifoto/CalculateMFT",
      data: {
          eur: $(ide).val()
      },
      success: function(data){
            $(idm).html(data.mft);
        },
      dataType: 'json'
    });
    
}

timerCMFT = null;

function calculateMFT( ide, idm ){    
    
    if(timerCMFT != null){
        clearTimeout(timerCMFT);
    }
    timerCMFT = setTimeout("_calculateMFT('" + ide + "', '" + idm + "')", 500);
    
}

function predracun( ide, idm, res ){
    
    if( $('#calculate-mft').html() == '0' ){
        alert('V okno "Napolni svoj MF račun za " vpišite željeno vsoto nato pa kliknite [Izdelaj predračun].');
        return false;
    }
    
    $.ajax({
      type: 'POST',
      url: URL_BASE + "Medifoto/Predracun",
      data: {
          eur: $(ide).val(),
          mft: $(idm).html()
      },
      success: function(data){
          
          $('#predracun-form').fadeOut(400, function(){
                $(res).hide().html(data).show();
          });
            
        },
      dataType: 'html'
    });

}

function predracunPoslji( ide, idm, res ){

    $("#predracunPoslji").val('V delu ...');
    $("#predracunPoslji").attr('disabled', 'disabled');
    
    $.ajax({
      type: 'POST',
      url: URL_BASE + "Medifoto/PredracunPoslji",
      data: {
          eur: $(ide).val(),
          mft: $(idm).html()
      },
      success: function(data){
            $(res).html(data);
        },
      dataType: 'html'
    });

}

function kosaricaOdstrani(idf){

    $.ajax({
      type: 'POST',
      url: URL_BASE + "Medifoto/IzKosarice",
      data: {
          idf: idf
      },
      success: function(data){
            if(data.status == 1){
                $('#file-' + idf).fadeOut(500, function(){ $(this).remove(); });
                $('#files-price').html( data.price );
            }
        },
      dataType: 'json'
    });

}

