// Last published: 24 Mar 2011 17:19



  
(function($) {
    $.fn.gpExpando = function(options, callback) {
        
            // default options - these are used when no others are specified
            $.fn.gpExpando.defaults = {
                slideToggleSpeed: 400,
                closedAncClass: "expand",
                openAncClass: "contract",
                closedAncTitle: "Show contents",
                openAncTitle: "Hide contents"
            };
        
        // build main options before element iteration
            var o = $.extend({}, $.fn.gpExpando.defaults, options);
             this.each(function() {
                var thisExpando = $(this);
                $(thisExpando).wrapInner('<div></div>');
        
                var firstHeading = $('div:first h2:first, div:first h3:first, div:first h4:first', thisExpando).get(0); //Get the first heading from the selected and assign it to a variable
                $(firstHeading)
                    .wrapInner('<a title="'+o.closedAncTitle+'" class="gpExpandoAnc '+o.closedAncClass+'"></a>')
                    .insertBefore($('div:first', thisExpando))
                    .addClass('gpExpandoHeading');
                $('div:first', thisExpando).hide();
            
                $('.gpExpandoAnc', thisExpando).click(function() {
                    var thisAnc = $(this);
                    if  ($(this).parent().next('div').is(":hidden")) {
                            $(thisAnc).attr('title', o.openAncTitle);
                            $(thisAnc).removeClass(o.closedAncClass);
                            $(thisAnc).addClass(o.openAncClass);
                        } else {
                            $(thisAnc).attr('title', o.closedAncTitle);
                            $(thisAnc).removeClass(o.openAncClass);
                            $(thisAnc).addClass(o.closedAncClass);
                        }
                    $(this).parent().next('div').slideToggle(o.slideToggleSpeed);
                });
            
            });
                 
            return this;
        };
        //  invoke the function we just created passing it the jQuery object
    })(jQuery); 

(function($) {
    $.fn.gpFormFieldHider = function(options, callback) {
        
            // default options - these are used when no others are specified
            $.fn.gpFormFieldHider.defaults = {
                inputParentClass: ".inputParent",
                hiddenId: "opt_fields_",
                actionType: 'slide'
            };
        
            // build main options before element iteration
            var o = $.extend({}, $.fn.gpFormFieldHider.defaults, options);
            this.each(function() {
                var thisHidden = $(this);
                var height = $(thisHidden).height();
                var optFieldRegEx = new RegExp('\\b'+o.hiddenId+'(\\w+)\\b'); 
                var thisInput = $(this).attr('id').match(optFieldRegEx)[1]; 
                var inputName = $('#'+thisInput).attr('name'); 
                
                function performAction(first) {
                    if ($('#'+thisInput).is(':checked') ) {
                        if (o.actionType == 'fade') {
                            $(thisHidden).fadeIn();
                        } else if (o.actionType == 'disable'){
                            $(thisHidden).find('input, textarea').each(function() {
                                $(this).removeAttr("disabled");                            
                            });
                        } else if (o.actionType == 'slide'){
                            $(thisHidden).show().animate({ height : height }, { duration: 200 });
                        }    
                    } else {                    
                        if (o.actionType == 'fade') {
                            if (first == 'yes') {
                                $(thisHidden).hide();
                            } else {
                                $(thisHidden).fadeOut();
                            }
                        } else if (o.actionType == 'disable'){
                            $(thisHidden).find('input, textarea').each(function() {
                                $(this).attr("disabled", "disabled");
                            });
                        } else if (o.actionType == 'slide'){
                            if (first == 'yes') {
                                $(thisHidden).animate({ height: 0 }, { duration: 1, complete: function () { 
                                    $(thisHidden).hide();
                                } 
                                });
                            } else {
                                $(thisHidden).animate({ height: 0 }, { duration: 200, complete: function () {
                                    $(thisHidden).hide();
                                } 
                                });
                            }
                        }
                    }
                };
                
                performAction('yes'); 
                $('input[type=radio][name='+inputName+'], #'+thisInput).click(function(){ 
                    performAction();
                });
            });
            return this;
        };
        
    })(jQuery); 

 
$.fn.reorder = function() {
 
  function randOrd() { return(Math.round(Math.random())-0.5); }
 
  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length; 
    if (childCount > 1) {
      $children.remove(); 
      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) { $this.append($children.eq(k)); }); 
    }
  }));
}    
        
$(document).ready(function() {

    
    $('div.randomWithin').reorder();

    
    $('table:not(.nozebra).simpledatatable tr:odd td').addClass('odd');
    $('table:not(.nozebra).simpledatatable tr:even td').addClass('even');
    
    $('table:not(.nozebra).simpledatatable tr td.even.nozebra').removeClass('even');
    $('table:not(.nozebra).simpledatatable tr td.odd.nozebra').removeClass('odd');
    
    $(".browser_back").before('<div><a href="#" onclick="history.back(); return false;">Previous page</a></div>').remove();
    
    $( '.splitlist2col ol, .splitlist2col ul').each(function() {
          var classes = $(this).attr('class');
          if($(this).is("ol")) { var ordered = true; }
          var colsize = Math.round($(this).find("li").size()/2);
          $(this).find("li").each(function(i) {
               if (i>=colsize) {
                    $(this).addClass('right_col');                    
               }
            });
          if(ordered) {
               $(this).find('.right_col').insertAfter(this).wrapAll('<ol start="' + (colsize+1) + '" class="' + classes + '"></ol>');                
          } else {
                $(this).find('.right_col').insertAfter(this).wrapAll('<ul class="' + classes + '"></ul>');            
            }                
     });
    $('.splitlist2col ul .right_col, .splitlist2col ol .right_col').parent().after('<div class="clear"> </div>');
    $('.splitlist2col ul, .splitlist2col ol').wrap('<div style="float: left; width: 49.9%;"></div>'); 
    $('.splitlist2col ol li').css('margin-left', '0.5em');
    
    
    $('input.fck_js').removeAttr("disabled");
    
    $('.fck_disabled_message').remove();

    
    $('div[class*=colminheight]').each(function(index) { 
        var cmheight = $(this).attr('class').match(/\bcolminheight(\d+)\b/)[1];             
    
        $(this).css({
                'min-height' : cmheight+'px'
                })
                .height (cmheight+'px'); 
    });

    $('body').addClass('js'); 
    $('.showhide_small').gpExpando();
    $('.showhide_large').gpExpando( {  
       closedAncClass: "expand_large",
       openAncClass: "contract_large"
    });   
    $('div[id^=opt_fields_]').gpFormFieldHider(); //Grab divs that start with opt_fields_
    $('div[id^=disabled_opt_fields_]').gpFormFieldHider({actionType: 'disable', hiddenId:'disabled_opt_fields_'});
    $('div[id^=fade_opt_fields_]').gpFormFieldHider({actionType: 'fade', hiddenId:'fade_opt_fields_'});
});

