$(document).ready(function(){
    
    $('select.select1').live('change',function(){
        var select=this;
        $.post("/xajax/xajax.php", {
            func: "getRbCategory",
            id_main_category: $(this).val()
        }, function(data){
            if(data=='')
            {
                
            }
            $(select).parent().children('select.select2').html(data);
        }, 'html');
        
    });
    $('input.foto-descr').focus(function(){
        if($(this).val()=='Введите описание фотографии')
        {
            $(this).val('');
            $(this).css('color','black');
        }
    })
    $('input.foto-descr').blur(function(){
        if($(this).val()=='')
        {
            $(this).val('Введите описание фотографии');
            $(this).css('color','gray');
        }
    })

    setGeoLoaders();
    $("#formSubmitImage").click(function(e){
        showWaitWindow();
        var event;
        event=e;
        if($("#regemail").size()>0)
        {
            $.ajax({
            url:"/xajax/xajax.php",
            async: false,
            type: 'post',
            data: {
            func: "checkLogin",
            login: $("#regemail").attr('value')
            },
            success: function(xml){
                //Пришел ответ-корректен ли логин
                var loginStat = $("login_status", xml).text();
                if (loginStat != "correct") 
                {
                    $.ajax({
                        url: "/xajax/xajax.php",
                        type: 'post',
                        async: false,
                        data: {
                    func: "checkAuthInformation",
                    login: $("#regemail").attr('value'),
                    password: $("#password").val()
                    },
                    success: function(xml){
                        var authStat = $("auth_status", xml).text();
                        if(authStat != "correct")
                        {
                            $('#password_error_ul').show();
                            $('#password_status_ul').hide();
                            error=checkEmptyFields();
                            event.preventDefault();                        
                        }
                        else
                        {
                            $('#password_error_ul').hide();
                            $('#password_status_ul').show();
                            error=checkEmptyFields();
                            if(error)
                            {
                                event.preventDefault();
                            }
                        }
                        closeWaitWindow();
                    }
                    });
                    
                } 
                else 
                {
                    error=checkEmptyFields();
                    if(error)
                    {
                        event.preventDefault();
                    }
                    closeWaitWindow();
                }
            }
            });
        }
        else
        {
            error=checkEmptyFields();
            if(error)
            {
                event.preventDefault();
            }            
            closeWaitWindow();
        }
    })
    if($("#regemail").size()>0)
    {
        $("#regemail").change(function(){
           check_email();
        });
        check_email(); 
    }
    $('.dop-parametres').hide();
    $('.dop-inf img').attr('src','/img/plus1.gif');
    $('.dop-inf a').toggle(function(event){$('.dop-parametres').show();$('.dop-inf img').attr('src','/img/minus.gif');return false;}, function(event){$('.dop-parametres').hide();$('.dop-inf img').attr('src','/img/plus1.gif');})
    $('input').change(function(e){
        checkQualityIndex();
    });
    $('textarea').change(function(e){
        checkQualityIndex();
    });
    $('select').change(function(e){
        checkQualityIndex();
    });
    checkQualityIndex();
    //$('a').live('click',function(){alert('a');});
// otrasl-fransh
     $('.add-category a').live('click', function(event){
        if($(".otrasl-fransh").size()>=3)
        {
            $('.add-category').hide()
        }
        $(".otralsi-fransh").append($(".otrasl-fransh-input").html());
        $(".otrasl-fransh").show();
        return false;
        });    
    $(".otrasl-fransh p a").live('click', function(event){
        $(this).parents(".otrasl-fransh").remove();
        if($(".otrasl-fransh").size()<4)
        {            
            $('.add-category').show();
        }
        return false;
    });
        if($(".otrasl-fransh").size()>=4)
        {
            $('.add-category').hide()
        }
        /* rating */
// dlia opera
    var isOpera = (navigator.userAgent.indexOf("Opera") != -1);
    if(isOpera){        
    width = document.body.clientWidth - $('.popup').width();                        
    height = document.body.clientHeight -  $('.popup').height();                       
    height_lb = document.body.clientHeight -  $('.link-beboss').height();                       
    width_fix = document.body.clientWidth;                        
  }
  
// dlia ie 
    var isIE = ((!isOpera)&&(navigator.appName.indexOf("Microsoft Internet Explorer") != -1));
    if(isIE){
    width_fix = document.documentElement.clientWidth;             
    width = document.documentElement.clientWidth - $('.popup').width();             
    height = document.documentElement.clientHeight - $('.popup').height(); 
    height_lb = document.documentElement.clientHeight  - $('.link-beboss').height();                            
    }
        
// dlia firfox
    var isMozzila = (navigator.userAgent.toLowerCase().indexOf("gecko")!=-1)
    if(isMozzila){    
    width_fix = window.innerWidth;                                 
    width = window.innerWidth - $('.popup').width();                                 
    height = window.innerHeight - $('.popup').height();                            
    height_lb = window.innerHeight - $('.link-beboss').height();                            
  } 

            var placeholder = $( ".franshise" );
            var index = $( ".index-kachestva" );
            $( window ).bind("scroll resize",function(){
                    var placeholderTop = placeholder.offset().top-20;
                    var viewTop = $( window ).scrollTop();
                    if ((viewTop > placeholderTop) &&!index.is( ".index-fixed" )){
                        placeholder.height(placeholder.height());
                        index.addClass( "index-fixed" );
                        $('.new-error').addClass( "error-fixed" );
                        if(isMozzila){ 
                            index.css( "left", (width_fix-1003)/2+781 +"px" );
                            $('.new-error').css( "left", (width_fix-1003)/2+626 +"px" );
                        }
                        if((isOpera)||isIE){ 
                            index.css( "left", (width_fix-1003)/2+790 +"px" );
                            $('.new-error').css( "left", (width_fix-1003)/2+635 +"px" );
                        }
                    } 
                    else if ((viewTop <= placeholderTop)&&index.is( ".index-fixed" )){
                        placeholder.css( "height", "auto" );
                        index.removeClass( "index-fixed" );
                        $('.error-fixed').removeClass( "error-fixed" );
                            index.css( "left",  "770px" );
                            $('.new-error').css( "left", "615px" );
                    }
                }
            );

     
});

function checkQualityIndex()
{
    var QualityIndex;
    QualityIndex=0;
    for (var key in arFields) {
        var val = arFields [key];
        var value;
        value=getValue(val,key);
        if(!empty(value))
        {
            if(val['points']===undefined)
            {
                QualityIndex++;
            }
            else
            {
                QualityIndex+=val['points'];
            }
            $("li.error_li_"+key).remove();
            if(empty($("#error_bottom_ul").html()))
            {
                $("#error_bottom_ul").hide();
            }
            if(empty($("#error_right_div ul").html()))
            {
                $("#error_right_div").hide();
            }
        }

    }
    var QualityPercents;
    QualityPercents=(QualityIndex+QualityIndex2)*100/40;
    QualityPercents=Math.round(QualityPercents);
    $('#qual_ssp').css('width',QualityPercents+'%');
    $('#qual_vsp').html(QualityPercents);
}

    function setGeoLoaders(){ //Ajax загрузка городов, регионов, стран
            $("#regcountry").change(function(e){
                e.preventDefault();
                $("#regcity").html('');
                $.post("/xajax/xajax.php", {
                    func: "getGeoList",
                    geoParent: $(this).attr('value'),
                    geoTarget: "region"
                }, function(xml){
                    //Пришел ответ-список регионов
                    $("#regregion").html($("option", xml).get());
                    $("#regregion").show();
                    $("#regionLabelHolder").show();
                    if ($.browser.msie && ($.browser.version.substr(0, 1) == '6')) {
                        $("#regregion option[value='0']").attr('checked', 'checked');
                    } else $("#regregion option[value='0']").attr('selected', 'selected');                                       
                    //Обработчик выбора региона 
                    $("#regregion").change(function(){
                        $.post("/xajax/xajax.php", {
                            func: "getGeoList",
                            geoParent: $("#regregion").attr('value'),
                            geoTarget: "city"
                        }, function(xml){
                            $("#regcity").show();
                            $("#regcityLabelHolder").show();
                            //Пришел ответ-список городов
                            $("#regcity]").html($("option", xml).get());
                        }, "html");
                    });                                
                }, "html");
                
            });
                    $("#regregion").change(function(){
                        $.post("/xajax/xajax.php", {
                            func: "getGeoList",
                            geoParent: $("#regregion").attr('value'),
                            geoTarget: "city"
                        }, function(xml){
                            $("#regcity").show();
                            $("#regcityLabelHolder").show();
                            //Пришел ответ-список городов
                            $("#regcity").html($("option", xml).get());                         
                            if ($.browser.msie && ($.browser.version.substr(0, 1) == '6')) {
                                $("#regcity option[value='0']").attr('checked', 'checked');
                            } else $("#regcity option[value='0']").attr('selected', 'selected');                    
                            
                        }, "html");
                    });
            
}

function checkEmptyFields()
{
    var sErrors;
    sErrors='';
    for (var key in arFields) {
        var val = arFields [key];
        if(val['req']==true)
        {
            var value;
            value=getValue(val,key);
            if((empty(value)))
            {
                sErrors=sErrors+'<li class="error_li_'+key+'">&mdash; Не заполнено поле «<span class="link-dashed"><a href="#field_'+key+'">'+val['name']+'</span></a>»</li>';
            }
        }
    }                 
    if(!empty(sErrors))
    {
        $("#error_bottom_ul").html(sErrors);
        $("#error_bottom_ul").show();
        $("#error_right_div ul").html(sErrors);
        $("#error_right_div").show();          
        return true;
    }
    else
    {
        return false;
    }
}

function getPhoneNumber()
{
    if($("input[name=phone_code]").size()>0)
    {
        var result;
        result='';
        var temp;
        temp=$("input[name=phone_code]").val()
        if(!empty(temp))
        {
            result=result+temp+' ';
        }
        var temp;
        temp=$("input[name=phone_city]").val()
        if(!empty(temp))
        {
            result=result+temp+' ';
        }
        var temp;
        temp=$("input[name=phone_region]").val()
        if(!empty(temp))
        {
            result=result+temp;
        }
        return result;
    }
    else
    {
        return 'auth';
    }
}

function getListingLogo()
{
    return $("#listing_logo").val();
}

function empty( mixed_var ) {  
    return ( mixed_var === "" || mixed_var === 0   ||  mixed_var === null );
}

function getValue(val,key)
{
    var value;
    if(val['function']==undefined)
    {
        if($(val['input_type']+'[name='+key+']').size()>0)
        {
            value=$(val['input_type']+'[name='+key+']').val();
        }
        else
        {
            return 'auth';
        }
    }
    else
    {
        value=eval(val['function']+'()');
    }
    return value;
}

function getRbCategory()
{
    var value; 
    value=$('select.select1:first').val();
    return value;
}
function check_email(){
    if(!empty($("#regemail").attr('value')))
    {
            $.post("/xajax/xajax.php", {
            func: "checkLogin",
            login: $("#regemail").attr('value')
        }, function(xml){
            //Пришел ответ-корректен ли логин
            var loginStat = $("login_status", xml).text();
            if (loginStat != "correct") {
                $("#regpassword").show();
            } else {
                $("#regpassword").hide();
            }
        });
    }
    else
    {
        $("#regpassword").hide();        
    }
}
function getHideAdress()
{
    return '1';
}
function getHideCost()
{
    return '1';
}
