var preview_box = new Object();

document.observe(
	"dom:loaded", function() {
		Event.observe(document,'mouseover',cntrlr.obsrv);
		Event.observe(document,'mouseout',cntrlr.obsrv);
	}
);

cntrlr={
	evt:'',
	elm:'',
	id:'',
	clss:'',
	debug:0,
	positioned:[],mX:0,mY:0, 
	obsrv: function(e)
	{
		var classes=$w(e.element().className);
		if(classes[0]&&classes[0].startsWith('img_preview_box')) {
			cntrlr.clss=classes[0];cntrlr.setNfo(e);
			if (e.type=='mouseover'&&window.showPreview_box) 
				showPreview_box();
			else if(e.type=='mouseout'&&window.hidePreview_box)
				hidePreview_box();
		}
	},
	setNfo: function(e)
	{
		cntrlr.mX=Event.pointerX(e);
		cntrlr.mY=Event.pointerY(e);
		cntrlr.elm=e.element();
		cntrlr.evt=e.type;
		cntrlr.id=$(cntrlr.elm).identify();
	}
}

function showPreview_box() {
	var imgID=cntrlr.id;
	var aftID=imgID.replace('imageThumb_','');
	
	var puID='preview_box_'+aftID;
	
	if(preview_box['img_'+aftID].ShowThumb==0)
		return;
	if(!$(puID))
		createPreview_box(aftID);
	
	var vpDims=document.viewport.getDimensions();
	var vpPos=document.viewport.getScrollOffsets();
	var puDims=$(puID).getDimensions();
	var offset=[];
	
	offset['x']=vpPos['left']+10;
	offset['y']=vpPos['top']-10;
	
	var thmbPos=$(imgID).viewportOffset();
	var thmbDim=$(imgID).getDimensions();
	
	if((thmbPos[0]+thmbDim['width']+puDims['width']+offset['x'])<=vpDims['width'])
		puDims['x']=thmbPos[0]+thmbDim['width']+offset['x'];
	else 
		puDims['x']=thmbPos[0]-puDims['width']-offset['x'];
	
	if((thmbPos[1]+puDims['height'])<=vpDims['height'])
		puDims['y']=thmbPos[1]+offset['y']
	else 
		puDims['y']=offset['y']+vpDims['height']-puDims['height'];

	if(puDims['y']<=vpPos['top'])
		puDims['y']=vpPos['top']+5;
	if($(puID)) {
		$(puID).style.left=puDims['x']+'px';
		$(puID).style.top=puDims['y']+'px';
		$(puID).show();
	}
}

function createPreview_box(aftID) {
	var tmp='';
	tmp += '<div id="preview_box_#{FileID}" class="preview_box" style="display:none; width:#{DimX}px;height:#{DimY}px;">';
	if(preview_box['img_'+aftID].AFT==8){
		if(preview_box['img_'+aftID].Aspect==169) {
			tmp+='<div class="img"><object width="#{DimX}" height="#{VidHt}" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0">';
			tmp+='<param name="movie" value="/_d/preview_box_player_hd.swf?videoLocation=#{ImgSRC}&ratio=#{Aspect}">';
			tmp+='<param name="quality" value="best">';
			tmp+='<param name="loop" value="true">';
			tmp+='<param name="wmode" value="transparent">';
			tmp+='<param name="FlashVars" value="videoLocation=#{ImgSRC}&ratio=#{Aspect}">';
			tmp+='<EMBED SRC="/_d/preview_box_player_hd.swf" LOOP="true" QUALITY="best" FlashVars="videoLocation=#{ImgSRC}&ratio=#{Aspect}" WIDTH="#{DimX}" HEIGHT="#{VidHt}" wmode="transparent">';
			tmp+='</object></div>';
		} else {
			tmp+='<div class="img"><object width="#{DimX}" height="#{VidHt}" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0">';
			tmp+='<param name="movie" value="/_d/preview_box_player.swf?videoLocation=#{ImgSRC}&ratio=#{Aspect}">';
			tmp+='<param name="quality" value="best">';
			tmp+='<param name="loop" value="true">';
			tmp+='<param name="wmode" value="transparent">';
			tmp+='<param name="FlashVars" value="videoLocation=#{ImgSRC}&ratio=#{Aspect}">';
			tmp+='<EMBED SRC="/_d/preview_box_player.swf" LOOP="true" QUALITY="best" FlashVars="videoLocation=#{ImgSRC}&ratio=#{Aspect}" WIDTH="#{DimX}" HEIGHT="#{VidHt}" wmode="transparent">';
			tmp+='</object></div>';}
	} else if (preview_box['img_'+aftID].isAudio)	{
		tmp+=preview_box['img_'+aftID].audioFlashCode;
	} else {
		tmp+='<img src="#{ImgSRC}" style="width:#{DimX}px;height:#{DimY}px;" alt="" />';
	}
	tmp+='</div>';
	
	var preview_boxTemplate=new Template(tmp);
	var html=preview_boxTemplate.evaluate(preview_box['img_'+aftID]);
	
	$('PreviewArea').insert({bottom:html});
}

function hidePreview_box() {
	var preview_boxID=cntrlr.id.replace('imageThumb_','preview_box_');
	var aftID=cntrlr.id.replace('imageThumb_','');
	
	if ($(preview_boxID)) {
		if(preview_box['img_'+aftID].isAudio||preview_box['img_'+aftID].AFT==8){
			$(preview_boxID).remove();
		}
		else {
			$(preview_boxID).hide();
		}
	}
}
