﻿$(document).ready(
    function (event) {

        //Redirect from chart to page
        $("div.chart").mousedown(
            function (event) {
                if (!($(this).attr("id").length == 0)) {
                    window.location = $(this).attr("id");
                }
                else {
                    //Do nothing
                }
            }
        );


        //////////////////////////////
        //Relevant for columns
        //////////////////////////////

        //Set titles for column charts
        $("div.chart > div").attr('title',
            function (event) {
                if (!($("h4", this).length == 0)) {
                    return $("h4", this).text()
                }
                else {
                    return $("h3", this).text()
                }
            }
        );

        //Generate text: How clients rate us...
        $("div.chart .bar > div.red").each(
            function (index, value) {
                $("<div class='legendtext'>How clients rate us...</div>").insertBefore(value);
            }
        );

        //Generate text: How agencies rate us...
        $("div.chart .bar > div.gray").each(
            function (index, value) {
                $("<div class='legendtext'>How agencies rate us...</div>").insertBefore(value);
            }
        );


        $("div.chart .bar").attr("title",
            function (event) {
                return $(this).children().text().replace('...', ' ').replace('How a', 'A').replace('How c', 'C') + ' of 5.00'
            }
        );

        //Set width for bars
        $("div.chart .bar > div.red").css("width",
            function (event) {
                return $(this).text() / 5 * 100 + '%'
            }
        );

        //Set width for bars
        $("div.chart .bar > div.gray").css("width",
            function (event) {
                return $(this).text() / 5 * 100 + '%'
            }
        );

        //The tipsy effect
        $("div.chart .bar").tipsy({ delayIn: 50, delayOut: 50, gravity: 'e' });

        //Bar mouseenter
        $("div.chart .bar").mouseenter(
            function (event) {
                $(this).animate({ opacity: 0.8 }, 100)
            }
        );

        //Bar mouseleave
        $("div.chart .bar").mouseleave(
            function (event) {
                $(this).animate({ opacity: 1 }, 100)
            }
        );

        //////////////////////////////
        //Relevant for tables
        //////////////////////////////
        $("div.chart tbody tr").mouseenter(
            function (event) {
                $(this).animate({ backgroundColor: '#b0adad', color: '#ffffff' }, 70)
                $(this).animate().css("font-weight", "bold", 70)
            }
        );

        $("div.chart tbody tr").mouseout(
            function (event) {
                $(this).animate({ backgroundColor: '#e5e4e5', color: '#5f5f5f' }, 70)
                $(this).animate().css("font-weight", "normal", 70)
            }
        );
    }
);
