/*global $, console, jQuery, prompt, saveState, window */

/**
 * A JQuery plugin that will setup the supplied selector elements to toggle their
 * respective defined next element.
 *
 * @version 1.0
 * @requires
 *      jquery.js
 *      jquery.cookie.js
 * @author Ed Rodriguez Ed.Rodriguez@swfwmd.state.fl.us
 * @param string tag The defined next tag to show and hide
 * @param string mode Startup options: show or hide
 * @param string speed The animation speed: slow, fast, normal
 * @param string cookie The name of the cookie
 * @param string prefix For dynamic id generation
 * @param string type The animation type: slide, fade, none
 * @param string path The cookie path
 * @param integer expires The cookie exipiration in days
 * @param function callback An optional callback to perform after toggling
 */
(function($){
    $.fn.toggleNext = function(options){
        var count = 1;
        var opts = $.extend({}, $.fn.toggleNext.defaults, options);
        var cookieOptions = {path: opts.path, expires: opts.expires};
        
        function log(message){
            if(typeof window.console != "undefined" && typeof window.console.log != "undefined"){
                console.log(message);
            }
        }
        
        log(opts.selector);
        if(opts.cookie === ''){
            opts.cookie = 'toggle';    
        }
        
        // BUILD SAVED OPEN STATES
        var states = $.cookie(opts.cookie);
        
        if(states === undefined || states === null){
            states = "";
        }
        
        // SETUP BUTTONS
        log("Elements Found: "+this.length);
        log("Next Tag: "+opts.tag);
        log("Cookie Name: "+opts.cookie);
        log("Cookie Value: "+states);
        
        this.each(function(){
            var elem = $(this);
            var next = elem.next();
            var found = false;
            
            for(var i=0; i<10; i++){
                if(next.is(opts.tag)){
                   found = true;
                   break;
                }else{
                    elem = elem.parent();
                    next = elem.next();
                }
            }
            
            if(found){
                // dynamic id creation
                var id = $(this).attr("id");
                if(id === '' && $(this).data("id") !== undefined){
                    id = $(this).data("id");
                }else if(id === ''){
                    id = opts.prefix+count;
                    $(this).data("id",id);
                    count++;
                }
                log("ID: "+id);
                
                
                // hide if needed
                if(opts.mode == "hide" && states.indexOf(id+",") < 0){
                    next.hide();
                }else if(opts.mode == "show" && states.indexOf(id+",") >= 0){
                    next.hide();
                }
                
                // click setups
                $(this).click(function(){
                    if(next.is(":hidden")){
                        if(opts.type == "slide"){
                            next.slideDown(opts.speed, opts.callback);
                        }else if(opts.type == "fade"){
                            next.fadeIn(opts.speed, opts.callback);
                        }else{
                            next.show();
                            if(opts.callback){
                                opts.callback(this);
                            }
                        }
                    }else{
                        if(opts.type == "slide"){
                            next.slideUp(opts.speed, opts.callback);
                        }else if(opts.type == "fade"){
                            next.fadeOut(opts.speed, opts.callback);
                        }else{
                            next.hide();
                            if(opts.callback){
                                opts.callback(this);
                            }
                        }
                    }
                    saveState(this, next);
                    return false;
                });
            }
        });
    
        // SAVE BUTTON STATE
        function saveState(elem, next){
            var id = $(elem).attr("id");
            if(id === '' && $(elem).data("id") !== undefined){
                id = $(elem).data("id");
            }
            if(id !== ''){
                var mode = (opts.mode == "hide") ? "add" : "remove";
                
                if(opts.type == "slide"){
                    if(next.height() == "1"){
                        mode = (opts.mode == "hide") ? "add" : "remove";
                    }else{
                        mode = (opts.mode == "hide") ? "remove" : "add";
                    }
                }else{
                    if(next.css('display') == "block"){
                        mode = (opts.mode == "hide") ? "add" : "remove";
                    }else{
                        mode = (opts.mode == "hide") ? "remove" : "add";
                    }
                }
                
                // ADD
                if(mode == "add"){
                    if(states.indexOf(id+",") < 0){
                        states += id+",";
                        $.cookie(opts.cookie, states, cookieOptions);
                    }
                    log("Add: "+id+" From: "+states);
                }
                // REMOVE
                else{
                    if(states.indexOf(id+",") >= 0){
                        states = states.replaceAll(id+",","");
                        $.cookie(opts.cookie, states, cookieOptions);
                    }
                    log("Remove: "+id+" From: "+states);
                }
            }
        }
    };
    $.fn.toggleNext.defaults = {
        tag: '', // tag to apply show and hide to
        selector: '',
        mode: 'show', // show, hide
        speed: 'fast', // slow, fast, normal
        type: 'slide', // slide, fade, none
        cookie: '', // an optional cookie name
        prefix: 'b', // for dynamic id generation
        path: '/', // cookie path
        expires: 365, // cookie expire in days
        callback: null
    };
    $.fn.toggleNext.showPrompt = function(type,url){
        if(type == "report"){
            var id = prompt("Enter the ID of the report.","");
            if(id !== undefined && id !== ""){
                window.open(url+id+"/");
            }
        }
        return(false);
    };
})(jQuery);
