var ajax_loader = "<div id='ajax_loader' class='floater'><img src='/_MVC/home/images/ajax-loader.gif' border='0' /></div>";

function check_default_text(me){

    id = me.attr('id');
    if ( me.val().length == 0) {
        me.val(id);
    } 
}

function reload_active_togglers(){
    if ( $('.active_toggle').length > 0 ){

        //console.debug('reloading active togglers');
        $('.active_toggle').unbind().click ( function(){

           me = $(this);
           idArr = $(this).attr("id").split(":");

           rowID = "#sortOrder_" + idArr[1];

           parms = "model=" + idArr[0] + "&id="+idArr[1];
           me.html( ajax_loader );
           $.ajax({
              type:'POST',
              url:"/common/active_toggler_ajax",
              data:parms,
              success: function( msg){

                if ( msg.length > 0){
                    json = JSON.parse ( msg );
                    if ( json.stat == 1 ){
                        $(rowID).removeClass("deactivated")
                        $(rowID).addClass("activated")
                    }else {
                        $(rowID).removeClass("activated")
                        $(rowID).addClass("deactivated")
                    }

                    me.html ( json.msg );
                }
                
              }
           });

        });

    }
}

$(document).ready( function(){

    reload_active_togglers();

    if ($('.sel_all').length > 0){

        $('.sel_all').click (function(){

            //stat = $(this).is(":checked");
            $('.sel_check').attr("checked", $(this).attr("checked"));

        });


    }

    $('.defaulter').click ( function(){
        if ( $(this).attr("id") == $(this).val() ){
            $(this).val("");
        }
    });

    $('.defaulter').blur ( function(){
        check_default_text ( $(this) );
    });

    $('.defaulter').each ( function(){
        check_default_text ( $(this) );
    });


    /*
     * Code to handle internal page links
     */
    if ( $('.dynamic_menu').length > 0){
        $('.dynamic_menu').find("a").each( function(){
           
            $(this).click ( function(){

                p = $(this).parent().parent().attr('id');
                href = $(this).attr('href');

                if (href[0] != '#') return true;
                else {
                    href = href.replace('#','');
                }

                //  event.preventDefault();
                $('.dynamic_menu').find("a").each( function(){
                    h = $(this).attr('href');

                    if (h[0] == '#'){
                        h = h.replace('#','');
                        
                        pp =  $(this).parent().parent().attr('id');
                        cl = $(this).parent().hasClass('selected');
                        //console.debug (h + ":" + href);
                        if (h != href && p == pp && cl){
                            $(this).parent().removeClass('selected');
                            $( "#" + h  ).slideUp();
                        }
                    }

                    
                    
                });


                $(this).parent().addClass('selected');
                
                $( "#" + href ).slideDown();


                return false;

            });
        });
    }


});

jQuery.fn.corners = function(options){
    $(this).addClass('ui-corner-all');
};




