
$(document).ready(function()
{
  $('#top-koszyk, #top-konto, #top-search-btn, .bover').each(imgOverSrc).bind('mouseover', imgOver).bind('mouseout', imgOut);
  $('#mm a img').each(mmOverSrc).bind('mouseover', mmOver).bind('mouseout', mmOut);
  fixButtons();
  $('#wiecej_nowosci').flash('images/index/wiecej_over.swf', 34, 211);
  $('#wiecej_promocje').flash('images/index/wiecej_over.swf?href=promocje.html', 34, 211);
  $('.hint').bind('focus', function() {
    if (!this.hintCleared) {
      this.value = '';
      this.hintCleared = true;
    }
  });
  $('a > img').each(function() {
    $(this).parent().attr('title', $(this).attr('alt'));
  });
  $('.t-opis').append('<img src="images/blank.gif" class="blank">');
  //$('.t-opis').select(function(){ alert('a'); return false; });
//  $(document).pngFix(); 
  produktyPowiazane();
  
  preloadImages(['images/powiekszenie-bg.png','images/loading.gif']);
});

function preloadImages(arr)
{
  for ( var x in arr )
  {
    var img = new Image();
    img.src = arr[x]; 
  }
}

function imgOverSrc()
{
  this.normalSrc = this.src;
  var src = this.src.split('.');
  var ext = src.pop();
  this.overSrc = src.join('.') + '-over.' + ext;
}

function imgOver() 
{
  this.src = this.overSrc;
}

function imgOut()
{
  this.src = this.normalSrc;
}

function mmOverSrc()
{
  this.normalSrc = this.src;
  this.overSrc = this.src.replace('mm_', 'mmo_');
}

function mmOver()
{
  this.src = this.overSrc;
}

function mmOut()
{
  this.src = this.normalSrc;
}

function fixButtons()
{
  
}

function confirmAnulujZamowienie()
{
  return confirm('Czy na pewno chcesz anulować wybrane zamówienie?');
}

function getSelText()
{
  var txt = '';
  if (window.getSelection)
  {
      txt = window.getSelection();
           }
  else if (document.getSelection)
  {
      txt = document.getSelection();
          }
  else if (document.selection)
  {
      txt = document.selection.createRange().text;
          }
  else return "";
  return txt;
}

var zdjecie2timeout;
var zdjecie2pokaz = '';

function zdjecie2(src, pid, inne)
{
  clearTimeout(zdjecie2timeout);
  
  zdjecie2pokaz = 'powiekszenie-' + pid;
  
  if ( $('.' + zdjecie2pokaz).length )
  {
    if ( $.browser.msie || true )
    {
      $('.' + zdjecie2pokaz).show();
    }
    else
    {
      $('.' + zdjecie2pokaz).stop().animate({opacity:1});
    }
    return;
  }

  $('#powiekszenie').remove();
  
  var pid2 = 'pid-' + pid;
  
  var ot = $('#' + pid2).get(0).offsetTop + 5 + $('table.t-lista').get(0).offsetTop;
  
  var alt = $('#' + pid2 + ' .t-foto').attr('alt');
  
  
  $('.t-pokaz').append('<div onmouseout="zdjecie2ukryj()" onmouseover=\"zdjecie2pokazuj()\" class="' + zdjecie2pokaz + '" id="powiekszenie" style="top:' + ot + 'px;"><table><tr><td id="powiekszenie-top-left"><div id="powiekszenie-min"></div></td><td id="powiekszenie-top-right"><div id="powiekszenie-img"><a href="javascript://" title="' + alt + '"><img src="images/blank.gif" alt="' + alt + '" onclick="zdjecie2popup2(' + pid + ')" id="powiekszenie-blank" /></a></div></td></tr><tr><td id="powiekszenie-bottom-left"></td><td id="powiekszenie-bottom-right"></td></div>');
  if ( !$.browser.msie )
  {
    //$('#powiekszenie').css({opacity:0});
  }
  $('#powiekszenie-img').append('<a href="javascript://" title="' + alt + '"><img id="powiekszenie-prod" alt="' + alt + '" src="zdjecia/' + src + '" onclick="zdjecie2popup2(' + pid + ')"></a>');
  $('#powiekszenie-min').hide().append('<a href="javascript://" title="' + alt + '"><img class="powiekszenie-min-cur" alt="' + alt + '" src="zdjecia_tn/' + src + '" onmouseover="zdjecie2zmien(this)"></a>');
  var dodatkowe = false;
  if ( inne != undefined && inne.length > 0 )
  {
    for ( x in inne )
    {
      detal = inne[x];
      
      if ( typeof(detal) == "string" )
      {
        if ( detal.length )
        {
          dodatkowe = true;
          $('#powiekszenie-min').append('<a href="javascript://" title="' + alt + '"><img src="zdjecia_tn/' + detal + '" onmouseover="zdjecie2zmien(this)" alt="' + alt + '"></a>');
        }
      }
      else
      {
          dodatkowe = true;
          var alt2 = alt;
          if ( detal[1].length )
          {
            alt2 += ", " + detal[1];
          }
          $('#powiekszenie-min').append('<a href="javascript://" title="' + alt2 + '"><img src="zdjecia_tn/' + detal[0] + '" onmouseover="zdjecie2zmien(this)" alt="' + alt2 + '"></a>');        
      }
    }
  }
  /*
  $('#' + pid2 + ' .t-kolor-img').each(function()
  {
    dodatkowe = true;
    var src = $(this).attr('href').replace('zdjecia/', 'zdjecia_tn/');
    var alt = $(this).attr('title');
    $('#powiekszenie-min').append('<a href="javascript://" title="' + alt + '"><img src="' + src + '" onmouseover="zdjecie2zmien(this)" alt="' + alt + '"></a>');
  });
  */
  if ( dodatkowe )
  {
    $('#powiekszenie-min').show();
    $('#powiekszenie-min img').click(function()
    {
      zdjecie2popup(pid, $(this).attr('src').replace('zdjecia_tn/', 'zdjecia/'));
    });
  }
  if ( !$.browser.msie )
  {
    //$('#powiekszenie').stop().animate({opacity:1});
  }
}

function zdjecie2zmien(img)
{
  $('.powiekszenie-min-cur').removeClass('powiekszenie-min-cur');
  $(img).addClass('powiekszenie-min-cur');
  $('#powiekszenie-prod').attr('src', 'images/blank.gif');
  $('#powiekszenie-prod').attr('src', img.src.replace('zdjecia_tn/', 'zdjecia/'));
  $('#powiekszenie-prod, #powiekszenie-blank').attr('alt', img.alt);
  $('#powiekszenie-prod, #powiekszenie-blank').parent().attr('title', img.alt);

}

function zdjecie2pokazuj()
{
  clearTimeout(zdjecie2timeout);
}

function zdjecie2ukryj()
{
  clearTimeout(zdjecie2timeout);
  zdjecie2timeout = setTimeout(zdjecie2close, 300);
}

function zdjecie2close()
{
  if ( $.browser.msie || true )
  {
    $('#powiekszenie').hide();
  }
  else
  {
    $('#powiekszenie').stop().animate({opacity:0}, function()
    {
      $(this).remove();
    });
  }
}

function zdjecie2popup(pid, src)
{
  //alert('pid ' + pid + '\nsrc ' + src);
  var width = 650;
  var height = 770;
  var left = Math.round((screen.availWidth - width) / 2);
  var top = Math.max(0, Math.round((screen.availHeight - height) / 3));
  window.open("zdjecie2.php?id=" + pid + "&img=" + src,'','width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + ', resizable=yes, scrollbars=yes, toolbars=no, status=yes');
}

function zdjecie2popup2(pid)
{
  var src = $('#powiekszenie-prod').attr('src');
  if ( src ) zdjecie2popup(pid, src); 
}

function alertWybierzPlatnosc(radio)
{
  radio.checked = false;
  alert('Proszę najpierw wybrać formę płatności.');
}

function produktyPowiazane()
{
  $('.powiazane').each(function()
  {
    $(this).find('td').each(function()
    {
      $(this).prepend('<img class="powiazane-space" src="images/blank.gif">');
    });
    var tdCount = $(this).find('td').size();
    if ( tdCount > 2 )
    {
      $(this).append('<img src="images/powiazane-left.gif" class="powiazane-left" onclick="produktyPowiazaneWLewo(this)" />');
      $(this).append('<img src="images/powiazane-right.gif" class="powiazane-right" onclick="produktyPowiazaneWPrawo(this)" />');
      var x = $(this).find('table').get(0);
      x.pozycja = 0;
      x.pozycjaMax = Math.ceil(tdCount / 2) - 1;
      //$(this).append('td: ' + tdCount + '<br>pozycja: ' + x.pozycjaMax);
    }
  });
  $('.powiazane-left').hide();
}

function produktyPowiazaneWLewo(box)
{
  produktyPowiazanePrzewin(box, -1);
}


function produktyPowiazaneWPrawo(box)
{
  produktyPowiazanePrzewin(box, 1);
}


function produktyPowiazanePrzewin(box, kierunek)
{
  var x = $(box).parent().find('table');
  if ( x.size() ) 
  {
    x = x.get(0);
    if ( kierunek == -1 && x.pozycja == 0 )
    {
      return;
    }
    if ( (x.pozycja + kierunek) > x.pozycjaMax )
    {
      return;
    }
    x.pozycja += kierunek;
    $(box).parents('.powiazane').find('.powiazane-left, .powiazane-right').show();
    if ( x.pozycja == 0 )
    {
      $(box).parents('.powiazane').find('.powiazane-left').hide();
    }
    if ( x.pozycja == x.pozycjaMax )
    {
      $(box).parents('.powiazane').find('.powiazane-right').hide();
    }
    $(x).animate({ left: -x.pozycja * 130  });
  }
}

function resetPrzesylka24()
{
  $('#wysylka24').attr('checked', false);
  $('#sb-wysylka24').submit();
}

function zmowienieFormStep1Validate()
{


  return true;
}
