// Thanks to safari, this has to be placed BELOW the iframes - waiting until
// document.ready will stop the iframe load binding from ever firing.  ever.

$("#tabs").show();
$("#loader").show();

autoHeightCheck = null;
loadedFrames = new Array();

// This function will force the frames to load if the load bind doesn't detect
// them for whatever reason.  This is a fallback and won't be run if the frame
// has already been loaded (so it won't refresh an already loaded iframe).
var loadFallback = function(){
	if (loadedFrames.length < $("iframe").length)
	{
		// Reload and then mark the frames as loaded, even if they're not.  This makes the tabs work
		$("iframe").each(function(i , item){
			// Is the frame loaded
			if ($.inArray($(this).attr('id'), loadedFrames) == -1)
			{
				// Mark the frame as loaded
				loadedFrames[loadedFrames.length] = $(this).attr('id');
				
				// Reload the iframe
				document.getElementById($(this).attr('id')).src = document.getElementById($(this).attr('id')).src;
			}
		});
		
		// Hide the loader graphic
		$("#loader").hide();
		
		// Hide any visible frames
		$("#.uploadFrame").hide().removeClass('autoHeight');
		
		// Make the my computer uploader visible
		$("#tab-computer-frame").addClass('autoHeight');
		$("#tab-computer-frame").show();
		
		// Add the autoheight resize
		autoHeightCheck = setInterval('doIframe()', 100);
	}
};

// Sometimes the iframes don't load because the .load bind doesn't fire correctly.
// If that happens, this will force the my computer iframe to display after five seconds.
setTimeout(loadFallback, 5000);

// Bind a load event to the iframe
$(".uploadFrame").bind("load", function(){
	// Only add it to the array if it's not already there (fallback may have been run)
	if ($.inArray($(this).attr('id'), loadedFrames) == -1)
	{
		loadedFrames[loadedFrames.length] = $(this).attr('id');	
	}
	
	// Should the frame be shown?
	if ($(this).attr('id') == $("#tabs .active:first").attr('id')+"-frame")
	{
		// First load (my computer uploader)
		if (autoHeightCheck === null)
		{
			autoHeightCheck = setInterval('doIframe()', 100);
		}
		
		$("#loader").hide();
		$(this).show();
	}
});

// Tab hover state changes
$("#tabs ul li").hover(function(){
	if ($.inArray($(this).attr("id")+"-frame", loadedFrames) != -1)
	{
		$(this).css({borderLeft:'1px solid #b4b4b4',borderTop:'1px solid #b4b4b4',borderRight:'1px solid #b4b4b4',color:'#000000',cursor:'pointer'});
	}
},function(){
	$(this).css({borderLeft:'1px solid #cfcfcf',borderTop:'1px solid #cfcfcf',borderRight:'1px solid #cfcfcf'});
	// Set the font to red if the tab is active, or grey if it's not
	$(this).css('color',($(this).is('.active')) ? '#ff0000' : '#5b5b5b');
});

// Tab click
$("#tabs ul li").bind("click", function(e){
	if ($.inArray($(this).attr("id")+"-frame", loadedFrames) != -1)
	{
		// If an upload is happening with the flash uploader, prevent the tab from being switched
		if ($("#tab-computer-frame:visible").length && $("#tab-computer-frame").contents().find(".progressContainer").length)
		{
			// If the progessContainer div isn't visible, then no uploads have been added.  Refresh the iframe because the
			// flash uploader will still break if it's used again after losing focus.
			if ($("#tab-computer-frame:visible").length && !$("#tab-computer-frame").contents().find(".progressContainer:visible").length)
			{
				document.getElementById("tab-computer-frame").src = document.getElementById("tab-computer-frame").src;
			}
			else
			{
				alert(i18n.text.uploader.error.tabswitch);
				return;	
			}
		}
		
		// Deselect any tabs currently selected
		$("#tabs .active").removeClass('active').css('color','#5b5b5b');
		// Select the tab that was clicked
		$(this).addClass('active').css('color','#ff0000');
		
		// Disable the iframe auto height check
		$(".uploadFrame").removeClass('autoHeight');
		clearInterval(autoHeightCheck);
	
		// Figure out what page to load
		$(".uploadFrame").hide();
		$("#"+$(this).attr('id')+"-frame").show().addClass('autoHeight');
		
		// iframe auto-height check
		autoHeightCheck = setInterval('doIframe()', 100);
	}
});

// Facebox bind for the email modal launched via upload.js
$(document).bind('reveal.facebox', function(){
	$("#facebox").find("#sendEmail").bind("click", function(e){
		e.preventDefault();

		$(this).attr('disabled', true).text("Please wait");
		
		$.ajax({type: "POST",
				cache: false,
				url: "/file/complete.if.php/"+$("#facebox").find("#hashList").val(),
				data: "func=email&recipientEmail="+$("#facebox").find("#recipientEmail").val()+"&userEmail="+$("#facebox").find("#userEmail").val(),
				dataType: "html",
				success: function(response){
					$("#facebox").find('.modal').fadeOut(500, function(){
						$(this).find("form").remove();
						$(this).find("p").replaceWith(response);
						$(this).fadeIn(500);
					});
				}
		});
	});
});

$(function(){
	$(".createButton").hover(function(){
		$(this).css({cursor:'pointer'});
	},
	function(){
		$(this).css({cursor:'auto'});
	})
	.click(function(e){
		window.location = "/member/signup.php";
	});
});