/*********************************/
// INITIALIZE 
/*********************************/
$(document).ready(function() {
    /** CREATE APP **/
    toggle_form_element('.select_license', 'license', true);
    toggle_form_element('.free', 'price', false);
    
    /** REPORTS DROPDOWN **/
    $('.reports .date input').click(function() {
		var sel = $('.reports .date select');
		window.location = window.location.pathname + '?month=' + sel.eq(0).attr('value') + '&year=' + sel.eq(1).attr('value');
	});
});
/*********************************/


jQuery.fn.extend({
  id: function() {
  	id = this.attr('id');
    return id.substr(id.indexOf('_') + 1);
  }
});

function modal(title, msg) {
	$('<div>' + msg + '</test>').dialog({
				bgiframe: true,
				modal: true,
				buttons: {
					Ok: function() {
						$(this).dialog('close');
					}
				},
				resizable: false,
				draggable: false,
				dialogClass: 'ui-dialog',
				title: title
			});
	
}

/*********************************/
// FUNCTIONS 
/*********************************/

// Generate 32 char random uuid 
function gen_uuid() {
    var uuid = ""
    for (var i=0; i < 32; i++) {
        uuid += Math.floor(Math.random() * 16).toString(16); 
    }
    return uuid
}

function toggle_form_element(watch_selector, toggle_field, bool) {
    $(watch_selector).change(function() {
        var str = $(this).attr('name');
        var type = str.substr(0, str.indexOf('-'));
        var elm = $('#id_' + type + '-' + toggle_field)[0];
        
        if(!elm) return false;
        
        if($(this).attr('checked') == bool) {
            $(elm).show();
        } else {
            $(elm).hide();
        }
    }).change();
}

/*********************************/
// UPLOAD MANAGER CLASSS 
/*********************************/
upload = {
	add: function(application_id) {
		var div_id = String((new Date()).getTime()).replace(/\D/gi,'');

		var iframe = $('<div id="upload_' + div_id + '"><iframe width="500" scrolling="no" height="40" frameborder="0" src="/application/' + application_id + '/upload/?div_id=upload_' + div_id + '"></iframe></div>');
		$('#uploaders').append(iframe);
	},

	reload_files: function(application_id) {
		$.ajaxSetup({ cache: false });
		$('#files').load('/application/' + application_id + '/files/list/', function() {
		    upload.bind_delete(application_id);
		});
	},
	
	bind_delete: function(application_id) {
	    $('.delete').click(function() {
    		conf = confirm('Are you sure you want to delete this file?');

    		if(!conf) return;
    		
    		var tr = $(this).parents('tr');
            var file_id = tr.attr('data-id');
            
            $(this).replaceWith('Deleting...');
            
            $.ajax({
                type: 'POST',
                url: '/application/' + application_id + '/file/delete/',
                data: 'file_id=' + file_id,
                dataType: 'json',
                success: function(response) {
                    if(response.status == 'success') {
                        $(tr).effect('pulsate', { times: 2 }, '600', function() { $(this).remove(); });
                    }
                },
                error: function() {
                    modal('Error', 'There was an error while deleting that file. Please try again.');
                }
            });
    	});
    }
};