var img = 0;
var maxImg = 1;  //default to 1;
var sport = '';

function callAjax() {   
  $.ajax({
      url: '/randomimg/index.php?sport=' + sport  + '&l=' + lang,
      type: 'GET',
      dataType: 'text',
      cache: false,
      error: function(xhr) {
         alert('Ajax request problem');
      },
      success: function(response) {
           img += 1;
           var p = $('#pic' + img);
           var l = $('#piclink' + img);
           var l1 = $('#piclink1' + img);
           var e = $('#event' + img);

           var nresponse = response.split('\n');
           var test1 = (nresponse[0].lastIndexOf('/'));
           test1 = nresponse[0].lastIndexOf('/',test1-1);
           test1--;
           var str = nresponse[0].substring(0,test1);
           var flag = true;
           for (var i=1; i<img; i++) { 
              if (str == $('#pic' + i).attr('src').substring(0,test1)) {
                 flag = false;
                 img -= 1;
                 break;
              }           
           }  
           if (flag) {         
              p.attr('src', nresponse[0]);
              if (l) {
                 l.attr('href',nresponse[1]); 
              }
              if (l1) {
                 l1.attr('href',nresponse[1]); 
              }
              if(e) {
                 e.html(nresponse[2]); 
              }
           }
           if (img < maxImg) {
              callAjax();
           }
           
       }  
   });  	
}




