jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

$.log = function(message) {
    if(window.console) {
        console.debug(message);
    } else {
        alert(message);
    }
}

function showDialog(url, width) {
    // Create overlay and dialog DIVs
    $('#head').before('<div id="overlay"></div>');
    $('#head').before('<div id="dialog"></div>');
    
    // Size overlay and dialog boxes
    sizeDialog(width);
    
    // Show overlay with animation
    $('#overlay').css({
        opacity: 0
    }).fadeTo('fast', 0.60, function() {
        $('#dialog').show();
    })
    
    // Resize dialog if window size changes
    $(window).resize(function() {
        sizeDialog(width);
    });
    
    // Call close
    $('#dialog .dialog_close').live('click', function(e) {
        hideDialog();
    });
    
    if(url) {
        $.ajax({
            url: url,
            success: function(data) {
                $('#dialog').html(data);
            }
        })
    }
}

function hideDialog() {
    $('#overlay').fadeOut('fast', function() {
        $(this).remove();
        $('#dialog').remove();
    });
}

function sizeDialog(width) {
    $('#overlay').width($(window).width());
    $('#overlay').height($(document).height());
    
    $('#dialog').width(width);
    
    $('#dialog').css('top', 150);
    $('#dialog').css('left', $(window).width() / 2 - $('#dialog').width() / 2);
}

function loading() {
    $('.button').attr('disabled', 'disabled').addClass('disabled').val('Loading...');
}


$(document).ready(function () {
    // Logo Popup Controls
    $('.home_trigger').mouseover(function() {
        $('.home_popup').css({
            opacity: 0
        }).animate({
            left: '-=' + 10 + 'px',
            opacity: 1
        }, 250, 'swing')
    });
    
    $('.home_trigger').mouseout(function() {
        $('.home_popup').animate({
            left: '+=' + 10 + 'px',
            opacity: 0
        }, 250, 'swing')
    });
    
    // Sortable List Helper
    $("#thumbs").sortable({
        cursor: 'pointer',
        opacity: 0.4,
        update: function() {
            $.ajax({
                type: 'POST',
                url: '/reorder',
                data: $(this).sortable('serialize'),
                success: function() {
                    $('#thumbs li').each(function(index) {
                        $(this).find(".num").html(index + 1);
                    });
                }
            });
        }
    });
});

function selectTheme(id) {
    // Are we using a custom theme?
    $('#use_default_theme').val((id=="custom") ? false : true);

    // Load background image. If id parameter is set to 
    // "custom" then load custom theme background. 
    // Otherwise, load one of the default backgrounds.
    var background_image = (id=="custom") ? designer.custom_theme_background : designer.default_themes[id].image

    // Set background-image to "none" if there is no 
    // background specified. Othervise, load background 
    // image before setting it. Set it.
    if (background_image == "") {
        $('#design_preview').css('background-image', "none");
    }

    // Set theme colors.
    if (id=="custom") { 
        $('#customization').show();
        $('#default_theme').val(null);
    } else { 
        $('#customization').hide();
        $('#default_theme').val(id);
    }
}
 
 // Initialize theme that user is currently using.
 // custom_theme must be defined in the view prior
 // to executing this function. 
 function initTheme() {
     // Note: Active theme variable is in the View
     // var custom_theme = 17;
     
     // Set active theme
     if (designer.use_default) {
         $('.themes #custom').addClass("selected");
         selectTheme("custom");
     } else {
         var default_theme_id = designer.default_id;
         $('.themes #' + default_theme_id).addClass("selected");
         selectTheme(default_theme_id);
     }
 }

$(document).ready(function () {
    
    $(function(){ $("input:checkbox, input:radio").uniform(); });
    
    $('#change_password_toggle').live("click", function (e) {
        $('#user_password').val("");
        $('#user_password_confirmation').val("");
        if ($('#change_password_toggle').html() == 'Cancel') {
            $('#change_password_box').hide();
            $('#change_password_toggle').html("Change your password");
        } else {
            $('#change_password_box').show();
            $('#change_password_toggle').html("Cancel");
            $('#user_password').focus();
        }
        return false;
    });
    
    // Featured Member wheel
    $('#featured-members').each(function () {
        // options
        var time = 500;
        var distance = 60;
        
        var members = $('#featured-members .member');
        
        show(members[window.member = 0]);
        
        var interval = setInterval(function() {
            hide(members[window.member]);
            show(nextMember(members));
        }, 20000);
        
        function hide(member) {
            $(member).animate({
                top: '-=' + distance + 'px',
                opacity: 0
            }, time, 'swing').animate({
                top: '+=' + distance*2 + 'px'
            });
        }
        
        function show(member) {
            $(member).animate({
                top: '-=' + distance + 'px',
                opacity: 1
            }, time, 'swing');
        }
        
        function nextMember(members) {
            if (window.member < (members.length - 1)) window.member++; else window.member = 0;
            return members[window.member];
        };
        
    });
    
    
    $('.themes .theme').click(function(e) {
        $('.themes .theme').removeClass("selected");
        $(this).addClass("selected");
        selectTheme($(this).attr("id"));
    });
    
    function genURL(name) {
        return name.replace(/[^\w]/g,'-').replace(/[^a-zA-Z0-9\-]+/g,'').toLowerCase();
    }
    
    $('#user_real_name').bind('keyup', function(e) {
        if ($(this).val() == "") return;
        var url = genURL($(this).val());
        
        $('#user_subdomain').val(url);
        $('#url_output').addClass("highlight").html(url);
    });
    
    $('#user_company').bind('keyup', function(e) {
        if ($(this).val() == "") return;
        var url = genURL($(this).val());
        
        $('#user_subdomain').val(url);
        $('#url_output').addClass("highlight").html(url);
    })
    
    $('#user_subdomain').bind('keyup', function(e) {
        var url = genURL($(this).val());
        
        $(this).val(url);
        $('#url_output').addClass("highlight").html(url);
    });
    
});

