function changeCheckbox (repl_id, hidden_id)
{
  repl = $('#'+repl_id);
  hidden = $('#'+hidden_id);
  if (repl.hasClass('checkbg'))
  {
    repl.removeClass('checkbg');
    repl.addClass('checkbg2');
    hidden.val('1');
  }
  else
  {
    repl.removeClass('checkbg2');
    repl.addClass('checkbg');
    hidden.val('0');
  }
}
var Lst;
function CngClass(obj)
{
  if (Lst) Lst.className='';
  obj.className='checkbg2';
  Lst=obj;
  if (Lst) Lst.className='';
  obj.className='checkbg2';
  Lst=obj;
}
var timeout	= 300;
var closetimer	= 0;
var ddmenuitem	= 0;
// open hidden layer
function mopen(id)
{	
  // cancel close timer
  mcancelclosetime();
  // close old layer
  if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
  // get new layer and show it
  ddmenuitem = document.getElementById(id);
  ddmenuitem.style.visibility = 'visible';
}
// close showed layer
function mclose()
{
  if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}
// go close timer
function mclosetime()
{
  closetimer = window.setTimeout(mclose, timeout);
}
// cancel close timer
function mcancelclosetime()
{
  if(closetimer)
  {
    window.clearTimeout(closetimer);
    closetimer = null;
  }
}
// close layer when click-out
document.onclick = mclose;

function sBasket(action, productID, amount)
{
  smallbasket_amount = '#smallbasket_amount';
  smallbasket_price = '#smallbasket_price';
  zone_price = $('.kokkuhind .transhind .red');
  totals_price = $('.allhind .lhind .red');
  totals_vat = $('.lhind .hindkokku3');
  switch(action)
  {
    case 'add':
      $.ajax(
      {
        type: "POST",
        data: "ajax=1&action=addProduct&ID="+productID+"&amount="+amount+"",
        dataType: "script",
        success: function(data)
        {
          basket = eval(data);
          amount = $(smallbasket_amount).text();
		  priceEEK = basket.overall.price_total_format * 15.6466;
          $(smallbasket_amount).text(basket.overall.amount + ' ' + amount.split(' ')[1]);
          $(smallbasket_price).text(basket.overall.price_total_format + ' EUR (' + priceEEK.toFixed(2) + ' EEK)');
        }
      });
      break;
    case 'remove':
      $.ajax(
      {
        type: "POST",
        data: "ajax=1&action=remProduct&ID="+productID+"&amount="+amount+"",
        dataType: "script",
        success: function(data)
        {
          basket = eval(data);
          amount = $(smallbasket_amount).text();
		  priceEEK = basket.overall.price_total_format * 15.6466;
          $(smallbasket_amount).text(basket.overall.amount + ' ' + amount.split(' ')[1]);
          $(smallbasket_price).text(basket.overall.price_total_format + ' EUR (' + priceEEK.toFixed(2) + ' EEK)');
        }
      });
      break;
    case 'removeAll':
      $.ajax(
      {
        type: "POST",
        data: "ajax=1&action=remProducts&ID="+productID,
        dataType: "script",
        success: function(data)
        {
          basket = eval(data);
          amount = $(smallbasket_amount).text();
		  priceEEK = basket.overall.price_total_format * 15.6466;  
          $(smallbasket_amount).text(basket.overall.amount + ' ' + amount.split(' ')[1]);
          $(smallbasket_price).text(basket.overall.price_total_format + ' EUR (' + priceEEK.toFixed(2) + ' EEK)');
          if (zone_price.length) {
		    shippingpriceEEK = basket.overall.shipping.price_format * 15.6466;
            zone_price.text(basket.overall.shipping.price_format + ' EUR (' + shippingpriceEEK.toFixed(2) + ' EEK)');
		  }
          if (totals_price.length)
            totals_price.text(basket.overall.price_total_format + ' EUR (' + priceEEK.toFixed(2) + ' EEK)');
          if (totals_vat.length) {
			if (basket.overall.vat_20_format === undefined) {
				totals_vat.text('0.00 EUR (0.00 EEK)');
			}
			else {
				vat_20_formatEEK = basket.overall.vat_20_format * 15.6466;
				totals_vat.text(basket.overall.vat_20_format + ' EUR (' + vat_20_formatEEK.toFixed(2) + ' EEK)');
			}
		  }
        }
      });
      break;
    case 'changeAmount':
      $.ajax(
      {
        type: "POST",
        data: "ajax=1&action=changeAmount&ID="+productID+"&amount="+amount+"",
        dataType: "script",
        success: function(data)
        {
          basket = eval(data);
          amount = $(smallbasket_amount).text();
		  priceEEK = basket.overall.price_total_format * 15.6466;
          $(smallbasket_amount).text(basket.overall.amount + ' ' + amount.split(' ')[1]);
          $(smallbasket_price).text(basket.overall.price_total_format + ' EUR (' + priceEEK.toFixed(2) + ' EEK)');
          if ($('#price_'+productID).length) {
		    productpriceEEK = basket.products[productID].price_total_format * 15.6466;
            $('#price_'+productID).text(basket.products[productID].price_total_format + ' EUR (' + productpriceEEK.toFixed(2) + ' EEK)');
		  }
          if (zone_price.length) {
		    shippingpriceEEK = basket.overall.shipping.price_format * 15.6466;
            zone_price.text(basket.overall.shipping.price_format + ' EUR (' + shippingpriceEEK.toFixed(2) + ' EEK)');
		  }
          if (totals_price.length)
            totals_price.text(basket.overall.price_total_format + ' EUR (' + priceEEK.toFixed(2) + ' EEK)');
          if (totals_vat.length) {
		    vat_20_formatEEK = basket.overall.vat_20_format * 15.6466;
            totals_vat.text(basket.overall.vat_20_format + ' EUR (' + vat_20_formatEEK.toFixed(2) + ' EEK)');
		  }
        }
      });
      break;
    case 'setShipping':
      $.ajax(
      {
        type: "POST",
        data: "ajax=1&action=setShipping&type="+amount+"&zoneID="+productID,
        dataType: "script",
        success: function(data)
        {
          basket = eval(data);
          amount = $(smallbasket_amount).text();
		  priceEEK = basket.overall.price_total_format * 15.6466;
          $(smallbasket_amount).text(basket.overall.amount + ' ' + amount.split(' ')[1]);
          $(smallbasket_price).text(basket.overall.price_total_format + ' EUR (' + priceEEK.toFixed(2) + ' EEK)');
          if (zone_price.length) {
		    shippingpriceEEK = basket.overall.shipping.price_format * 15.6466;
            zone_price.text(basket.overall.shipping.price_format + ' EUR (' + shippingpriceEEK.toFixed(2) + ' EEK)');
		  }
          if (totals_price.length)
            totals_price.text(basket.overall.price_total_format + ' EUR (' + priceEEK.toFixed(2) + ' EEK)');
          if (totals_vat.length) {
		    vat_20_formatEEK = basket.overall.vat_20_format * 15.6466;
            totals_vat.text(basket.overall.vat_20_format + ' EUR (' + vat_20_formatEEK.toFixed(2) + ' EEK)');
		  }
        }
      });
      break;
    default:
      alert('Missing action!');
  }
}
function show_gallery(show, target)
{
  if (show == true)
  {
    content = $('#gallery .content');
    content.css('left', ($(document).width()/2 - parseInt(content.css('width'))/2));
    content.css('top', ($(window).height() - 500)/2 + $(document).scrollTop());
    
    $('#galleryProduct_left').css({'top':(parseInt(content.css('top'))), 'left':($(document).width()/2 - parseInt(content.css('width'))/2 - 31)});
    $('#galleryProduct_right').css({'top':(parseInt(content.css('top'))), 'left':(parseInt(content.css('left')) + parseInt(content.css('width')) + 24)});
    
    $('#gallery').css('height', $(document).height());
    $('#gallery .background').css('opacity', '0.8');
    $('#gallery').fadeIn(500);
  }
  else
  {
    $('#gallery').hide();
  }
}
var scroller;
function scrollerMove(direction, speed)
{
  container = $('div.slider');
  content = container.find('table');
  mL = parseInt(content.css('margin-left'));
  limit = parseInt(content.width() - container.width());
  switch(direction)
  {
    case 'left':
      if (mL >= -1*limit)
        content.css('margin-left', mL-speed+'px');
      break;
    case 'right':
      if (mL <= 0)
        content.css('margin-left', mL+speed+'px');
      break;
  }
}
var cloneImg = null;
$(document).ready(function()
{
  $('.tootekorvcontent td.in a').click(function(e)
  {
    productID = $(this).parent().parent().find('td input:hidden').val();
    amount = $(this).parent().parent().find('td span.number input').val();
    sBasket('removeAll', productID, 0);
    $(this).parents('div.tootekorvcontent').remove();
  });
  $('#m1 a').click(function(e)
  {
    value = $(this).find('input:hidden').val();
    if (value == '')
    {
      sBasket('setShipping', 0, 'none');
      $(this).hide();
      $(this).parent().prev().text(msg_choose_zone);
      $("textarea[name*=p_delivery]").parent().hide();
    }
    else
    {
      sBasket('setShipping', value, 'default');
      $('#m1 a:first').show();
      $(this).parent().prev().text($(this).text());
      $("textarea[name*=p_delivery]").parent().show();
    }
  });
  $('#m2 a').click(function(e)
  {
    index = $(this).index();
    $(this).parent().prev().text($(this).text());
    if (index == 0)
    {
      $('input[name*=p_firstname]').val(msg_p_firstname);
      $('input[name*=p_firstname]').blur(function() { if ($(this).val() == '') $(this).val(msg_p_firstname); });
      $('input[name*=p_lastname]').val(msg_p_lastname);
      $('input[name*=p_lastname]').blur(function() { if ($(this).val() == '') $(this).val(msg_p_lastname); });
      $('input[name*=p_type]').val('0');
    }
    else if (index == 1)
    {
      $('input[name*=p_firstname]').val(msg_p_company);
      $('input[name*=p_firstname]').blur(function() { if ($(this).val() == '') $(this).val(msg_p_company); });
      $('input[name*=p_lastname]').val(msg_p_contact);
      $('input[name*=p_lastname]').blur(function() { if ($(this).val() == '') $(this).val(msg_p_contact); });
      $('input[name*=p_type]').val('1');
    }
  });
  $('a.tingimused').click(function(e)
  {
    show_gallery(true, $('div.headername').offset().top);
  });
  $('#gallery .content img, #gallery .background').click(function()
  {
    show_gallery(false, null);
  });
  $('#gallery #galleryProduct_left').click(function()
  {
    $visibleImg = $('#gallery .content div:visible');
    $prevImg = $visibleImg.prev();
    if ($prevImg.length)
    {
      $visibleImg.hide();
      $prevImg.show();
    }
  });
  $('#gallery #galleryProduct_right').click(function()
  {
    $visibleImg = $('#gallery .content div:visible');
    $nextImg = $visibleImg.next();
    if ($nextImg.length)
    {
      $visibleImg.hide();
      $nextImg.show();
    }
  });
  $('.pangalink a').click(function()
  {
    $('input[name*=p_bank]').val($(this).find('input:hidden').val());
    if ($('input[name*=p_bank]').val() != '')
      $('form[name*=korv]').submit();
  });
  $('a.FAQ_question').click(function(e)
  {
    $(this).next().toggleClass('FAQ_question_opened','FAQ_question_closed');
  });
  $('li.kogusenumber input[name*=kogus]').keyup(function()
  {
    if ($(this).val() < $('li.korv a').next().next().val())
    {
      alert(msg_must_minamount);
      $(this).val($('li.korv a').next().next().val());
    }
  });
  $('td.in span input[name*=kogus]').keyup(function()
  {
    minamount = $(this).parent().next().next().val();
    if ($(this).val() < minamount)
    {
      alert(msg_must_minamount);
      $(this).val(minamount);
    }
    else
    {
      amount = $(this).val();
      productID = $(this).parent().next().val();
      //alert(productID + ' ' + amount);
      sBasket('changeAmount', productID, amount);
    }
  });
  $('.slide_left').hover
  (
    function() { scroller = setInterval('scrollerMove("right", 5)', 5); },
    function(){ clearInterval(scroller); }
  );
  $('.slide_right').hover
  (
    function() { scroller = setInterval('scrollerMove("left", 5)', 5); },
    function(){ clearInterval(scroller); }
  );
  $('#big_image').click(function(e)
  {
    data = $(this).attr('src').split('&');
    alt = $(this).attr('alt');
    //$('#gallery .content').html('<div style="text-align: center;"><img src="'+data[0]+'&'+data[1]+'&maxwidth=640&maxheight=480" alt="'+alt+'" />'+((alt != '')?('<p>'+alt+'</p>'):'')+'</div>');
    show_gallery(true, $(this).offset().top);
  });
  $('li.thumb a').click(function(e)
  {
    img = $(this).find('img');
    src = img.attr('src').split('&');
    alt = img.attr('alt');
    if (alt == 'YouTube')
    {
      $YouTubeLink = img.data('link');
      $YouTubeLink = $YouTubeLink.replace('watch?v=', 'v/');
      $YouTube = '<object width="258" height="170"><param name="movie" value="'+$YouTubeLink+'&hl=en_US&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+$YouTubeLink+'&hl=en_US&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="258" height="170"></embed></object>';
      $('#big_image').parent().hide();
      $('#youTubeContainer').html($YouTube);
      $('#youTubeContainer').show();
    }
    else
    {
      $('#youTubeContainer').hide();
      $('#big_image').parent().show();
	  if (src[0].indexOf('picture_w') !== -1) {
		src[0] = src[0].replace('picture_w', 'picture_w2');
	  }
      $('#big_image').attr('src', src[0]+'&'+src[1]+'&width=258&height=170');
      $('#big_image').attr('alt', alt);
      $pictures = $('#gallery .content div');
      $pictures.hide();
      showImg = $pictures.get($('li.thumb a').index($(this)));
      $(showImg).show();
    }
  });
  
  $('li.korv a').click(function(e)
  {
    ID = $(this).next().val();
    amount = $('input[name*=kogus]').val();
    basket = $('#smallbasket');
    target = $(this);
    if (cloneImg == null)
    {
      $.ajax(
      {
        type: "POST",
        data: "ajax=1&action=addProduct&ID="+ID+"&amount="+amount+"",
        dataType: "script",
        beforeSend: function()
        {
          if ($('.toodesmall a img:first').is('img'))
            el = $('.toodesmall a img:first').clone(true);
          else
            el = $('#picture_small_'+ID).clone(true);
          
          el.hide();
          el.insertAfter(target);
          el.show();
          el.css('position', 'absolute');
          el.css('z-index', '10000');
          cloneImg = el;
        },
        success: function(data)
        {
          sBasket = eval(data);
          amount = $('#smallbasket_amount').text();
		  priceEEK = sBasket.overall.price_total_format * 15.6466;
          $('#smallbasket_amount').text(sBasket.overall.amount + ' ' + amount.split(' ')[1]);
          $('#smallbasket_price').text(sBasket.overall.price_total_format + ' EUR (' + priceEEK.toFixed(2) + ' EEK)');
          
          el = cloneImg;
          qtop = el.offset().top - basket.offset().top;
          qleft = basket.offset().left - el.offset().left;
          el.animate({
          opacity: 0,
          left: '+='+qleft,
          bottom: '+='+1.5*qtop,
          height: '-=100'
          }, 1000, function()
          {
            el.remove(); cloneImg = null;
          });
        }
      });
    }
  });
  $('.sliderinfo ul li a').click(function(e)
  {
    ID = $(this).next().val();
    amount = $(this).next().next().val();
    basket = $('#smallbasket');
    target = $(this);
    if (cloneImg == null)
    {
      $.ajax(
      {
        type: "POST",
        data: "ajax=1&action=addProduct&ID="+ID+"&amount="+amount+"",
        dataType: "script",
        beforeSend: function()
        {
          el = $('#picture_small_'+ID).clone(true);
          el.hide();
          el.insertBefore(target);
          el.show();
          el.css('position', 'absolute');
          el.css('z-index', '10000');
          cloneImg = el;
        },
        success: function(data)
        {
          sBasket = eval(data);
          amount = $('#smallbasket_amount').text();
		  priceEEK = sBasket.overall.price_total_format * 15.6466;
          $('#smallbasket_amount').text(sBasket.overall.amount + ' ' + amount.split(' ')[1]);
          $('#smallbasket_price').text(sBasket.overall.price_total_format + ' EUR (' + priceEEK.toFixed(2) + ' EEK)');
          
          el = cloneImg;
          qtop = el.offset().top - basket.offset().top;
          qleft = basket.offset().left - el.offset().left;
          
          el.animate({
          opacity: 0,
          left: '+='+qleft,
          bottom: '+='+1.5*qtop,
          height: '-=100'
          }, 1000, function()
          {
            el.remove(); cloneImg = null;
          });
        }
      });
    }
  });
});
