function popupOpen(src, title, params) {
  // Close existing dialog
  $('.popup-dialog').dialog('destroy').remove();

  popup = $(document.createElement('div'))
    .attr('class', 'popup-dialog')
    .css('display', 'none')
    .appendTo('body');

  dialogOpts = {
    title: title,
    modal:     true, 
    bgiframe:  true,
    autoOpen:  true,
    draggable: false,
    resizable: false,
    width:     ($.browser.msie && parseFloat($.browser.version) < 7) ? '400' : 'auto',
    height:    ($.browser.msie && parseFloat($.browser.version) < 7) ? '350' : 'auto',
    position:  'center',
    maxHeight: 600,
    maxWidth:  800,
    zIndex: 5000,
    closeOnEscape: true,
    hide: "fade",
    open: function(event, ui) {
      if ($.browser.msie && parseFloat($.browser.version) <= 7) {
        if ($(this).dialog('option', 'width') == 'auto') {
          $(this).dialog('option', 'width', $(this).width());
        }
      }
    }
  };

  if (undefined !== params) {
    for (var i in params) {
      dialogOpts[i] = params[i];
    }
  }
  
  $(popup).html('<div class="info_popup_loading"><img src="/skin1/images/new/ajax-loader.gif" width="32" height="32" alt="" /><br />loading</div>');
  $(popup).dialog(dialogOpts);
  $(popup).load(src + ' .popup_content', function() {
    	$(popup).dialog(dialogOpts);
  });
  
  // Small hack to close a dialog on window.close() call
  window.close = function() {
    $(popup).dialog('close');
  }
  return true;
}


function controlPrImagesLoading() {
	tmpImgCounter1 = tmpImgCounter1+1;
	if (tmpImgCounter1==tmpImgCounter2) {
		setTimeout(assetPrImages, 200);
	}
}

function assetPrImages() {
	allPrWidthSpace = 570;
	animationEffect = 'easeOutQuart';
	duration = 400;
	prImagesLoaded[0] =1;
	loadingFrame = 1;
	
	loadingTimer = setInterval(_animate_loading, 66);
	clearInterval(loadingTimer);
	
	$('#pr_thumb'+currentPrImage).addClass('current');
	
	//set the width of the product image container
	currentImageWidth = parseInt(defaultPrImageWidth) +2;
	
	tmpwidth = currentImageWidth;
	tmpwidth = currentImageWidth + 'px';
	$('#pr_img').css('width', tmpwidth);
	
	tmpwidth = allPrWidthSpace - currentImageWidth-10;
	tmpwidth = tmpwidth + 'px';
	//$('#pr_thumb_list_container').css('width', tmpwidth);
	//$('#scrollWrapper').css('width', tmpwidth);
	
	$('#pr_thumb_list_container').stop().fadeTo(400, 1, function() {
		
		prImageAnimation = false;
		
		//preload some hover images
		$.ImagePreload(skinImagesDir + "/product/loading-product-bg.png");
		$.ImagePreload(skinImagesDir + "/product/ajax-loader1.gif");
		$.ImagePreload(skinImagesDir + "/product/scroll-left-mouseover.png");
		$.ImagePreload(skinImagesDir + "/product/scroll-right-mouseover.png");
	
		
		$("#pr_thumb_list_container").smoothDivScroll({
			scrollStep: 3,
			scrollInterval:10,
			mouseDownSpeedBooster:3,
			visibleHotSpots: "always"
		})
	});
}

function showPrImg(id, imgUrl, imgWidth) {
	if (id!=currentPrImage && prImageAnimation==false) {
		$('#pr_thumb'+currentPrImage).removeClass('current');
		
		$("#pr_thumb_list_container").smoothDivScroll("destroy");
		
		currentPrImage = id;
		$('#pr_thumb'+currentPrImage).addClass('current');
				
		tmpStr = "javascript:show_detailed_image('" + id + "')";
		$('#zoom_link').attr('href', tmpStr);
		
		currentImageWidth = $('#pr_img').find('img:first').width() +2;
		currentImageHeight = $('#pr_img').find('img:first').height() +2;
		
		if (prImagesLoaded[id]!=1) {
			//set position of preloader and show it
			tmpheight = Math.round(currentImageHeight/2)-20;
			tmpheight = tmpheight + 'px';
			tmpwidth = Math.round(currentImageWidth/2)-20;
			tmpwidth = tmpwidth + 'px';
			
			if (loadingTimer) {
				clearInterval(loadingTimer);
			}
			
			$('#pr_loading_area').css('top', tmpheight);
			$('#pr_loading_area').css('left', tmpwidth);
			$('#pr_loading_area').show();
			loadingTimer = setInterval(_animate_loading, 66);
		}
		
		//preload new image
		var TempImage = new Image();
		TempImage.onload = function() {			
			prImagesLoaded[id]=1;
			clearInterval(loadingTimer);
			
			$('#pr_loading_area').hide();
			
			newImageWidth = parseInt(imgWidth) + 2;
			animatePrImg(imgUrl);
		}
		TempImage.src = imgUrl;
	}
}

_animate_loading = function() {
	$('#pr_loading_area').find('div:first').css('margin-top', (loadingFrame * -40) + 'px');
	loadingFrame = (loadingFrame + 1) % 12;
};

function animatePrImg(imgUrl) {
	prImageAnimation = true;
	
	if (newImageWidth>currentImageWidth) {
		
		//animation, then change containers
		$("#product_thumbnail").ImageStop(true,true);
		$("#product_thumbnail").ImageSwitch(
			{Type:"FadeIn", NewImage:imgUrl, Speed: duration},
			function(){
				tmpwidth = allPrWidthSpace - newImageWidth-10;
				tmpwidth = tmpwidth + 'px';
				$('#scrollWrapper').animate(
					{width: tmpwidth},
					duration,
					animationEffect,
					function() {
						$("#pr_thumb_list_container").smoothDivScroll({
							scrollStep: 3,
							scrollInterval:10,
							mouseDownSpeedBooster:3,
							visibleHotSpots: "always"
						})
					}
				);
				$('#pr_thumb_list_container').animate(
					{width: tmpwidth},
					duration,
					animationEffect
				);
				
				$('#pr_img').animate(
					{width: newImageWidth},
					duration,
					animationEffect,
					function() {
						prImageAnimation =false;
					}
				);
			}
		);
	} else {
		
		//change containers, then animation
		tmpwidth = allPrWidthSpace - newImageWidth-10;
		tmpwidth = tmpwidth + 'px';
		
		$('#scrollWrapper').animate(
			{width: tmpwidth},
			duration,
			animationEffect,
			function() {
				$("#pr_thumb_list_container").smoothDivScroll({
					scrollStep: 3,
					scrollInterval:10,
					mouseDownSpeedBooster:3,
					visibleHotSpots: "always"
				})
			}
		);
		$('#pr_thumb_list_container').animate(
			{width: tmpwidth},
			duration,
			animationEffect
		);
		
		$('#pr_img').animate(
			{width: newImageWidth},
			duration,
			animationEffect,
			function() {
				$("#product_thumbnail").ImageStop(true,true);
				$("#product_thumbnail").ImageSwitch(
					{Type:"FadeIn", NewImage:imgUrl, Speed: duration},
					function(){
						prImageAnimation =false;
					}
				);
			}
		);
	}

}

function show_detailed_image(id) {
	$('#product_view'+id).trigger('click');
}
