/**
 * Ebax Edujee Javascript UI Enhancements
 * Copyright: Ebax Oy
 * Version: SVN $Id: ebax_edujee.js 2225 2010-08-27 13:08:42Z miska $
**/
$(document).ready(function() {

    // workspace admin menu toggle
    $('a#ebax_workspace_admin_menu_title').click(function() {
        $('div#ebax_workspace_admin_menu ul').toggle('normal');
        return false;
    });
    $('div#ebax_workspace_admin_menu ul').hide();

    // textarea.copypaste select
    $('textarea.copypaste')
        .attr('readonly', 'readonly')
        .click(function() {
            this.select();
        });

    // search result select row
    $('table.search_result tbody tr')
        .click(function() {
            $(this).toggleClass('selected');
        });

    // hide add comment box
    $('div#ebax_add_comment dl').hide();
    $('div#ebax_add_comment')
        .click(function() {
            $('div#ebax_add_comment img.comment_add').remove();
            $('div#ebax_add_comment dl').show(100);
            $('input#form_title').focus();
            $(this).css('cursor', 'default');
            $(this).unbind('click');
        })
        .css('cursor', 'pointer');
    $('div#ebax_add_comment fieldset').append('<img src="https://static.ebaxsite.com/images/icon/chameleon_graphite/ebaxgray/add.gif" alt="add" class="comment_add" >');

    // tooltip help
    jQuery.fn.qtip.styles.ebax_style = {
        width: {
            min: 250,
            max: 400
        },
        border: {
            width: 3,
            radius: 5
        },
        tip: 'leftMiddle',
        name: 'cream'
    };
    $('img.ebax_tooltip_help').qtip({
        content: {
            title: {
                text: '<b>Edujee help</b>',
                button: '<a href="help.php?topic=help" title="Edujee help">?</a>'
            }
        },
        hide: {
            fixed: true,
            delay: 1000
        },
        style: 'ebax_style',
        position: {
            corner: {
                target: 'rightMiddle',
                tooltip: 'leftMiddle'
            },
            adjust: {
                screen: true
            }
        }
    });

    // metadata tagcloud
    var tag_cloud_selector = 'div#ebax_tag_cloud';
    $(tag_cloud_selector).ready(function() {
        edujee_tag_cloud_loader(tag_cloud_selector);
    });

    function edujee_tag_cloud_loader(selector, tag_count, ref_type, ref_id, callback) {
        var backend_path = 'jsdata.php?task=tag_cloud';
        if(isNaN(tag_count) || tag_count < 1) {
            tag_count = 20;
        }
        backend_path += '&limit=' + escape(tag_count);
        if(ref_type == 'workspace' && ref_id) {
            backend_path += '&id_workspace=' + ref_id;
        }
        else if(ref_type == 'user' && ref_id) {
            backend_path += '&id_user=' + ref_id;
        }
        $(selector).hide();
        $(selector).addClass('ebax_tag_cloud');
        jQuery.getJSON(backend_path, function(tag_data) {
            var min_count = tag_data.min_count;
            var max_count = tag_data.max_count;
            var base_size = 50;
            var size_range = $(selector).hasClass('tc_large') ? 200 : 100;
            jQuery.each(tag_data.tags, function(tag, data) {
                tag_count = data.c;
                tag_size = (size_range * (tag_count - min_count)) / (max_count - min_count);
                tag_size = (tag_count > min_count) ? (base_size + tag_size) : base_size;
                tag_html = '<a href="search?tags=' + escape(tag) +'" style="font-size: ' + Math.round(tag_size) + '%;" class="ebax_tag">' + escape(tag)  + '</a> ';
                $(selector).append(tag_html);
            });
            $(selector).show();
        });
    }

    // idea ajax voting
    $('div.edujee_idea_vote input:image').click(function(event) {
        var vote_url = $(this).parent().attr('action') + '&mode=json&vote=' + $(this).val();
        $('div.edujee_idea_vote').ajaxError(function() {
            var current_background_color = $('div.edujee_idea_vote').stop(true, true).css('background-color');
            $('div.edujee_idea_vote')
                .animate({backgroundColor: 'red'}, 500)
                .animate({backgroundColor: current_background_color}, 1000);
        });
        jQuery.post(vote_url, '', function(result) {
            if(result['status'] == 'ok') {
                $('div.edujee_idea_vote span.vote_result')
                    .text(result['points'])
                    .attr('title', result['votes'] + ' votes');
                var current_background_color = $('div.edujee_idea_vote').stop(true, true).css('background-color');
                $('div.edujee_idea_vote')
                    .animate({backgroundColor: 'lime'}, 500)
                    .animate({backgroundColor: current_background_color}, 1000);
            }
        }, 'json');
        return false;
    });

});
