﻿rules = {};

rules.openEditor = function(id, callback) {
    //$('#result').slideUp('normal');
    $('#result').hide();
    //$('.editor').slideUp('normal');
    $('.editor').hide();
    $(id + ' .editor-help .editor-help-item').hide();
    $(id + ' .editor-help .editor-help-default').show();
    //$(id).slideDown('normal', callback);
    $(id).show(callback);
}

rules.closeEditor = function(callback) {
    //$('.editor').slideUp('normal');
    $('.editor').hide();
    //$('#result').slideDown('normal', callback);
    $('#result').show(callback);
}

rules.editorHelp = function() {
    $('.editor h2').live("click", function(e) {
        $('.editor-help .editor-help-item', $(this).parents('.editor')).hide();
        $('.editor-help .editor-help-default', $(this).parents('.editor')).show();
    });
    $('.editor-grid tr *').live("focusout", function(e) {
        $('.editor-help .editor-help-item', $(this).parents('.editor')).hide();
        $('.editor-help .editor-help-default', $(this).parents('.editor')).show();
    });

    $('.editor-grid tr *').live("click focusin", function(e) {
        var topic = $(this).parents('tr').attr('topic');

        if (topic != null) {
            $('.editor-help .editor-help-default', $(this).parents('.editor')).hide();
            $('.editor-help .editor-help-item[topic!=' + topic + ']', $(this).parents('.editor')).hide();
            $('.editor-help .editor-help-item[topic=' + topic + ']', $(this).parents('.editor')).show();
        }
        else {
			$('.editor-help .editor-help-item', $(this).parents('.editor')).hide();
			$('.editor-help .editor-help-default', $(this).parents('.editor')).show();
        }
    });
};

rules.projectDetail = function() {
    $('.simulation-table thead .simulation-project-head .simulation-project-actions .action-detail').live("click", function(e) {
        $('.simulation-project-detail', $(this).parents('.simulation-project-head')).show();
        $('.action-detail', $(this).parents('.simulation-project-actions')).hide();
        $('.action-close-detail', $(this).parents('.simulation-project-actions')).show();
    });

    $('.simulation-table thead .simulation-project-head .simulation-project-actions .action-close-detail').live("click", function(e) {
        $('.simulation-project-detail', $(this).parents('.simulation-project-head')).hide();
        $('.action-close-detail', $(this).parents('.simulation-project-actions')).hide();
        $('.action-detail', $(this).parents('.simulation-project-actions')).show();
    });
};

rules.simulationDetail = function() {
    $('.simulation-table tbody .simulation-category-actions .action-detail').live("click", function(e) {
        var category = $(this).attr('category');

        if (category != null) {
            $('tr.simulation-detail[@category=' + category + ']', $(this).parents('tbody')).show();
            $('.action-detail', $(this).parents('.simulation-category-actions')).hide();
            $('.action-close-detail', $(this).parents('.simulation-category-actions')).show();
        }
    });

    $('.simulation-table tbody .simulation-category-actions .action-close-detail').live("click", function(e) {
        var category = $(this).attr('category');

        if (category != null) {
            $('tr.simulation-detail[@category=' + category + ']', $(this).parents('tbody')).hide();
            $('.action-close-detail', $(this).parents('.simulation-category-actions')).hide();
            $('.action-detail', $(this).parents('.simulation-category-actions')).show();
        }
    });
};

rules.global = function() {
	$('#actions-ideas').live("click", function(e) {
		util.sendToIdeas();
	});
	$('.news-block-ideas span.link').live("click", function(e) {
		util.sendToIdeas();
	});
	$('#actions-contact').live("click", function(e) {
		util.sendToContact();
	});

    rules.editorHelp();
    rules.projectDetail();
    rules.simulationDetail();
};