function openImageViewer(url, imgWidth, imgHeight) {
  maxWidth = document.viewport.getWidth() - 100;
  if (imgWidth < 300) {
    imgWidth = 300;
  }
  w = Math.min(imgWidth -29, maxWidth);
  
  tb_show('', url + "?width=" + w + "&height=540&KeepThis=true&TB_iframe=true&TB_CustomBorders=true", false);
}

function openVideoViewer(url, imgWidth, imgHeight) {
  maxWidth = document.viewport.getWidth() - 100;
  if (imgWidth < 300) {
    imgWidth = 300;
  }
  w = Math.min(imgWidth -29, maxWidth);
  
  tb_show('', url + "?width=" + w + "&height=460&KeepThis=true&TB_iframe=true&TB_CustomBorders=true", false);
}

function showImageNextPrev(url) {
  showFullScreenLoading('media-container-loading');
  new Ajax.Updater("image-preview", url, { asynchronous:true, evalScripts:true, method:'get', 
                    onComplete:function() { resizeImageToFrame(100); }});
}

function embedFlash(src_flv, splash) {
  flashembed("video-container", 
		{
			src:'/mediaplayer/FlowPlayerDark.swf',
      width:'100%',
      height:'100%',
      wmode:'transparent'
		},
		{ config: {   
  			autoPlay: false,
				play: {opacity: 1}, 
  			showPlayButton: true,
  			autoBuffering: true,
  			controlBarBackgroundColor:'0x2e8860',
  			initialScale: 'scale',
  			controlsOverVideo: 'ease',
  			controlBarBackgroundColor: '-1',
  			controlBarGloss: 'low', 
  			loop: false,
  			splashImageFile: splash,
  			videoFile: src_flv
		}} 
	);  
}

function showVideoNextPrev(url, w, h) {
  showFullScreenLoading('media-container-loading');
  new Ajax.Updater("video-preview", url, { asynchronous:true, evalScripts:true, method:'get', 
                    onComplete:function() { resizeVideoToFrame(100, w, h); }});
}

function resizeVideoToFrame(intPad, vWidth, vHeight) {
  iframe = parent.$("TB_iframeContent");
  if (iframe != null) {
    resizeTB(false);
    dim = Element.getDimensions(iframe);
    w = dim.width;
    h = dim.height;
    if (w == 0 || h == 0) {
      setTimeout("resizeVideoToFrame(" + intPad + "," + vWidth + "," + vHeight + ")", 1);
    } else {
      if (h < 240) { h = 240; }

      if ((vHeight + intPad) > h) {
        sh = h / (vHeight + intPad);

        newH = parseInt(vHeight * sh);
        newW = parseInt(vWidth * sh);
        
        vHeight = newH;
        vWidth = newW;
        
        $("video-container").setStyle({width:newW + "px", height:newH + "px"});
      }

      if (vWidth < 640) {
        $("video-container").setStyle({marginLeft:parseInt((640-vWidth) / 2) + "px" });
      }

      parent.$("TB_iframeContent").setStyle({width: Math.max(640, vWidth) + "px"});
      parent.$("TB_iframeContent").setStyle({height: (vHeight + intPad + 3) + "px"});
      repositionTB(parent.$('TB_window'), Math.max(640, vWidth), vHeight + (intPad + 3));      
    }
  }  
}

function resizeImageToFrame(intPad) {
  iframe = parent.$("TB_iframeContent");
  if (iframe != null) {
    resizeTB(false);
    dim = Element.getDimensions(iframe);
    w = dim.width;
    h = dim.height;
    if (w == 0 || h == 0) {
      setTimeout("resizeImageToFrame(" + intPad + ")", 1);
    } else {
      if (h < 420) { h = 420; }
      img = $("preview-image");
      imgW = img.width;
      imgH = img.height;

      if (imgW < 50 || imgH < 50) {
        setTimeout("resizeImageToFrame(" + intPad + ")", 1);
        return;
      }
      
      if ((img.height + intPad) > h) {
        sh = h / (img.height + intPad);
        newH = parseInt(img.height * sh);
        newW = parseInt(img.width * sh);
        
        img.height = newH;
        img.width = newW;
      }

      parent.$("TB_iframeContent").setStyle({width: Math.max(640, img.width) + "px"});
      parent.$("TB_iframeContent").setStyle({height: (img.height + intPad + 3) + "px"});
      repositionTB(parent.$('TB_window'), Math.max(640, img.width), img.height + (intPad + 3));      
    }
  }
}