var attr,from,to,now,code,i;
function same (attr,from,to) {
    if (attr=='h' || attr=='sq') $(to).css('height',$(from).height());
    if (attr=='h' || attr=='sq') $(to).css('width',$(from).width());
}
$(document).ready(function() {
    $('.wrap').not('#menuG .wrap').css('overflow','hidden');
    $('#window .close').click(function(){
        $('#curtain, #window').hide();
    });
    $('#toBid').mouseover(function() {
        $('#toBid').css('background-image','url(/img/toBidHover.png)');
    });
    $('#toBid').mouseleave(function () {
        $('#toBid').css('background-image','url(/img/toBid.png)');
    });
    $('.toForm').click(function(){
        from=$(this).parent().children('form');
        same('sq',from,'#userFormBg');
        $(this).parent().children('form').slideDown('slow');
        $('#userFormBg').slideDown('slow');
        $('.toForm').hide();
    });
    $('.closeUserForm').click(function(){
        $('.userForm form, #userFormBg').hide();
        $('.toForm').show();
    });
    $('.userForm :input').change(function() {
        now=this;
        code=Math.floor(Math.random() * (50+1));
        $.post('/js/checkForm.php', {val: $(now).val(), input:$(now).attr('name'), check: code},
            function (data) {
                $(now).parent().children('.er, .ok').remove();
                if (data=='exist')      $(now).after('<div class=er>уже существует</div>');
                if (data=='cyril')      $(now).after('<div class=er>без кириллицы</div>');
                if (data=='check')      $(now).after('<div class=er>некорректные данные</div>');
                if (data=='count')      $(now).after('<div class=er>слишком коротко</div>');
                if (data=='city')       $(now).after('<div class=er>необходим реальный город</div>');
                if (data=='noPass')     $(now).after('<div class=er>новый пароль не введен</div>');
                if (data=='noConfirm')  $(now).after('<div class=er>пароли не совпадают</div>');
                if (data=='noExists')   $(now).after('<div class=er>проверьте введенные данные</div>');
                if (data=='noAuth')     $(now).after('<div class=er>проверьте введенные данные</div>');
                if (data=='true')       $(now).after('<span class=ok>ok</span>');//&radic;
                if (data=='true' || data=='') {
                    if(!$('.userForm div').hasClass('er')) $(now).parent().parent().children(':last-child').children(':submit').removeAttr('disabled');
                }
                else $(now).parent().parent().children(':last-child').children(':submit').attr('disabled','disabled');
                same('sq',$(now).parent().parent(),'#userFormBg');
            });
    });
    $('.addBid :input').mouseleave(function () {
        if ($(':input[name=person]').val()=='' || $(':input[name=phone]').val()=='' || $(':input[name=comment]').val()=='' || $(':input[name=description]').val()=='' || $(':selected').size()==0 || $(':input[name=description]').val()=='Описание конкурсной работы...') {
            $('.addBid :submit').attr('disabled','disabled');
            $('.addBid :submit').attr('title','Сначала заполните обязательные поля');
        }
        else {
            $('.addBid :submit').removeAttr('disabled');
            $('.addBid :submit').removeAttr('title');
        }
    });
    $('.toMsgToOrg').click(function() {
        $(this).hide();
        $(this).parent().parent().children('.msgToOrg').slideDown('normal');
    });
    $('.msgToOrg :input').mouseleave(function () {
        if($(".msgToOrg :input[name='person']").val()=='' || $(".msgToOrg :input[name='email']").val()==''){
            $('.msgToOrg :submit').attr('disabled','disabled');
        }
        else $('.msgToOrg :submit').removeAttr('disabled');
    });
    //menuG
    $('#menuG a').mouseleave(function(){
        $('#menuG .item').css('background-image','none');
        $('.mRow').not('#item .mRow').hide();
    });
    $('#menuG a').mouseover(function(){
        $(this).parent().css('background-image','url(/img/bgMenuGhover.png)');
        //$(this).css('text-shadow','#000000 0px 0px 15px');
        //$(this).parent().children('.mRow').css('display','block');
    });
    var x = $('#item .item').width();
    x = Math.ceil(x/7)*7;
    $('#item .item').css('width',x);
    //фрагмент, который в фоне повторяется 7пх в ширину
    $('.theme').mouseover(function(){
        $(this).css('background-image','url(/img/bgMenuGhover.png)');
        $(this).parent().children('.mRow').show();
    });
    $('.theme').mouseleave(function(){
        $('.theme :hidden').not('.s').parent().css('background-image','url(/img/bgMenuG.png)');
    });
    $('#theme a').click(function() {
        $('.theme :hidden').removeClass('s');
        $(this).children(':hidden').addClass('s');
        $('.theme').css('background-image','url(/img/bgMenuG.png)');
        $(this).css('background-image','url(/img/bgMenuGhover.png)');
        var n = $(this).children(':hidden').val();
        $('.bid').hide();
        $('.bid a.th'+n+'').parent().fadeIn('normal');
    });
    $('.bid').click (function() {
        $('#audio, #video').remove();
        var x = $(this).children().children(':hidden[name=src]').val();
        //alert(x);
        $('#window img').not('#mark img').attr('src',x);
        $('#window img').not('#mark img').css('width','auto');
        if (x==undefined) {
          $('#window img').not('#mark img').attr('src',' ');
          var x = $(this).children().children(':hidden[name=movie]').val();
          $('#window img').not('#mark img').before('<div id=video><iframe width="560" height="315" src="'+x+'" frameborder="0" allowfullscreen></iframe></div>');
          $('#window img').not('#mark img').css('width',555);
        }
        var x = $(this).children().children(':hidden[name=sound]').val();
        if (x!=undefined) $('#mark').before('<span id=audio><object type="application/x-shockwave-flash" data="/dewplayer.swf?mp3='+x+'" width="200" height="20"><param name="movie" value="/dewplayer.swf?mp3='+x+'" /></object></span>');
        //if (x!=undefined) $('#mark').before('<span id=audio><object type="application/x-shockwave-flash" data="http://flv-mp3.com/i/pic/ump3player_500x70.swf" height="39" width="260"><param name="wmode" value="transparent" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="movie" value="http://flv-mp3.com/i/pic/ump3player_500x70.swf" /><param name="FlashVars" value="way='+x+'&amp;swf=http://flv-mp3.com/i/pic/ump3player_500x70.swf&amp;w=260&amp;h=39&amp;time_seconds=0&amp;autoplay=0&amp;q=&amp;skin=white&amp;volume=70&amp;comment=" /></object></span>');
        //if (x!=undefined) $('#mark').before('<span id=audio><object id="audioplayer1" data="/player.swf" wmode="transparent" type="application/x-shockwave-flash" width="180" height="16"> <param value="/player.swf" name="movie"> <param value="playerID=1&amp;bg=0x67452E&amp;leftbg=0xB3B3B3&amp;lefticon=0xoooooo&amp;rightbg=0x67452E&amp;rightbghover=0x999999&amp;rightcon=0xoooooo&amp;righticonhover=0xffffff&amp;text=0x666666&amp;slider=0x8CA4C0&amp;track=0x8CA4C0&amp;border=0x666666&amp;loader=0x9FFFB8&amp;loop=no&amp;autostart=no&amp;soundFile=http://xn----7sblcuh5axmc0a2g.xn--p1ai/'+x+'" name="FlashVars"> <param value="high" name="quality"> <param value="false" name="menu"> <param value="transparent" name="wmode"> </object></span>');
        var x = $(this).children().children(':hidden[name=comment]').val();
        $('#window div .nameOrg').text(x);
        var x = $(this).children().children(':hidden[name=desc]').val();
        $('#window div span:eq(1) p').text(x);
        var x = $(this).children().children(':hidden[name=comp]').val();
        $('#window div span:eq(2) p').text(x);
        //var x = $(this).children().children(':hidden[name=date]').val();
        //$('#window div span:eq(3) p').text('Работа была добавлена '+x+'.');
        $('#mark').html('Оцени работу: <img src=/img/starH.png><img src=/img/starH.png><img src=/img/starH.png><img src=/img/starH.png><img src=/img/starH.png>');
        var x = $(this).children().children(':hidden[name=id]').val();
        $('#window div :hidden').val(x);
        //alert(x);
        $('#curtain, #window').show();
        $('#mark img').mouseover(function() {
            var x = $(this).index();
            for(i=0;i<5;i++) if (i<=x) $('#mark img:eq('+i+')').attr('src','/img/starW.png'); else $('#mark img:eq('+i+')').attr('src','/img/starH.png');
        });
        $('#mark img').click(function() {
            var x = $(this).index();
            x++;
            $.post('/js/mark.php', {id:$('#window div :hidden').val(), mark: x},
            function () {
                $('#mark').html('<p>Вы оценити эту работу на '+x+'</p>');
            });
        });
        $('#mark').mouseleave(function() {
            $('#mark img').attr('src','/img/starH.png');
        });
    });
    $('.photo').click(function() {
        var x = $(this).children().children(':hidden[name=src]').val();
        $('#photo img').attr('src',x);
        $('#curtain, #photo, #close').show();
        $('#photo img').load(function() {
          x = $('#photo img').width();
          x = Math.round(x/2);
          $('#photo').css('margin-left','-'+x+'px');
          x = $('#photo img').height();
          x = Math.round(x/2);
          $('#photo').css('margin-top','-'+x+'px');
        });
    });
    $('#col2 div span').click(function() {
        //$('#col2 div p').hide();
        $(this).parent('div').children('p').toggle('slow');
        //$(this).parent('div').toggle();
        //$(this).parent('div').slideDown('slow');
    });
    $('#close').click(function(){
        $('#curtain, #photo').hide();
    });
    //adminDelete
    $('.adminDelete').click(function() {
        var now=this;
        $.post('/js/deleteItem.php', {id:$(this).children(':hidden[name=id]').val(), table:$(this).children(':hidden[name=table]').val()},
        function (data) {
            //alert(data);
            $(now).parent().hide();
        });
    });
    $('.adminVis').click(function() {
        var now=this;
        $.post('/js/invisItem.php', {id:$(this).children(':hidden[name=id]').val(), table:$(this).children(':hidden[name=table]').val()},
        function (data) {
            alert(data);
            $(now).parent().hide();
        });
    });
    //scroll
    $('.leftScroll').click(function(){
        var y=$('.scroll').css('left');
        var x=y.substring(0,y.length -2);
        if (x>-700) $('.scroll').animate({'left': '-=200px'}, 'slow');
    });
    $('.rightScroll').click(function(){
        var y=$('.scroll').css('left');
        var x=y.substring(0,y.length -2);
        if (x<10) $('.scroll').animate({'left': '+=200px'}, 'slow');
    });
});
