$(function() {

    if ($('.keys .item').length > 0) {
        $('.keys .item .keys-preview a').click(function() {
            $('.keys .item .keys-content').css('display', 'none');
            $('.keys .item .keys-preview').css('display', 'block');
            $(this).parent().parent().css('display', 'none');
            $(this).parent().parent().parent().find('.keys-content').css('display', 'block');
        });
        $('.keys .item .keys-content a').click(function() {
            $('.keys .item .keys-content').css('display', 'none');
            $('.keys .item .keys-preview').css('display', 'block');
            //event.preventDefault();
        });
    }

    if ($('.player-tabs .item').length > 0) {
        $('.player-tabs .item').each(function(i) {
            $(this).click(function() {
                if (!$(this).hasClass('active')) {
                    $('.player-tabs .item').removeClass('active');
                    $(this).addClass('active');
                    $('.player-tabs-content .items').removeClass('active');
                    $($('.player-tabs-content .items')[i]).addClass('active');
                }
            });
        });
    }

    if ($('.player-tabs .bigitem').length > 0) {
        $('.player-tabs .bigitem').each(function(i) {
            $(this).click(function() {
                if (!$(this).hasClass('selected')) {
                    $('.player-tabs .bigitem').removeClass('selected');
                    $(this).addClass('selected');
                    $('.player-tabs-content .items').removeClass('active');
                    $($('.player-tabs-content .items')[i]).addClass('active');
                }
            });
        });
    }

    iehoverfix($('.button'));
    iehoverfix($('.regbutton'));

    iehoverfix($('.confrestrict'));
    iehoverfix($('.regclose'));
    iehoverfix($('.regsite'));
    iehoverfix($('.participant'));

    iehoverfix($('div.nav a'));
    iehoverfix($('ul.nav li'));

    iehoverfix($('.sch-tab .column'));


    var tabsObj = $('.tab');
    tabsObj.click(function() {
        if (this.className.indexOf('1') > 0) {
            var aTab, dTab, cntObj;
            if (this.className.indexOf('tab-l') > 0) {
                this.className = this.className.replace('1', '2');
                tabsObj[0].className = tabsObj[0].className.replace('2', '1');
                $('.tabs .content .posts').show();
                $('.tabs .content .twits').hide();
                if ($('.tabs .all a').get(0) != null)
                    $('.tabs .all a').get(0).href = "http://blogs.msdn.com/msplatforma/";
            }
            else {
                tabsObj[1].className = tabsObj[1].className.replace('2', '1');
                this.className = this.className.replace('1', '2');
                $('.tabs .content .posts').hide();
                $('.tabs .content .twits').show();
                if ($('.tabs .all a').get(0) != null)
                    $('.tabs .all a').get(0).href = "http://msplatforma.ru/twitter/";
            }
        }
    });
    if ($('div.nav').length) {
        $('.nav li').mouseover(function() {
            var re = /b[0-9]{1,}$/i;
            var picClass = re.exec(this.className);
            $('.nav .pics .pic').each(function() {
                this.style.left = "-10000px";
            });
            $('.nav .pics .pic.' + picClass).get(0).style.left = "0px";
        });
        var autoSwitcherId = autoSwitchPics();
        $('.nav').hover(function() {
            clearInterval(autoSwitcherId);
        }, function() {
            autoSwitcherId = autoSwitchPics();
        });
    }
    if ($('.barsection .greenbar .button').length) {
        $('.barsection .greenbar .button a').click(function() {
            var section = this.parentNode.parentNode.parentNode.parentNode;
            var button = $('.greenbar .button', section);
            button.toggleClass('minus');
            if (button.hasClass('minus')) {
                this.innerHTML = "Скрыть";
                $('.content', section).show();
            }
            else {
                this.innerHTML = "Показать";
                $('.content', section).hide();
            }
            return false;
        });
    }
    if ($('.lcol .more').length) {
        $('.lcol .more').click(function() {
            $(this).toggleClass('minus');
            if ($(this).hasClass('minus')) {
                this.innerHTML = "Скрыть";
                $(this).next().show();
            }
            else {
                this.innerHTML = "Подробности";
                $(this).next().hide();
            }
            return false;
        })
    }
    if ($('.rating .stars').length) {
        $('.rating .stars a').hover(function() {
            $(this).prevAll().andSelf().each(function() {
            $(this).addClass('full-hover');
            });
            $(this).nextAll().each(function() {
                $(this).addClass('empty-hover');
            });
        }, function() {
            $(this).prevAll().andSelf().each(function() {
                $(this).removeClass('full-hover');
            });
            $(this).nextAll().each(function() {
                $(this).removeClass('empty-hover');
            });
        });
    }
    if ($('.collapsed').length) {
        $('.collapsed .expander a').click(function() {
            $(this).hide();
            $('.content', this.parentNode.parentNode).show();
            return false;
        });
    }
    if ($('.comment-form').length) {
        $('.comment-form .expander a').click(function() {
            $('.content', this.parentNode.parentNode).show();
            return false;
        });
    }
})

autoSwitchPics = function() {
    return setInterval(function() {
        var picsObj = $('.nav .pics .pic');
        var actualPic;
        picsObj.each(function(i) {
            if (this.offsetLeft == 0)
                actualPic = i + 1;
            this.style.left = "-10000px";
        });
        if (actualPic == picsObj.length) actualPic = 0;
        picsObj[actualPic].style.left = "0px";
    }
	, 10000);
}

iehoverfix = function(els) {
    if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
        for (var i = 0; i < els.length; i++) {
          els[i].prefix = '';
          if ( $(els[i]).hasClass('report-selected') ) els[i].prefix = 'sel';
            els[i].onmouseover = function() {
                this.className += ' ' + this.prefix + 'hover';
            }
            els[i].onmouseout = function() {
                this.className = this.className.replace(' ' + this.prefix + 'hover', '');
            }
        }
    }
}

   $(function() {
   	var spt = $('span.mailme');
   	var at = / at /;
   	var dot = / dot /g;
   	var addr = $(spt).text().replace(at, "@").replace(dot, ".");
   	$(spt).after('<a href="mailto:' + addr + '" title="Отправить письмо">' + addr + '</a>')
.hover(function() { window.status = "Отправить письмо!"; }, function() { window.status = ""; });
   	$(spt).remove();
   });

//   var imph = '/styles/i/';
//   var aImages = new Array(
//	imph + 'nav-active.png',
//	imph + 'nav-hover.png',
//	imph + 'nav-lb-1.png',
//	imph + 'nav-lb-2.png',
//	imph + 'nav-lm-1.png',
//	imph + 'nav-lm-2.png',
//	imph + 'nav-lt-1.png',
//	imph + 'nav-lt-2.png',
//	imph + 'nav-normal.png',
//	imph + 'nav-rb-1.png',
//	imph + 'nav-rb-2.png',
//	imph + 'nav-rm-1.png',
//	imph + 'nav-rm-2.png',
//	imph + 'nav-rt-1.png',
//	imph + 'nav-rt-2.png',
//	imph + 'box/bg.png',
//	imph + 'buttons/btn-small-green-bg.png',
//	imph + 'buttons/btn-small-green-bg_hover.png',
//	imph + 'buttons/btn-small-green-left.png',
//	imph + 'buttons/btn-small-green-left_hover.png',
//	imph + 'buttons/btn-small-green-right.png',
//	imph + 'buttons/btn-small-green-right_hover.png',
//	imph + 'buttons/btn-small-orange-bg.png',
//	imph + 'buttons/btn-small-orange-left.png',
//	imph + 'buttons/btn-small-orange-right.png',
//	imph + 'buttons/button-green-bg.png',
//	imph + 'buttons/button-green-bg_hover.png',
//	imph + 'buttons/button-green-left.png',
//	imph + 'buttons/button-green-left_hover.png',
//	imph + 'buttons/button-green-right.png',
//	imph + 'buttons/button-green-right_hover.png',
//	imph + 'buttons/button-sky-bg.png',
//	imph + 'buttons/button-sky-bg_hover.png',
//	imph + 'buttons/button-sky-left.png',
//	imph + 'buttons/button-sky-left_hover.png',
//	imph + 'buttons/button-sky-right.png',
//	imph + 'buttons/button-sky-right_hover.png'
//);

   //PreloadImages(aImages);

   function PreloadImages(aImages) {
   	if (aImages != null) {
   		for (var i = 0; i < aImages.length; i++) {
   			var img = new Image();
   			img.src = aImages[i];
   		}
   	}
   }

   function sch_leg_show(show) {
   	var leg_l = $('.sch-legeng_l');
   	var leg_r = $('.sch-legeng_r');
   	var leg_info = $('.sch-legeng_info');
   	if (leg_l != null && leg_r != null && leg_info != null) {
   		leg_l.css('display', show);
   		leg_r.css('display', show);
   		show = show == 'none' ? 'block' : 'none';
   		leg_info.css('display', show);
   	}
   }