/*
 * js-шаблонизатор
 * howto: http://sreznikov.blogspot.com/2010/01/supplant.html
 */
String.prototype.supplant = function(o) {
	return this.replace(/{([^{}]*)}/g,
		function(a, b) {
			var r = o[b];
			return typeof r === 'string' || typeof r === 'number' ? r : a;
		}
	);
};

isInteger = function( s ) {
  return !isNaN( parseInt( s ) );
}

remHeight = function (){
  $('.block-st1').removeClass('h460');
}

function set_txtblk_hei(){
    var hei_1 = $('.ctgsb').height();
    var hei_2 = $('.gdbl').height();
    $('.block-st2').css('min-height', hei_1 + hei_2 - 45 + 'px' );
}

$(function() {
  set_txtblk_hei();
	$('.navigation li.current + li').css("background", "none");
	$(".wrapp-bl-category-bl").each(function() {
			$(".wrapp-bl-category-bl").css("padding-top", function() {
				return (76 - $(this).height()) / 2 + "px";
			});
		});
});


function openlivechat() {
	  leftVal = (screen.width -500) / 2;
	  topVal = (screen.height-480) / 2;
	  window.open('http://messenger.providesupport.com/messenger/elfmoney.html', '_blank', 'menubar=0,location=0,scrollbars=auto,resizable=1,status=0,width=500,height=480,left='+leftVal+',top='+topVal);
}

function server_chng(item){
    var id = item.attr('id').split('-')[1];
    var link = $('.server-form-'+id).attr('action');
    $('.item-link-'+id).attr('href',link+ '#'+ item.val() )
}

$(document).ready(function() {
    var consult_block_height = $('.userbar-consult').height(),
        consult_block_height_diff = 4

    $("div.levelup-item-open-link a").click(function() {
        var curLink = $(this);
        curLink.parent().parent().parent().parent().toggleClass("levelup-item-open");
        return false;
    });

    $(".consult-open").click (function() {
        openlivechat();
    });

    $('.wrapp-bl-category-sub select').each(function(){
        server_chng( $(this) );
    });

    $('.wrapp-bl-category-sub select').bind("change", function(){
        server_chng( $(this) );
        return false;
    });

    $('div.top-login-form form p input').each(function() {
        var curInput = $(this);
        if (curInput.val() != '') {
            curInput.parent().find('span').hide();
        }
    });

    $('div.top-login-form form p input').focus(function() {
        var curInput = $(this);
        curInput.parent().find('span').hide();
    });

    $('div.top-login-form form p input').blur(function() {
        var curInput = $(this);
        if (curInput.val() == '') {
            curInput.parent().find('span').show();
        }
    });
    
    $('.navigation-user a').click(function() {
        $(this).parent().animate({
            opacity: '0',
            right: '-=' + 150
        }, {
            duration: 300,
            complete: function(){
                $(this).hide()
                $('.top-login-form').show().animate({
                    opacity: '1',
                    right: '+=' + 100
                }, { duration: 300 });
            }
        });
        return false;
    });

    // включаем/выключаем кнопку закрытия панельки
    // меняем высоту блока поддержки
    $(window).scroll(function(e) {
        var yScroll;
        if (self.pageYOffset) {
            yScroll = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {
            yScroll = document.documentElement.scrollTop;
        } else if (document.body) {
            yScroll = document.body.scrollTop;
        }
        
        if (yScroll > 0) {
            $('.userbar-consult').height(consult_block_height - consult_block_height_diff)
        } else {
            $('.userbar-consult').height(consult_block_height)
        }

    });

    $('.userbar-news').mouseover(function() {
        $('.userbar-news').addClass('userbar-news-open');
        return false;
    });
    
    $('.userbar-news').mouseout(function() {
        $('.userbar-news').removeClass('userbar-news-open');
        return false;
    });

	$('.buyback-tbl .subscribe-all, .buyback-tbl .subscribe-none').click(function() {
        $('.buyback-tbl .subscribe-all, .buyback-tbl .subscribe-none').toggle()
        $('input[type="checkbox"]').attr('checked', $(this).hasClass('subscribe-all') && true || false);
        return false;
    })
});

