/*
 * Name:        Mondot Script
 * Description: This script powers the advanced features of the Mondot.
 *              It needs jQuery to function properly.
 * Author:      Mike Gossman <mike@c572.ca>
 * Date:        2010/10/01
 */

$(function(){
    var mondot = $("#mondot");
    mondot.content = $("#mondot-content");
    mondot.content.menu = mondot.content.find(".menu")[0];
    mondot.content.state = "open";
    mondot.indicator = $("#mondot-indicator");
    mondot.indicator.state = mondot.indicator.hasClass("active") ? "on" : "off";
    mondot.indicator.timer = null;
    mondot.indicator.opacity = 1;
    mondot.logout = $("#mondot-logout");

    //Prevent AJAX caching
    $.ajaxSetup({cache: false});

    //------------------------------Functions--------------------//

    // Function to close the mondot
    mondot.content.slideIn = function(callback){
        mondot.content.stop(true);
        mondot.content.css({'overflow': 'hidden'});
        mondot.content.animate({'width': 0}, 400, callback);
        mondot.content.state = "closed";
    }

    // Function to open the mondot
    mondot.content.slideOut = function(callback){
        mondot.content.animate({'width': mondot.content.menu.offsetWidth+24+'px'}, 400, function(){
            mondot.content.stop(true);
            mondot.content.css({'overflow': 'visible'});
            mondot.content.state = "open";
            if (callback) callback();
        });
    }

    // Pulses the indicator
    mondot.indicator.startPulse = function(speed){
        if (mondot.indicator.state == "off"){
            mondot.indicator.opacity = 0;
            mondot.indicator.css({'opacity': '0'});
            mondot.indicator.addClass('active');
            speed *= 0.1
        }else speed *= -0.1;
        mondot.indicator.timer = setTimeout(function(){mondot.indicator.pulse(speed)}, 150);
    }

    mondot.indicator.pulse = function(speed){
        mondot.indicator.opacity += speed;
        if (mondot.indicator.opacity < 0){
            mondot.indicator.opacity = 0;
            speed *= -1;
        }
        if (mondot.indicator.opacity > 1){
            mondot.indicator.opacity = 1;
            speed *= -1;
        }
        mondot.indicator.css({'opacity': mondot.indicator.opacity});
        mondot.indicator.timer = setTimeout(function(){mondot.indicator.pulse(speed)}, 150);
    }

    mondot.indicator.stopPulse = function(){
        clearTimeout(mondot.indicator.timer);
        if (mondot.indicator.state == "off"){
            mondot.indicator.animate({opacity: '0'}, 'fast', function(){
                mondot.indicator.removeClass('active');
                mondot.indicator.opacity = 1;
                mondot.indicator.css({'opacity': '1'});
            });
        }else{
            mondot.indicator.opacity = 1;
            mondot.indicator.animate({opacity: '1'}, 'fast');
        }
    }

    //------------------------------------Bindings-------------------//

    //Fancy login stuff
    $("form.login", mondot).live("submit", function(){
        mondot.content.slideIn(function(){
            mondot.indicator.startPulse(1);
            $("form.login", mondot).ajaxSubmit({
                url: "?returnType=mondot",
                success: function(responseText){
                    var newMenu = $('#mondot-content', $(responseText)).html();
                    mondot.content.html($("#mondot-content", $(responseText)).html());
                    mondot.content.menu = mondot.content.find(".menu")[0];
                    if ($("form .error", $(responseText)).length == 0){
                        mondot.indicator.state="on";
                    }
                    mondot.content.slideOut();
                    mondot.indicator.stopPulse();
//                     $("#content").animate({opacity: '0'}, "fast", function(){
//                         $("#content").html($("#content", $(responseText)).html().html);
//                         $("#content").animate({opacity: '1'}, "fast");
//                     });
                }
            });
        });
        return false;
    });

    //Navigating within the dot
    $(".mondot-nav", mondot).live("click", function(e){
        if (e.which == 1){
            e.preventDefault();
            mondot.content.slideIn(function(){
                mondot.indicator.startPulse(1);
                $.get("?returnType=mondot", {logout : ''}, function(responseText){
                    mondot.content.html($("#mondot-content", $(responseText)).html());
                    mondot.content.menu = mondot.content.find(".menu")[0];
                    mondot.indicator.state="off";
                    mondot.content.slideOut();
                    mondot.indicator.stopPulse();
//                     $("#content").animate({opacity: '0'}, "fast", function(){
//                         $("#content").html($("#content", $(responseText)).html());
//                         $("#content").animate({opacity: '1'}, "fast");
//                     });
                });
            });
        }
    });

    //Hide when scrolling
    $(window).scroll(function(){
        viewY = $(window).scrollTop();
        mondot.css({'top': viewY+'px'});
        if (viewY > 0 && !mondot.is(":animated")){
            mondot.content.slideIn();
        }
        if (viewY < 24 && !mondot.is(":animated")){
            mondot.content.slideOut();
        }
    });

    //Show contents on click
    mondot.indicator.click(function(){
        mondot.indicator.stopPulse();
        if(mondot.content.state == "closed"){
            mondot.content.slideOut();
        }else if ($(window).scrollTop() > 24){
            mondot.content.slideIn();
        }
    });

});
