// motor-sport.hu javascript - require('jquery')

// minden .gallery class-u anchor-tag egy popup galeriat fog nyitni
function bind_gallery_popup() {
  $('a.gallery').click(function() {
    window.open(this.href,'mgallery','width=793px,height=685px,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
    return false;
  });
};

// segedmetodus, ami megmondja, hol van az adott elem (ppk az isten - http://www.quirksmode.org/js/findpos.html)
function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
  }
  return [curleft,curtop];
}

// a kezdooldalon levo vfolder lapozo logikajat definialja - ajax es lista vegenek/elejenek erzekelese, lekezelese
function activate_vfolder_list(offset,count,max) {
  if (offset > 0) {
    $('ul#vfolder_list li.prev').html('<img src="http://static.motor-sport.hu/images/media_arrow_left.png" style="cursor:pointer">');
    $('ul#vfolder_list li.prev').click(function() {
      var prevoffset = offset-count; // amit ha klikkelnek, kernunk kell
      if (prevoffset<0) { prevoffset = 0; } // ne menjunk a legelso ele
      $.ajax({
        url: '/ajax/VFOLDERLIST/'+prevoffset+'/'+count,
        type: 'GET',
        dataType: 'xml',
        timeout: 1000,
        success: function(xml) {
          var sum = $(xml).find('sum').text();
          var lis = '<li class="controll prev"><img src="http://static.motor-sport.hu/images/media_arrow_inactive.png"></li>';
          $(xml).find('r').each(function() {
              lis += '<li><a href="/gallery/' + $(this).attr('id') + '/' + $(this).attr('main_picture') + '" class="gallery" title="' + $(this).attr('x_name') + '"><img src="http://static.motor-sport.hu/kepek/w90h65/' + $(this).text() + '" alt="' + $(this).attr('x_name') + '"></a><br>' + $(this).attr('x_name') + '</li>';
          });
          lis += '<li class="controll next"><img src="http://static.motor-sport.hu/images/media_arrow_inactive.png"></li>';
          $('ul#vfolder_list').html(lis);
          bind_gallery_popup();
          activate_vfolder_list(prevoffset,count,sum);
        }
      });
    });
  }
  var nextoffset = offset+count;
  if (nextoffset < max) {
    $('ul#vfolder_list li.next').html('<img src="http://static.motor-sport.hu/images/media_arrow_right.png" style="cursor:pointer">');
    $('ul#vfolder_list li.next').click(function() {
      if (nextoffset+count > max) { nextoffset = max-count; }
      $.ajax({
        url: '/ajax/VFOLDERLIST/'+nextoffset+'/'+count,
        type: 'GET',
        dataType: 'xml',
        timeout: 1000,
        success: function(xml) {
          var sum = $(xml).find('sum').text();
          var lis = '<li class="controll prev"><img src="http://static.motor-sport.hu/images/media_arrow_inactive.png"></li>';
          $(xml).find('r').each(function() {
              lis += '<li><a href="/gallery/' + $(this).attr('id') + '/' + $(this).attr('main_picture') + '" class="gallery" title="' + $(this).attr('x_name') + '"><img src="http://static.motor-sport.hu/kepek/w90h65/' + $(this).text() + '" alt="' + $(this).attr('x_name') + '"></a><br>' + $(this).attr('x_name') + '</li>';
          });
          lis += '<li class="controll next"><img src="http://static.motor-sport.hu/images/media_arrow_inactive.png"></li>';
          $('ul#vfolder_list').html(lis);
          bind_gallery_popup();
          activate_vfolder_list(nextoffset,count,sum);
        }
      });
    });
  }
}

// a cikkoldalon levo vfolder lapozo logikajat definialja - ajax es lista vegenek/elejenek erzekelese, lekezelese
function activate_right_vfolder_list(offset,count,max) {
  if (offset > 0) {
    $('#right #media ul.controll li.prev').html('<img src="http://static.motor-sport.hu/images/media_right_arrow_left.png" style="cursor:pointer">');
    $('#right #media ul.controll li.prev').click(function() {
      var prevoffset = offset-count; // amit ha klikkelnek, kernunk kell
      if (prevoffset<0) { prevoffset = 0; } // ne menjunk a legelso ele
      $.ajax({
        url: '/ajax/VFOLDERLIST/'+prevoffset+'/'+count,
        type: 'GET',
        dataType: 'xml',
        timeout: 1000,
        success: function(xml) {
          var sum = $(xml).find('sum').text();
          var lis = '';
          $(xml).find('r').each(function() {
              lis += '<li><a href="/gallery/' + $(this).attr('id') + '/' + $(this).attr('main_picture') + '" class="gallery" title="' + $(this).attr('x_name') + '"><img src="http://static.motor-sport.hu/kepek/w90h65/' + $(this).text() + '" alt="' + $(this).attr('x_name') + '"></a><br>' + $(this).attr('x_name') + '</li>';
          });
          $('#right #media ul#vfolder_list').html(lis);
          bind_gallery_popup();
          $('#right #media ul.controll').html('<li class="prev"><img src="http://static.motor-sport.hu/images/media_right_arrow_inactive.png"></li><li class="next"><img src="http://static.motor-sport.hu/images/media_right_arrow_inactive.png"></li>');
          activate_right_vfolder_list(prevoffset,count,sum);
        }
      });
    });
  }
  var nextoffset = offset+count;
  if (nextoffset < max) {
    $('#right #media ul.controll li.next').html('<img src="http://static.motor-sport.hu/images/media_right_arrow_right.png" style="cursor:pointer">');
    $('#right #media ul.controll li.next').click(function() {
      if (nextoffset+count > max) { nextoffset = max-count; }
      $.ajax({
        url: '/ajax/VFOLDERLIST/'+nextoffset+'/'+count,
        type: 'GET',
        dataType: 'xml',
        timeout: 1000,
        success: function(xml) {
          var sum = $(xml).find('sum').text();
          var lis = '';
          $(xml).find('r').each(function() {
              lis += '<li><a href="/gallery/' + $(this).attr('id') + '/' + $(this).attr('main_picture') + '" class="gallery" title="' + $(this).attr('x_name') + '"><img src="http://static.motor-sport.hu/kepek/w90h65/' + $(this).text() + '" alt="' + $(this).attr('x_name') + '"></a><br>' + $(this).attr('x_name') + '</li>';
          });
          $('#right #media ul#vfolder_list').html(lis);
          bind_gallery_popup();
          $('#right #media ul.controll').html('<li class="prev"><img src="http://static.motor-sport.hu/images/media_right_arrow_inactive.png"></li><li class="next"><img src="http://static.motor-sport.hu/images/media_right_arrow_inactive.png"></li>');
          activate_right_vfolder_list(nextoffset,count,sum);
        }
      });
    });
  }
}

// a DOM betoltodese utan...
$(document).ready(function() {

  // "minden mas" menu
  $('#header #toggle_others').click(function() {
    var coords = findPos(this);
    var w = this.offsetWidth;
    $('div#nav_others').css({ left: coords[0], top: coords[1]+23 });
    $('div#nav_others').width(w-2);
    $('div#nav_others').slideToggle('normal');
    return false;
  });

  // #topstory .story cserergetes es li.selected valtas
  $('ul#topimg_small li').each(function() {
    var n = $(this).attr('id').substring(3); // az id szama - li#topN
    $(this).click(function() {
      $('div#topstories li.selected').removeClass('selected');
      $('div#topstories li#top'+n).addClass('selected');
      $('div#topstories ul#topimg_big li').hide();
      $('div#topstories ul#topimg_big li#storyimage'+n).show();
      $('div#topstories div.story').hide();
      $('div#topstories div#story'+n).show();
      return false;
    });
  });

  // galleria kepvaltogatas
  $('#gallery ul#thumbnails a').click(function() {
    var pid = $(this).attr('id').substring(9); // kep id - a#thumbnailN
    var pfile = $(this).children('img')[0].src.substring(42); // kep fileneve - http://static.motor-sport.hu/kepek/w55h55/FOOBAR
    $('div#bigpicture span').html('<a href="/picture/'+pid+'"><img src="http://static.motor-sport.hu/kepek/w550h550/'+pfile+'"></a>');
    return false;
  });

  // galleria nagy kep popup
  $('#gallery div#bigpicture span').click(function() {
    var pop = window.open($(this).children('a')[0].href,'foto','width=600,height=600,directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no');
    return false;
  });

  // a vfolder lapozok feluldefinialasa - kezdesnel fake max erteket adunk meg, remeljuk bejon
  activate_vfolder_list(0,4,99);
  activate_right_vfolder_list(0,3,99);

  // a lapozaskor ne mozogjon a box magassaga
  $('#media').each(function() {
    var h = $(this).height();
    $(this).height(h);
  });

  // popup galleryk
  bind_gallery_popup();

  // #freshstories #news_list cserelgetes es ul#tabs li.selected valtas
  $('div#freshstories ul.tabs li').each(function() {
    var tag = $(this).attr('id').substring(6); // kategoria - li#fresh_TAG
    $(this).click(function() {
      $('div#freshstories li.selected').removeClass('selected');
      $('div#freshstories li#fresh_'+tag).addClass('selected');
      $.ajax({
        url: '/ajax/NEWSLIST/'+tag.toUpperCase(),
        type: 'GET',
        dataType: 'xml',
        timeout: 1000,
        error: function() {
          if (tag == 'all' || tag == 'others') {
            window.location = '/';
          } else {
            window.location = '/tag/'+tag;
          }
        },
        success: function(xml) {
          var rows = "";
          var count = $(xml).find('r').size();
          $(xml).find('r').each(function(i) {
            if (i < count-1) {
              rows += '<tr>';
            } else {
              rows += '<tr class="last">';
            }
            if (tag == 'all' || tag == 'others') {
              rows += '<td class="date">' + $(this).attr('publishdate') + '</td><td class="clear"><span class="category ' + $(this).attr('tournament_class') + '"><span>' + $(this).attr('tournament_tag') + '</span></span></td><td><a href="/article/' + $(this).attr('x_url_title') + '" title="' + $(this).text() + '">' + $(this).text() +'</a></td></tr>\n';
            } else {
              rows += '<td class="date">' + $(this).attr('publishdate') + '</td><td class="separator">|</td><td><a href="/article/' + $(this).attr('x_url_title') + '" title="' + $(this).text() + '">' + $(this).text() +'</a></td></tr>\n';
            }
          });
          $('table#news_list').html(rows);
        }
      });
      return false;
    });
  });

  // forma-1 box tabvaltogatas
  $('div#f1box ul.tabs li').each(function() {
    var n = $(this).attr('id').substring(5); // az id szama - li#f1tabN
    $(this).click(function() {
      $('div#f1box ul.tabs li.selected').removeClass('selected');
      $('div#f1box ul.tabs li#f1tab'+n).addClass('selected');
      $('div#f1box div, div#f1box ul.list').hide();
      $('div#f1box #f1body'+n).show();
      return false;
    });
  });
});
