/**
 * funcs and voids for sportleader
 * required: jquery, jquery UI, jcarousellite_1.0.1
 * @author Sergey Ponomarev <ponomarevsa@yandex.ru>
 */


/** timer for promo.center auto pagination */
var timerPromo;
var timerPromoInterval = 5;

function next_gallery_photo(el) {
    var cur = 0;
    var count = 0;
    $(el).parent().parent().parent().find('img.galim').each(function(){
        count++;
        if($(this).css('display') == 'block') {
            cur = $(this).attr('rel');
        }
    });

    $(el).parent().parent().parent().find('img.galim').hide();
    cur++;
    
    if(cur == count) {
        $(el).parent().parent().parent().find('img[rel=0]').css('display', 'block')
    } else {
        $(el).parent().parent().parent().find('img[rel='+cur+']').css('display', 'block');
    }
}

function prev_gallery_photo(el) {
    var cur = 0;
    var count = 0;
    $(el).parent().parent().parent().find('img.galim').each(function(){
        count++;
        if($(this).css('display') == 'block') {
            cur = $(this).attr('rel');
        }
    });

    $(el).parent().parent().parent().find('img.galim').hide();
    cur--;
    count--;
    if(cur < 0) {
        $(el).parent().parent().parent().find('img[rel='+count+']').css('display', 'block')
    } else {
        $(el).parent().parent().parent().find('img[rel='+cur+']').css('display', 'block');
    }
}


function root_load() {
    window.location.href = '/';
}

function showquestionsontitle(){
    $('#titleblockposts').hide();
    $('#titleblockquest').show();
}

function showblogstitle(){
    $('#titleblockquest').hide();
    $('#titleblockposts').show();
}
/**
 * open new window
 */
function open_window(href)
{
    window.open(href,'mywin','left=20,top=20,width=500,height=500,toolbar=0,resizable=0');
//window.open("","","toolbar=no,location=no,left=0,top=0,directories=no,status=yes,menubar=no,scrollbars=no,resizable=yes,width="+width+",height="+height+"");
} 

function vacancy_update_captcha(el) {
    $.ajax({
        type: "GET",
        url: "/getcaptcha.php",
        dataType:'html',
        cache: false,
        success: function(html)
        {
            $(el).parent().parent().find('img').attr("src", '/bitrix/tools/captcha.php?captcha_sid=' + html);
            $(el).parent().parent().find('input').val(html);
        }
    });
}

/**
 * show, hide message panel
 */
function showMessage(str) {
    var text = $('#messagepanel').find('p.messagetext').html();
    text = text + '\n' + str  ;

    $('#messagepanel').find('p.messagetext').html(text);
    $('#messagepanel').slideDown();
}
function hidemessagepanel() {
    $('#messagepanel').find('p.messagetext').html('');
    $('#messagepanel').slideUp();
}


/**
 * send data from current popop window
 */
function submitpopup() {
    var formname  = $(".shadowBlock").find('form:visible').attr('name');
    if(formname == 'authform') {
        sendauthorize();
    }
    else if(formname == 'registerform') {
        sendregister();
    }
    else if(formname == 'NEW_QUESTION_FORM') {
        //
        var q = $('.shadowBlock').find('form[name=NEW_QUESTION_FORM]').find('textarea[name=form_textarea_7]').val();
        if(q != "") {
            sendnewquestion();
        }
    }
    else if(formname == 'forgotform') {
        $('form[name=forgotform]').submit();
    }
    else if(formname == 'registertoeventform') {
        sendregistertoeventrequest();
    }
    else if(formname == 'CHANGE_PASSWORD') {
        sendchangepass();
    }
}

function closepopup() {
    $(".shadowBlock").hide();
}

function sendregistertoeventrequest() {
    
    var formdata= $('.shadowBlock').find('form[name=registertoeventform]').serialize();
    $('.shadowBlock').find('img.loader').show();
    $.post('/registertoevent.php', formdata, function(data, textStatus) {
        $('.shadowBlock').find('img.loader').hide();
        if(data.status == 'OK') {
            $('.shadowBlock').find('.con').html('<p class="statusmessage"></p>');
            $('p.statusmessage').css('color', 'green');
        //window.setTimeout("closepopup();", 3000);
        }
        else if (data.status == 'OK_NEW_REG') {
            $('.shadowBlock').find('.con').html('<p class="statusmessage"></p>');
            $('p.statusmessage').css('color', 'green');
        }
        else {
            $('p.statusmessage').css('color', 'red');
            $('div.shadowBlock').find('div.con>p.statusmessage').html(data.message);
            /** renew captcha */
            $.ajax({
                type: "GET",
                url: "/getcaptcha.php",
                dataType:'html',
                cache: false,
                success: function(html)
                {
                    $("img[alt=CAPTCHA]").attr("src", '/bitrix/tools/captcha.php?captcha_sid=' + html);
                    $("input[name=captcha_sid]").val(html);
                    $("input[name=captcha_word]").val("");
                    $('input[name=register_submit_button]').removeAttr('disabled');
                }
            });
            $('.shadowBlock').find('input[type=button]').removeAttr('disabled');
            return false;
        //$('p.statusmessage').css('color', 'red');
        }
        $('p.statusmessage').html(data.message);
    }, "json");
}

/**
 * remain day counter
 */
function counter(el, d) {
    $.ajax({
        type: "GET",
        url: "/counter.php",
        dataType:'html',
        cache: false,
        data: 'd='+d,
        success: function(html)
        {
            $(el).text(html);
        }
    });
    return false;
}

function registertoevent() {
    var dialogbody = $("#registertoeventform").html();
    var dialogtitle = $("#registertoeventform").find('div.popuptitle').text();
    $('.shadowBlock').find('.con').html(dialogbody);
    $('.shadowBlock').find('div.leftBlock>span').text(dialogtitle);
    $(".shadowBlock").show();
    return true;
}


/**
 * show register dialog window
 **/
function register() {
    /* align */
    $('.shadowBlock').find('.reg').css('top', '50%');
    var N = 200;
    $('.shadowBlock').find('.reg').css('margin', '-'+N+'px 0 0 -282px');
    
    var dialogbody = $("#registerform").html();
    var dialogtitle = $("#registerform").find('div.popuptitle').text();
    $('.shadowBlock').find('.con').html(dialogbody);
    $('.shadowBlock').find('div.leftBlock>span').text(dialogtitle);
    $(".shadowBlock").show();
    return true;
}

/**
 * show auth dialog window
 */
function authorize() {
    /* align */
    $('.shadowBlock').find('.reg').css('top', '50%');
    var N = 100;
    $('.shadowBlock').find('.reg').css('margin', '-'+N+'px 0 0 -282px');

    var dialogbody = $("#authform").html();
    var dialogtitle = $("#authform").find('div.popuptitle').text();
    $('.shadowBlock').find('.con').html(dialogbody);
    $('.shadowBlock').find('div.leftBlock>span').text(dialogtitle);
    $(".shadowBlock").show();
    return true;
}

function newquestion(coach_login, coach_name) {
    /* align */
    $('.shadowBlock').find('.reg').css('top', '50%');
    var N = 200;
    $('.shadowBlock').find('.reg').css('margin', '-'+N+'px 0 0 -282px');
    
    var dialogbody = $("#newquestionform").html();
    //alert(dialogbody);
    var dialogtitle = $("#newquestionform").find('div.popuptitle').text();
    $('.shadowBlock').find('.con').html(dialogbody);
    $('.shadowBlock').find('div.leftBlock>span').text(dialogtitle);
    $(".shadowBlock").show();
    $('input[name=form_text_30]').val(coach_name);
    $('input[name=form_text_9]').val(coach_login);
    return true;
}


function sendchangepass() {
    $('.shadowBlock').find('input[type=button]').attr('disabled','disabled');
    $('.shadowBlock').find('img.loader').show();
    var formdata=$('.shadowBlock').find('form[name=CHANGE_PASSWORD]').serialize();
    $.post('/changepass.php', formdata, function(data, textStatus) {
        if(data.status == 'ERROR') {
            $('.shadowBlock').find('img.loader').hide();
            $('p.statusmessage').css('color', 'red');
            $('p.statusmessage').html(data.message);
            $('.shadowBlock').find('input[type=button]').removeAttr('disabled');
            return false;
        }
        else {
            $('.shadowBlock').find('img.loader').hide();
            $('p.statusmessage').css('color', 'green');
            $('p.statusmessage').html(data.message);
            $('.shadowBlock').find('input[type=button]').removeAttr('disabled');
            window.setTimeout("root_load();", 3000);
            return false;
        }
    }, "json");
    return false;
}
/**
 * send authorization request and
 * process result. Reload page if success.
 */
function sendauthorize() {
    $('.shadowBlock').find('input[type=button]').attr('disabled','disabled');
    $('.shadowBlock').find('img.loader').show();
    var formdata=$('.shadowBlock').find('form[name=authform]').serialize();
    $.post('/auth.php', formdata, function(data, textStatus) {
        if(data.status == 'ERROR') {
            $('.shadowBlock').find('img.loader').hide();
            $('p.statusmessage').css('color', 'red');
            $('div.shadowBlock').find('div.con>p.statusmessage').html(data.message);
            $('.shadowBlock').find('input[type=button]').removeAttr('disabled');
            return false;
        }
        else {
            var windowhref = window.location.href;
            windowhref = windowhref.replace("logout=yes", "");
            window.location.href = windowhref;
            return true;
        }
    }, "json");
    return false;
}

/**
 * send register request and
 * process result. Reload page if success.
 */
function sendregister() {
    $('.shadowBlock').find('input[type=button]').attr('disabled','disabled');
    $('.shadowBlock').find('img.loader').show();
    var formdata=$('.shadowBlock').find('form[name=registerform]').serialize();
    $.post('/reg.php', formdata, function(data, textStatus) {
        $('.shadowBlock').find('img.loader').hide();
        if(data.status == 'ERROR') {
            $('p.statusmessage').css('color', 'red');
            $('div.shadowBlock').find('div.con>p.statusmessage').html(data.message);
            /** renew captcha */
            $.ajax({
                type: "GET",
                url: "/getcaptcha.php",
                dataType:'html',
                cache: false,
                success: function(html)
                {
                    $("img[alt=CAPTCHA]").attr("src", '/bitrix/tools/captcha.php?captcha_sid=' + html);
                    $("input[name=captcha_sid]").val(html);
                    $("input[name=captcha_word]").val("");
                    $('input[name=register_submit_button]').removeAttr('disabled');
                }
            });
            $('.shadowBlock').find('input[type=button]').removeAttr('disabled');
            return false;
        }
        else {
            $('.shadowBlock').find('.con').html('<p class="statusmessage"></p>');
            $('p.statusmessage').css('color', 'green');
            $('div.shadowBlock').find('div.con>p.statusmessage').html(data.message);

            var windowhref = window.location.href;
            windowhref = windowhref.replace("logout=yes", "");
            //window.location.href = windowhref;
            return true;
        }
    }, "json");
    return false;
}

function forgotpass() {
    var dialogbody = $("#forgotform").html();
    var dialogtitle = $("#forgotform").find('div.popuptitle').text();
    $('.shadowBlock').find('.con').html(dialogbody);
    $('.shadowBlock').find('div.leftBlock>span').text(dialogtitle);
    $(".shadowBlock").show();
    return true;
}

/**
 * send new question request and
 * process result. Reload page if success.
 */
function sendnewquestion() {
    var formdata=$('.shadowBlock').find('form[name=NEW_QUESTION_FORM]').serialize();
    $('.shadowBlock').find('img.loader').show();
    $.post('/newquest.php', formdata, function(data, textStatus) {
        if(data.status == 'ERROR') {
            $('.shadowBlock').find('img.loader').hide();
            $('p.statusmessage').css('color', 'red');
            $('div.shadowBlock').find('div.con>p.statusmessage').html(data.message);
            $('.shadowBlock').find('input[type=button]').removeAttr('disabled');
            return false;
        }
        else {
            $('.shadowBlock').find('.con').html('<p class="statusmessage"></p>');
            $('p.statusmessage').css('color', 'green');
            $('div.shadowBlock').find('div.con>p.statusmessage').html(data.message);
        }
    }, "json");
    return false;
}

/**
 * automatic switch promo.center blocks
 */
function auto_switch_promo_center() {

    if(timerPromoInterval == 0) {
        return false;
    }
    var sec = timerPromoInterval * 1000;
    /** switch next */
    // get current page num
    var totalcount = 0;
    var curpage = 0;
    var html = $(".conteiner")
    .find(".center")
    .find(".main_block1")
    .find(".left")
    .find(".block1")
    .find(".block1")
    .each(function() {
        totalcount++;
        if($(this).css('display') == 'block') {
            curpage = $(this).find("span[class=promocenterpagenum]").text();
        }
        $(this).css('display', 'none');
    });

    // switch next
    if(curpage == totalcount) {
        curpage = 1
    }
    else {
        curpage++;
    }
    $("#promocenter" + curpage).show();
    // sleep
    timerPromo = window.setTimeout("auto_switch_promo_center();", sec);
}

$(document).ready(function(){
    //alert( $.cookie("continueie6") );
    // Check for IE6
    //    if (!$.browser.msie && $.browser.version.substring(0,1) != '6') {
    //        //var msg = $('#noie6').html();
    //        //showMessage(msg);
    //        window.location.href = '/noie6.php';
    //    }

    /** register, click link */
    $("a.newregisterbutton").click(function() {
        register();
        return false;
    });

    /** authorize, click link */
    $("#authorize").click(function(){
        authorize();
        return false;
    });

    /** new question, click link */
    $(".newquestion").click(function(){
        var coach_login = $(this).attr('rel');
        var coach_name = $(this).parent().find('a.coachname').text();
        newquestion(coach_login, coach_name);
        return false;
    });


    $(".registertoevent").click(function(){
        registertoevent();
        /** user status event registration choise */
    	$(".whoareyou").click(function(){
    		var this_val = $(".whoareyou:checked").val();
    		var __target = $("#" + this_val + "_div");
    		if (!__target.is(":visible"))
			{
    			$(".userform").slideUp("slow", function(){
    				__target.slideDown("slow");
    			});
    			$('.shadowBlock').find('input[type=button]').toggleClass("log-in-btn");
			}
    	});
    	return false;
    });

    /** event: close popup window */
    $(".closepopup").click(function(){
        $(".shadowBlock").hide();
        return false;
    });

    /** promo center pages */
    $(".promocenterpagenum").click(function(){
        clearTimeout (timerPromo);
        var num = $(this).text();
        $(this).parent().parent().parent().parent().find(".block1").hide();
        $("#promocenter" + num).show();
    });

    // carusel
    $(function() {
        $("#champcarusel").jCarouselLite({
            btnNext: ".champcaruselnext",
            btnPrev: ".champcaruselprev",
            scroll:1
        });
    });
    $(function() {
        $(".photo_report").jCarouselLite({
            btnNext: ".photocaruselnext",
            btnPrev: ".photocaruselprev"
        });
    });
    $(function() {
        $(".champions").jCarouselLite({
            btnNext: ".champcaruselnext",
            btnPrev: ".champcaruselprev"
        });
    });

    /**
     * get settings for promo.center timer
     */
    $.getJSON('/getparams.php', {
        "paramcode":"promocenterinterval"
    }, function(json){
        timerPromoInterval = json.interval;
        var sec = timerPromoInterval * 1000;
        timerPromo = window.setTimeout("auto_switch_promo_center();", sec);
    });

    /**
     * prev, next buttons for promo.top
     */
    $('.promotopnext').click(function(){
        $('.promotopprev').find("img").attr('src', '/images/l_arrow2a.gif');
        var btn = this;
        var promotopcurpage = 0;
        var yep = false;
        $(".announcements")
        .find(".item")
        .each(function() {
            if(yep == true) {
                yep = false;
                $(this).parent().show();
                if($(this).parent().attr("rel") == 3) {
                    $(btn).find("img").attr('src', '/images/r_arrow2.gif');
                    return false;
                }
                return true;
            }
            if($(this).parent().css("display") == 'block') {
                if($(this).parent().attr("rel") == 3) {
                    return false;
                }
                yep = true;
                $(this).parent().hide();
            }
        });
    });
    $('.promotopprev').click(function(){
        $('.promotopnext').find("img").attr('src', '/images/r_arrow2a.gif');
        var prevpage = -1;
        $(".announcements")
        .find(".item")
        .each(function() {
            if($(this).parent().css("display") == 'block') {
                return false;
            }
            prevpage = $(this).parent().attr("rel");
        });
        if(prevpage == -1) {
            $('.promotopprev').find("img").attr('src', '/images/l_arrow2.gif');
            return false;
        }
        $(".announcements")
        .find(".item")
        .each(function() {
            if($(this).parent().attr('rel') == prevpage) {
                $(this).parent().show();
            }
            else {
                $(this).parent().hide();
            }
        });
        if(prevpage == 0) {
            $('.promotopprev').find("img").attr('src', '/images/l_arrow2.gif');
            return false;
        }
    });

    /**
     * expand button for event page
     */
    $(".morebutton").click(function(){
        $("#"+$(this).attr("rel")).slideToggle();
        if($("#"+$(this).attr("rel")+"tr").hasClass("expanded")) {
            $("#"+$(this).attr("rel")+"tr").removeClass("expanded");
            $(this).text('Подробнее');
        }else {
            $("#"+$(this).attr("rel")+"tr").addClass("expanded");
            $(this).text('Свернуть');
        }
    })
    $(".coachmorebutton").click(function(){
        if(!$(this).hasClass("coachmorebuttonexpand")) {
            $(this).parent().removeClass("more");
            $(this).addClass("coachmorebuttonexpand");
            $(this).find("span").css('border-bottom','1px dashed #78AA3C');
        } else {
            $(this).parent().addClass("more");
            $(this).removeClass("coachmorebuttonexpand");
            $(this).find("span").css('border-bottom','1px dashed #E83227');
        }
        $("#"+$(this).attr("rel")).slideToggle();
    });
    $(".othercoachexpand").click(function(){
        if(!$(this).hasClass("coachmorebuttonexpand")) {
            $(this).parent().removeClass("more");
            $(this).addClass("coachmorebuttonexpand");
            $(this).find("span").css('border-bottom','1px dashed #78AA3C');
            $(this).parent().css('float','right');
        } else {
            $(this).parent().addClass("more");
            $(this).removeClass("coachmorebuttonexpand");
            $(this).find("span").css('border-bottom','1px dashed #E83227');
        }
        $(".persons").find(".about").slideToggle();
    });
    /**
     * mouseover fuctions for events page
     */
    $("li").hover(
        function () {
            $(this).addClass("style1");
            $(this).find(".block1").removeClass("bg1");
        },
        function () {
            $(this).removeClass("style1");
            $(this).find(".block1").addClass("bg1");
        }
        );

    /**
     * Events archiv filter swincher
     */
    $('#evtarcmnt').click(function() {
        var month = $('#evtarcmntmon').attr('rel');
        var year = $('#evtarcmntyer').html();
        $(this).attr('href', '/events/archive/' + '?filter='+month+year);
    });
    //FIXME: copypaste
    $('#evtarcmnt2').click(function() {
        var cat = $('#evtarcdatecatfilter').attr('rel');
        var month = $('#evtarcmntmon').attr('rel');
        var year = $('#evtarcmntyer').text();
        $(this).attr('href', '/events/archive/section/'+cat + '/?filter='+month+year);
    });
    $('#evtarcmntmon').click(function(){
        var listid = $(this).attr('id');
        $('#'+listid+'list').show();
    });
    $('.evtarcmntmon').click(function(){
        var listid = $(this).attr('class');
        var monthnum = $(this).attr('rel');
        var monthname = $(this).text();
        $('#'+listid+'list').hide();
        $('#evtarcmntmon').text(monthname);
        $('#evtarcmntmon').attr('rel', monthnum);
        $(this).parent().parent().find('li').removeClass('active');
        $(this).parent().addClass('active');

    });

    //FIXME: copypaste
    $('#evtarcdatecatfilter').click(function(){
        var listid = $(this).attr('id');
        $('#'+listid+'list').show();
    });
    $('.evtarcdatecatfilter').click(function(){
        var listid = $(this).attr('class');
        var monthnum = $(this).attr('rel');
        var monthname = $(this).text();
        $('#'+listid+'list').hide();
        $('#evtarcdatecatfilter').text(monthname);
        $('#evtarcdatecatfilter').attr('rel', monthnum);
        $(this).parent().parent().find('li').removeClass('active');
        $(this).parent().addClass('active');

    });


    $('#evtarcmntyer').click(function(){
        var listid = $(this).attr('id');
        $('#'+listid+'list').show();
    });
    $('.evtarcmntyer').click(function(){
        var listid = $(this).attr('class');
        var monthnum = $(this).attr('rel');
        var monthname = $(this).text();
        $('#'+listid+'list').hide();
        $('#evtarcmntyer').text(monthname);
        $(this).parent().parent().find('li').removeClass('active');
        $(this).parent().addClass('active');
    });

    /** blog date filter */
    $('#blogdatefilter').click(function(){
        var url = window.location.href;
        url = url.split('?', 1);
        url = url.join();
        url = url.split('/');
        var tmp = '';
        var part = '';
        for(var el in url){
            part = url[el]
            if((part.substr(0, 6) != 'filter') && (part != '')) {
                tmp = tmp + url[el] + '/';
            }
        }
        url = tmp.replace('http:/', 'http://');
        var m = $('#evtarcmntmonlist').find('li.active').find('a').attr('rel');
        var y = $('#evtarcmntyerlist').find('li.active').find('a').text();
        window.location.href = url + '?filter='+m+y;
    });
    $('#blogpostdatefilter').click(function(){
        var url = $(this).attr('rel');
        var m = $('#evtarcmntmonlist').find('li.active').find('a').attr('rel');
        var y = $('#evtarcmntyerlist').find('li.active').find('a').text();
        window.location.href = url + '?filter='+m+y;
    });

    /** related day count */
    $('div.conteiner')
    .find('div.center')
    .find('div.left')
    .find('div.txt')
    .find('td.col2')
    .each(function(){
        var foo = $(this).attr('rel');
        if(typeof(foo) !== 'undefined' && foo != null) {
            counter(this, foo);
        }
    });

    $('.leaderslistquotesNEXTquote').click(function(){
        var yep = false;
        var first = true;
        var donext = false;
        var firstEntry = '';
        $(this)
        .parent()
        .parent()
        .find('div.block')
        .each(function(){
            if(first) {
                // save first block
                firstEntry = this;
                first = false;
            }
            if(yep) {
                $(this).show();
                donext = true;
                return false;
            }
            if($(this).css('display') == 'block') {
                $(this).hide();
                yep = true;
            }
        });
        // if last show first
        if(!donext) {
            $(firstEntry).show();
        }
    });
    $('.leaderslistquotesPREVquote').click(function(){
        var yep = false;
        var entry = '';
        var prev = true;
        var prevEntry = '';
        var donext = false;
        $(this)
        .parent()
        .parent()
        .find('div.block')
        .each(function(){
            if($(this).css('display') == 'block') {
                $(this).hide();
                entry = prevEntry;
            }

            // save for prev block
            prevEntry = this;

        });
        if(entry == '') {
            $(prevEntry).show();
        } else {
            $(entry).show();
        }
    });
    // FIXME: copypast becouse another class name
    $('.leaderslistquotesNEXTquoteBlock').click(function(){
        var yep = false;
        var first = true;
        var donext = false;
        var firstEntry = '';
        $(this)
        .parent()
        .parent()
        .find('div.con')
        .each(function(){
            if(first) {
                // save first block
                firstEntry = this;
                first = false;
            }
            if(yep) {
                $(this).show();
                donext = true;
                return false;
            }
            if($(this).css('display') == 'block') {
                $(this).hide();
                yep = true;
            }
        });
        // if last show first
        if(!donext) {
            $(firstEntry).show();
        }
    });
    $('.leaderslistquotesPREVquoteBlock').click(function(){
        var yep = false;
        var entry = '';
        var prev = true;
        var prevEntry = '';
        var donext = false;
        $(this)
        .parent()
        .parent()
        .find('div.con')
        .each(function(){
            if($(this).css('display') == 'block') {
                $(this).hide();
                entry = prevEntry;
            }

            // save for prev block
            prevEntry = this;

        });
        if(entry == '') {
            $(prevEntry).show();
        } else {
            $(entry).show();
        }
    });

    /**
     * expand submenu
     */
    $('#leftmenu').hover(
        function() {
            $(this).find('div.top').show();
            $(this).addClass('aboutactive');
            $(this).find('div.sub').show();
            $(this).find('.subaboutmain').css('color', '#ebe4e1');
        },
        function() {
            $(this).find('div.top').hide();
            $(this).removeClass('aboutactive');
            $(this).find('div.sub').hide();
            if(!$(this).find('.subaboutmain').hasClass('subaboutmain2')) {
                $(this).find('.subaboutmain').css('color', '#E23613');
            }

        }
        );

    /* the vacancy page */
    /**
     * expand vacancy detail
     */
    $('.expandvacancybtn').click(function(){
        var id = $(this).attr('rel');
        // expand
        $('.moreaboutvacancy' + id).slideToggle();
        // change title
        var tmp = $(this).find('span').text();
        $(this).find('span').text($(this).find('span').attr('rel'));
        $(this).find('span').attr('rel', tmp);
        // change color
        $(this).parent().parent().parent().toggleClass('expanded');
    });
    $('.respondtojob').click(function(){
        $(this).parent().hide();
        $(this).parent().next().slideDown();
    });
    // expand if anchor
    var anchor  = location.hash.substring (1);
    if((anchor.length > 0) && anchor.substr(0, 7) == 'vacancy') {
        $('#'+anchor).find('font.notetext').show();
        $('#'+anchor).show();
        $('#'+anchor).prev().hide();

    }

    /* personal */
    /* fio */
    $("#change_fio_btn").click(function(){
        $(this).parent().hide();
        $(this).parent().next().show();
    });
    $("#change_fio_btn_send").click(function(){
        var el = $(this);
        var name = $(this).parent().prev().find('#change_fio_name').val();
        var lastname = $(this).parent().prev().find('#change_fio_lastname').val();

        $.post('modify.php', {
            "type": "fio",
            "name": name,
            "lastname": lastname
        }, function(data, textStatus) {
            $("#profilestatustext").remove();
            //$(data.mess).insertBefore($(el).parent().parent());
            if(data.status == 'OK') {
                $(el).parent().parent().hide();
                $(el).parent().parent().parent().find('h1').find('#profilelastname').text(lastname);
                $(el).parent().parent().parent().find('h1').find('#profilename').text(name);
                $(el).parent().parent().parent().find('h1').show();             
            }
        }, "json");
    });
    /* change_pass_btn */
    $('#change_pass_btn').click(function(){
        $(this).parent().hide();
        $(this).parent().next().show();
    });
    $('#change_pass_btn_send').click(function(){
        var el = $(this);
        var curr = $(this).parent().parent().find('#change_pass_btn_curr').val();
        var pas1 = $(this).parent().parent().find('#change_pass_btn_pas1').val();
        var pas2 = $(this).parent().parent().find('#change_pass_btn_pas2').val();

        $.post('modify.php', {
            "type": "pas",
            "curr": curr,
            "pas1": pas1,
            "pas2": pas2
        }, function(data, textStatus) {
            $("#profilestatustext").remove();
            $(data.mess).insertBefore($(el).parent().parent());
        }, "json");
    });
    /* chage photo */
    $('#change_photo_btn').click(function(){
        $(this).parent().parent().hide();
        $(this).parent().parent().next().show();
    });
    $('#remove_photo_btn_send').click(function(){
        $.post('modify.php', {
            "type": "removephoto"
        }, function(data, textStatus) {
            $("#profilestatustext").remove();
            //$(data.mess).insertBefore($(el).parent().parent());
            $("#remove_photo_btn_send").parent().parent().parent().find('img').hide();
            $("#remove_photo_btn_send").hide();
        }, "json");
    });

    $('.expandanswer').click(function(){
        var tmp = $(this).children().text();
        $(this).children().text($(this).children().attr('rel'));
        $(this).children().attr('rel', tmp);
        $(this).parent().parent().parent().toggleClass('expanded');
        $(this).parent().parent().parent().find('.answer').slideToggle();
    });

    $("#gotofacebook").hover(
        function () {
            $(this).css('background-position', "0 -18px");
        },
        function () {
            $(this).css('background-position', "0 0");
        }
        );
    $("#gotovk").hover(
        function () {
            $(this).css('background-position', "0 -18px");
        },
        function () {
            $(this).css('background-position', "0 0");
        }
        );
    $("#gototwitter").hover(
        function () {
            $(this).css('background-position', "0 -18px");
        },
        function () {
            $(this).css('background-position', "0 0");
        }
        );
    $("#gotolj").hover(
        function () {
            $(this).css('background-position', "0 -18px");
        },
        function () {
            $(this).css('background-position', "0 0");
        }
        );
    $("#gotoyoutube").hover(
        function () {
            $(this).css('background-position', "0 -18px");
        },
        function () {
            $(this).css('background-position', "0 0");
        }
        );



    $("input[name=ATTACH_IMG]").change(function() {
        $('form[name=formchangeavatar]').submit();
    });

    // FIXME: error if not exist
    $('.titleyourquestion').tbHinter({
        text: "Ваш вопрос"
    });

    $('.titlesearch').tbHinter({
        text: "Найти"
    });
    $('.titlesearcenc').tbHinter({
        text: "Поиск термина"
    });
/*

    jQuery('#champcarusel222').jcarousel({
    	wrap: 'circular',
            btnNext: ".champcaruselnext",
            btnPrev: ".champcaruselprev"
        
    });*/
    //wcc = $('#champcarusel').find('ul').attr('rel')+"%";
    //alert(wcc);
    //wcc = "200.98%";
    //$('#champcarusel').find('ul').css("width",  wcc);

    //$('#champcarusel').find('div.header').css("width", "95%");
    
                $('#leaders-slider-prev').hide();
            $('#leaders-slider-prevs').show();
            $('#leaders-slider-next').show();
            $('#leaders-slider-nexts').hide();


});
