var ajax_loader = "<div id='ajax_loader' class='floater'><img src='/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);
    } 
}

$(document).ready( function(){

    if ( $('.delete_selected').length > 0){

        $('.delete_selected').click (function(){

            model = $(this).attr('id');
            //stat = $(this).is(":checked");
            list = "";
            c = 0;
            $('.sel_check').each ( function(){

                if ( $(this).is(":checked")){
                    if ( list.length > 0 ) list += ",";
                    list += $(this).val();
                    c++;
                }

            })

            if ( !confirm('Are you sure you want to remove the ' + c + ' selected items')  ) return;

            parms = "modelName=" + model + "&ids=" + list;

            $.ajax({
                type: "POST",
                url: "/common/multi_delete",
                data: parms,
                success: function(msg){

                    listArr = list.split(",");
                    for ( i = 0;i < listArr.length;i++){
                        $('#sortOrder_'+listArr[i]).fadeOut("normal",function(){
                            $(this).remove();
                        });
                    }

                }
            });

        });

    }


    if ($('.sel_all').length > 0){

        $('.sel_all').click (function(){

            //stat = $(this).is(":checked");
            $('.sel_check').attr("checked", $(this).attr("checked"));

        });


    }

    if ($('.datepicker').length > 0){

        $('.datepicker').each ( function(){

            vArr = $(this).val().split("/");
            d = vArr[2] + "-" + vArr[0] + "-" + vArr[1];
            $(this).datepicker( {
                defaultDate:d,
                changeYear:true
            });

        });
    }

    if ( $('.sortable').length > 0){

        $('.sortable').each( function(){

            modelName = $(this).attr("id");

            $(this).sortable({
                handle: '.sort_handle',
                update : function () {
                    parms = "modelName=" + modelName + "&" + $('#'+modelName).sortable('serialize');

                    $.ajax({
                        type: "POST",
                        url: "/common/sorter",
                        data: parms,
                        success: function(msg){

                        }
                    });

                }
            });
        });

    }

    if ( $('.tabs').length > 0){

        navSel = $('.tabs').attr("id");

        $(".tabs").tabs({
            selected: navSel,
            fx: {
                opacity: 'toggle' ,
                duration:"fast"
            },
            select: function(event, ui) {
                href = ui.tab.href;

                d = href.split("##")

                if ( d.length > 1){
                    url = "/" + d[1];
                    location.href = url;
                    return false;
                }

                return true;
            },
            ajaxOptions: {
                async: false
            }
        });

        $('.tabs ul li a').click(function () {
            //location.hash = $(this).attr('href');
            });

    }

    if ( $('.tabs2').length > 0){

        navSel = $('.tabs2').attr("id");

        $(".tabs2").tabs({
            selected: navSel,
            fx: {
                opacity: 'toggle' ,
                duration:"fast"
            },
            select: function(event, ui) {
                href = ui.tab.href;

                d = href.split("##")

                if ( d.length > 1){
                    url = "/" + d[1];
                    location.href = url;
                    return false;
                }

                return true;
            },
            ajaxOptions: {
                async: false
            }
        });

        $('.tabs2 ul li a').click(function () {
            //location.hash = $(this).attr('href');
            });

    }

    $('.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');
};



