$(function() {

    $('.photo_box_l').click(function(){
        var total = $("#total").val();
        var id = $(this).attr('id')*1; // step 1 learn id to show
        var pid = "p" + id; //make this id proper i.e. added 'p' to it
        var idp = id + "p";
        var newId = 0;
        if (id == 1)
        {
            newId = total;
        }
        else
        {
            newId = id - 1; //step 2 make id for new turn
        }

        $(this).attr('id',newId); //assign this id to link

        //step 3 figure out id for right link and assign it
        var rid = $('.photo_box_r').attr('id')*1 - 1;
        if (rid == 0)
        {
            rid = total;
        }
        
        $('.photo_box_r').attr('id',rid);

        //step 4 remove all active and restor new active
        $(".active").removeClass('active').addClass('passive');
        $("#"+pid).removeClass('passive').addClass('active');
        $("#"+idp).removeClass('passive').addClass('active');
        
        if (pid == 4)
        {
            
        }
    });

    $('.photo_box_r').click(function(){
        var total = $("#total").val();
        var id = $(this).attr('id')*1;//step 1 learn id to show
        var pid = "p" + id;//make this id proper
        var idp = id + "p";
        var newid = 0;
        //step 2 make id for new turn
        if (id == total)
        {
            newid = 1;
        }
        else
        {
            newid = id + 1;
        }

        $(this).attr('id',newid);//assign this id to link


        var lid = $('.photo_box_l').attr('id')*1 + 1;
        if (lid > total)
        {
            lid = 1;
        }
        
        $('.photo_box_l').attr('id',lid);

        $(".active").removeClass('active').addClass('passive');
        $("#"+pid).removeClass('passive').addClass('active');
        $("#"+idp).removeClass('passive').addClass('active');

    });

    $('.passive').click(function()
    {
        var idp = $(this).attr('id');
        var id = idp.replace('p','');
        var pid = "p" + id;
        var total = $("#total").val();

        var newLid = id*1 - 1;
        var newRid = id*1 + 1;
        if (idp == 1)
        {
            newLid = total;
        }
        else if (idp > total)
        {
            newRid = 1;
        }
        //alert(newLid + " " + idp + " " + newRid);
        $('.photo_box_l').attr('id',newLid);
        $('.photo_box_r').attr('id',newRid);

        $(".active").removeClass('active').addClass('passive');
        $("#"+pid).removeClass('passive').addClass('active');
        $("#"+idp).removeClass('passive').addClass('active');

    });

    $('.active').click(function()
        {
            var idp = $(this).attr('id');
            var id = idp.replace('p','');
            var pid = "p" + id;

            var total = $("#total").val();

        var newLid = id*1 - 1;
        var newRid = id*1 + 1;
        if (idp == 1)
        {
            newLid = total;
        }
        else if (idp > total)
        {
            newRid = 1;
        }
        //alert(newLid + " " + idp + " " + newRid);
        $('.photo_box_l').attr('id',newLid);
        $('.photo_box_r').attr('id',newRid);

            $(".active").removeClass('active').addClass('passive');
            $("#"+pid).removeClass('passive').addClass('active');
            $("#"+idp).removeClass('passive').addClass('active');

        });

    $('a.passivep').click(function()
    {
        var id = $(this).attr('id');
        var did = "#d"+id;
        $('.activep').removeClass('activep').addClass('passivep');
        $(this).removeClass('passivep').addClass('activep');
        $("div.passivep").hide();
        $("div.d"+id+" div.passivep").show();
    });
    $('a.activep').click(function()
    {
        var id = $(this).attr('id');
        var did = "#d"+id;

        $('.activep').removeClass('activep').addClass('passivep');
        $(this).removeClass('passivep').addClass('activep');
        $("div.passivep").hide();
        $("div.d"+id+" div.passivep").show();
    });
    /*$("#good").click(function()
    {
        $('.forms').hide();
        $('ul.add li a.active').removeClass('active');
        $(this).addClass('active');
        $('#good_add').show();

    });*/
    $(".adlinks").click(function()
    {
        var id = $(this).attr('id');
        $('.forms').hide();
        $('ul.add li a.active').removeClass('active');
        $(this).addClass('active');
        $('#'+ id+ '_add').show();

    });
     $("#send").click(function()
    {
        //var val = $(this).val();
        //if (val == 'Send ->') {
        var name = $('#name').val();
        var email = $('#email').val();
        var subject = $('#topic').val();
        var text = $('#text').val();
        //alert(name + email + subject + text);
        $.get(ajaxFolder + "ajax.php", {name : name, email : email, subject: subject, text : text, type : "contacts"},
		function(data){
		$("li.result").html(data);
	    });

    });

    $("#text").click(function()
    {
        var val = $(this).val();

        if (val == 'Сообщение...') {
            $(this).val('');

        }

    });
    $("#text").focusout(function()
    {
        var val = $(this).val();
        if (val == '') {
            $(this).val('Сообщение...');
        }
    });
    $(".apassive,.aactive").click(function()
    {
        var id = $(this).attr('id');

            $('.aactive').removeClass('aactive').addClass('apassive');
            $(this).removeClass('apassive').addClass('aactive');
            $(".map").hide();        
            $("#i"+id).show();

    });
    $('.image_type').change(function()
    {
        var val = $(this).val();
        if (val == 'no')
        {
            alert('Нужно выбрать тип контента!');
        }
        else
        {
          $.get(ajaxFolder + "ajax.php", {val : val, type : "image"},
            function(data){
		    $("li.name").html(data);
	        });
        }
    });
    $('.name').change(function()
    {
        var val = $(".category").val();
        //alert(val);
        //return false;
        if (val == 'no')
        {
            alert('Нужно выбрать категорию!');
        }
        else
        {
          $.get(ajaxFolder + "ajax.php", {val : val, type : "products"},
            function(data){
		    $("li.products").html(data);
	        });
        }
    });
});
