path = "http://chakrarestaurant.com/";

function editItem(id) {
    //$('#content').load("events/editform.php");
    window.location = path+"events_admin.php?mode=edit&id="+id;
}

function addItem() {
    //$('#content').load("events/addform.php");
    window.location = path+"events_admin.php?mode=add";
}

function delItem(id) {
    if (confirm('Do you want to delete event with "id"='+id+' ?')) {
        var p={};
        p['act'] = 'delete';
        p['id'] = id;
        $.getJSON("events/process_req.php",p, function (obj) {
            if (obj.result == 'success') window.location = path+"events_admin.php";
        });
    }
}

function showEvent(id) {
    //alert(id);
    $('#content').load('events/process_req.php?act=item',{'id': id});
}

function showCalendar() {
    $('#content').load('events/process_req.php?act=calendar');
}

function browseItems() {
    window.location = path+"events_admin.php";
}

function dbInsert() {
    var p={};
    p['item_title'] = $('#item_title').val();
    p['description'] = tinyMCE.get('description').getContent();
    p['date'] = $('#date').val();
    p['image_path'] = $('#image_path').val();
    p['image_url'] = $('#image_url').val();
    p['add_item'] = 'insert please';
    $.getJSON("events/process_form.php", p, function(obj) {
        if (obj.result == 'success') {
            $('#res').html('<p>Success!</p>');
            window.location = path+"events_admin.php";
        } else {
            $('#res').html('<p>Error!</p><p>'+obj.msg+'</p>');
        }
    });
}

function dbUpdate() {
    var p={};
    p['item_title'] = $('#item_title').val();
    p['description'] = tinyMCE.get('description').getContent();
    p['date'] = $('#date').val();
    p['image_path'] = $('#image_path').val();
    p['image_url'] = $('#image_url').val();
    p['add_item'] = 'update please';
    p['id'] = $('#item_id').val();
    $.getJSON("events/process_form.php", p, function(obj) {
        if (obj.result == 'success') {
            $('#res').html('<p>Success!</p>');
            window.location = path+"events_admin.php";
        } else {
            $('#res').html('<p>Error!</p><p>'+obj.msg+'</p>');
        }
    });
}

$(document).ready(function() {
$('#fileInput').uploadify({
'uploader'  : 'events/uploadify.swf',
'script'    : 'events/uploadify.php',
'checkScript' : 'events/check.php',
'cancelImg' : 'events/cancel.png',
'auto'      : false,
'folder'    : '/images',
'fileDesc'  : '*.jpg',
'fileExt'   : '*.jpg',
'onComplete' : function(event,queueID,fileObj,response,data) {
                    if (response == '1') {
                        //$('#response').html('<p>File "'+fileObj.name+'" successfully uploaded\n\
                        //<input type="text" class="addform" name="image_path" value="'+fileObj.filePath+
                        //'" disabled="1"/></p>');
                        $('#image_path').val(fileObj.filePath);
                        $('#response').html('<img align="right" width="300" style="border: 0px none ; margin: 0px 10px 10px; padding: 0px;" src="'+ fileObj.filePath +'" alt="PIC"/>');
                        //alert($('#image_path').val());
                    } else {
                        $('#response').html('<p>Error loading file</p>');
                    }
                    //$('#response').append(response);
                },
 'onError'  : function(event,queueID,fileObj,errorObj) {
                    $('#response').html(errorObj.type + '<hr>' + errorObj.info);
                }
});
});
