$(document).bind("mainmenu-created", function(){
    $('#mainmenu a').each(function(i){$(this).prepend('<u></u><b></b>').addClass('a'+i);});
    //подсвечивание в главном меню // |qwe\.aspx
    if (window.location.pathname.match(/(Main\.aspx|Default\.aspx|News\.aspx)/)) {
        $('#mainmenu a.a0').addClass('selected');
    } else if (window.location.pathname.match(/(ViewDisciplineInfo\.aspx)/)) {
        $('#mainmenu a.a1').addClass('selected');
    } else if (window.location.pathname.match(/(Testing\.aspx|TestingSelect\.aspx|TestingWelcome\.aspx|TestingResult\.aspx|ResultBlanket\.aspx|ResultRecommendation\.aspx)/)) {
        $('#mainmenu a.a2').addClass('selected');
    } else if (window.location.pathname.match(/(Information\.aspx|TestType\.aspx|ViewInformationCourses\.aspx|ViewInformationTasks\.aspx|OcinkaZavdan\.aspx|ViewInformationTests\.aspx|Poslugy\.aspx|ViewInformationZNO\.aspx|KorInfo\.aspx|ViewAdvicesUEPrepare\.aspx|ViewAdvicesUEConducting\.aspx|ViewAdvicesParent\.aspx|ViewRegAndPayment\.aspx|ViewTestingProcess\.aspx)/)) {
        $('#mainmenu a.a3').addClass('selected');
    } else if (window.location.pathname.match(/(ViewCourse\.aspx|ViewCourseTest\.aspx|ViewTest\.aspx|ViewCourseDetailed\.aspx)/)) {
        $('#mainmenu a.a4').addClass('selected');
    } else if (window.location.pathname.match(/(CoursePayment\.aspx|PaymentInfo\.aspx|ClientPayments\.aspx)/)) {
        $('#mainmenu a.a5').addClass('selected');
    } else if (window.location.pathname.match(/(PersonProfile\.aspx)/)) {
        $('.profile-pencil a.a9').addClass('selected');
    }
    if(window.location.pathname == '/')
        $('#mainmenu a.a0').addClass('selected');
    //ie6 has problems with qwe.class1.class2 so we have to merge it
    var vFix = $('#mainmenu a.selected,.profile-pencil a.selected').attr("class");
    if(typeof vFix == 'string' && vFix.match(/a[0-9] selected/)) $('#mainmenu a.selected,.profile-pencil a.selected').addClass(vFix.replace(' ','_'));
});
$(function(){
    $('#menu li:first-child').css({border: '0px'});
    myResize();
    $('#main').resize(function(){myResize();});
    $(window).resize(function(){myResize();});
});

$(window).load(function(){ myResize();});
$(window).ajaxComplete(function(){ myResize();});

function myResize(){
    var dbh = $(document).height(); // document.body.clientHeight;
    if(Math.abs($('div.bg-img').height() - dbh) > 1) {
      $('div.bg-img,#main').height("auto");
      $('div.bg-img').height(dbh);
      $('#main').height(dbh-95);
    }
    $('#footer').css('bottom', '0');//ie6 forgets...
    return false;
}

//dukebg; use on '.znotable td'; val = (true|false)
$.fn.setZnoMark = function (val) {
    this.queue(function () {
        //if ($(this).filter('.znotable td').length == 0) alert('debug: incorrect usage of setZnoMark');
        var par_tr = $(this).parent('tr');
        var columns = par_tr.children('td');
        var rows = par_tr.parent().children('tr');
        var col_ndx = columns.index($(this));
        var row_ndx = rows.index(par_tr);
        //alert(col_ndx + ' ' + col_count + ' ' + row_ndx + ' ' + row_count);
        if (col_ndx == columns.length - 1 && row_ndx == rows.length - 1) {
            if (val) {
                $(this).removeClass("lstb_nobg").addClass("lstb");
            } else {
                $(this).removeClass("lstb").addClass("lstb_nobg");
            }
        } else if (col_ndx == 0 && row_ndx == rows.length - 1) {
            if (val) {
                $(this).removeClass("fstb_nobg").addClass("fstb");
            } else {
                $(this).removeClass("fstb").addClass("fstb_nobg");
            }
        } else {
            if (val) {
                $(this).addClass("bg");
            } else {
                $(this).removeClass("bg");
            }
        }
        if (val) {
            $(this).html('<img src="/Images/index/answer.gif" alt="" />');
        } else {
            $(this).html('&nbsp;');
        }
        $(this).dequeue();
    });
    return this;
};

var InvadeRadioButtons, InvadeTableExtremeBorders, InvadeHeadedTableNoWhiteNeck;

function TabsOnShow(event, ui) {
    $prev = $(ui.tab).parents('.tabs:first').find('.ui-tabs-nav li:has(.tlc)');
    $prev.children().replaceWith($prev.find('a'));
    $(ui.tab).wrap('<div class="tlc"><div class="trc"><div class="blc"><div class="brc"></div></div></div></div>');
    myResize();
}

$(function(){
        $('#pages .sel').wrap('<span class="tlc"><span class="trc"></span></span>');

        $('.tabs').each(function(){
            var tabsOptions = {
                show: TabsOnShow
            }
            if($(this).hasClass('tab-cookie') && this.id != "")
                tabsOptions.cookie = { expires: 300, name: "tabs-" + this.id };
            $(this).tabs(tabsOptions);
        });
        
        InvadeTableExtremeBorders = function() {
          $('table.silver, table.blue').each(function(){
                  $(this).find('tr:eq(0)').children('td').css({borderTop: '0px'});
                  $(this).find('tr').each(function(){
                          $(this).children('td:last-child').css({borderRight: '0px', borderBottom: '0px'});
                  });//.find('td.btns').width(29);
          });
          $('table.reestr,table.tests,table.tests-sett,table.anwers_table,table.pay_inside')
              .find('tr:eq(0)').children('td').css({borderTop: '0px'});
          $('table.tests, table.tests-sett, table.anwers_table, table.big-t,table.pay_inside').each(function(i){
                  $(this).find('tr').children('td:last-child').css({borderRight: '0px'});
          });

          $('table.option_cost').children('tbody').andSelf().children('tr').each(function(){
              $(this).children('td:first-child').css({width: '33%'});
          });
          $('table.silver').children('tbody').andSelf().children('tr').each(function(){
              $(this).children('td:eq(1)').css({width: '25%'});
          });
          
        };
        InvadeTableExtremeBorders();
        
        InvadeHeadedTableNoWhiteNeck = function() {
            $('table.anwers_table:has(td.top), table.tests-sett:has(td.top), table.big-t').each(function(i){
                    $(this).find('tr:eq(1)').children('td').css({borderTop: '0px'});
            });
        };
        InvadeHeadedTableNoWhiteNeck();
        
        $('table.znotable').each(function(i){
                if($(this).find('tr td.corner').length > 0) {
                    $(this).find('tr').each(function(i){
                            $(this).children('td:first-child').addClass('bg');
                    });
                    $(this).find('tr:last-child').children('td:first-child').addClass('fstb');
                } else {
                    $(this).find('tr:last-child').children('td:first-child').addClass('fstb_nobg');
                }
                $(this).find('tr:eq(0)').children('td').addClass('bg');
                $(this).find('tr:eq(0)').children('td:first-child').addClass('fst');
                $(this).find('tr:eq(0)').children('td:last-child').addClass('lst');
                $(this).find('tr:last-child').children('td:last-child').addClass('lstb_nobg').end().children('td').css('border-bottom','3px solid white');
                $(this).find('tr').children('td:first-child').css('border-left','3px solid white');
        });

        $('span.toggler,span.toggler_dead').each(function(i){
            if(i > 0) { $(this).parent('td').addClass('arr'); }
        });
        
        $('#content table.eng_t tr:first td').css('text-align', 'center');
        
        // input radio
        InvadeRadioButtons = function() {
          $('input:radio').each(function(){
                  var ra = $(this);
                  // don't invade: if (alreadyInvaded || notVisible) continue;
                  if (ra.prev('div.radio').length > 0 || ra.css('display') == 'none') return true;
                  ra.css({display: 'none'}); //ra.css({visibility: 'hidden'});
                  $('<div class="radio '+ GetRadioName(ra) +'"></div>').insertBefore(ra)
                  .each(function() {
                      var $self = $(this);
                      if(ra.attr('checked'))
                          $self.css({backgroundPosition: '0px -12px'});
                  });
          });
        };
        InvadeRadioButtons();
        $('div.radio').live('click', function(e){
                var nr = $(e.target).next('input:radio');
                if(!nr.attr('disabled'))
                    nr.attr({checked: 'checked'}).click();
        });
        $('input:radio').live('click', function(e) {
                if((typeof (is_znotable) != 'undefined') && is_znotable) return;
                var cl = GetRadioName($(e.target));
                $('div.'+cl).css({backgroundPosition: '0px 0px'});
                $(e.target).prev('div.radio').css({backgroundPosition: '0px -12px'});
                //trouble is because it's display:none some browsers won't check the input
                $(e.target).attr({checked: 'checked'}); 
                //trouble then continues with validators so we revalidate relevant ones
                if(typeof Page_Validators != 'undefined') {
                  $($.grep(Page_Validators, function(val){ return val.controltovalidate == cl; }))
                  .each(function(i){ ValidatorValidate(this); });
                }
        });
        // END input radio
        
        // дисэйбленные линки (напр. прохождение: назад-вперед)
        $('a[disabled]').css({color: '#666', textDecoration: 'none', cursor: 'default'}).click(function(){return false;});
        
        // исправление проблем IE6-7 для positioned. ie8 имея documentMode которое в 7 или старые ие не имеющие documentMode 
        // jQuery.browser.version не подходит, т.к. значение в нем берет истоки из browser mode, а не layout.
        if (jQuery.browser.msie 
            && ((document.documentMode && document.documentMode == 7) || !document.documentMode)
            // т.к. радио теперь обычное, то делаем грязное дело только на резльтатах и QT (при наличии ВО) ТЕПЕРЬ И ОПЛАТА
            && ($('.wrap-av-mark,#big-result,#ClientBasket').length > 0)) {
          globIePostionJumpers = $('.wrap-av-mark,.keout,#header,#adv-top .adv-test,#footer');
          $().ajaxStop(function(){
            globIePostionJumpers = $('.wrap-av-mark,.keout,#header,#adv-top .adv-test,#footer');
          });
          globIePostionFix = setInterval(function(){
            globIePostionJumpers.each(function(){
                this.className += ""; //touch
            });
          }, 300);
        };

        $(document).trigger("designComplete");
});

function GetRadioName(wrappedInput) {
   return wrappedInput.attr('name').replace(/\$/g, '_')
}

jQuery.expr.filters.hiddenx = function(elem){return "hidden" === elem.type};