function updateSlideshowCookie()
{
  if ((getCookie('SlideshowIsRunning') != 'Yes') && (getCookie('SlideshowIsRunning') != 'No'))
  {
  	var date = new Date();
  	date.setTime(date.getTime()+(1000*24*60*60*1000)); // 1000 days
  	setCookie('SlideshowIsRunning', 'Yes', date.toGMTString(), "/");
  }
}

function updateSlideshowControl()
{
  if (getCookie('SlideshowIsRunning') == 'Yes')
  	$('slideshow-control').update($F('slideshow-stop'));
  else
  	$('slideshow-control').update($F('slideshow-start'));
}

function toggleSlideshowCookie()
{
  var date = new Date();
  date.setTime(date.getTime()+(1000*24*60*60*1000)); // 1000 days
  setCookie('SlideshowIsRunning', (getCookie('SlideshowIsRunning') == 'Yes')? 'No' : 'Yes', date.toGMTString(), "/");
}

function toggleSlideshowUser(giftId, galleryImagesCount)
{
  toggleSlideshowCookie();

  updateSlideshowControl();
}

function startSlideshow(giftId, galleryImagesCount)
{
  setInterval('nextSlide('+giftId+','+galleryImagesCount+')', 5000);
}

function nextSlide(giftId, galleryImagesCount)
{
  if (getCookie('SlideshowIsRunning') == 'Yes')
  {
    deactivateAllThumbs();

    slideIndex++;
    if (slideIndex > galleryImagesCount) slideIndex = 0;

    if (slideIndex != 0)
    	url = '/products/gift_' + giftId + '_gallery_' + slideIndex + '.jpg';
    else
    	url = '/products/gift_' + giftId + '_main.jpg';

    crossfade(document.getElementById('main_photograph_img'), url, '2', '');

    $('thumb_'+slideIndex).addClassName('activeThumb');
  }
}

function showImage(raiser, url)
{
  var date = new Date();
  date.setTime(date.getTime()+(1000*24*60*60*1000)); // 1000 days
  setCookie('SlideshowIsRunning', 'No', date.toGMTString(), "/");
  $('slideshow-control').update($F('slideshow-start'));
  deactivateAllThumbs();
  $(raiser).addClassName('activeThumb');
//  $('main_photograph_div').style.backgroundImage = url;
  $('main_photograph_img').src = url;
}

function deactivateAllThumbs()
{
  for (i = 0; i <= 25; i++)
  {
     if ($('thumb_' + i) != null)
     {
       $('thumb_' + i).removeClassName('activeThumb');
       $('thumb_' + i).addClassName('inactiveThumb');
     }
  }
}

function recalculateTotal()
{
   var fields = $$('input.textfield5');

   var total = 0;
   fields.each(function(item) {
     var count = $F(item.readAttribute('id'));

     if ($F(item.readAttribute('id')).length > 0)
     {
        total += $F(item.readAttribute('id')) * getGiftPrice(item.readAttribute('name'));
     }
   });


   $('basket_total_sum').update(setPriceSpaces(total));

   updateBasketCookie();
}

function updateBasketCookie()
{
   var fields = $$('input.textfield5');

   var newCookieValue = '';
   fields.each(function(item) {
       var fieldname = item.readAttribute('name');
       var underscore = fieldname.indexOf('_', 0);
       var giftId = fieldname.substring(underscore + 1, fieldname.length);
       var quantity = $F(item.readAttribute('id'));
       for (i = 0; i < quantity; i++)
       {
        if (newCookieValue.length != 0)
         { newCookieValue += ":" + giftId; }
        else
         { newCookieValue += giftId; }
       }
   });

   var date = new Date();
   date.setTime(date.getTime()+(180*24*60*60*1000)); // 180 days

   setCookie('goods', newCookieValue, date.toGMTString(), "/");
}

function getGiftPrice(fieldname)
{
  var underscore = fieldname.indexOf('_', 0);
  var giftId = fieldname.substring(underscore + 1, fieldname.length);
  return $('price_' + giftId + '_div').readAttribute('class');
}

function removeFromBasket(giftId)
{
  var value = getCookie('goods');
  if ((value != null) && (value.length > 0)) {
    var splitCookie = value.split(":");
    var newCookieValue = '';
    for (i = 0; i < splitCookie.length; i++) {
      if (splitCookie[i] != ("" + giftId)) {
        if (newCookieValue.length != 0)
         { newCookieValue += ":" + splitCookie[i]; }
        else
         { newCookieValue += splitCookie[i]; }
      }
    }
    //
    var date = new Date();
    date.setTime(date.getTime()+(180*24*60*60*1000)); // 180 days

    setCookie('goods', newCookieValue, date.toGMTString(), "/")

    //$("gift_cell_" + giftId).remove();
    document.location.reload();
  }
}

function buy(giftId)
{
  $('gradient_top').style.backgroundImage = 'url(/resources/img/zenneg_bg_top_inbasket.gif)';
  $('gradient_bg').style.backgroundImage = 'url(/resources/img/zenneg_bg_inbasket.gif)';
  $('gradient_bottom').style.backgroundImage = 'url(/resources/img/zenneg_bg_btm_inbasket.gif)';
  $('reserve').update('<span style="color:#b3ad82; font-family:Georgia; font-size:17px;">Товар добавлен в <a href="/checkout" style="color:#fbf1ae">корзину</a></span>');

  var value = getCookie('goods');
  if ((value == null) || value.length == 0) {
      value = "" + giftId;
  }
  else {
      value = value + ":" + giftId;
  }

  var date = new Date();
  date.setTime(date.getTime()+(180*24*60*60*1000)); // 180 days

  setCookie('goods', value, date.toGMTString(), "/");
}


function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}


function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function setPriceSpaces(price) {
  var pstr = ""  + price;

  if (pstr.length <= 3) return price;

  return setPriceSpaces(
       pstr.substr(0, pstr.length - 3)) + '&nbsp;' +
       pstr.substr(pstr.length - 3);
}


//	Начало скрипта выпадающего меню

 oldLayer = false;
    function place_me(posEl, divEl, show) {
//	posEl = document.getElementById('positionElement');


	if (divEl && posEl) {
	    if (show) {
		if (oldLayer) {
		    oldLayer.style.visibility = 'hidden';
		}

		oldLayer = divEl;
		divEl.style.left = findPosX(posEl);
		divEl.style.top = findPosY(posEl) + 18;
		divEl.style.visibility = 'visible';
	    } else {
		divEl.style.visibility = 'hidden';
	    }
	}
    }

    function mouse_move(e) {
	if (oldLayer) {
	    x = e ? e.screenX : event.x;
	    y = e ? e.screnY : event.y;
	    lx = oldLayer.style.left.replace(/.*?(\d+).*/, '$1') - 0;
	    ly = oldLayer.style.top.replace(/.*?(\d+).*/, '$1') - 0;
	    lh = oldLayer.clientHeight;


	    if (x < (lx - 20) || x > (lx + 170) || y < (ly - 140) || y > (ly + lh + 70)) {
		oldLayer.style.visibility = 'hidden';
		oldLayer = false;
	    }
	}
    }

    function findPosX(obj) {
        var curleft = 0;
        if (obj.offsetParent) {
            while (obj.offsetParent) {
                curleft += obj.offsetLeft
                obj = obj.offsetParent;
            }
        } else if (obj.x) {
            curleft += obj.x;
        }
        return curleft;
    }

    function findPosY(obj) {
        var curtop = 0;
        if (obj.offsetParent) {
            while (obj.offsetParent) {
                curtop += obj.offsetTop;
                obj = obj.offsetParent;
            }
        } else if (obj.y) {
            curtop += obj.y;
        }
        return curtop;
    }

    function hlite_on(srcEl, name) {
	el = document.getElementById(name);
	place_me(srcEl, el, true);
    }


    document.onmousemove = mouse_move;

//	Конец скрипта выпадающего меню