

//framework template
(function(){
    var m = {}; 
    window.FNG = m; 

    m.AjaxUtil = {
        LoaderOn : function(){ //create:  create window
            //console.log("load started");
        }, 
        LoaderOff : function(){ //wa: 'window action
           //console.log("load ended");
        },
        Message : function(m){ //create:  create window
            var a = $("#alert_call");
            if(a) a.remove();
            $("#right_col").prepend(this.CreateMessageBox(m));
        },
        MessageClose : function(m){ //create:  create window
            $("#alert_call").remove();
        },
        CreateMessageBox : function(m){
            return "<div id='alert_call' class='clearfix'><div class='r_al_b1'><div class='r_al_b2'><div class='r_al_b3'><div class='r_al_b4'><strong>" + m + "</strong><a href='#close' onclick='return(FNG.AjaxUtil.MessageClose())' class='close_message'></a></div></div></div></div></div>";
        }
    },
    m.SomeClass = {
        w : {}, //settings
        create : function(url, wId, f){ //create:  create window
        
        }, 
        action : function(id, m){ //wa: 'window action
           
        }
    },
    m.Util = {
        object : {
            //makes a copy of an object: creates a new instance
            //http://javascript.crockford.com/prototypal.html
            create : function(o) {
                function F() {}
                F.prototype = o;
                return new F();
            }
        }
    }
    
})();


//jquery link-to-form plugin
(function($) {
   
    $.fn.HtmlInput = function(op) {
        
        var l = $("a", this);
        var i = $("input", this);
        l.each(function() {
            $(this).click(function(){
                //resets selected class
                l.removeClass("selected");
                //asigns selected class
                $(this).addClass("selected");
                h = $(this).attr("href");
                //updates form vlaue
                i.val(h);
            });
        });

        return false;
    };
    
    //obtions object
    $.fn.HtmlInput.defaults = {
        selectedClass: 'selected',
        y: 'y'
    };

})(jQuery);





//jquery plugin template
(function($) {
    var L = FNG.AjaxUtil;
    // plugin definition
    
    //private meothod that reads all texboxes
    var R = function(op){
    
        // build main options before element iteration
        var o = $.extend({}, $.fn.Form.defaults, op);
    
        //post pair and falues object
        var b = {};
        //b["m"] = o.postMethod;

        //conlect form data from the taged imputs
        //TODO: look at this bug
        //var I = $(o.inputClass, this);
        
        var I = $(o.inputClass);
        I.each(function() {
            //id of the element is used as a value property
            b[$(this).attr("id")] = $(this).val();
        });
        
        return b;
    }
   
    $.fn.Form = function(op) {
        //TODO
        //so something more elegant than try/catch
        //plugin does not work for multiple forms... "each" function is not working!!
        try{
 
            //form
            var F = $(this);
            
            // build main options before element iteration
            var o = $.extend({}, $.fn.Form.defaults, op);
            
            //form action: ajax url      
            var u = F.attr("action");

            var B = $(o.buttonId);
            
            //form submit
            B.click(function(){
 
                //hides form
                //F.hide();
                F.fadeOut("fast");
            
                //loader image on
                L.LoaderOn();
                
                //sends data
                $.post(u, R(),
                    function(d){
                        //loader image off
                        L.LoaderOff();
                        
                        L.Message(d.message);
                        
                        //detects sucess code
                        if(d.sucess == 1){
                           //popuplatest updated area
                    
                           if(d.data.returnHtml){
                                $(o.returnHtmlWrapper).prepend(d.data.returnHtml);
                           }
                        }
                        else{
                            //F.show();
                            F.fadeIn("fast");
                        }
                }, "json");
                
                return false;
            });
        
        }
        catch(e){
        
        }
        
        //not used
        return false;
    };
    
    //obtions object
    $.fn.Form.defaults = {
        postMethod: 'postVersusComment',
        buttonId: '#button',
        inputClass: '.input',
        errorMessage: 'some error',
        returnHtmlWrapper: '#comments_wrp'
    };

})(jQuery);


//Response Form Plugin
(function($) {
    var L = FNG.AjaxUtil;

    //private meothod that reads all texboxes
    var R = function(ob){
        
        // build main options before element iteration
        var o = $.extend({}, $.fn.hilight.defaults);
        var b = {};
        var I = $(o.inputClass, ob);
        I.each(function() {
            //id of the element is used as a value property
            b[$(this).attr("name")] = $(this).val();
        });

        return b;
    }

    // plugin definition
    $.fn.hilight = function(options) {
        //debug(this);
        
        // build main options before element iteration
        var opts = $.extend({}, $.fn.hilight.defaults, options);
        
        // iterate and reformat each matched element
        return this.each(function() {
            $this = $(this);
            
            var F = $(this);
            
            // build element specific options
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
            
            var B = $('button', F);
            
            var u = F.attr("action");
            
            //B.hover(function(){
            B.click(function(){
                L.LoaderOn();
                
                $.post(u, R(F),
                    function(d){
                        //loader image off
                        L.LoaderOff();
                        F.hide();
                        L.Message(d.message);
                        
                        //detects sucess code
                        if(d.sucess == 1){
                           //popuplatest updated area
                           if(d.data.returnHtml){
                                F.before(d.data.returnHtml);
                           }
                        }
                        else{
                            //F.show();
                            F.fadeIn("fast");
                        }
                }, "json");
                
                return false;
            });
            
        });
        
        
        
        
        
        
        
    };

    // private function for debugging
    function debug($obj) {
        if (window.console && window.console.log)
        window.console.log('hilight selection count: ' + $obj.size());
    };

    // define and expose our format function
    $.fn.hilight.format = function(txt) {
        return '<strong>' + txt + '</strong>';
    };

    //obtions object
    $.fn.hilight.defaults = {
        postMethod: 'postVersusComment',
        buttonId: '#button',
        inputClass: '.input',
        errorMessage: 'some error',
        returnHtmlWrapper: '#comments_wrp'
    };
        
})(jQuery);


$(document).ready(function(){

    $('.versus_comment_response').hilight();

    $('#insert_versus').Form();
    
    $(".show_form_btn").click(function () {
        $("#insert_versus_wrp").show();
        return false;
    });


      $('.login').click(function() {         
        $.blockUI({ 
                message: $('#pop_login_wrp'), 
                fadeIn: 90, 
                fadeOut: 90, 
                timeout: 30000, 
                showOverlay: true, 
                centerY: false, 
                css: { 
                    width: '350px', 
                    top: '20%',
                    border: 'none', 
                    backgroundColor: '#fff', 
                    '-webkit-border-radius': '10px', 
                    '-moz-border-radius': '10px', 
                    opacity: 1, 
                    color: '#fff' 
                } 
            });
             $('.blockOverlay').click($.unblockUI); 
        return false;      
        //setTimeout($.unblockUI, 2000);     
    }); 

});
