﻿RoS.Apps = {
  setUpMenus: function(bodySection, allowClick, allowDefault) {

    // setup menus
    $('#top-menu li.tab a[class!=omit]').each(function() {
      if (!allowClick) {
        $(this).css("cursor", "default");
      }
      if (allowClick) {
        $(this).click(function(e) {
          $(this).parents('ul').find('a').removeClass('active');
          $(this).addClass('active');
          $('div.sub').each(function() {
            $(this).hide();
          });
          $(bodySection).hide();
          var subMenuId = this.getAttribute('href');
          if (subMenuId.indexOf('#') > 0) {
            subMenuId = subMenuId.substring(subMenuId.indexOf('#'));
          }
          var selector = subMenuId + ' li';
          $(subMenuId).show();
          if (-1 == subMenuId.indexOf('-submenu')) {
            $('#sub-menu').animate({ height: '5px' });
          } else {
            $('#sub-menu').animate({ height: '29px' });
            $(selector).removeClass('selected')
                .filter(':first').addClass('selected')
                .contents('a').click();
          }
          if (!allowDefault) {
            e.preventDefault();
          }
        });
      } else {
        $(this).click(function(e) {
          if (!allowDefault) {
            e.preventDefault();
          }
        });
      }

      var subMenuId = this.getAttribute('href');
      if (subMenuId.indexOf('#') > 0) {
        subMenuId = subMenuId.substring(subMenuId.indexOf('#'));
      }
      var selector = subMenuId + ' li';
      var sectionId = subMenuId;
      if (-1 != subMenuId.indexOf('-submenu')) {
        selector += ' a';
        $(selector).each(function() {
          $(this).click(function(e) {
            $(this).parents('ul').find('li').removeClass('selected');
            $(this).parent().addClass('selected');
            $(selector).each(function() {
              var id = this.getAttribute('href');
              if (id.indexOf('#') > 0) {
                id = id.substring(id.indexOf('#'));
              }
              $(id).hide();
            });
            var id = this.getAttribute('href');
            if (id.indexOf('#') > 0) {
              id = id.substring(id.indexOf('#'));
            }
            $(id).show();
            if (!allowDefault) {
              e.preventDefault();
            }
          });
        });
      }
    });
  },

  setUpNextPreviousBtns: function(nextBtn, prevBtn) {


    $(nextBtn).click(function(e) {
      var $el = $('#top-menu li.tab a.active');
      if (-1 != $el.attr('href').indexOf('-section')) {
        $children = $el.parent().next().children();
        if (0 != $children.length) {
          $children.eq(0).click();
        }
      } else {
        var id = $el.attr('href');
        if (id.indexOf('#') > 0) {
          id = id.substring(id.indexOf('#'));
        }
        var $sub = $(id + ' li.selected a');
        $children = $sub.parent().next().children();
        if (0 != $children.length) {
          $children.eq(0).click();
        } else {
          $children = $el.parent().next().children();
          if (0 != $children.length) {
            $children.eq(0).click();
          }
        }
      }

      e.preventDefault();
    });



    $(prevBtn).click(function(e) {
      var id = '';
      var $el = $('#top-menu li.tab a.active');
      if (-1 != $el.attr('href').indexOf('-section')) {
        $children = $el.parent().prev().children();
        if (0 != $children.length) {
          var $child = $children.eq(0);
          $child.click();
          if (-1 == $child.attr('href').indexOf('-section')) {
            id = $child.attr('href');
            if (id.indexOf('#') > 0) {
              id = id.substring(id.indexOf('#'));
            }
            $(id + ' li:last a').click();
          }
        }
      } else {
        id = $el.attr('href');
        if (id.indexOf('#') > 0) {
          id = id.substring(id.indexOf('#'));
        }
        var $sub = $(id + ' li.selected a');
        $children = $sub.parent().prev().children();
        if (0 != $children.length) {
          $children.eq(0).click();
        } else {
          $children = $el.parent().prev().children();
          if (0 != $children.length) {
            var $child = $children.eq(0);
            $child.click();
            if (-1 == $child.attr('href').indexOf('-section')) {
              id = $child.attr('href');
              if (id.indexOf('#') > 0) {
                id = id.substring(id.indexOf('#'));
              }
              $(id + ' li:last a').click();
            }
          }
        }
      }

      e.preventDefault();
    });

  },

  setUpTableFilter: function(table, textFilter, clearBtn, sortListing, columns, pager) {
    var $tbl = $(table).tablesorter(
        { debug: false, sortList: sortListing, widgets: ['zebra'] }
    ).tablesorterFilter(
      {
        filterContainer: $(textFilter),
        filterClearContainer: $(clearBtn),
        filterColumns: columns,
        filterCaseSensitive: false
      }
    );

    if ($(pager).length > 0) {
      $tbl.tablesorterPager({ container: $(pager), positionFixed: false });
    }
  },

  setUpCriteriaMenus: function(bodySection) {
    // setup menus
    $('.criteria-menu li.tab a').each(function() {
      $(this).click(function(e) {
        $(this).parents('ul').find('a').removeClass('active');
        $(this).addClass('active');
        $('div.sub').each(function() {
          $(this).hide();
        });

        var $criteriaMenu = $(this).parents('div.criteria-menu');
        var $fieldsets = $criteriaMenu.nextAll('div.criteria-main').children('fieldset');
        $fieldsets.hide();

        $criteriaMenu.next().animate({ height: '5px' });
        var $children = $criteriaMenu.children('ul').children();
        var index = $children.index($(this).parent());
        $fieldsets.eq(index).show();

        e.preventDefault();
      });
    });
  },

  showDialog: function(closeBtnIds, width) {
    if (!width) width = "500px";
    RoS.Apps.prepareDialog(null, "#confirm", closeBtnIds, width);
  },

  prepareDialog: function(openDialogBtnId, dialogDivId, dialogCloseBtnIds, dialogWidth) {


    if (openDialogBtnId) {
      $(openDialogBtnId).click(function(e) {
        e.preventDefault();

        var headerWidth = (dialogWidth.replace("px", "") - 13) + 'px';
        $('#confirm > div.dialog-header').css('width', headerWidth);

        $('#main').block({
          message: $(dialogDivId),
          css: {
            border: 'none',
            cursor: 'default',
            'text-align': 'left',
            top: '150px',
            width: dialogWidth
          }
        });

        $.each($.makeArray(dialogCloseBtnIds), function() {
          var $dialogCloseBtn = $(this + '');
          $dialogCloseBtn.click(function() {
            $('#main').unblock();
          });
        });
      });
    } else {
    
      var headerWidth = (dialogWidth.replace("px", "") - 13) + 'px';
      $('#confirm > div.dialog-header').css('width', headerWidth);

      $('#main').block({
        message: $(dialogDivId),
        css: {
          border: 'none',
          cursor: 'default',
          'text-align': 'left',
          top: '150px',
          width: dialogWidth
        }
      });

      $.each($.makeArray(dialogCloseBtnIds), function() {
        var $dialogCloseBtn = $(this + '');
        $dialogCloseBtn.click(function() {
          $('#main').unblock();
        });
      });
    }
  }
};

RoS.Apps.Tooltips = {
  addTooltip: function(el) {
    var id = el.attr("href").substring(1);
    $.get("/tooltipservice.axd", { id: id }, function(tooltip) {
      el.append($('<span class="viewtip"> <p>' + tooltip + '</p></span>').fadeIn(500));
    });
  },

  removeTooltip: function(el) {
    el.find("span.viewtip").fadeOut(400);
  }
};

$(document).ready(function() {
  $('#lnkChangePassword').click(function(e) {
    e.preventDefault();
    $('#main').block({
      message: $('#changepassword'),
      css: {
        border: 'none',
        cursor: 'default',
        'text-align': 'left',
        top: '150px',
        width: '410px'
      }
    });
    $('#changepassword-close, #btnChangePasswordCancel').click(function() {
      $('#main').unblock();
    });
  });

  // sets up tooltips
  $("a.jTip").hover(function() {
    var $el = $(this);
    RoS.Apps.Tooltips.addTooltip($el);
  }, function() {
    var $el = $(this);
    RoS.Apps.Tooltips.removeTooltip($el);
  });
});
