$(document).ready(function(){
	if (window.top != window)
	{
		window.top.location = document.location.href ;
	}
	
	//Show and hide ShareThis pop up when switching tabs.	
	var showPanel = function() {
		$('.stframe').show();
	};
	
	var hidePanel = function() {
		$('.stframe').hide();
	};			
	
	$('.tabs a[href="#rate"]').click(hidePanel);
	$('.tabs a[href="#share"]').click(showPanel);
	$('.tabs a[href="#flag"]').click(hidePanel);	
	
	// Bind the preview for any images
	$(".showPreview").unbind("click").bind("click", function(e){
		e.preventDefault();
		
		footerData = '<p style="margin: 0; padding: 0; color: #777; font-size: 1em; text-align: left">\
						Preview of '+$("#name-"+$(this).attr('id')).text()+'\
					  </p>';
		if ($("#link-"+$(this).attr('id')).text() != "")
		{
			footerData += '<p style="margin: 0; padding: 0; color: #777; font-size: 1em; text-align: left">\
							<a href="'+$("#link-"+$(this).attr('id')).text()+'" target="_blank">'+i18n.text.file.modalPreviewLink+'</a>\
						   </p>';
		}
		
		// Grab the viewhash
		if ($(this).hasClass('video'))
		{
			$.facebox({image:'/file/'+$(this).attr('id')+'/preview/v/preview.jpg'}, null, null, footerData);
		}
		else
		{
			$.facebox({image:'/file/'+$(this).attr('id')+'/preview/m/preview.jpg'}, null, null, footerData);
		}
	})
	.hoverIntent(function(){
		if ($(this).hasClass('nothumb'))
		{
			return;
		}
		
		// Get the hash of the file
		imgHash = $(this).attr('id');
		// Get the name of the file
		imgFilename = $(this).parents("tr").find(".name a").attr('href')
		// Get the offset of the icon
		imgOffset = $(this).offset();
		// If the offset-110px is < 0 the preview would be off-screen, so send it to the right
		if ((imgOffset.left-110) < 0)
		{
			expandDirection = 'right';
			leftOffset = imgOffset.left+20;
		}
		else
		{
			expandDirection = 'left';
			leftOffset = imgOffset.left-60;
		}
		
		// Create a container div
		newDiv = '<div id="thumb-'+imgHash+'" style="width: 50px; height: 50px; position: absolute; top: '+(imgOffset.top-20)+'px; left: '+leftOffset+'px; background: #000 url(/img/filemanager/previewloader.gif) 50% 50% no-repeat; border: 1px solid #163aad;">\
				  	<table cellspacing="0" cellpadding="0" border="0" style="width: 100%; height: 100%;">\
				  		<tr>\
				  			<td id="imgPreview" style="width: 100%; height: 100%; vertical-align: middle; text-align: center;"></td>\
				  		</tr>\
				  	</table>\
				  </div>';
		// Add the div
		$("#main").append(newDiv);
		$("#thumb-"+imgHash).fadeIn(300);
		
		// Load the image and then resize the container when it's loaded
		var image = new Image();
		image.onload = function() {
			// IE sucks such an amazingly huge amount.
			imgHeight = image.height;
			imgWidth = image.width;
			
			$("#thumb-"+imgHash).find("#imgPreview").append('<img style="display: none;" id="thumb" src="'+image.src+'"/>');
			setTimeout(resizeThumb, 300);
		}
		image.src = '/file/'+imgHash+'/preview/f/preview.jpg';
	}, function(){
		if ($(this).hasClass('nothumb'))
		{
			return;
		}
		
		hash = $(this).attr('id');
		$("#thumb-"+hash).fadeOut(300).remove();
	});
	
	var resizeThumb = function()
	{
		// Figure out which way to expand (left/right depending on the margin)
		var animateParams = '';
		if (expandDirection == 'left')
		{
			animateParams = {height:imgHeight,width:imgWidth,top:(imgOffset.top-(imgHeight/2)+5),left:(imgOffset.left-(imgWidth+10))};
		}
		else
		{
			animateParams = {height:imgHeight,width:imgWidth,top:(imgOffset.top-(imgHeight/2)+5)};
		}
		
		$("#thumb-"+imgHash).animate(animateParams, 250, function() {
			$("#thumb-"+imgHash).find("#thumb").fadeIn(300);
		});
	}
	
	// These are used for the image hover preview
	var imgHash = '';
	var imgFilename = '';
	var imgOffset = '';
	var imgHeight = 0;
	var imgWidth = 0;
	var expandDirection = '';
});