$(document).ready(function(){
    /*print*/
    $('#print').click(function(){
        window.print();
    });

    /*phones list*/
    $('#top-phones > .main-phone a').click(function(){
        $('#phones-list-wrapper').show();
        t = setTimeout("$('#phones-list-wrapper').hide();", 10000);
        return false;
    });

    $('#phones-list .main-phone a').click(function(){
        $('#phones-list-wrapper').hide();
        clearTimeout(t);
        return false;
    });

    /*cources list*/
    $('#langs-categories a.more, #langs-categories a.closed-more').click(function(){
        $(this).siblings('.langs-list-wrapper').show();
        return false;
    });

    $('.langs-list a.more, .langs-list .opened-more').click(function(){
        $(this).closest('.langs-list-wrapper').hide();
        return false;
    });

    /*slider 1*/
    var pages = [];
    $("#slider-paginator a").each(function(){
        pages.push($(this));
    });
    
    $('#main-promo-wrapper').jCarouselLite({
        auto: 5000,
        speed: 0,
        visible: 1,
        btnGo: pages,
        afterEnd: function(a) {
            pos = $(a).attr('rel');
            $('#slider-paginator a').removeClass('select');
            $('#slider-paginator a[rel='+pos+']').addClass('select');
        }
    });

    /*slider 2*/
    $('#scroller-wrapper>div').jCarouselLite({
        auto: 5000,
        speed: 1000,
        visible: 3,
        btnNext: '#next',
        btnPrev: '#prev'
    });

    
    /*rating*/
    getRating();
    
    $('#raiting .raiting-minus').live('click', function(e){
        var obj      = this,
            slider   = $('#raiting-slider'),
            curValue = slider.slider('value'),
            min      = slider.slider('option', 'min');

        if( curValue > min )
            slider.slider('value', curValue - 1);

        return false;
    });

    $('#raiting .raiting-plus').live('click', function(e){
        var obj      = this,
            slider   = $('#raiting-slider'),
            curValue = slider.slider('value'),
            max      = slider.slider('option', 'max');

        if( curValue < max )
            slider.slider('value', curValue + 1);

        return false;
    });

    /*left and main page exam slider*/
    setInterval(function(){
        $('#left-banners .left-banner, #faq-head .left-banner').toggleClass('active');
    }, 4000);

    /*show/hide hide form layers*/
    $('.shadow').click(function(){
        $(this).parent().hide();
    });

    /*auth layer*/
    $('#auth-link').click(function(){
        $('#auth-layer').show();
        return false;
    });
    
    $('#close-auth-layer').click(function(){
        $('#auth-layer').hide();
        return false;
    });
    
    /*feedback layer*/
    $('.feedback-link, #footer-contacts').click(function(){
        $('#feedback-layer').show();
        return false;
    });

    $(' #close-feedback-layer').click(function(){
        $('#feedback-layer').hide();
        return false;
    });

    /*question layer*/
    $('#ask-question, #ask-questuion a').click(function(){
        $('#question-layer').show();
        return false;
    });

    $(' #close-question-layer').click(function(){
        $('#question-layer').hide();
        return false;
    });

    /*opinion layer*/
    $('#send-opinion, #opinion-link a').click(function(){
        $('#opinion-layer').show();
        return false;
    });

    $('#close-opinion-layer').click(function(){
        $('#opinion-layer').hide();
        return false;
    });

    /*form labels*/
    $('form input, form textarea').each(function(){
        if($(this).attr('value') != '') $(this).siblings('label').hide();
    });

    $('form input:not([type=checkbox]), form textarea')
    .live('focus', function(){
        $(this).siblings('label').hide();
    })
    .live('blur', function(){
        var value = $(this).val();
        if(value.replace(/[s]+/, '') === ''){
            $(this).siblings('label').show();
        }
    });

    $('form label').live('click', function(){
        $(this).css('display', 'none');
        $(this).siblings('input, textarea').focus();
    });

    /*form select*/
    $('form .form-select > a').live('click', function(){
        var form = $(this).closest('form');
        $('.select-options', form).hide();
        $(this).siblings('.select-options').show();
        return false;
    });
    
    $('.select-options > a').live('click', function(){
        $(this).parent('.select-options').hide();
        return false;
    });

    $('.select-options ul a').live('click', function(){
        var val = $(this).attr('rel');
        var text = $(this).html();
        var sibling = $(this).closest('.form-select');
        var options = $(this).closest('.select-options');

        sibling.siblings('input.select-text').attr('value', val);
        $('> a', sibling).html(text);
        $('ul a', options).removeClass('active');
        $(this).addClass('active');
        options.hide();

        return false;
    });

	getTesttingTime();
	
    /*choose date/time for pre-testing form*/
    $('#testing-date ul a').live('click', function(){
        $('#testing-time').prev('.select-text').attr('value', '');
        $('#testing-time > a').html('Желаемое время тестирования');
		getTesttingTime();
    })

    selectedOption();

    /*login/registration*/
    $('input[type=submit][name=cmdwebsignup]').live('click', function(){
        $('#reg_button_action').val($(this).attr('name'));
    });

    $('input[type=submit][name=cmdweblogin]').live('click', function(){
        var form = $(this).closest('form');
        $('input[name=button_action]', form).val($(this).attr('name'));
    });
    
    /*restorePass form display/hide*/
    $('#remember-pass').live('click', function(){
        $('#form-auth').hide();
        $('#form-restore').show();
        $('#auth-form > h2').html('Восстановить пароль');
        return false;
    });
    
    $('#restore-cancel').live('click', function(){
        $('#form-restore').hide();
        $('#form-auth').show();
        $('#auth-form > h2').html('Вход на сайт');
        return false;
    });
    
    $('#restore-button-wrapper input[name=cmdweblogin]').live('click',function(){
        $('input[name=txtpwdrem]').val(1);
    });

    /*Events/Articles*/
    $('#event-link').click(function(){
        $('#articles').hide();
        $('#events').show();
        $(this).siblings('a').removeClass('active');
        $(this).addClass('active');
        return false;
    });

    $('#articles-link').click(function(){
        $('#events').hide();
        $('#articles').show();
        $(this).siblings('a').removeClass('active');
        $(this).addClass('active');
        return false;
    });

    /*subMenus*/
    $('#top-submenu a.sub-menu-head, #top-submenu a.sub-menu-open').click(function(){
        $('.hide-submenu-wrapper').hide();
        $(this).parent('div').siblings('.hide-submenu-wrapper').show();
        return false;
    });

    $('#top-submenu .hide-submenu-wrapper a.sub-menu-head, #top-submenu a.sub-menu-close').click(function(){
        $(this).parent('div').parent('.hide-submenu').parent('.hide-submenu-wrapper').hide();
        return false;
    });

    /*tables*/
    if ($.browser.msie && $.browser.version < 9){
        $('.content table:not(.YMaps-b-balloon-frame) tr:even').each(function(){
            $(this).addClass('odd');
        });

        $('.content table:not(.YMaps-b-balloon-frame) tr td:first-child, .content table:not(.YMaps-b-balloon-frame) tr th:first-child').each(function(){
            $(this).addClass('first');
        });

        $('.content table:not(.YMaps-b-balloon-frame) tr td:last-child, .content table:not(.YMaps-b-balloon-frame) tr th:last-child').each(function(){
            $(this).addClass('last');
        });
    }

    /*ie text-shadow*/
    if ($.browser.msie){
        $('.reg-testing-wrapper a').textShadow({
            x: "0",
            y: "1px",
            radius:  "1px",
            color:   "#bd0803"
        });

        if($.browser.version > 7){
            $('#column-1 h2').textShadow({
                x: "0",
                y: "1px",
                radius:  "1px",
                color:   "#f15c00"
            });

            $('#column-2 h2').textShadow({
                x: "0",
                y: "1px",
                radius:  "1px",
                color:   "#0e7838"
            });

            $('.langs-menu #column-3 h2').textShadow({
                x: "0",
                y: "1px",
                radius:  "1px",
                color:   "#ced3da"
            });
            
            $('#top-submenu-teaser a').textShadow({
                x: "0",
                y: "1px",
                radius:  "1px",
                color:   "#199a5b"
            });
        }

        /*$('#auth-form input.enter-button').textShadow({
            x: "0",
            y: "1px",
            radius:  "1px",
            color:   "#00003f"
        });

        $('#feedback-form input.enter-button, #feedback-button-wrapper-c input.enter-button, #question-form input.enter-button, #opinion-form input.enter-button').textShadow({
            x: "0",
            y: "1px",
            radius:  "1px",
            color:   "#cb0000"
        });
        
        $('#opinion-form input.enter-button').textShadow({
            x: "0",
            y: "1px",
            radius:  "1px",
            color:   "#0e7132"
        });*/
    }
});

function getRating(){
    $('#raiting-slider').slider({
        value:0,
        min: -5,
        max: 5,
        step: 1,
        create: function(event, ui) {
            $('input',this).val(0);
            var pLeft = 0;
            for(var i = $(this).slider('option', 'min'); i <= $(this).slider('option', 'max'); i++){
                $(this).append('<span class="raiting-point" style="left:'+pLeft+'%;"><em>'+(i !== 0 ? i : '')+'</em></span>');
                pLeft += 100/(Math.abs($(this).slider('option', 'min')) + Math.abs($(this).slider('option', 'max')));
            }
        },
        change: function(event, ui) {
            $('input',this).val(ui.value);
        }
    });
}

/*select options after faq/opinion form error*/
function selectedOption(){
    $('.select-text').each(function(){
            var val = $(this).val();
            var sibling = $(this).siblings('.form-select');
            var link = $('.select-options ul a[rel='+val+']', sibling);
            var text = link.html();
            if(text) $('> a', sibling).html(text);
            link.addClass('active');
    });
}

function getTesttingTime(){
	source = $('#testing input[name=date]').attr('value');
	if(source){
		$.get("/index.php?id=202&source="+parseInt(source), function(data) {
			$('#time-list').html(data);
		});
	}
}

/*succesfull feedback send*/
function feedbackas_responseCallback(){
    if($('#feedback-form p.thanks').length != 0){
        setTimeout("$('#feedback-layer').hide();", 1800);
    }
}

/*succesfull opinion send or reload rating slider and selected option if form error*/
function opinionas_responseCallback(){
    if($('#opinion-form p.thanks').length != 0){
        setTimeout("$('#opinion-layer').hide();", 1800);
    }else{
        if($('#opinion-form div.errors').length != 0){
            getRating();
            selectedOption();
        }
    }
}

function testingas_responseCallback(){
	if($('#testing div.errors').length != 0){
		selectedOption();
		getTesttingTime();
	}
}

/*succesfull faq send or reload selected option if form error*/
function faqas_responseCallback(){
    if($('#question-layer p.thanks').length != 0){
        setTimeout("$('#question-layer').hide();", 1800);
    }else{
        if($('#question-form div.errors').length != 0){
            selectedOption();
        }
    }
}

/*succesfull login*/
function authas_successCallback(){
    $('#registraion-link').hide();
    setTimeout('window.location.reload();', 1000);
}

/*succesfull pass restore*/
function restoreas_successCallback(){
    $('#registraion-link').hide();
    setTimeout('window.location.reload();', 2000);
}

/*restore pass error*/
function restoreas_responseCallback(){
    if($('#auth-errors').length != 0){
        $('#form-auth').hide();
        $('#form-restore').show();
        $('#auth-form > h2').html('Восстановить пароль');
    }
}

/*confirm restored pass*/
setTimeout(checkRestore, 1000);
function checkRestore(){
    if($('#auth-errors').length){
        $('#auth-layer').show();
    }
}

/*registration form errors*/
function registrationas_responseCallback(){
    if($('#reg-errors').length){
        var _this = $('#reg-errors');
        var regForm = _this.next('#registration');
        $('span', _this).each(function(){
            rel = $(this).attr('rel');
            if(rel.length){
                $('input[name='+rel+']', regForm).addClass('error');
                $('label[for='+rel+']', regForm).addClass('error');
            }
        });
    }
}
