var uploadMaxSize 			= 252428800;
var uploadMaxSizeReadable 	= '200MB';

var fileupload = 
{
	uploadEvents: null,
	setupFileupload: function(container, uploadEvents, expressInstallConfig)
	{
		if (!container.id)
		{
			var container_id;
			do
			{
				container_id = uniqueId();
			}
			while(document.getElementById(container_id));
			container.id = container_id;
		}

		fileupload.uploadEvents = uploadEvents;

		if (swfobject.hasFlashPlayerVersion("9.0.115")) {
			var fn = function() {
				var att = {data:websiteUrl +"image/flash/fileupload.swf", width:"100", height:"25", wmode:'transparent', bgcolor:'#ffffff'};
				var par = {
					flashvars:	"event_success=fileupload.uploadEvents.success" +
								"&event_select=fileupload.uploadEvents.select" +
								"&event_progress=fileupload.uploadEvents.progress" +
								"&event_error=fileupload.uploadEvents.error" +
								"&event_start=fileupload.uploadEvents.start" +
								"&event_complete=fileupload.uploadEvents.complete" +
								"&receiver=upload.php" +
								"&fileMaxSize=" + uploadMaxSize,
					bgcolor: '#ffffff',
                    allowscriptaccess: true,
					wmode: 'transparent'
				};
				var flashObj = swfobject.createSWF(att, par, container.id);
				if (uploadEvents.init)
				{
					var flashInitInterval = setInterval(function(){
						if (flashObj && flashObj.setFileFilter)
						{
							clearInterval(flashInitInterval);
							uploadEvents.init(flashObj, container.id);
						}
					}, 100);
				}
			};
			swfobject.addDomLoadEvent(fn);
		}
		else if (expressInstallConfig)
		{
			jQuery(expressInstallConfig.container).empty();
			var flashCont = jQuery('<div class="flashCont"></div>').appendTo(expressInstallConfig.container).get(0);
			setupExpressInstall(flashCont, expressInstallConfig.width, expressInstallConfig.height);
			if (expressInstallConfig.onExpressInstall)
			{
				expressInstallConfig.onExpressInstall();
			}
		}
	},
	setupExpressInstall: function(container, width, height)
	{
		if (!container.id)
		{
			var container_id;
			do
			{
				container_id = uniqueId();
			}
			while(document.getElementById(container_id));
			container.id = container_id;
		}

		width = width || 260;
		height = height || 170;

		var fn = function() {
			var att = {data:websiteUrl +"image/expressInstall.swf", width:width, height:height, wmode:'transparent', bgcolor:'#ffffff'};
			var par = {
				flashvars:	"",
				bgcolor: '#ffffff'
			};
			var flashObj = swfobject.createSWF(att, par, container.id);
			if (uploadEvent.init)
			{
				var flashInitInterval = setInterval(function(){
					if (flashObj && flashObj.setFileFilter)
					{
						clearInterval(flashInitInterval);
						uploadEvent.init(flashObj, container.id);
					}
				}, 100);
			}
		};
		swfobject.addDomLoadEvent(fn);
	}
}

var videoFileUploadCallbacks =
{
    $progressBar:   null,
    $filename:      null,
	flashObj:       null,
    
	init: function(flashObj, id)
    {
        this.$progressBar   = $('#progress-bar-meter');
        this.$filename      = $('input[name=video_file]');

        this.reset();
		this.flashObj = flashObj;
		this.setup(this.flashObj);
	},

	setup: function(flashObj)
    {
		flashObj = flashObj || this.flashObj;
		var setupInterval = setInterval(function(){
			if (flashObj && flashObj.setFileFilter) {
				clearInterval(setupInterval);

				flashObj.setFileFilter([{
					description: 'Videos (.wmv, .mov, .flv, .3gp, .avi, .mpg, .mp4)',
					extension: '*.wmv;*.mov;*.flv;*.3gp;*.avi;*.mpg;*.mp4'
				}]);

				flashObj.setReceiver(websiteUrl + 'service/upload');
			}
		}, 100);
	},

	browse: function()
    {
		this.reset();
		this.setup(this.flashObj);
		this.flashObj.browse();
	},

	success: function(response)
    {
        this.reset();
        $('input[name=video_filepath]').val(response);

        var formData = $('form[name=upload-form]').serialize();
        $.post(websiteUrl + 'service/video/create-new/', formData, function(response)
        {
            $('#submit-loading').hide();
            //alert(systemMessages.verification_email_sent);
            $('#submit-button').show();
            window.location.replace(websiteUrl);
        });
	},

	select: function(filename)
    {
		fileSelected = true;
        this.$filename.val(filename);
        //this.upload();
	},

	start: function()
    {
	},

	progress: function(pct)
    {
        pct = Math.ceil(pct);
		this.$progressBar.progressbar('option', 'value', pct);
	},

	complete: function()
    {
	},

	error: function(response)
    {
		fileuploading = false;
		alert(response);
	},

	upload: function()
    {
        $('div.progress-bar-wrapper').show();
		this.flashObj.upload();
	},

	reset: function()
    {
        $('div.progress-bar-wrapper').hide();
		fileuploading = false;
        this.$progressBar.progressbar('option', 'value', 0);
        this.$filename.val('');
	}
}