﻿//var $ = jQuery.noConflict();
/*########################################################################################
JQuery Plugin to monitor changes to a text field
########################################################################################*/
(function($){
    $.fn.Monitor = function(options){
        var o = $.extend({},$.fn.Monitor.defaults,options);
        return $(this).each(function(){
            $this = $(this);
            var orgBackground = $this.css('background-color');
            var orgText = $this.val();
            $(this).blur(function(){
                if($(this).val() != orgText){
                    $(this).addClass('modified').css('background-color',o.color);
                }else{
                    $(this).removeClass('modified').css('background-color',orgBackground);
                }
            });
        });
    };
    $.fn.Monitor.defaults = {color:'red'};
})(jQuery);

/*########################################################################################
JQuery Plugin to check all the checkboxes 
########################################################################################*/
(function($){
    $.fn.CheckAll = function(options){
        var o = $.extend({},$.fn.CheckAll.defaults,options);
        return $(this).each(function(){
            $this = $(this);
            var sel = '';
            if(o.selector == ''){
                sel = ':input:not(.check-all):checkbox';
            }else{
                sel = '.' + o.selector;
            }
            var total = $(sel).length;
            $(this).click(function(){
                var par = $(this).parent();                
                
                if($(this).is(':checked')){
                    $(sel,par).attr('checked',true);
                }else{
                    $(sel,par).attr('checked',false);
                }
            });
            
            $(sel).click(function(){
                var checked = $(sel + ':checked').length;                
                if(checked == total){
                    $this.attr('checked',true);
                }else{
                    $this.attr('checked',false);
                }
            });
        });
    };
    $.fn.CheckAll.defaults = {selector:''}
})(jQuery);

/*_________________________________________________________________*/

/*########################################################################################
JQuery Plugin to standardize editing grids
########################################################################################*/
(function($){
    $.fn.GridEdit = function(options){
        var o = $.extend({},$.fn.GridEdit.defaults,options);
        
        return $(this).each(function(){
            $this = $(this);
            //Hide new row
            if(gridHideNew){
                $('.' + o.newRow,this).hide();
            }
            
            //Hide edit row
            $('.' + o.editRow,this).hide();
            
            //Assign add event
            $('.' + o.add,$this).click(function(){
                $('.' + o.newRow,$this).fadeIn('slow')
            });
            
            //Assign edit event
            $('.' + o.edit,$this).click(function(){
                $(this).parents('tr:first').hide().next('.' + o.editRow).show();
            });
            
            //Assign close event
            $('.' + o.close,$this).click(function(){                
                $(this).parents('tr.' + o.editRow).hide().prev('.' + o.recordRow).show();
                //$(this).parents('tr:first').hide().prev('.' + o.recordRow).show();
            });
            
            //Assign cancel event
            $('.' + o.cancel,$this).click(function(){
                $(this).parents('tr:first').fadeOut();
            });
        });
    };
    $.fn.GridEdit.defaults = {
         add:'button-add'
        ,edit:'button-edit'
        ,close:'button-close'
        ,cancel:'button-cancel'
        ,recordRow:'record'
        ,editRow:'edit'
        ,newRow:'new'
    };
})(jQuery)

/*_________________________________________________________________*/

$(document).ready(function(){
    $('.monitor').Monitor({color:'#C79810'});
    
    $('.check-all').CheckAll({'selector':'chkThis input:checkbox'});
    
    //fadein Testimonial
    $('.fadein').fadeIn('slow');
    
    $('.GridEdit').GridEdit();
});



function checkModification(elem){
    if($(elem).parent('td').parent().find('.modified').length == 0){
        alert('There are no modifications to save.');
        return false
    }else{
        return true;
    }
}
function askUser(msg){
    var ask = confirm(msg);
    return ask;
}
