$(document).ready(function() 
{
    
    var BASE_PATH = $('base').attr('href');
    
    
    /**
    * Set default query value
    * 
    */
    var default_query_value = 'How to...';
    
    $('#howto').focus(function() 
    {
        if (default_query_value == $.trim($(this).val())) {
            $(this).val('');
        }
    });
    $('#howto').blur(function() 
    {
        if ('' == $.trim($(this).val())) {
            $(this).val(default_query_value);
        }
    }).blur();
    
    
    
    
    
    /**
    * Change captcha image
    * 
    */
    $('#change_captcha').click(function() 
    {
        $('#captcha_image').attr('src', 'captcha.jpg?' + Math.random());
    });
    
    
    /**
     * Control "State" depending on country.
     *
     */
     $('#country').change(function() 
     {
        // set select disable property
        $('#state').attr('disabled', state = 'United States' == $('#country option:selected').val() ? false : true);
        if (state) {
            $('#state').attr('selectedIndex', 0);
        }
        
     }).change();
     
     /**
     * Check for existing username.
     *
     */
     $('#username').blur(function() 
     {
        $.getJSON('?module=register&service=username&value=' + $('#username').val(), 
            function(response) 
            {
                if (response.exists) {
                    $('#username_error').html(response.error_message);
                    $('#username_error').addClass('show');
                } else {
                    $('#username_error').removeClass('show');
                }
            });
    });
    
    $('#format').change(function() 
    {
        $('.attachment').css('display', 'none');
        
        var selected = $('#format option:selected').val();
        $('.attachment-' + selected).css('display', 'block');
        /*$('#attachment-' + selected + '-container').css('display', 'block');*/
    }).change();
    
    
    // submit html form
    $('#html_button').click(function() 
    {
        $('form[name=html_form]').submit();
    });
    
    
    
    
    /**
    * youtube attachments
    * 
    */
    
    // delete
    $(".attachment-youtube .remove").click(function() 
    {
        if($(".attachment-youtube div.overflow").length > 1) {
            var element = $(this).parent().parent();
            element.slideUp("slow", function () {element.remove();}); 
        } else {
            alert('Can not remove the last value!');
        }
    });
    
    // add
    $(".attachment-youtube .add").click(function() 
    {
        var cloned = $(this).prev().clone(true);
        cloned.find('textarea').text('');
        
        // increment name
        var name = cloned.find('textarea.attachment-youtube').attr('name').replace('attachment[youtube]', '');
        cloned.find('textarea.attachment-youtube').attr('name', increment(name, 'attachment[youtube]', '[code]'));
        
        var name = cloned.find('textarea.attachment-youtube_desc').attr('name').replace('attachment[youtube]', '');
        cloned.find('textarea.attachment-youtube_desc').attr('name', increment(name, 'attachment[youtube]', '[description]'));
        
        cloned.insertBefore(this);
    });
    
    /**
    * @desc Increment value. 
    * 
    * @param string 
    * @param string 
    * @return string 
    */
    function increment(value, prefix, postfix) 
    {
        var re = /\[(\d+)\]/;
        var mathes = re.exec(value);
        var number = parseInt(mathes[1]) + 1;
        return prefix + '[' + number + ']' + postfix;
    }
    
});
