var first = 0;
var show_count = 0;


$(function() {
  prepareOffers();
  $('div.top_advices div.item').hover(
     function(){
       var info_div = $('div.top_advice_info');
       var item_width = $('div.top_advices div.item').width();
       var all_width = $('div.top_advices').width() - parseInt($('div.top_advices').css('padding-left'));
       info_div.find('div.title').html($(this).find('div.name').html());
       info_div.find('div.description').html($(this).find('div.description').html());
       var prices = $(this).find('div.prices').html();
       //info_div.find('div.prices').html(prices.replace('<br>', ' | ').replace('<BR>', ' | '));
       //info_div.find('div.prices').html(prices.replace('<br>', '  ').replace('<BR>', '  '));
       var offset = $(this).offset();
       var left = offset.left+item_width;
       if(left+140 > all_width) left = offset.left - item_width-20;
       info_div.css('left', left+'px');
       info_div.css('width', '140px');
       info_div.show();
     },
     function(){
       $('div.top_advice_info').hide();
     }
  );
  $('div.top_advices div.prev img').hover(
     function(){
       var src = $(this).attr('src');
       if(!src.match(/_gr\.gif$/)) $(this).attr('src', "image/top_advice_specimg/prev_on2.gif");
     },
     function(){
       var src = $(this).attr('src');
       if(!src.match(/_gr\.gif$/)) $(this).attr('src', "image/top_advice_specimg/prev2.gif");
     }
  );
  $('div.top_advices div.next img').hover(
     function(){
       var src = $(this).attr('src');
       if(!src.match(/_gr\.gif$/)) $(this).attr('src', "image/top_advice_specimg/next_on2.gif");
     },
     function(){
       var src = $(this).attr('src');
       if(!src.match(/_gr\.gif$/)) $(this).attr('src', "image/top_advice_specimg/next2.gif");
     }
  );
  
});


$(window).resize(function(){
  prepareOffers();
});


function prepareOffers() {
  var item_width = $('div.top_advices div.item').width();
  var all_width = $('div.top_advices').width() - parseInt($('div.top_advices').css('padding-left'))-257;
  $('div.top_advice_info').width(all_width);
  var next_div = $('div.top_advices div.next');
  var prev_div = $('div.top_advices div.prev');
  if(item_width*offers_count <= all_width) {
    next_div.css('display', 'none');
    prev_div.css('display', 'none');
    first = 0;
    show_count = offers_count;
  } else {
    next_div.css('display', 'block');
    prev_div.css('display', 'block');
    show_count = Math.floor((all_width - next_div.width() - prev_div.width())/item_width);
  }
  showOffers();
}

function showOffers() {
  var i = 0;
  var item_width = $('div.top_advices div.item').width();
  var button_width = (show_count == offers_count ? 0 : parseInt($('div.top_advices div.next').css('width'))*2);
  var container_width = $('div.top_advices').width() - parseInt($('div.top_advices').css('padding-left')) - button_width - 257;//последний параметр - ширина большой картинки в шапке 
  var margin = Math.floor((container_width - item_width*show_count)/show_count)-4;
  
  //if(margin > 30) {
    if(jQuery.browser.msie ) {
      margin = 0;
      preparePng();
    }
  //}

  $('div.top_advices div.item').each(function() {
     if(i >= first && i < first+show_count) { 
       this.style.display = 'block';
       this.style.marginLeft = (margin < 0 ? 0 : margin )+'px';
     } else this.style.display = 'none';
     i++;
  });
}

function next_offer() {
  first += show_count;
  if(first >= offers_count-show_count) first = offers_count-show_count;
  checkArrows()
  showOffers();
}

function checkArrows() {
  var prev_img = $('div.top_advices div.prev img');
  var next_img = $('div.top_advices div.next img');
  if(first == 0) {
    prev_img.attr('src', 'image/top_advice_specimg/prev_gr.gif');
    prev_img.attr('onClick', '');
    prev_img.css('cursor', 'default');
  } else {
    prev_img.attr('src', 'image/top_advice_specimg/prev.gif');
    prev_img.attr('onClick', 'javascript:prev_offer()');
    prev_img.css('cursor', 'pointer');
  }
  if(first == offers_count-show_count) {
    next_img.attr('src', 'image/top_advice_specimg/next_gr.gif');
    next_img.attr('onClick', '');
    next_img.css('cursor', 'default');
  } else {
    next_img.attr('src', 'image/top_advice_specimg/next.gif');
    next_img.attr('onClick', 'javascript:next_offer()');
    next_img.css('cursor', 'pointer');
  }
  
}

function prev_offer() {
  first -= show_count;
  if(first < 0) first = 0;
  checkArrows()
  showOffers();
}

function preparePng() {
  var img;
  var div;
  $('div.top_advices_container img').each(function(){
	  if(this.src.match(/\.png/)) {
	    img = $(this);
	    img.css('display', 'none');
	    div = $(img.parent().get(0));
	    div.css('width', '151px');
	    div.css('height', '111px');
	    div.css('background', 'none');
	    div.css('cursor', 'pointer');
	    div.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src=\''+img.attr('src')+'\')');
	  }
  });
}
