var PRODUCT_MENU_WIDTH = 312;
var PRODUCT_MINU_ITEM_WIDTH = 100+3;

var is_in_animation = false;

function startup()
{
    Shadowbox.init( { language: "ru" } );

    // ------------------------------------------------------------------------

    $('.handler__auto_choose').click(
    function()
    {
        var showup_auto_id = $(this).attr('auto_id');

        if( $('.auto-block-id'+showup_auto_id).css('display') != 'none' )
        {
            return false;
        }

        if( is_in_animation == true ) return false;
        is_in_animation = true;

        var is_block_found = false;

        $('.auto-block').each(
        function() 
        {
            if( $(this).css('display') != 'none' )
            {
                $(this).slideUp( 300,
                function() 
                {
                    is_block_found = true;
                    $('.auto-block-id'+showup_auto_id).slideDown( 500, function() { is_in_animation=false; } );
                } );
            }
        } );

        if( is_block_found == false )
        {
            $('.auto-block-id'+showup_auto_id).slideDown( 500, function() { is_in_animation=false; } );
        }

        return true;
    } );

    // ------------------------------------------------------------------------

    $('.handler__news_shower').click(
    function()
    {
        var showup_news_id = $(this).attr('news_id');

        if( $('.news-item-id'+showup_news_id).css('display') != 'none' )
        {
            return false;
        }

        if( is_in_animation == true ) return false;
        is_in_animation = true;

        if( parseInt( showup_news_id ) == 0 )
        {
            $('.about-clicker').slideUp(333);
        }
        else
        {
            if( $('.about-clicker').css('display') == 'none' )
            {
                $('.about-clicker').slideDown(333);
            }
        }

        $('.news-item2').removeClass('active');
        $('.news-item2-id'+showup_news_id).addClass('active');

        $('.news-item').each(
        function() 
        {
            if( $(this).css('display') != 'none' )
            {
                $(this).fadeOut( 300,
                function() 
                {
                    $('.news-item-id'+showup_news_id).fadeIn( 500, function() { is_in_animation=false; } );
                } );
            }
        } );

        return true;
    } );

    // ------------------------------------------------------------------------

    $('.handler__acces_shower').click(
    function()
    {
        var showup_acces_id = $(this).attr('acces_id');

        if( $('.acces-item-id'+showup_acces_id).css('display') != 'none' ) return false;
        if( is_in_animation == true ) return false;
        is_in_animation = true;

        $('.acces-item-link').removeClass('active');
        $('.acces-item-link-id'+showup_acces_id).addClass('active');

        $('.acces-item').each(
        function() 
        {
            if( $(this).css('display') != 'none' )
            {
                $(this).fadeOut( 300,
                function() 
                {
                    $('.acces-item-id'+showup_acces_id).fadeIn( 500, function() { is_in_animation=false; } );
                } );
            }
        } );

        return true;
    } );

    // ------------------------------------------------------------------------

    $('.handler__product_menu_item_hint').mouseover(
    function()
    {
        var container_obj = $('.product_menu_item_hint' + $(this).attr('block_id') );

        container_obj.stop(true,true).hide();
        container_obj.attr( 'buffer_hint', container_obj.html() );
        container_obj.html( $(this).attr('hint') );
        container_obj.fadeIn(300).show();

        return true;
    } );

    $('.handler__product_menu_item_hint').mouseout(
    function()
    {
        var container_obj = $('.product_menu_item_hint' + $(this).attr('block_id') );

        container_obj.stop(true,true).hide();
        container_obj.html( container_obj.attr('buffer_hint') );
        container_obj.fadeIn(300).show();

        return true;
    } );

    $('.handler__product_subitems').each(
    function()
    {
        var count = $(this).attr('count');
        if( count <= 3 ) return true;

        // - - - - - - - - - - - - - - - - - - - - - - - -

        var block_index = $(this).attr('block_index');
        var blocks_over_count = count-3;

        $(this).mousemove(
        function(e)
        {
            var inner_offset_left  = 40;
            var inner_offset_right = 40;
            var active_block_width = PRODUCT_MENU_WIDTH-1 -inner_offset_left -inner_offset_right;

            var site_left    = Math.max( 0, Math.floor( ($(window).width()-950)/2 ) );
            var block_offset = (block_index-1) * (PRODUCT_MENU_WIDTH+7) + inner_offset_left;

            var position_x   = parseInt( e.pageX ) -site_left -block_offset;
            var position_per = Math.min( Math.max( position_x/active_block_width, 0 ), 1 );

            var offset       = Math.floor( ( PRODUCT_MINU_ITEM_WIDTH * blocks_over_count ) * position_per );

            $('.product-menu-items-subcontaner'+block_index).css( 'left', -offset + 'px' );

            return true;
        } );

        return true;
    } );

    //-------------------------------------------------------------------------

    var startup_functions = new Array();

    $('.handler__startup_function').each(
    function()
    {
        var function_name = $(this).attr('function');

        if( jQuery.inArray( function_name, startup_functions ) == -1 )
        {
            startup_functions[ startup_functions.length ] = function_name;
        }
        return true;
    } );

    for( var lp=0; lp<startup_functions.length; lp++ )
    {
        eval( startup_functions[lp] );
    }

    //-------------------------------------------------------------------------

    return true;
}

/*****************************************************************************/
/*** BUGGY *******************************************************************/
/*****************************************************************************/

function buggy_clean()
{
    $('.buggy_error_object').removeClass( 'buggy_error_object' );
    $('.buggy_error_hint').remove();

    return true;
}

function buggy_markup( field_errors_list )
{
    for( var key in field_errors_list )
    {
        $('.buggy_obj__'+key).addClass( 'buggy_error_object' );
        $('.buggy_obj__'+key).parent()
            .append( $(document.createElement('div') )
                .addClass( 'buggy_error_hint' )
                .html(
                    $(document.createElement('span') )
                        .addClass( 'sizeSmall' )
                        .html( field_errors_list[key] )
                 )
                .hide()
                .fadeIn( 1000 )
            );
    }

    return true;
}

/*****************************************************************************/
/*** PROFILES ****************************************************************/
/*****************************************************************************/

function profile_remind__startup()
{
    $('form.handler__form_profile_remind').each(
    function()
    {
        return true;
    } );

    $('.handler__profile_remind_submit').click(
    function()
    {
        $('form.handler__form_profile_remind').submit();
        return true;
    } );

    return true;
}

function profile__startup( startup_tab_id )
{
    $('form.handler__form_profile').each(
    function()
    {
        $(this).ajaxForm( {
            beforeSubmit: null,
            success:      profile_response,
            dataType:     'json'
        } );

        return true;
    } );

    $('.handler__profile_submit').click(
    function()
    {
        $('.handler__profile_submit').attr('disabled',true);
        buggy_clean();
        $('form.handler__form_profile').submit();
        return true;
    } );

    $('.handler__profile_changepasswd_show').click(
    function()
    {
        $("#profile_form_changepasswd_flag").attr( 'value', 'true' );

        $('.profile-form-changepasswd-link').stop(true,true);
        $('.profile-form-changepasswd-container').stop(true,true);

        $('.profile-form-changepasswd-link').slideUp( 200, function() { $('.profile-form-changepasswd-container').slideDown(500); } );
        
        return true;
    } );

    $('.handler__profile_changepasswd_hide').click(
    function()
    {
        $("#profile_form_changepasswd_flag").attr( 'value', 'false' );

        $('.profile-form-changepasswd-link').stop(true,true);
        $('.profile-form-changepasswd-container').stop(true,true);

        $('.profile-form-changepasswd-container').slideUp( 400, function() { $('.profile-form-changepasswd-link').slideDown(250); } );

        return true;
    } );

    return true;
}

function profile_response( responseBody, responseStatus )
{
    if( responseStatus == 'success' && responseBody )
    {
        $('.handler__profile_submit').attr('disabled',false);

        buggy_clean();
        if( responseBody.status == 'error' )
        {
            if( responseBody.captcha_source )
            {
                $('img#image-captcha').attr('src',responseBody.captcha_source);
            }

            buggy_markup( responseBody.error_messages );
            return true;
        }
        else if( responseBody.status == 'error_message' )
        {
            alert( responseBody.error_message );
            return false;
        }
        else if( responseBody.status == 'done' )
        {
            $('.profile-form-objects').hide();
            $('.profile-form-complite').fadeIn( 500 );

            return true;
        }
    }

    return true;
}

/*****************************************************************************/
/*** ADD TO BASKET ***********************************************************/
/*****************************************************************************/

function basket_manage__startup( startup_tab_id )
{
    $('form.handler__form_basket_manage').each(
    function()
    {
        $(this).ajaxForm( {
            beforeSubmit: null,
            success:      basket_manage_response,
            dataType:     'json'
        } );

        return true;
    } );

    $('.handler__basket_manage__add').click(
    function()
    {
        var add_item_id = $(this).attr('add_item_id');
        $('form#formAddToBasket'+add_item_id).submit();

        $('.priceclick_add_item'+add_item_id).hide();

        return true;
    } );

    return true;
}

function basket_manage_response( responseBody, responseStatus )
{
    if( responseStatus == 'success' && responseBody )
    {
        if( responseBody.status == 'error' )
        {
            alert('Возникла ошибка');
        }
        else if( responseBody.status == 'done' )
        {
            $('.priceclick_res_item' + responseBody.item_id ).fadeIn(500);

            $('.basket_amount_empty').hide();
            $('.basket_amount_value').show();
            $('#basket_amount').hide().html( responseBody.new_basket_amount ).fadeIn(300);

            return true;
        }
    }

    return true;
}

/*****************************************************************************/
/*** ORDER COMPLITE **********************************************************/
/*****************************************************************************/

function order_confirm__startup()
{
    $('form.handler__form_order_confirm').each(
    function()
    {
        $(this).ajaxForm( {
            beforeSubmit: null,
            success:      order_confirm_response,
            dataType:     'json'
        } );

        return true;
    } );

    $('.handler__order_confirm_submit').click(
    function()
    {
        $('.handler__order_confirm_submit').attr('disabled',true);
        buggy_clean();
        $('form.handler__form_order_confirm').submit();
        return true;
    } );

    $('.handler__delivery_item_checked').click( function()
    {
        var price = parseInt( $(this).attr('price') );

        if( price == 0 )
        {
            $('.delivery_address').fadeOut('200');
        }
        else
        {
            $('.delivery_address').fadeIn('200');
        }
    } );

    return true;
}

function order_confirm_response( responseBody, responseStatus )
{
    if( responseStatus == 'success' && responseBody )
    {
        $('.handler__order_confirm_submit').attr('disabled',false);

        buggy_clean();
        if( responseBody.status == 'error' )
        {
            buggy_markup( responseBody.error_messages );
            return true;
        }
        else if( responseBody.status == 'error_message' )
        {
            alert( responseBody.error_message );
            return false;
        }
        else if( responseBody.status == 'done' )
        {
            $('.order_confirm-form-objects').hide();
            $('.order_confirm-form-complite').fadeIn( 500 );

            return true;
        }
    }

    return true;
}
