
$(document).ready(function(){
    $(".msg_active").each(function(){
        $(this).children().css({'font-weight':'bold'});
        $(this).click(function(){              
            jQuery.ajax({data:({authenticity_token : encodeURIComponent('<%= form_authenticity_token %>'),
                id:$(this).attr('message_id')}), dataType:'script', type:'post', url:'/account/inbox/read'});
        });
    });
    $(".msg_inactive").click(function(){
        $("#message_content_"+$(this).attr('message_id')).toggle('blind');
    });
    $(".subopt").click(function(){
        $(".subopt").css({"background-position": "top"});
        $(".subopt input").attr("checked", false);
        $(this).css({"background-position": "bottom"});
        $(this).children("input").attr("checked", "checked");
    });
    $(".change_payments").click(function(){
        $(".change_payments").toggle('blind');
        var button = $(this);
        $(".opt-payment").each(function(b){
            if($(this).attr("type")){
                $(this).parent().children("input").attr("checked", false);
                $(this).parent().css({"background-position": "top"});
                $(this).parent().toggle('blind');
            }
        });
    });
});


var loadingTimer;
jQuery.ajaxSetup({
    'beforeSend': function(xhr) {
        startSpinner();
    },
    'complete': function(xhr,status){
        stopSpinner();        
    }
})

function stopSpinner(){
    $("#fancy_loading").hide();
    if ($("#fancy_overlay").is(':visible')) {
        $("#fancy_overlay").fadeOut("fast");
    }
    return false;
}

function startSpinner(){
    $('#fancy_overlay').css({'background-color': '#666','opacity':0.3}).show();
    clearInterval(loadingTimer);
    var w = $.fn.fancybox.getViewport();
    $("#fancy_loading").css({'left': ((w[0] - 40) * 0.5 + w[2]), 'top': ((w[1] - 40) * 0.5 + w[3])}).show();
    loadingTimer = setInterval($.fn.fancybox.animateLoading, 66);
}

function setupMenu(){
    $('#nav span').mouseover(function(){
        $('#nav li').each(function(){
          var tmp_id = this.id;
          $('#sub_nav_'+tmp_id).css({'display': 'none'});
          $("#"+tmp_id+" a").each(function(){
              $(this).css({'background': 'none'});
          })
          return;
        });
        var this_parent = $(this).parent().parent().parent().parent();
        $("#sub_nav_"+this_parent.attr('id')).css({'display': 'inline-block'});
        var sel = this_parent.hasClass("sel-first");
        $("#"+this_parent.attr('id')+" a").each(function(){
            var str;
            if(sel || this_parent.hasClass("sel-l")){
            str = 'url("/images/menu/menu-selector-white.png") bottom no-repeat';
            }
            else{
            str = 'url("/images/menu/menu-selector.png") bottom no-repeat';
            }
            $(this).css({'background': str});
            return;
        });
        return;
    });

    $('.sel').each(function(){
        var selected_id = $(this).parent().parent().attr("id")
        var str = '#sub_nav_'+selected_id;
        $(str).css({'display': 'inline-block'});
        str = 'url("/images/menu/menu-selector-white.png") bottom no-repeat';
        $("#"+selected_id+" a").css({'background': str});
        return;
    });
    return;
}

