var slideItmsCnt = 0;
var slideItmsP = 0;
var slideMLeft = 0;
var slideRTop = 0;
var slideTimer = true;
var removeTimer;



$(document).ready(function() {

  var htmlM = '<table cellpadding="0" cellspacing="0"><tr>';
  var htmlR = '<table width="110" cellpadding="0" cellspacing="0">';

  $('#dslidelist li').each(function (i) {
    htmlM += '<td style="background: transparent url('+$(this).find('.photo').attr('src').replace(/ /g, '%20') +') no-repeat center center; width:510px;height:291px; cursor:pointer" onclick="slideTimer=false;window.location.href=\''+$(this).find('a').attr("href")+'\'" valign="bottom">';
    htmlM += '<div style="width:510px">';
    htmlM += '<div style="width:90%;color: #FFFFFF; font-size: 20px; opacity: 0.8; padding:2px 5px" class="'+$(this).find('a').attr("class")+'">'+$(this).find('.title').html() +'</div>';
    htmlM += '<div style="font-size: 13px;margin-top:5px;color: #FFFFFF; opacity: 0.8; padding:2px 5px 8px 5px; background-color:#B90808">'+$(this).find('.text').html() +'</div>';
    htmlM += '</div></td>';

    htmlR += '<tr><td style="padding-bottom:4px"><div onclick="slideTimer=false;sliderMSMove('+(slideItmsCnt+1)+');return false"  style="cursor:pointer;background-color: #FFFFFF; border: 1px solid #DDDDDD; padding:1px" onmouseover="$(this).css({\'border-color\':\'#b60d0d\', \'box-shadow\':\'0 0 4px #000000\'})" onmouseout="$(this).css({\'border-color\':\'#DDDDDD\', \'box-shadow\':\'none\'})"><div style="height:61px;background:transparent url('+$(this).find('.thumb').attr("src").replace(/ /g, '%20')+') no-repeat center center"></div></div></td></tr>';
    slideItmsCnt++;
  });
  
  htmlM += '</tr></table>';
  htmlR += '</table>';

  $('#dslider').html(htmlM);
  $('#dslidem').html(htmlR);

  if (slideItmsCnt > 4) {
    $('#slider-down').removeClass('slider-down-ina').addClass('slider-down');
  }
  

  sliderActionPnt = setTimeout("sliderAction()", 6000);
});

function sliderAction() {
  if (slideTimer) {
    slideItmsP++;
    if (slideItmsP >= slideItmsCnt) {
      slideItmsP = 0;
    }
    sliderMSMove(slideItmsP+1);

    sliderActionPnt = setTimeout("sliderAction()", 6000);
  }
}

function sliderMSMove(n) {
  $('#dslider').stop(true,true);
  var mt = 0 - (510*(n-1));
  $('#dslider').animate({ left: mt }, 470);
}
function sliderLSMove(n) {

  if ( (n == -1 && (($('#dslidem').position().top*-1) + (4*69)) < $('#dslidem').height()-5) || (n == 1 && $('#dslidem').position().top < 0) ) {
    $('#slider-down').removeClass('slider-down').addClass('slider-down-ina');
    $('#slider-up').removeClass('slider-up').addClass('slider-up-ina');
    var mt = $('#dslidem').position().top + (69*n);
    $('#dslidem').animate({ top: mt }, 200, function() {
      if ((($('#dslidem').position().top*-1) + (4*69)) < $('#dslidem').height()-5) {
        $('#slider-down').removeClass('slider-down-ina').addClass('slider-down');
      }
      if ($('#dslidem').position().top < 0) {
        $('#slider-up').removeClass('slider-up-ina').addClass('slider-up');
      }
    });
  }
}
