/*
$Id: icons_box.js,v 1.9 2008/12/04 11:06:34 max Exp $
vim: set ts=2 sw=2 sts=2 et:
*/

var saved_product_thumbnail = false;
var outImgTO = false;
var dpiconMSIE6Fixed = false;

/* Icon onmouseover handler */
function dicon_over(url, w, h, isPng) {
	var img = document.getElementById('product_thumbnail');
  if (!img)
    return false;

  if (outImgTO) {
    clearTimeout(outImgTO);
    outImgTO = false;
  }

	if (!saved_product_thumbnail) {
    saved_product_thumbnail = new Image();
		saved_product_thumbnail.src = getImgSrc(img);
    saved_product_thumbnail.width = img.width;
    saved_product_thumbnail.height = img.height;
    saved_product_thumbnail.isPng = isPngFix(img);
  }

//  if (!this.onmouseout)
//    this.onmouseout = dicon_out;

  return dicon_set_image(url, w, h, isPng);
}

/* Icon onmouseout handler */
function dicon_out() {
  outImgTO = setTimeout(
    function() {
      dicon_set_image(saved_product_thumbnail.src, saved_product_thumbnail.width, saved_product_thumbnail.height, saved_product_thumbnail.isPng);
    },
    100
  );
  return true;
}

/* Change product thumbnail (temporary) */
function dicon_set_image(src, w, h, isPNG) {
  var img = document.getElementById('product_thumbnail');
  if (!img)
    return false;

//  img.style.display = 'none';
//  img.onload = dicon_show;
//	$("#img_td").css("width", w + "px");
 // img.width = 200;
//  img.width = w;
 img.height = 186;

  if ($.browser.msie && !dpiconMSIE6Fixed && parseInt($.browser.version) < 7 && $('.image-box').height() == config_image_height) {
    $('.image-box').height(config_image_height + 2);
    dpiconMSIE6Fixed = true;
  }

  setTimeout(
    function() {
      img.src = src;
      if (isPNG)
        pngFix(img);
    },
    20
  );

  return true;
}

/* Icon onclick handler */
function dicon_click(index) {
	if (!det_images_popup_data)
		return true;

  setTimeout(
    function() {
//      popup_image2('D', det_images_popup_data.productid, det_images_popup_data.max_x, det_images_popup_data.max_y, det_images_popup_data.product, index);
      popup_image2('D', det_images_popup_data.productid, det_images_popup_data.max_x, det_images_popup_data.max_y, det_images_popup_data.product, index);
    },
    100
  );
  return false;
}

function popup_image2(type, id, max_x, max_y, title, page) {
	if (!page)
		page = 1;

  max_x = parseInt(max_x);
  max_y = parseInt(max_y);

  max_x = (max_x < 1 || !isNaN(max_x)) ? max_x + 40 : 420;
	max_y = (max_y < 1 || !isNaN(max_y)) ? max_y + 40 : 420;

  var url = xcart_web_dir + '/popup_image.php?type=' + type + '&id=' + id + '&title=' + title + '&area=' + current_area + '&page=' + page;

  var res = false;
	res = popupOpen(
      url,
      title,
      Math.max(Math.min(max_x, $(window).width() - 150), 420),
      Math.max(Math.min(max_y, $(window).height() - 150), 420)
    );


  if (res)
    return true;

	return window.open(
    url + "&is_modal=Y",
		'images',
		'width=' + max_x + ',height=' + max_y + ',toolbar=no,status=no,scrollbars=yes,resizable=yes,menubar=no,location=no,direction=no'
	);
}

/* Show delayed product thumbnail */
function dicon_show() {
  var img = document.getElementById('product_thumbnail');
  if (img)
    img.style.display = '';
}



function pngFix(elm) {
  if (!elm || !elm.tagName || !$.browser.msie || elm.tagName.toUpperCase() != 'IMG')
    return false;

  var src = elm.src.replace(/\(/g, '%28').replace(/\)/g, '%29');
  elm.src = images_dir + '/spacer.gif';
  elm.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '",sizingMethod="scale")';

  return true;
}

function getImgSrc(elm) {
  if (!elm || !elm.tagName || elm.tagName.toUpperCase() != 'IMG' || !elm.src)
    return false;

  if ($.browser.msie && elm.src.search(/\/spacer\.gif$/) != -1 && elm.filters['DXImageTransform.Microsoft.AlphaImageLoader'])
    return elm.filters['DXImageTransform.Microsoft.AlphaImageLoader'].src;

  return elm.src;
}

function isPngFix(elm) {
  return $.browser.msie && elm && elm.tagName && elm.tagName.toUpperCase() == 'IMG' && elm.src && elm.src.search(/\/spacer\.gif$/) != -1 && elm.filters['DXImageTransform.Microsoft.AlphaImageLoader'];
}
