$(document).ready(function(){

    if ($('#content_main').size()>0){
        
        /*
        $('#chatButton').click(function(){
            //$('#chatIFrame').contentWondow.showChat();
            //alert(0);
            //document.getElementById("chatIFrame").showChat();
            window.frames["chatIFrame"].showChat()
        });
        */
        
        $('#subscribe_email').focus(function(){
            $(this).addClass("subscribe_email_hover");
            if ($(this).val()=='Почта'){
                $(this).val('');
            }
        });
        
        $('#subscribe_email').blur(function(){
            $(this).removeClass("subscribe_email_hover");
            if ($(this).val()==''){
                $(this).val('Почта');
            }
        });
        $('#subscribe_button').click(function(){
            if (checkEmail($('#subscribe_email').val())){
                $.get('/',{reqMode:'xHTTP',reqType:'Subscribe',reqAction:'Add',reqEmail:encodeURIComponent($('#subscribe_email').val()),reqLanguage:languageID},function(data){
                    $('#subscription_text').text(data);
                    $('#subscription_text').oneTime(3000,function(){
                        $(this).text('');
                    });
                });
            }else{
                $('#subscription_text').text('Проверьте ваш адрес');
                $('#subscription_text').oneTime(3000,function(){
                    $(this).text('');
                });
            }
        });
        
        $('div.item').mouseover(function(){
            $(this).children('div').eq(1).show();
        });
        
        $('div.item').mouseout(function(){
            $(this).children('div').eq(1).hide();
        });
        
        buildGrid();
        buildGrid2();
        $(window).resize(buildGrid);
        $(window).resize(buildGrid2);
        
    }

    if ($('#content_gallery').size()>0){
        
        var current='list';
        var staff=new Array();
        var pointer=0;
        var pointer_current=0;
        var anchor=location.hash.substr(1,location.hash.length);
        
        if (!anchor){
            anchor='random';
        }
        
        $('#list').click(function(){
            if (current=='order'){
                $('#'+current).removeClass('current');
                $(this).addClass('current');
                current='list';
                $('#staff').show();
                $('#staff_navigation').hide();
            }
        });

        $('#order').click(function(){
            if (current=='list'){
                $('#'+current).removeClass('current');
                $(this).addClass('current');
                current='order';
                $('#staff').hide();
                $('#staff_navigation').show();
            }
        });

        $('div.staff span').click(function(){
            $(this).addClass('lined');
            $.cookie($(this).attr('name'),1);
            location.hash=$(this).attr('name');
            $('#gallery_back').css({'background':$('#gallery_front').css('background')});
            $('#gallery_back').css({'background-position':'center top'});
            $('#gallery_front').hide();
            $.get('/',{reqMode:'xHTTP',reqType:'Team',reqTeam:$(this).attr('name'),reqStructure:nStructureId},function(data){
                value=eval(data);
                about=value['about'];
                $(document.createElement('img')).attr('src',value['photo']+'?rnd='+Math.random()).load(function(){
                    $('#gallery_front').css({'background':'#'+value["color"]+' url('+value['photo']+') no-repeat center top'});
                    $('#gallery_front').show();
                });
                
                $('#staff_about').html('<span>'+value['name']+'</span>&nbsp;&nbsp;<img src="Images/other/line_31.png">&nbsp;&nbsp;'+about.replace('[line]','&nbsp;&nbsp;<img src="Images/other/line_31.png">&nbsp;&nbsp;'));
                
            },'json');
        });

            $.get('/index.php',{reqMode:'xHTTP',reqType:'Team',reqTeam:anchor,reqStructure:nStructureId},function(data){
                value=eval(data);
                about=value['about'];
                $(document.createElement('img')).attr('src',value['photo']+'?rnd='+Math.random()).load(function(){
                    $('#gallery_front').css({'background':'#'+value["color"]+' url('+value['photo']+') no-repeat center top'});
                    $('#gallery_front').show();
                    $('span[name="'+value['href']+'"]').addClass('lined');
                    $.cookie(value['href'],1);
                });
                
                $('#staff_about').html('<span>'+value['name']+'</span>&nbsp;&nbsp;<img src="Images/other/line_31.png">&nbsp;&nbsp;'+about.replace('[line]','&nbsp;&nbsp;<img src="Images/other/line_31.png">&nbsp;&nbsp;'));
                
            },'json');
        
        $('div.staff span').each(function(){
            if ($.cookie($(this).attr('name'))==1){
                $(this).addClass('lined');
            }
            staff[pointer]=$(this).attr('name');
            if (location.hash=='#'+$(this).attr('name')){
                pointer_current=pointer;
                $(this).trigger('click');
            }
            pointer++;
        });
        
        $('#staff_prev').mouseover(function(){$(this).addClass('prev_hover')});
        $('#staff_prev').mouseout(function(){$(this).removeClass('prev_hover')});
        $('#staff_prev').mousedown(function(){$(this).addClass('prev_push')});
        $('#staff_prev').mouseup(function(){$(this).removeClass('prev_push')});
        $('#staff_prev').mouseleave(function(){$(this).removeClass('prev_push')});

        $('#staff_next').mouseover(function(){$(this).addClass('next_hover')});
        $('#staff_next').mouseout(function(){$(this).removeClass('next_hover')});
        $('#staff_next').mousedown(function(){$(this).addClass('next_push')});
        $('#staff_next').mouseup(function(){$(this).removeClass('next_push')});
        $('#staff_next').mouseleave(function(){$(this).removeClass('next_push')});
        
        $('#staff_prev').click(function(){
            if (pointer_current==0){
                pointer_current=staff.length-1;
            }else{
                pointer_current--;
            }
            //$('#'+staff[pointer_current]).trigger('click');
            $('div.staff span').eq(pointer_current).trigger('click');
        });
        
        $('#staff_next').click(function(){
            if (pointer_current==staff.length-1){
                pointer_current=0;
            }else{
                pointer_current++;
            }
            //$('#'+staff[pointer_current]).trigger('click');
            $('div.staff span').eq(pointer_current).trigger('click');
        });
        
        
    }
    
    if ($('#year_drop_down').size()>0){
        
        var open_year=false;
        var click_year=false;
        
        $('#year_drop_down_select').click(function(){
            if (!open_year){
                open_year=true;
                $(this).addClass('year_drop_down_push');
                $('#year_drop_down_menu').show();
            }else{
                open_year=false;
                $(this).removeClass('year_drop_down_push');
                $('#year_drop_down_menu').hide();
            }
            click_year=true;
        });
        $('#year_drop_down_menu>div').mouseover(function(){
            $(this).addClass('drop_down_hover');
            $(this).next().children('div').eq(0).addClass('drop_down_unline');
        });
        $('#year_drop_down_menu>div').mouseout(function(){
            $(this).removeClass('drop_down_hover');
            $(this).next().children('div').eq(0).removeClass('drop_down_unline');
        });
        $('body').click(function(){
            if (!click_year){
                if (open_year){ $('#year_drop_down_select').trigger('click') }
            }
            click_year=false;
        });
        $('#year_drop_down_menu>div').click(function(){location.href=$(this).attr('id')});
        
    }

    if ($('#category_drop_down').size()>0){
        
        var open_category=false;
        var click_category=false;
        
        $('#category_drop_down_select').click(function(){
            if (!open_category){
                open_category=true;
                $(this).addClass('category_drop_down_push');
                $('#category_drop_down_menu').show();
            }else{
                open_category=false;
                $(this).removeClass('category_drop_down_push');
                $('#category_drop_down_menu').hide();
            }
            click_category=true;
        });
        $('#category_drop_down_menu>div').mouseover(function(){
            $(this).addClass('drop_down_hover');
            $(this).next().children('div').eq(0).addClass('drop_down_unline');
        });
        $('#category_drop_down_menu>div').mouseout(function(){
            $(this).removeClass('drop_down_hover');
            $(this).next().children('div').eq(0).removeClass('drop_down_unline');
        });
        $('body').click(function(){
            if (!click_category){
                if (open_category){ $('#category_drop_down_select').trigger('click') }
            }
            click_category=false;
        });
        $('#category_drop_down_menu>div').click(function(){location.href=$(this).attr('id')});
        
    }

    if ($('#content_works').size()>0){
        
        $('div.item').mouseover(function(){
            $(this).children('div').eq(1).show();
        });
        
        $('div.item').mouseout(function(){
            $(this).children('div').eq(1).hide();
        });
        
        buildGrid3();
        $(window).resize(buildGrid3);

       // alert($('table').width());
        
    }
    
    if ($('#content_list').size()>0){

        var ratePosition=0;
        
        updateDropDown();
        $(window).resize(updateDropDown);
        
    }
    
    if ($('#content_singlework').size()>0 || $('#content_special').size()>0){
        
        var tmpClass='';
        
        if ($.cookie('rate_'+$('div.rate').attr('id'))!=1){
            $('div.rate').mouseover(function(){tmpClass=$(this).attr('class')});
            $('div.rate').mouseout(function(){tmpClass=$(this).attr('class',tmpClass)});
            $('div.rate').mousemove(function(event){
                var rate=Math.ceil((event.pageX-$(event.target).offset().left)/19.4);
                $(this).attr('class','rate work star'+rate);
            });
            $('div.rate').click(function(event){
                $.cookie('rate_'+$(this).attr('id'),1,{expires: 3650});
                var rate=Math.ceil((event.pageX-$(event.target).offset().left)/19.4);
                object=$(this);
                $.get('/',{reqMode:'xHTTP',reqType:'Rate',reqObject:$(this).attr('id'),reqPoints:rate},function(data){
                    tmpClass='rate work star'+data;
                    object.attr('class',tmpClass);
                    object.unbind();
                },'text');
            });
        }
        
    }

    if ($('#content_special').size()>0){
        
        var opened=true;
        
        $('#button').click(function(){
            if (opened){
                $('#button').text('показать информацию');
                $('#description').hide();
                opened=false;
            }else{
                $('#button').text('скрыть информацию');
                $('#description').show();
                opened=true;
            }
        });
        $('#button').mouseover(function(){$(this).addClass('hide_hover')});
        $('#button').mouseout(function(){$(this).removeClass('hide_hover')});
        $('#button').mousedown(function(){$(this).addClass('hide_click')});
        $('#button').mouseup(function(){$(this).removeClass('hide_click')});
        $('#button').mouseleave(function(){$(this).removeClass('hide_click')});
    }
    
    if ($('#content_partners').size()>0){

        buildGrid4();
        $(window).resize(buildGrid4);
        
    }
    
    if ($('#content_contacts').size()>0){
        
        var disable=true;
        var current_theme=0;
        
        var configuration=[[0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0],[1,1,1,1,0,1,1,1,1],[1,1,1,0,0,0,0,0,0],[1,1,1,0,0,1,0,1,1],[1,1,1,1,1,0,0,0,0],[1,1,1,0,0,1,0,1,1],[1,1,0,0,1,0,0,1,1],[1,1,1,1,1,1,1,1,1]];
        var elements=['name','email','phone','job','href','url','photo','file','about'];
        
        if (location.hash){
            current_theme=location.hash.substr(1,location.hash.length);
        }
        
        $('input.input_text').focus(function(){$(this).addClass('input_text_focus')});
        $('input.input_text').blur(function(){$(this).removeClass('input_text_focus')});
        $('textarea').focus(function(){$(this).addClass('textarea_focus')});
        $('textarea').blur(function(){$(this).removeClass('textarea_focus')});
        $('input.file').change(function(){
           $(this).siblings('input').eq(0).val($(this).val());
        });
        $('#send_btn').mouseover(function(){
            if (!disable){
                $(this).addClass('hovered');
            }
        });
        $('#send_btn').mouseout(function(){
            if (!disable){
                $(this).removeClass('hovered');
            }
        });
        $('#send_btn').click(function(){
            if (!disable){
                //alert('click');
                checkForm(current_theme);
            }
        });
        
        if ($('#theme_drop_down').size()>0){
            
            var open_theme=false;
            var click_theme=false;
            var f_action=$('#fcontacts').attr('action');
            
            $('#theme_drop_down_select').click(function(){
                if (!open_theme){
                    open_theme=true;
                    $(this).addClass('theme_drop_down_push');
                    $('#theme_drop_down_menu').show();
                }else{
                    open_theme=false;
                    $(this).removeClass('theme_drop_down_push');
                    $('#theme_drop_down_menu').hide();
                }
                click_theme=true;
            });
            $('#theme_drop_down_menu>div').mouseover(function(){
                $(this).addClass('drop_down_hover');
                $(this).next().children('div').eq(0).addClass('drop_down_unline');
            });
            $('#theme_drop_down_menu>div').mouseout(function(){
                $(this).removeClass('drop_down_hover');
                $(this).next().children('div').eq(0).removeClass('drop_down_unline');
            });
            $('body').click(function(){
                if (!click_theme){
                    if (open_theme){ $('#theme_drop_down_select').trigger('click') }
                }
                click_theme=false;
            });
            $('#theme_drop_down_menu>div').click(function(){
                $('#theme_drop_down_select').text($(this).text());
                if (open_theme){$('#theme_drop_down_select').trigger('click')}
                var id=$(this).attr('id');
                location.hash=id;
                current_theme=id;
                $('#input_type').val(current_theme);
                //alert($('#fcontacts').attr('action'));
                $('#fcontacts').attr('action',f_action+'#'+current_theme);
                //alert($('#fcontacts').attr('action'));
                //alert(f_action+'#'+current_theme);
                for (var key in configuration[id]){
                    if (configuration[id][key]){
                        $('#form_'+elements[key]).removeClass('disable');
                        $('#form_'+elements[key]).children('input').eq(0).removeClass('disable');
                        $('#form_'+elements[key]).children('textarea').eq(0).removeClass('disable');
                        $('#form_'+elements[key]).children('input').attr('disabled','');
                        $('#form_'+elements[key]).children('textarea').attr('disabled','');
                    }else{
                        $('#form_'+elements[key]).addClass('disable');
                        $('#form_'+elements[key]).removeClass('star');
                        $('#form_'+elements[key]).children('input').eq(0).addClass('disable');
                        $('#form_'+elements[key]).children('textarea').eq(0).addClass('disable');
                        $('#form_'+elements[key]).children('input').attr('disabled','disabled');
                        $('#form_'+elements[key]).children('textarea').attr('disabled','disabled');
                        $('#form_'+elements[key]).children().val('');
                    }
                }
                if (id){
                    disable=false;
                    $('#send_btn').removeClass('disabled');
                }
            });
            $('#'+current_theme).trigger('click');
            
        }
        
    }
    
    function checkForm(id){
        var flag=true;
        var configuration=[[0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0],[1,1,1,1,0,1,1,1,1],[1,1,1,0,0,0,0,0,0],[1,1,1,0,0,1,0,1,1],[1,1,1,1,1,0,0,0,0],[1,1,1,0,0,1,0,1,1],[1,1,0,0,1,0,0,1,1],[1,1,1,1,1,1,1,1,1]];
        var requirements= [[0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0],[1,1,0,1,0,0,1,0,0],[1,1,1,0,0,0,0,0,0],[1,1,1,0,0,0,0,0,0],[1,1,1,1,1,0,0,0,0],[0,1,0,0,0,0,0,0,0],[0,1,0,0,1,0,0,0,0],[1,0,0,0,0,0,0,0,0]];
        var elements=['name','email','phone','job','href','url','photo','file','about'];
        var types=['text','email','phone','text','url','url','text','text','text'];
        for (var key in configuration[id]){
            if (configuration[id][key]){
                if (!checkField($('#input_'+elements[key]).val(),types[key]) && requirements[id][key]){
                    $('#form_'+elements[key]).addClass('star');
                    flag=false;
                }else{
                    $('#form_'+elements[key]).removeClass('star');
                }
            }
        }
        if (flag){
            $('#fcontacts').submit();
        }
    }
    
    function checkField(content,type){
        var flag=true;
        switch (type){
            case 'text':
                flag=checkText(content);
                break;
            case 'email':
                flag=checkEmail(content);
                break;
            case 'phone':
                flag=checkPhone(content);
                break;
            case 'url':
                flag=checkUrl(content);
                break;
        }
        return flag;
    }
    
    function checkText(content){
        if (content==''){
            return false;
        }else{
            return true;
        }
    }

    function checkEmail(content){
        content=content.replace(/^\s+|\s+$/g, '');
        return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(content);
    }

    function checkPhone(content){
        if (content==''){
            return false;
        }else{
            return true;
        }
    }

    function checkUrl(content){
        if (content==''){
            return false;
        }else{
            return true;
        }
    }

    function buildGrid(){
        var count=Math.floor($('table').width()/328);
        var total=$('div.item').size();
        if (count>total){
            count=total;
        }
        var padding=($('table').width()-count*328)/(count*2);
        for (var i=0;i<count;i++){
            $('div.item').eq(i).show();
            if (i==0){
                $('div.item').eq(i).css({left:padding});
            }else{
                $('div.item').eq(i).css({left:$('div.item').eq(i-1).position().left+328+2*padding});
            }
        }
        if (total>count){
            for (var j=i;j<total;j++){
                $('div.item').eq(j).hide();
            }
        }
    }

    function buildGrid2(){
        var count=Math.floor($('table').width()/320);
        var total=$('div.item2').size();
        if (count>total){
            count=total;
        }
        var padding=($('table').width()-count*320)/(count*2);
        for (var i=0;i<count;i++){
            $('div.item2').eq(i).show();
            if (i==0){
                $('div.item2').eq(i).css({left:padding});
            }else{
                $('div.item2').eq(i).css({left:$('div.item2').eq(i-1).position().left+320+2*padding});
            }
        }
        if (total>count){
            for (var j=i;j<total;j++){
                $('div.item2').eq(j).hide();
            }
        }
    }

    function buildGrid3(){
        var row=0;
        var count=Math.floor($('table').width()/328);
        var total=$('div.item').size();
        var totalRows=Math.ceil(total/count);
        if (count>total){
            count=total;
        }
        $('#content_works').css({height:totalRows*334});
        var padding=($('table').width()-count*328)/(count*2);
        for (var i=0;i<total;i++){
            if (i==0){
                $('div.item').eq(i).css({left:padding,'margin-top':row*334});
            }else if (i%count==0){
                row++;
                $('div.item').eq(i).css({left:padding,'margin-top':row*334});
            }else{
                $('div.item').eq(i).css({left:$('div.item').eq(i-1).position().left+328+2*padding,'margin-top':$('div.item').eq(i-1).css('margin-top')});
            }
        }
    }
    
    function buildGrid4(){
        $('div.partner').hide();
        var row=0;
        var count=Math.floor($('table').width()/360);
        var total=$('div.partner').size();
        var totalRows=Math.ceil(total/count);
        if (count>total){
            count=total;
        }
        $('#content_partners').css({height:totalRows*232-30});
        var padding=($('table').width()-count*340)/(count*2);
        //alert(padding);
        for (var i=0;i<total;i++){
            if (i==0){
                $('div.partner').eq(i).css({left:padding,'margin-top':row*202});
            }else if (i%count==0){
                row++;
                $('div.partner').eq(i).css({left:padding,'margin-top':row*232});
            }else{
                if (row==0){
                    $('div.partner').eq(i).css({left:$('div.partner').eq(i-1).position().left+340+1*padding,'margin-top':row*202});
                }else{
                    $('div.partner').eq(i).css({left:$('div.partner').eq(i-1).position().left+340+1*padding,'margin-top':row*232});
                }
            }
            $('div.partner').eq(i).show();
        }
    }
    
    function updateDropDown(){
        if (!ratePosition){
            ratePosition=$('div.rate').offset().left;
        }
        ddPosition=$('table').width()/2-279;
        
        if (ratePosition<=ddPosition){
            $('div.rate').addClass('list_absolute');
            $('#drop_down_works').css({left:'50%','margin-left':-285});
        }else{
            $('div.rate').removeClass('list_absolute');
            $('#drop_down_works').css({left:ratePosition,'margin-left':0});
        }
    }
    
});

